File: generate_proto.py

package info (click to toggle)
python-protorpc-standalone 0.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,452 kB
  • ctags: 4,140
  • sloc: python: 11,561; sh: 30; makefile: 29
file content (127 lines) | stat: -rwxr-xr-x 3,860 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python
#
# Copyright 2010 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from __future__ import with_statement

__author__ = 'rafek@google.com (Rafe Kaplan)'

import logging

from . import descriptor
from . import generate
from . import messages
from . import util


__all__ = ['format_proto_file']


@util.positional(2)
def format_proto_file(file_descriptor, output, indent_space=2):
  out = generate.IndentWriter(output, indent_space=indent_space)

  if file_descriptor.package:
    out << 'package %s;' % file_descriptor.package

  def write_enums(enum_descriptors):
    """Write nested and non-nested Enum types.

    Args:
      enum_descriptors: List of EnumDescriptor objects from which to generate
        enums.
    """
    # Write enums.
    for enum in enum_descriptors or []:
      out << ''
      out << ''
      out << 'enum %s {' % enum.name
      out << ''

      with out.indent():
        if enum.values:
          for enum_value in enum.values:
            out << '%s = %s;' % (enum_value.name, enum_value.number)

      out << '}'

  write_enums(file_descriptor.enum_types)

  def write_fields(field_descriptors):
    """Write fields for Message types.

    Args:
      field_descriptors: List of FieldDescriptor objects from which to generate
        fields.
    """
    for field in field_descriptors or []:
      default_format = ''
      if field.default_value is not None:
        if field.label == descriptor.FieldDescriptor.Label.REPEATED:
          logging.warning('Default value for repeated field %s is not being '
                          'written to proto file' % field.name)
        else:
          # Convert default value to string.
          if field.variant == messages.Variant.MESSAGE:
            logging.warning(
              'Message field %s should not have default values' % field.name)
            default = None
          elif field.variant == messages.Variant.STRING:
            default = repr(field.default_value.encode('utf-8'))
          elif field.variant == messages.Variant.BYTES:
            default = repr(field.default_value)
          else:
            default = str(field.default_value)

          if default is not None:
            default_format = ' [default=%s]' % default

      if field.variant in (messages.Variant.MESSAGE, messages.Variant.ENUM):
        field_type = field.type_name
      else:
        field_type = str(field.variant).lower()
            
      out << '%s %s %s = %s%s;' % (str(field.label).lower(),
                                   field_type,
                                   field.name,
                                   field.number,
                                   default_format)

  def write_messages(message_descriptors):
    """Write nested and non-nested Message types.

    Args:
      message_descriptors: List of MessageDescriptor objects from which to
        generate messages.
    """
    for message in message_descriptors or []:
      out << ''
      out << ''
      out << 'message %s {' % message.name

      with out.indent():
        if message.enum_types:
          write_enums(message.enum_types)

        if message.message_types:
          write_messages(message.message_types)

        if message.fields:
          write_fields(message.fields)

      out << '}'

  write_messages(file_descriptor.message_types)