OLD | NEW |
1 # Protocol Buffers - Google's data interchange format | 1 # Protocol Buffers - Google's data interchange format |
2 # Copyright 2008 Google Inc. All rights reserved. | 2 # Copyright 2008 Google Inc. All rights reserved. |
3 # http://code.google.com/p/protobuf/ | 3 # http://code.google.com/p/protobuf/ |
4 # | 4 # |
5 # Redistribution and use in source and binary forms, with or without | 5 # Redistribution and use in source and binary forms, with or without |
6 # modification, are permitted provided that the following conditions are | 6 # modification, are permitted provided that the following conditions are |
7 # met: | 7 # met: |
8 # | 8 # |
9 # * Redistributions of source code must retain the above copyright | 9 # * Redistributions of source code must retain the above copyright |
10 # notice, this list of conditions and the following disclaimer. | 10 # notice, this list of conditions and the following disclaimer. |
(...skipping 18 matching lines...) Expand all Loading... |
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
31 """Contains helper functions used to create protocol message classes from | 31 """Contains helper functions used to create protocol message classes from |
32 Descriptor objects at runtime backed by the protocol buffer C++ API. | 32 Descriptor objects at runtime backed by the protocol buffer C++ API. |
33 """ | 33 """ |
34 | 34 |
35 __author__ = 'petar@google.com (Petar Petrov)' | 35 __author__ = 'petar@google.com (Petar Petrov)' |
36 | 36 |
37 import copy_reg | 37 import copy_reg |
38 import operator | 38 import operator |
39 from google.protobuf.internal import _net_proto2___python | 39 from protobuf26.internal import _net_proto2___python |
40 from google.protobuf.internal import enum_type_wrapper | 40 from protobuf26.internal import enum_type_wrapper |
41 from google.protobuf import message | 41 from protobuf26 import message |
42 | 42 |
43 | 43 |
44 _LABEL_REPEATED = _net_proto2___python.LABEL_REPEATED | 44 _LABEL_REPEATED = _net_proto2___python.LABEL_REPEATED |
45 _LABEL_OPTIONAL = _net_proto2___python.LABEL_OPTIONAL | 45 _LABEL_OPTIONAL = _net_proto2___python.LABEL_OPTIONAL |
46 _CPPTYPE_MESSAGE = _net_proto2___python.CPPTYPE_MESSAGE | 46 _CPPTYPE_MESSAGE = _net_proto2___python.CPPTYPE_MESSAGE |
47 _TYPE_MESSAGE = _net_proto2___python.TYPE_MESSAGE | 47 _TYPE_MESSAGE = _net_proto2___python.TYPE_MESSAGE |
48 | 48 |
49 | 49 |
50 def GetDescriptorPool(): | 50 def GetDescriptorPool(): |
51 """Creates a new DescriptorPool C++ object.""" | 51 """Creates a new DescriptorPool C++ object.""" |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 | 292 |
293 class ExtensionDict(object): | 293 class ExtensionDict(object): |
294 """Extension dictionary added to each protocol message.""" | 294 """Extension dictionary added to each protocol message.""" |
295 | 295 |
296 def __init__(self, msg): | 296 def __init__(self, msg): |
297 self._message = msg | 297 self._message = msg |
298 self._cmsg = msg._cmsg | 298 self._cmsg = msg._cmsg |
299 self._values = {} | 299 self._values = {} |
300 | 300 |
301 def __setitem__(self, extension, value): | 301 def __setitem__(self, extension, value): |
302 from google.protobuf import descriptor | 302 from protobuf26 import descriptor |
303 if not isinstance(extension, descriptor.FieldDescriptor): | 303 if not isinstance(extension, descriptor.FieldDescriptor): |
304 raise KeyError('Bad extension %r.' % (extension,)) | 304 raise KeyError('Bad extension %r.' % (extension,)) |
305 cdescriptor = extension._cdescriptor | 305 cdescriptor = extension._cdescriptor |
306 if (cdescriptor.label != _LABEL_OPTIONAL or | 306 if (cdescriptor.label != _LABEL_OPTIONAL or |
307 cdescriptor.cpp_type == _CPPTYPE_MESSAGE): | 307 cdescriptor.cpp_type == _CPPTYPE_MESSAGE): |
308 raise TypeError('Extension %r is repeated and/or a composite type.' % ( | 308 raise TypeError('Extension %r is repeated and/or a composite type.' % ( |
309 extension.full_name,)) | 309 extension.full_name,)) |
310 self._cmsg.SetScalar(cdescriptor, value) | 310 self._cmsg.SetScalar(cdescriptor, value) |
311 self._values[extension] = value | 311 self._values[extension] = value |
312 | 312 |
313 def __getitem__(self, extension): | 313 def __getitem__(self, extension): |
314 from google.protobuf import descriptor | 314 from protobuf26 import descriptor |
315 if not isinstance(extension, descriptor.FieldDescriptor): | 315 if not isinstance(extension, descriptor.FieldDescriptor): |
316 raise KeyError('Bad extension %r.' % (extension,)) | 316 raise KeyError('Bad extension %r.' % (extension,)) |
317 | 317 |
318 cdescriptor = extension._cdescriptor | 318 cdescriptor = extension._cdescriptor |
319 if (cdescriptor.label != _LABEL_REPEATED and | 319 if (cdescriptor.label != _LABEL_REPEATED and |
320 cdescriptor.cpp_type != _CPPTYPE_MESSAGE): | 320 cdescriptor.cpp_type != _CPPTYPE_MESSAGE): |
321 return self._cmsg.GetScalar(cdescriptor) | 321 return self._cmsg.GetScalar(cdescriptor) |
322 | 322 |
323 ext = self._values.get(extension, None) | 323 ext = self._values.get(extension, None) |
324 if ext is not None: | 324 if ext is not None: |
325 return ext | 325 return ext |
326 | 326 |
327 ext = self._CreateNewHandle(extension) | 327 ext = self._CreateNewHandle(extension) |
328 self._values[extension] = ext | 328 self._values[extension] = ext |
329 return ext | 329 return ext |
330 | 330 |
331 def ClearExtension(self, extension): | 331 def ClearExtension(self, extension): |
332 from google.protobuf import descriptor | 332 from protobuf26 import descriptor |
333 if not isinstance(extension, descriptor.FieldDescriptor): | 333 if not isinstance(extension, descriptor.FieldDescriptor): |
334 raise KeyError('Bad extension %r.' % (extension,)) | 334 raise KeyError('Bad extension %r.' % (extension,)) |
335 self._cmsg.ClearFieldByDescriptor(extension._cdescriptor) | 335 self._cmsg.ClearFieldByDescriptor(extension._cdescriptor) |
336 if extension in self._values: | 336 if extension in self._values: |
337 del self._values[extension] | 337 del self._values[extension] |
338 | 338 |
339 def HasExtension(self, extension): | 339 def HasExtension(self, extension): |
340 from google.protobuf import descriptor | 340 from protobuf26 import descriptor |
341 if not isinstance(extension, descriptor.FieldDescriptor): | 341 if not isinstance(extension, descriptor.FieldDescriptor): |
342 raise KeyError('Bad extension %r.' % (extension,)) | 342 raise KeyError('Bad extension %r.' % (extension,)) |
343 return self._cmsg.HasFieldByDescriptor(extension._cdescriptor) | 343 return self._cmsg.HasFieldByDescriptor(extension._cdescriptor) |
344 | 344 |
345 def _FindExtensionByName(self, name): | 345 def _FindExtensionByName(self, name): |
346 """Tries to find a known extension with the specified name. | 346 """Tries to find a known extension with the specified name. |
347 | 347 |
348 Args: | 348 Args: |
349 name: Extension full name. | 349 name: Extension full name. |
350 | 350 |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
620 return self.ListFields() == other.ListFields() | 620 return self.ListFields() == other.ListFields() |
621 | 621 |
622 def __ne__(self, other): | 622 def __ne__(self, other): |
623 return not self == other | 623 return not self == other |
624 | 624 |
625 def __hash__(self): | 625 def __hash__(self): |
626 raise TypeError('unhashable object') | 626 raise TypeError('unhashable object') |
627 | 627 |
628 def __unicode__(self): | 628 def __unicode__(self): |
629 # Lazy import to prevent circular import when text_format imports this file. | 629 # Lazy import to prevent circular import when text_format imports this file. |
630 from google.protobuf import text_format | 630 from protobuf26 import text_format |
631 return text_format.MessageToString(self, as_utf8=True).decode('utf-8') | 631 return text_format.MessageToString(self, as_utf8=True).decode('utf-8') |
632 | 632 |
633 # Attach the local methods to the message class. | 633 # Attach the local methods to the message class. |
634 for key, value in locals().copy().iteritems(): | 634 for key, value in locals().copy().iteritems(): |
635 if key not in ('key', 'value', '__builtins__', '__name__', '__doc__'): | 635 if key not in ('key', 'value', '__builtins__', '__name__', '__doc__'): |
636 setattr(cls, key, value) | 636 setattr(cls, key, value) |
637 | 637 |
638 # Static methods: | 638 # Static methods: |
639 | 639 |
640 def RegisterExtension(extension_handle): | 640 def RegisterExtension(extension_handle): |
(...skipping 13 matching lines...) Expand all Loading... |
654 cls.FromString = staticmethod(FromString) | 654 cls.FromString = staticmethod(FromString) |
655 | 655 |
656 | 656 |
657 | 657 |
658 def _AddPropertiesForExtensions(message_descriptor, cls): | 658 def _AddPropertiesForExtensions(message_descriptor, cls): |
659 """Adds properties for all fields in this protocol message type.""" | 659 """Adds properties for all fields in this protocol message type.""" |
660 extension_dict = message_descriptor.extensions_by_name | 660 extension_dict = message_descriptor.extensions_by_name |
661 for extension_name, extension_field in extension_dict.iteritems(): | 661 for extension_name, extension_field in extension_dict.iteritems(): |
662 constant_name = extension_name.upper() + '_FIELD_NUMBER' | 662 constant_name = extension_name.upper() + '_FIELD_NUMBER' |
663 setattr(cls, constant_name, extension_field.number) | 663 setattr(cls, constant_name, extension_field.number) |
OLD | NEW |