matlab-prodserver-client 24.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. google/__init__.py +7 -0
  2. google/protobuf_ml/__init__.py +33 -0
  3. google/protobuf_ml/descriptor.py +1230 -0
  4. google/protobuf_ml/descriptor_database.py +177 -0
  5. google/protobuf_ml/descriptor_pb2.py +1925 -0
  6. google/protobuf_ml/descriptor_pool.py +1295 -0
  7. google/protobuf_ml/internal/__init__.py +0 -0
  8. google/protobuf_ml/internal/_parameterized.py +443 -0
  9. google/protobuf_ml/internal/api_implementation.py +168 -0
  10. google/protobuf_ml/internal/builder.py +130 -0
  11. google/protobuf_ml/internal/containers.py +708 -0
  12. google/protobuf_ml/internal/decoder.py +1068 -0
  13. google/protobuf_ml/internal/descriptor_database_test.py +127 -0
  14. google/protobuf_ml/internal/descriptor_pool_test.py +1149 -0
  15. google/protobuf_ml/internal/descriptor_test.py +1076 -0
  16. google/protobuf_ml/internal/encoder.py +829 -0
  17. google/protobuf_ml/internal/enum_type_wrapper.py +124 -0
  18. google/protobuf_ml/internal/extension_dict.py +213 -0
  19. google/protobuf_ml/internal/generator_test.py +354 -0
  20. google/protobuf_ml/internal/import_test.py +49 -0
  21. google/protobuf_ml/internal/json_format_test.py +1285 -0
  22. google/protobuf_ml/internal/keywords_test.py +103 -0
  23. google/protobuf_ml/internal/message_factory_test.py +299 -0
  24. google/protobuf_ml/internal/message_listener.py +78 -0
  25. google/protobuf_ml/internal/message_test.py +2573 -0
  26. google/protobuf_ml/internal/proto_builder_test.py +106 -0
  27. google/protobuf_ml/internal/python_message.py +1539 -0
  28. google/protobuf_ml/internal/reflection_test.py +3381 -0
  29. google/protobuf_ml/internal/service_reflection_test.py +139 -0
  30. google/protobuf_ml/internal/symbol_database_test.py +133 -0
  31. google/protobuf_ml/internal/test_util.py +878 -0
  32. google/protobuf_ml/internal/testing_refleaks.py +142 -0
  33. google/protobuf_ml/internal/text_encoding_test.py +67 -0
  34. google/protobuf_ml/internal/text_format_test.py +2447 -0
  35. google/protobuf_ml/internal/type_checkers.py +435 -0
  36. google/protobuf_ml/internal/unknown_fields_test.py +461 -0
  37. google/protobuf_ml/internal/well_known_types.py +887 -0
  38. google/protobuf_ml/internal/well_known_types_test.py +1013 -0
  39. google/protobuf_ml/internal/wire_format.py +268 -0
  40. google/protobuf_ml/internal/wire_format_test.py +252 -0
  41. google/protobuf_ml/json_format.py +912 -0
  42. google/protobuf_ml/message.py +424 -0
  43. google/protobuf_ml/message_factory.py +189 -0
  44. google/protobuf_ml/proto_builder.py +134 -0
  45. google/protobuf_ml/reflection.py +95 -0
  46. google/protobuf_ml/service.py +228 -0
  47. google/protobuf_ml/service_reflection.py +295 -0
  48. google/protobuf_ml/symbol_database.py +194 -0
  49. google/protobuf_ml/text_encoding.py +110 -0
  50. google/protobuf_ml/text_format.py +1842 -0
  51. google/protobuf_ml/unknown_fields.py +120 -0
  52. matlab/__init__.py +43 -0
  53. matlab/bin/win64/libmwfoundation_matlabdata.dll +0 -0
  54. matlab/bin/win64/libmwfoundation_matlabdata_standalone.dll +0 -0
  55. matlab/extern/bin/win64/libMatlabDataArray.dll +0 -0
  56. matlab/extern/bin/win64/matlabmultidimarrayforpython.cp310-win_amd64.pyd +0 -0
  57. matlab/extern/bin/win64/matlabmultidimarrayforpython.cp39-win_amd64.pyd +0 -0
  58. matlab/extern/bin/win64/matlabmultidimarrayforpython.pyd +0 -0
  59. matlab/production_server/__init__.py +2 -0
  60. matlab/production_server/_internal_mps/__init__.py +2 -0
  61. matlab/production_server/_internal_mps/feval_handler.py +76 -0
  62. matlab/production_server/_internal_mps/invocation_handler_utils.py +52 -0
  63. matlab/production_server/_internal_mps/matlab_array_builder.py +201 -0
  64. matlab/production_server/_internal_mps/matlab_response_builder.py +134 -0
  65. matlab/production_server/_proto_generated/MATLABArray_pb2.py +129 -0
  66. matlab/production_server/_proto_generated/MATLABError_pb2.py +27 -0
  67. matlab/production_server/_proto_generated/MATLABParams_pb2.py +26 -0
  68. matlab/production_server/_proto_generated/MATLABResult_pb2.py +27 -0
  69. matlab/production_server/_proto_generated/__init__.py +0 -0
  70. matlab/production_server/client.py +140 -0
  71. matlab/production_server/mpsexceptions.py +56 -0
  72. matlab_prodserver_client-24.2.dist-info/LICENSE.txt +27 -0
  73. matlab_prodserver_client-24.2.dist-info/METADATA +94 -0
  74. matlab_prodserver_client-24.2.dist-info/RECORD +77 -0
  75. matlab_prodserver_client-24.2.dist-info/WHEEL +5 -0
  76. matlab_prodserver_client-24.2.dist-info/top_level.txt +3 -0
  77. six.py +953 -0
