PageRenderTime 164ms CodeModel.GetById 15ms RepoModel.GetById 7ms app.codeStats 0ms

/thirdparty/breakpad/third_party/protobuf/protobuf/python/google/protobuf/internal/cpp_message.py

http://github.com/tomahawk-player/tomahawk
Python | 616 lines | 467 code | 85 blank | 64 comment | 28 complexity | 399a14e7b3a48efa83766c2cb6b94f76 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  1. # Protocol Buffers - Google's data interchange format
  2. # Copyright 2008 Google Inc. All rights reserved.
  3. # http://code.google.com/p/protobuf/
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are
  7. # met:
  8. #
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above
  12. # copyright notice, this list of conditions and the following disclaimer
  13. # in the documentation and/or other materials provided with the
  14. # distribution.
  15. # * Neither the name of Google Inc. nor the names of its
  16. # contributors may be used to endorse or promote products derived from
  17. # this software without specific prior written permission.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. """Contains helper functions used to create protocol message classes from
  31. Descriptor objects at runtime backed by the protocol buffer C++ API.
  32. """
  33. __author__ = 'petar@google.com (Petar Petrov)'
  34. import operator
  35. from google.protobuf.internal import _net_proto2___python
  36. from google.protobuf import message
  37. _LABEL_REPEATED = _net_proto2___python.LABEL_REPEATED
  38. _LABEL_OPTIONAL = _net_proto2___python.LABEL_OPTIONAL
  39. _CPPTYPE_MESSAGE = _net_proto2___python.CPPTYPE_MESSAGE
  40. _TYPE_MESSAGE = _net_proto2___python.TYPE_MESSAGE
  41. def GetDescriptorPool():
  42. """Creates a new DescriptorPool C++ object."""
  43. return _net_proto2___python.NewCDescriptorPool()
  44. _pool = GetDescriptorPool()
  45. def GetFieldDescriptor(full_field_name):
  46. """Searches for a field descriptor given a full field name."""
  47. return _pool.FindFieldByName(full_field_name)
  48. def BuildFile(content):
  49. """Registers a new proto file in the underlying C++ descriptor pool."""
  50. _net_proto2___python.BuildFile(content)
  51. def GetExtensionDescriptor(full_extension_name):
  52. """Searches for extension descriptor given a full field name."""
  53. return _pool.FindExtensionByName(full_extension_name)
  54. def NewCMessage(full_message_name):
  55. """Creates a new C++ protocol message by its name."""
  56. return _net_proto2___python.NewCMessage(full_message_name)
  57. def ScalarProperty(cdescriptor):
  58. """Returns a scalar property for the given descriptor."""
  59. def Getter(self):
  60. return self._cmsg.GetScalar(cdescriptor)
  61. def Setter(self, value):
  62. self._cmsg.SetScalar(cdescriptor, value)
  63. return property(Getter, Setter)
  64. def CompositeProperty(cdescriptor, message_type):
  65. """Returns a Python property the given composite field."""
  66. def Getter(self):
  67. sub_message = self._composite_fields.get(cdescriptor.name, None)
  68. if sub_message is None:
  69. cmessage = self._cmsg.NewSubMessage(cdescriptor)
  70. sub_message = message_type._concrete_class(__cmessage=cmessage)
  71. self._composite_fields[cdescriptor.name] = sub_message
  72. return sub_message
  73. return property(Getter)
  74. class RepeatedScalarContainer(object):
  75. """Container for repeated scalar fields."""
  76. __slots__ = ['_message', '_cfield_descriptor', '_cmsg']
  77. def __init__(self, msg, cfield_descriptor):
  78. self._message = msg
  79. self._cmsg = msg._cmsg
  80. self._cfield_descriptor = cfield_descriptor
  81. def append(self, value):
  82. self._cmsg.AddRepeatedScalar(
  83. self._cfield_descriptor, value)
  84. def extend(self, sequence):
  85. for element in sequence:
  86. self.append(element)
  87. def insert(self, key, value):
  88. values = self[slice(None, None, None)]
  89. values.insert(key, value)
  90. self._cmsg.AssignRepeatedScalar(self._cfield_descriptor, values)
  91. def remove(self, value):
  92. values = self[slice(None, None, None)]
  93. values.remove(value)
  94. self._cmsg.AssignRepeatedScalar(self._cfield_descriptor, values)
  95. def __setitem__(self, key, value):
  96. values = self[slice(None, None, None)]
  97. values[key] = value
  98. self._cmsg.AssignRepeatedScalar(self._cfield_descriptor, values)
  99. def __getitem__(self, key):
  100. return self._cmsg.GetRepeatedScalar(self._cfield_descriptor, key)
  101. def __delitem__(self, key):
  102. self._cmsg.DeleteRepeatedField(self._cfield_descriptor, key)
  103. def __len__(self):
  104. return len(self[slice(None, None, None)])
  105. def __eq__(self, other):
  106. if self is other:
  107. return True
  108. if not operator.isSequenceType(other):
  109. raise TypeError(
  110. 'Can only compare repeated scalar fields against sequences.')
  111. # We are presumably comparing against some other sequence type.
  112. return other == self[slice(None, None, None)]
  113. def __ne__(self, other):
  114. return not self == other
  115. def __hash__(self):
  116. raise TypeError('unhashable object')
  117. def sort(self, sort_function=cmp):
  118. values = self[slice(None, None, None)]
  119. values.sort(sort_function)
  120. self._cmsg.AssignRepeatedScalar(self._cfield_descriptor, values)
  121. def RepeatedScalarProperty(cdescriptor):
  122. """Returns a Python property the given repeated scalar field."""
  123. def Getter(self):
  124. container = self._composite_fields.get(cdescriptor.name, None)
  125. if container is None:
  126. container = RepeatedScalarContainer(self, cdescriptor)
  127. self._composite_fields[cdescriptor.name] = container
  128. return container
  129. def Setter(self, new_value):
  130. raise AttributeError('Assignment not allowed to repeated field '
  131. '"%s" in protocol message object.' % cdescriptor.name)
  132. doc = 'Magic attribute generated for "%s" proto field.' % cdescriptor.name
  133. return property(Getter, Setter, doc=doc)
  134. class RepeatedCompositeContainer(object):
  135. """Container for repeated composite fields."""
  136. __slots__ = ['_message', '_subclass', '_cfield_descriptor', '_cmsg']
  137. def __init__(self, msg, cfield_descriptor, subclass):
  138. self._message = msg
  139. self._cmsg = msg._cmsg
  140. self._subclass = subclass
  141. self._cfield_descriptor = cfield_descriptor
  142. def add(self, **kwargs):
  143. cmessage = self._cmsg.AddMessage(self._cfield_descriptor)
  144. return self._subclass(__cmessage=cmessage, __owner=self._message, **kwargs)
  145. def extend(self, elem_seq):
  146. """Extends by appending the given sequence of elements of the same type
  147. as this one, copying each individual message.
  148. """
  149. for message in elem_seq:
  150. self.add().MergeFrom(message)
  151. def MergeFrom(self, other):
  152. for message in other[:]:
  153. self.add().MergeFrom(message)
  154. def __getitem__(self, key):
  155. cmessages = self._cmsg.GetRepeatedMessage(
  156. self._cfield_descriptor, key)
  157. subclass = self._subclass
  158. if not isinstance(cmessages, list):
  159. return subclass(__cmessage=cmessages, __owner=self._message)
  160. return [subclass(__cmessage=m, __owner=self._message) for m in cmessages]
  161. def __delitem__(self, key):
  162. self._cmsg.DeleteRepeatedField(
  163. self._cfield_descriptor, key)
  164. def __len__(self):
  165. return self._cmsg.FieldLength(self._cfield_descriptor)
  166. def __eq__(self, other):
  167. """Compares the current instance with another one."""
  168. if self is other:
  169. return True
  170. if not isinstance(other, self.__class__):
  171. raise TypeError('Can only compare repeated composite fields against '
  172. 'other repeated composite fields.')
  173. messages = self[slice(None, None, None)]
  174. other_messages = other[slice(None, None, None)]
  175. return messages == other_messages
  176. def __hash__(self):
  177. raise TypeError('unhashable object')
  178. def sort(self, sort_function=cmp):
  179. messages = []
  180. for index in range(len(self)):
  181. # messages[i][0] is where the i-th element of the new array has to come
  182. # from.
  183. # messages[i][1] is where the i-th element of the old array has to go.
  184. messages.append([index, 0, self[index]])
  185. messages.sort(lambda x,y: sort_function(x[2], y[2]))
  186. # Remember which position each elements has to move to.
  187. for i in range(len(messages)):
  188. messages[messages[i][0]][1] = i
  189. # Apply the transposition.
  190. for i in range(len(messages)):
  191. from_position = messages[i][0]
  192. if i == from_position:
  193. continue
  194. self._cmsg.SwapRepeatedFieldElements(
  195. self._cfield_descriptor, i, from_position)
  196. messages[messages[i][1]][0] = from_position
  197. def RepeatedCompositeProperty(cdescriptor, message_type):
  198. """Returns a Python property for the given repeated composite field."""
  199. def Getter(self):
  200. container = self._composite_fields.get(cdescriptor.name, None)
  201. if container is None:
  202. container = RepeatedCompositeContainer(
  203. self, cdescriptor, message_type._concrete_class)
  204. self._composite_fields[cdescriptor.name] = container
  205. return container
  206. def Setter(self, new_value):
  207. raise AttributeError('Assignment not allowed to repeated field '
  208. '"%s" in protocol message object.' % cdescriptor.name)
  209. doc = 'Magic attribute generated for "%s" proto field.' % cdescriptor.name
  210. return property(Getter, Setter, doc=doc)
  211. class ExtensionDict(object):
  212. """Extension dictionary added to each protocol message."""
  213. def __init__(self, msg):
  214. self._message = msg
  215. self._cmsg = msg._cmsg
  216. self._values = {}
  217. def __setitem__(self, extension, value):
  218. from google.protobuf import descriptor
  219. if not isinstance(extension, descriptor.FieldDescriptor):
  220. raise KeyError('Bad extension %r.' % (extension,))
  221. cdescriptor = extension._cdescriptor
  222. if (cdescriptor.label != _LABEL_OPTIONAL or
  223. cdescriptor.cpp_type == _CPPTYPE_MESSAGE):
  224. raise TypeError('Extension %r is repeated and/or a composite type.' % (
  225. extension.full_name,))
  226. self._cmsg.SetScalar(cdescriptor, value)
  227. self._values[extension] = value
  228. def __getitem__(self, extension):
  229. from google.protobuf import descriptor
  230. if not isinstance(extension, descriptor.FieldDescriptor):
  231. raise KeyError('Bad extension %r.' % (extension,))
  232. cdescriptor = extension._cdescriptor
  233. if (cdescriptor.label != _LABEL_REPEATED and
  234. cdescriptor.cpp_type != _CPPTYPE_MESSAGE):
  235. return self._cmsg.GetScalar(cdescriptor)
  236. ext = self._values.get(extension, None)
  237. if ext is not None:
  238. return ext
  239. ext = self._CreateNewHandle(extension)
  240. self._values[extension] = ext
  241. return ext
  242. def ClearExtension(self, extension):
  243. from google.protobuf import descriptor
  244. if not isinstance(extension, descriptor.FieldDescriptor):
  245. raise KeyError('Bad extension %r.' % (extension,))
  246. self._cmsg.ClearFieldByDescriptor(extension._cdescriptor)
  247. if extension in self._values:
  248. del self._values[extension]
  249. def HasExtension(self, extension):
  250. from google.protobuf import descriptor
  251. if not isinstance(extension, descriptor.FieldDescriptor):
  252. raise KeyError('Bad extension %r.' % (extension,))
  253. return self._cmsg.HasFieldByDescriptor(extension._cdescriptor)
  254. def _FindExtensionByName(self, name):
  255. """Tries to find a known extension with the specified name.
  256. Args:
  257. name: Extension full name.
  258. Returns:
  259. Extension field descriptor.
  260. """
  261. return self._message._extensions_by_name.get(name, None)
  262. def _CreateNewHandle(self, extension):
  263. cdescriptor = extension._cdescriptor
  264. if (cdescriptor.label != _LABEL_REPEATED and
  265. cdescriptor.cpp_type == _CPPTYPE_MESSAGE):
  266. cmessage = self._cmsg.NewSubMessage(cdescriptor)
  267. return extension.message_type._concrete_class(__cmessage=cmessage)
  268. if cdescriptor.label == _LABEL_REPEATED:
  269. if cdescriptor.cpp_type == _CPPTYPE_MESSAGE:
  270. return RepeatedCompositeContainer(
  271. self._message, cdescriptor, extension.message_type._concrete_class)
  272. else:
  273. return RepeatedScalarContainer(self._message, cdescriptor)
  274. # This shouldn't happen!
  275. assert False
  276. return None
  277. def NewMessage(message_descriptor, dictionary):
  278. """Creates a new protocol message *class*."""
  279. _AddClassAttributesForNestedExtensions(message_descriptor, dictionary)
  280. _AddEnumValues(message_descriptor, dictionary)
  281. _AddDescriptors(message_descriptor, dictionary)
  282. def InitMessage(message_descriptor, cls):
  283. """Constructs a new message instance (called before instance's __init__)."""
  284. cls._extensions_by_name = {}
  285. _AddInitMethod(message_descriptor, cls)
  286. _AddMessageMethods(message_descriptor, cls)
  287. _AddPropertiesForExtensions(message_descriptor, cls)
  288. def _AddDescriptors(message_descriptor, dictionary):
  289. """Sets up a new protocol message class dictionary.
  290. Args:
  291. message_descriptor: A Descriptor instance describing this message type.
  292. dictionary: Class dictionary to which we'll add a '__slots__' entry.
  293. """
  294. dictionary['__descriptors'] = {}
  295. for field in message_descriptor.fields:
  296. dictionary['__descriptors'][field.name] = GetFieldDescriptor(
  297. field.full_name)
  298. dictionary['__slots__'] = list(dictionary['__descriptors'].iterkeys()) + [
  299. '_cmsg', '_owner', '_composite_fields', 'Extensions']
  300. def _AddEnumValues(message_descriptor, dictionary):
  301. """Sets class-level attributes for all enum fields defined in this message.
  302. Args:
  303. message_descriptor: Descriptor object for this message type.
  304. dictionary: Class dictionary that should be populated.
  305. """
  306. for enum_type in message_descriptor.enum_types:
  307. for enum_value in enum_type.values:
  308. dictionary[enum_value.name] = enum_value.number
  309. def _AddClassAttributesForNestedExtensions(message_descriptor, dictionary):
  310. """Adds class attributes for the nested extensions."""
  311. extension_dict = message_descriptor.extensions_by_name
  312. for extension_name, extension_field in extension_dict.iteritems():
  313. assert extension_name not in dictionary
  314. dictionary[extension_name] = extension_field
  315. def _AddInitMethod(message_descriptor, cls):
  316. """Adds an __init__ method to cls."""
  317. # Create and attach message field properties to the message class.
  318. # This can be done just once per message class, since property setters and
  319. # getters are passed the message instance.
  320. # This makes message instantiation extremely fast, and at the same time it
  321. # doesn't require the creation of property objects for each message instance,
  322. # which saves a lot of memory.
  323. for field in message_descriptor.fields:
  324. field_cdescriptor = cls.__descriptors[field.name]
  325. if field.label == _LABEL_REPEATED:
  326. if field.cpp_type == _CPPTYPE_MESSAGE:
  327. value = RepeatedCompositeProperty(field_cdescriptor, field.message_type)
  328. else:
  329. value = RepeatedScalarProperty(field_cdescriptor)
  330. elif field.cpp_type == _CPPTYPE_MESSAGE:
  331. value = CompositeProperty(field_cdescriptor, field.message_type)
  332. else:
  333. value = ScalarProperty(field_cdescriptor)
  334. setattr(cls, field.name, value)
  335. # Attach a constant with the field number.
  336. constant_name = field.name.upper() + '_FIELD_NUMBER'
  337. setattr(cls, constant_name, field.number)
  338. def Init(self, **kwargs):
  339. """Message constructor."""
  340. cmessage = kwargs.pop('__cmessage', None)
  341. if cmessage is None:
  342. self._cmsg = NewCMessage(message_descriptor.full_name)
  343. else:
  344. self._cmsg = cmessage
  345. # Keep a reference to the owner, as the owner keeps a reference to the
  346. # underlying protocol buffer message.
  347. owner = kwargs.pop('__owner', None)
  348. if owner is not None:
  349. self._owner = owner
  350. self.Extensions = ExtensionDict(self)
  351. self._composite_fields = {}
  352. for field_name, field_value in kwargs.iteritems():
  353. field_cdescriptor = self.__descriptors.get(field_name, None)
  354. if field_cdescriptor is None:
  355. raise ValueError('Protocol message has no "%s" field.' % field_name)
  356. if field_cdescriptor.label == _LABEL_REPEATED:
  357. if field_cdescriptor.cpp_type == _CPPTYPE_MESSAGE:
  358. for val in field_value:
  359. getattr(self, field_name).add().MergeFrom(val)
  360. else:
  361. getattr(self, field_name).extend(field_value)
  362. elif field_cdescriptor.cpp_type == _CPPTYPE_MESSAGE:
  363. getattr(self, field_name).MergeFrom(field_value)
  364. else:
  365. setattr(self, field_name, field_value)
  366. Init.__module__ = None
  367. Init.__doc__ = None
  368. cls.__init__ = Init
  369. def _IsMessageSetExtension(field):
  370. """Checks if a field is a message set extension."""
  371. return (field.is_extension and
  372. field.containing_type.has_options and
  373. field.containing_type.GetOptions().message_set_wire_format and
  374. field.type == _TYPE_MESSAGE and
  375. field.message_type == field.extension_scope and
  376. field.label == _LABEL_OPTIONAL)
  377. def _AddMessageMethods(message_descriptor, cls):
  378. """Adds the methods to a protocol message class."""
  379. if message_descriptor.is_extendable:
  380. def ClearExtension(self, extension):
  381. self.Extensions.ClearExtension(extension)
  382. def HasExtension(self, extension):
  383. return self.Extensions.HasExtension(extension)
  384. def HasField(self, field_name):
  385. return self._cmsg.HasField(field_name)
  386. def ClearField(self, field_name):
  387. if field_name in self._composite_fields:
  388. del self._composite_fields[field_name]
  389. self._cmsg.ClearField(field_name)
  390. def Clear(self):
  391. return self._cmsg.Clear()
  392. def IsInitialized(self, errors=None):
  393. if self._cmsg.IsInitialized():
  394. return True
  395. if errors is not None:
  396. errors.extend(self.FindInitializationErrors());
  397. return False
  398. def SerializeToString(self):
  399. if not self.IsInitialized():
  400. raise message.EncodeError(
  401. 'Message is missing required fields: ' +
  402. ','.join(self.FindInitializationErrors()))
  403. return self._cmsg.SerializeToString()
  404. def SerializePartialToString(self):
  405. return self._cmsg.SerializePartialToString()
  406. def ParseFromString(self, serialized):
  407. self.Clear()
  408. self.MergeFromString(serialized)
  409. def MergeFromString(self, serialized):
  410. byte_size = self._cmsg.MergeFromString(serialized)
  411. if byte_size < 0:
  412. raise message.DecodeError('Unable to merge from string.')
  413. return byte_size
  414. def MergeFrom(self, msg):
  415. if not isinstance(msg, cls):
  416. raise TypeError(
  417. "Parameter to MergeFrom() must be instance of same class.")
  418. self._cmsg.MergeFrom(msg._cmsg)
  419. def CopyFrom(self, msg):
  420. self._cmsg.CopyFrom(msg._cmsg)
  421. def ByteSize(self):
  422. return self._cmsg.ByteSize()
  423. def SetInParent(self):
  424. return self._cmsg.SetInParent()
  425. def ListFields(self):
  426. all_fields = []
  427. field_list = self._cmsg.ListFields()
  428. fields_by_name = cls.DESCRIPTOR.fields_by_name
  429. for is_extension, field_name in field_list:
  430. if is_extension:
  431. extension = cls._extensions_by_name[field_name]
  432. all_fields.append((extension, self.Extensions[extension]))
  433. else:
  434. field_descriptor = fields_by_name[field_name]
  435. all_fields.append(
  436. (field_descriptor, getattr(self, field_name)))
  437. all_fields.sort(key=lambda item: item[0].number)
  438. return all_fields
  439. def FindInitializationErrors(self):
  440. return self._cmsg.FindInitializationErrors()
  441. def __str__(self):
  442. return self._cmsg.DebugString()
  443. def __eq__(self, other):
  444. if self is other:
  445. return True
  446. if not isinstance(other, self.__class__):
  447. return False
  448. return self.ListFields() == other.ListFields()
  449. def __ne__(self, other):
  450. return not self == other
  451. def __hash__(self):
  452. raise TypeError('unhashable object')
  453. def __unicode__(self):
  454. return text_format.MessageToString(self, as_utf8=True).decode('utf-8')
  455. # Attach the local methods to the message class.
  456. for key, value in locals().copy().iteritems():
  457. if key not in ('key', 'value', '__builtins__', '__name__', '__doc__'):
  458. setattr(cls, key, value)
  459. # Static methods:
  460. def RegisterExtension(extension_handle):
  461. extension_handle.containing_type = cls.DESCRIPTOR
  462. cls._extensions_by_name[extension_handle.full_name] = extension_handle
  463. if _IsMessageSetExtension(extension_handle):
  464. # MessageSet extension. Also register under type name.
  465. cls._extensions_by_name[
  466. extension_handle.message_type.full_name] = extension_handle
  467. cls.RegisterExtension = staticmethod(RegisterExtension)
  468. def FromString(string):
  469. msg = cls()
  470. msg.MergeFromString(string)
  471. return msg
  472. cls.FromString = staticmethod(FromString)
  473. def _AddPropertiesForExtensions(message_descriptor, cls):
  474. """Adds properties for all fields in this protocol message type."""
  475. extension_dict = message_descriptor.extensions_by_name
  476. for extension_name, extension_field in extension_dict.iteritems():
  477. constant_name = extension_name.upper() + '_FIELD_NUMBER'
  478. setattr(cls, constant_name, extension_field.number)