@@ -0,0 +1,424 @@
1
+ # Protocol Buffers - Google's data interchange format
2
+ # Copyright 2008 Google Inc. All rights reserved.
3
+ # https://developers.google.com/protocol-buffers/
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
+
31
+ # TODO(robinson): We should just make these methods all "pure-virtual" and move
32
+ # all implementation out, into reflection.py for now.
33
+
34
+
35
+ """Contains an abstract base class for protocol messages."""
36
+
37
+ __author__ = 'robinson@google.com (Will Robinson)'
38
+
39
+ class Error(Exception):
40
+ """Base error type for this module."""
41
+ pass
42
+
43
+
44
+ class DecodeError(Error):
45
+ """Exception raised when deserializing messages."""
46
+ pass
47
+
48
+
49
+ class EncodeError(Error):
50
+ """Exception raised when serializing messages."""
51
+ pass
52
+
53
+
54
+ class Message(object):
55
+
56
+ """Abstract base class for protocol messages.
57
+
58
+ Protocol message classes are almost always generated by the protocol
59
+ compiler. These generated types subclass Message and implement the methods
60
+ shown below.
61
+ """
62
+
63
+ # TODO(robinson): Link to an HTML document here.
64
+
65
+ # TODO(robinson): Document that instances of this class will also
66
+ # have an Extensions attribute with __getitem__ and __setitem__.
67
+ # Again, not sure how to best convey this.
68
+
69
+ # TODO(robinson): Document that the class must also have a static
70
+ # RegisterExtension(extension_field) method.
71
+ # Not sure how to best express at this point.
72
+
73
+ # TODO(robinson): Document these fields and methods.
74
+
75
+ __slots__ = []
76
+
77
+ #: The :class:`google.protobuf_ml.descriptor.Descriptor` for this message type.
78
+ DESCRIPTOR = None
79
+
80
+ def __deepcopy__(self, memo=None):
81
+ clone = type(self)()
82
+ clone.MergeFrom(self)
83
+ return clone
84
+
85
+ def __eq__(self, other_msg):
86
+ """Recursively compares two messages by value and structure."""
87
+ raise NotImplementedError
88
+
89
+ def __ne__(self, other_msg):
90
+ # Can't just say self != other_msg, since that would infinitely recurse. :)
91
+ return not self == other_msg
92
+
93
+ def __hash__(self):
94
+ raise TypeError('unhashable object')
95
+
96
+ def __str__(self):
97
+ """Outputs a human-readable representation of the message."""
98
+ raise NotImplementedError
99
+
100
+ def __unicode__(self):
101
+ """Outputs a human-readable representation of the message."""
102
+ raise NotImplementedError
103
+
104
+ def MergeFrom(self, other_msg):
105
+ """Merges the contents of the specified message into current message.
106
+
107
+ This method merges the contents of the specified message into the current
108
+ message. Singular fields that are set in the specified message overwrite
109
+ the corresponding fields in the current message. Repeated fields are
110
+ appended. Singular sub-messages and groups are recursively merged.
111
+
112
+ Args:
113
+ other_msg (Message): A message to merge into the current message.
114
+ """
115
+ raise NotImplementedError
116
+
117
+ def CopyFrom(self, other_msg):
118
+ """Copies the content of the specified message into the current message.
119
+
120
+ The method clears the current message and then merges the specified
121
+ message using MergeFrom.
122
+
123
+ Args:
124
+ other_msg (Message): A message to copy into the current one.
125
+ """
126
+ if self is other_msg:
127
+ return
128
+ self.Clear()
129
+ self.MergeFrom(other_msg)
130
+
131
+ def Clear(self):
132
+ """Clears all data that was set in the message."""
133
+ raise NotImplementedError
134
+
135
+ def SetInParent(self):
136
+ """Mark this as present in the parent.
137
+
138
+ This normally happens automatically when you assign a field of a
139
+ sub-message, but sometimes you want to make the sub-message
140
+ present while keeping it empty. If you find yourself using this,
141
+ you may want to reconsider your design.
142
+ """
143
+ raise NotImplementedError
144
+
145
+ def IsInitialized(self):
146
+ """Checks if the message is initialized.
147
+
148
+ Returns:
149
+ bool: The method returns True if the message is initialized (i.e. all of
150
+ its required fields are set).
151
+ """
152
+ raise NotImplementedError
153
+
154
+ # TODO(robinson): MergeFromString() should probably return None and be
155
+ # implemented in terms of a helper that returns the # of bytes read. Our
156
+ # deserialization routines would use the helper when recursively
157
+ # deserializing, but the end user would almost always just want the no-return
158
+ # MergeFromString().
159
+
160
+ def MergeFromString(self, serialized):
161
+ """Merges serialized protocol buffer data into this message.
162
+
163
+ When we find a field in `serialized` that is already present
164
+ in this message:
165
+
166
+ - If it's a "repeated" field, we append to the end of our list.
167
+ - Else, if it's a scalar, we overwrite our field.
168
+ - Else, (it's a nonrepeated composite), we recursively merge
169
+ into the existing composite.
170
+
171
+ Args:
172
+ serialized (bytes): Any object that allows us to call
173
+ ``memoryview(serialized)`` to access a string of bytes using the
174
+ buffer interface.
175
+
176
+ Returns:
177
+ int: The number of bytes read from `serialized`.
178
+ For non-group messages, this will always be `len(serialized)`,
179
+ but for messages which are actually groups, this will
180
+ generally be less than `len(serialized)`, since we must
181
+ stop when we reach an ``END_GROUP`` tag. Note that if
182
+ we *do* stop because of an ``END_GROUP`` tag, the number
183
+ of bytes returned does not include the bytes
184
+ for the ``END_GROUP`` tag information.
185
+
186
+ Raises:
187
+ DecodeError: if the input cannot be parsed.
188
+ """
189
+ # TODO(robinson): Document handling of unknown fields.
190
+ # TODO(robinson): When we switch to a helper, this will return None.
191
+ raise NotImplementedError
192
+
193
+ def ParseFromString(self, serialized):
194
+ """Parse serialized protocol buffer data into this message.
195
+
196
+ Like :func:`MergeFromString()`, except we clear the object first.
197
+
198
+ Raises:
199
+ message.DecodeError if the input cannot be parsed.
200
+ """
201
+ self.Clear()
202
+ return self.MergeFromString(serialized)
203
+
204
+ def SerializeToString(self, **kwargs):
205
+ """Serializes the protocol message to a binary string.
206
+
207
+ Keyword Args:
208
+ deterministic (bool): If true, requests deterministic serialization
209
+ of the protobuf, with predictable ordering of map keys.
210
+
211
+ Returns:
212
+ A binary string representation of the message if all of the required
213
+ fields in the message are set (i.e. the message is initialized).
214
+
215
+ Raises:
216
+ EncodeError: if the message isn't initialized (see :func:`IsInitialized`).
217
+ """
218
+ raise NotImplementedError
219
+
220
+ def SerializePartialToString(self, **kwargs):
221
+ """Serializes the protocol message to a binary string.
222
+
223
+ This method is similar to SerializeToString but doesn't check if the
224
+ message is initialized.
225
+
226
+ Keyword Args:
227
+ deterministic (bool): If true, requests deterministic serialization
228
+ of the protobuf, with predictable ordering of map keys.
229
+
230
+ Returns:
231
+ bytes: A serialized representation of the partial message.
232
+ """
233
+ raise NotImplementedError
234
+
235
+ # TODO(robinson): Decide whether we like these better
236
+ # than auto-generated has_foo() and clear_foo() methods
237
+ # on the instances themselves. This way is less consistent
238
+ # with C++, but it makes reflection-type access easier and
239
+ # reduces the number of magically autogenerated things.
240
+ #
241
+ # TODO(robinson): Be sure to document (and test) exactly
242
+ # which field names are accepted here. Are we case-sensitive?
243
+ # What do we do with fields that share names with Python keywords
244
+ # like 'lambda' and 'yield'?
245
+ #
246
+ # nnorwitz says:
247
+ # """
248
+ # Typically (in python), an underscore is appended to names that are
249
+ # keywords. So they would become lambda_ or yield_.
250
+ # """
251
+ def ListFields(self):
252
+ """Returns a list of (FieldDescriptor, value) tuples for present fields.
253
+
254
+ A message field is non-empty if HasField() would return true. A singular
255
+ primitive field is non-empty if HasField() would return true in proto2 or it
256
+ is non zero in proto3. A repeated field is non-empty if it contains at least
257
+ one element. The fields are ordered by field number.
258
+
259
+ Returns:
260
+ list[tuple(FieldDescriptor, value)]: field descriptors and values
261
+ for all fields in the message which are not empty. The values vary by
262
+ field type.
263
+ """
264
+ raise NotImplementedError
265
+
266
+ def HasField(self, field_name):
267
+ """Checks if a certain field is set for the message.
268
+
269
+ For a oneof group, checks if any field inside is set. Note that if the
270
+ field_name is not defined in the message descriptor, :exc:`ValueError` will
271
+ be raised.
272
+
273
+ Args:
274
+ field_name (str): The name of the field to check for presence.
275
+
276
+ Returns:
277
+ bool: Whether a value has been set for the named field.
278
+
279
+ Raises:
280
+ ValueError: if the `field_name` is not a member of this message.
281
+ """
282
+ raise NotImplementedError
283
+
284
+ def ClearField(self, field_name):
285
+ """Clears the contents of a given field.
286
+
287
+ Inside a oneof group, clears the field set. If the name neither refers to a
288
+ defined field or oneof group, :exc:`ValueError` is raised.
289
+
290
+ Args:
291
+ field_name (str): The name of the field to check for presence.
292
+
293
+ Raises:
294
+ ValueError: if the `field_name` is not a member of this message.
295
+ """
296
+ raise NotImplementedError
297
+
298
+ def WhichOneof(self, oneof_group):
299
+ """Returns the name of the field that is set inside a oneof group.
300
+
301
+ If no field is set, returns None.
302
+
303
+ Args:
304
+ oneof_group (str): the name of the oneof group to check.
305
+
306
+ Returns:
307
+ str or None: The name of the group that is set, or None.
308
+
309
+ Raises:
310
+ ValueError: no group with the given name exists
311
+ """
312
+ raise NotImplementedError
313
+
314
+ def HasExtension(self, extension_handle):
315
+ """Checks if a certain extension is present for this message.
316
+
317
+ Extensions are retrieved using the :attr:`Extensions` mapping (if present).
318
+
319
+ Args:
320
+ extension_handle: The handle for the extension to check.
321
+
322
+ Returns:
323
+ bool: Whether the extension is present for this message.
324
+
325
+ Raises:
326
+ KeyError: if the extension is repeated. Similar to repeated fields,
327
+ there is no separate notion of presence: a "not present" repeated
328
+ extension is an empty list.
329
+ """
330
+ raise NotImplementedError
331
+
332
+ def ClearExtension(self, extension_handle):
333
+ """Clears the contents of a given extension.
334
+
335
+ Args:
336
+ extension_handle: The handle for the extension to clear.
337
+ """
338
+ raise NotImplementedError
339
+
340
+ def UnknownFields(self):
341
+ """Returns the UnknownFieldSet.
342
+
343
+ Returns:
344
+ UnknownFieldSet: The unknown fields stored in this message.
345
+ """
346
+ raise NotImplementedError
347
+
348
+ def DiscardUnknownFields(self):
349
+ """Clears all fields in the :class:`UnknownFieldSet`.
350
+
351
+ This operation is recursive for nested message.
352
+ """
353
+ raise NotImplementedError
354
+
355
+ def ByteSize(self):
356
+ """Returns the serialized size of this message.
357
+
358
+ Recursively calls ByteSize() on all contained messages.
359
+
360
+ Returns:
361
+ int: The number of bytes required to serialize this message.
362
+ """
363
+ raise NotImplementedError
364
+
365
+ @classmethod
366
+ def FromString(cls, s):
367
+ raise NotImplementedError
368
+
369
+ @staticmethod
370
+ def RegisterExtension(extension_handle):
371
+ raise NotImplementedError
372
+
373
+ def _SetListener(self, message_listener):
374
+ """Internal method used by the protocol message implementation.
375
+ Clients should not call this directly.
376
+
377
+ Sets a listener that this message will call on certain state transitions.
378
+
379
+ The purpose of this method is to register back-edges from children to
380
+ parents at runtime, for the purpose of setting "has" bits and
381
+ byte-size-dirty bits in the parent and ancestor objects whenever a child or
382
+ descendant object is modified.
383
+
384
+ If the client wants to disconnect this Message from the object tree, she
385
+ explicitly sets callback to None.
386
+
387
+ If message_listener is None, unregisters any existing listener. Otherwise,
388
+ message_listener must implement the MessageListener interface in
389
+ internal/message_listener.py, and we discard any listener registered
390
+ via a previous _SetListener() call.
391
+ """
392
+ raise NotImplementedError
393
+
394
+ def __getstate__(self):
395
+ """Support the pickle protocol."""
396
+ return dict(serialized=self.SerializePartialToString())
397
+
398
+ def __setstate__(self, state):
399
+ """Support the pickle protocol."""
400
+ self.__init__()
401
+ serialized = state['serialized']
402
+ # On Python 3, using encoding='latin1' is required for unpickling
403
+ # protos pickled by Python 2.
404
+ if not isinstance(serialized, bytes):
405
+ serialized = serialized.encode('latin1')
406
+ self.ParseFromString(serialized)
407
+
408
+ def __reduce__(self):
409
+ message_descriptor = self.DESCRIPTOR
410
+ if message_descriptor.containing_type is None:
411
+ return type(self), (), self.__getstate__()
412
+ # the message type must be nested.
413
+ # Python does not pickle nested classes; use the symbol_database on the
414
+ # receiving end.
415
+ container = message_descriptor
416
+ return (_InternalConstructMessage, (container.full_name,),
417
+ self.__getstate__())
418
+
419
+
420
+ def _InternalConstructMessage(full_name):
421
+ """Constructs a nested message."""
422
+ from google.protobuf_ml import symbol_database # pylint:disable=g-import-not-at-top
423
+
424
+ return symbol_database.Default().GetSymbol(full_name)()
@@ -0,0 +1,189 @@
1
+ # Protocol Buffers - Google's data interchange format
2
+ # Copyright 2008 Google Inc. All rights reserved.
3
+ # https://developers.google.com/protocol-buffers/
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
+
31
+ """Provides a factory class for generating dynamic messages.
32
+
33
+ The easiest way to use this class is if you have access to the FileDescriptor
34
+ protos containing the messages you want to create you can just do the following:
35
+
36
+ message_classes = message_factory.GetMessages(iterable_of_file_descriptors)
37
+ my_proto_instance = message_classes['some.proto.package.MessageName']()
38
+ """
39
+
40
+ __author__ = 'matthewtoia@google.com (Matt Toia)'
41
+
42
+ from google.protobuf_ml.internal import api_implementation
43
+ from google.protobuf_ml import descriptor_pool
44
+ from google.protobuf_ml import message
45
+
46
+ if api_implementation.Type() == 'python':
47
+ from google.protobuf_ml.internal import python_message as message_impl
48
+ else:
49
+ from google.protobuf_ml.pyext import cpp_message as message_impl # pylint: disable=g-import-not-at-top
50
+
51
+
52
+ # The type of all Message classes.
53
+ _GENERATED_PROTOCOL_MESSAGE_TYPE = message_impl.GeneratedProtocolMessageType
54
+
55
+
56
+ class MessageFactory(object):
57
+ """Factory for creating Proto2 messages from descriptors in a pool."""
58
+
59
+ def __init__(self, pool=None):
60
+ """Initializes a new factory."""
61
+ self.pool = pool or descriptor_pool.DescriptorPool()
62
+
63
+ # local cache of all classes built from protobuf descriptors
64
+ self._classes = {}
65
+
66
+ def GetPrototype(self, descriptor):
67
+ """Obtains a proto2 message class based on the passed in descriptor.
68
+
69
+ Passing a descriptor with a fully qualified name matching a previous
70
+ invocation will cause the same class to be returned.
71
+
72
+ Args:
73
+ descriptor: The descriptor to build from.
74
+
75
+ Returns:
76
+ A class describing the passed in descriptor.
77
+ """
78
+ if descriptor not in self._classes:
79
+ result_class = self.CreatePrototype(descriptor)
80
+ # The assignment to _classes is redundant for the base implementation, but
81
+ # might avoid confusion in cases where CreatePrototype gets overridden and
82
+ # does not call the base implementation.
83
+ self._classes[descriptor] = result_class
84
+ return result_class
85
+ return self._classes[descriptor]
86
+
87
+ def CreatePrototype(self, descriptor):
88
+ """Builds a proto2 message class based on the passed in descriptor.
89
+
90
+ Don't call this function directly, it always creates a new class. Call
91
+ GetPrototype() instead. This method is meant to be overridden in subblasses
92
+ to perform additional operations on the newly constructed class.
93
+
94
+ Args:
95
+ descriptor: The descriptor to build from.
96
+
97
+ Returns:
98
+ A class describing the passed in descriptor.
99
+ """
100
+ descriptor_name = descriptor.name
101
+ result_class = _GENERATED_PROTOCOL_MESSAGE_TYPE(
102
+ descriptor_name,
103
+ (message.Message,),
104
+ {
105
+ 'DESCRIPTOR': descriptor,
106
+ # If module not set, it wrongly points to message_factory module.
107
+ '__module__': None,
108
+ })
109
+ result_class._FACTORY = self # pylint: disable=protected-access
110
+ # Assign in _classes before doing recursive calls to avoid infinite
111
+ # recursion.
112
+ self._classes[descriptor] = result_class
113
+ for field in descriptor.fields:
114
+ if field.message_type:
115
+ self.GetPrototype(field.message_type)
116
+ for extension in result_class.DESCRIPTOR.extensions:
117
+ if extension.containing_type not in self._classes:
118
+ self.GetPrototype(extension.containing_type)
119
+ extended_class = self._classes[extension.containing_type]
120
+ extended_class.RegisterExtension(extension)
121
+ if extension.message_type:
122
+ self.GetPrototype(extension.message_type)
123
+ return result_class
124
+
125
+ def GetMessages(self, files):
126
+ """Gets all the messages from a specified file.
127
+
128
+ This will find and resolve dependencies, failing if the descriptor
129
+ pool cannot satisfy them.
130
+
131
+ Args:
132
+ files: The file names to extract messages from.
133
+
134
+ Returns:
135
+ A dictionary mapping proto names to the message classes. This will include
136
+ any dependent messages as well as any messages defined in the same file as
137
+ a specified message.
138
+ """
139
+ result = {}
140
+ for file_name in files:
141
+ file_desc = self.pool.FindFileByName(file_name)
142
+ for desc in file_desc.message_types_by_name.values():
143
+ result[desc.full_name] = self.GetPrototype(desc)
144
+
145
+ # While the extension FieldDescriptors are created by the descriptor pool,
146
+ # the python classes created in the factory need them to be registered
147
+ # explicitly, which is done below.
148
+ #
149
+ # The call to RegisterExtension will specifically check if the
150
+ # extension was already registered on the object and either
151
+ # ignore the registration if the original was the same, or raise
152
+ # an error if they were different.
153
+
154
+ for extension in file_desc.extensions_by_name.values():
155
+ if extension.containing_type not in self._classes:
156
+ self.GetPrototype(extension.containing_type)
157
+ extended_class = self._classes[extension.containing_type]
158
+ extended_class.RegisterExtension(extension)
159
+ if extension.message_type:
160
+ self.GetPrototype(extension.message_type)
161
+ return result
162
+
163
+
164
+ _FACTORY = MessageFactory()
165
+
166
+
167
+ def GetMessages(file_protos):
168
+ """Builds a dictionary of all the messages available in a set of files.
169
+
170
+ Args:
171
+ file_protos: Iterable of FileDescriptorProto to build messages out of.
172
+
173
+ Returns:
174
+ A dictionary mapping proto names to the message classes. This will include
175
+ any dependent messages as well as any messages defined in the same file as
176
+ a specified message.
177
+ """
178
+ # The cpp implementation of the protocol buffer library requires to add the
179
+ # message in topological order of the dependency graph.
180
+ file_by_name = {file_proto.name: file_proto for file_proto in file_protos}
181
+ def _AddFile(file_proto):
182
+ for dependency in file_proto.dependency:
183
+ if dependency in file_by_name:
184
+ # Remove from elements to be visited, in order to cut cycles.
185
+ _AddFile(file_by_name.pop(dependency))
186
+ _FACTORY.pool.Add(file_proto)
187
+ while file_by_name:
188
+ _AddFile(file_by_name.popitem()[1])
189
+ return _FACTORY.GetMessages([file_proto.name for file_proto in file_protos])