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,295 @@
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
+ """Contains metaclasses used to create protocol service and service stub
32
+ classes from ServiceDescriptor objects at runtime.
33
+
34
+ The GeneratedServiceType and GeneratedServiceStubType metaclasses are used to
35
+ inject all useful functionality into the classes output by the protocol
36
+ compiler at compile-time.
37
+ """
38
+
39
+ __author__ = 'petar@google.com (Petar Petrov)'
40
+
41
+
42
+ class GeneratedServiceType(type):
43
+
44
+ """Metaclass for service classes created at runtime from ServiceDescriptors.
45
+
46
+ Implementations for all methods described in the Service class are added here
47
+ by this class. We also create properties to allow getting/setting all fields
48
+ in the protocol message.
49
+
50
+ The protocol compiler currently uses this metaclass to create protocol service
51
+ classes at runtime. Clients can also manually create their own classes at
52
+ runtime, as in this example::
53
+
54
+ mydescriptor = ServiceDescriptor(.....)
55
+ class MyProtoService(service.Service):
56
+ __metaclass__ = GeneratedServiceType
57
+ DESCRIPTOR = mydescriptor
58
+ myservice_instance = MyProtoService()
59
+ # ...
60
+ """
61
+
62
+ _DESCRIPTOR_KEY = 'DESCRIPTOR'
63
+
64
+ def __init__(cls, name, bases, dictionary):
65
+ """Creates a message service class.
66
+
67
+ Args:
68
+ name: Name of the class (ignored, but required by the metaclass
69
+ protocol).
70
+ bases: Base classes of the class being constructed.
71
+ dictionary: The class dictionary of the class being constructed.
72
+ dictionary[_DESCRIPTOR_KEY] must contain a ServiceDescriptor object
73
+ describing this protocol service type.
74
+ """
75
+ # Don't do anything if this class doesn't have a descriptor. This happens
76
+ # when a service class is subclassed.
77
+ if GeneratedServiceType._DESCRIPTOR_KEY not in dictionary:
78
+ return
79
+
80
+ descriptor = dictionary[GeneratedServiceType._DESCRIPTOR_KEY]
81
+ service_builder = _ServiceBuilder(descriptor)
82
+ service_builder.BuildService(cls)
83
+ cls.DESCRIPTOR = descriptor
84
+
85
+
86
+ class GeneratedServiceStubType(GeneratedServiceType):
87
+
88
+ """Metaclass for service stubs created at runtime from ServiceDescriptors.
89
+
90
+ This class has similar responsibilities as GeneratedServiceType, except that
91
+ it creates the service stub classes.
92
+ """
93
+
94
+ _DESCRIPTOR_KEY = 'DESCRIPTOR'
95
+
96
+ def __init__(cls, name, bases, dictionary):
97
+ """Creates a message service stub class.
98
+
99
+ Args:
100
+ name: Name of the class (ignored, here).
101
+ bases: Base classes of the class being constructed.
102
+ dictionary: The class dictionary of the class being constructed.
103
+ dictionary[_DESCRIPTOR_KEY] must contain a ServiceDescriptor object
104
+ describing this protocol service type.
105
+ """
106
+ super(GeneratedServiceStubType, cls).__init__(name, bases, dictionary)
107
+ # Don't do anything if this class doesn't have a descriptor. This happens
108
+ # when a service stub is subclassed.
109
+ if GeneratedServiceStubType._DESCRIPTOR_KEY not in dictionary:
110
+ return
111
+
112
+ descriptor = dictionary[GeneratedServiceStubType._DESCRIPTOR_KEY]
113
+ service_stub_builder = _ServiceStubBuilder(descriptor)
114
+ service_stub_builder.BuildServiceStub(cls)
115
+
116
+
117
+ class _ServiceBuilder(object):
118
+
119
+ """This class constructs a protocol service class using a service descriptor.
120
+
121
+ Given a service descriptor, this class constructs a class that represents
122
+ the specified service descriptor. One service builder instance constructs
123
+ exactly one service class. That means all instances of that class share the
124
+ same builder.
125
+ """
126
+
127
+ def __init__(self, service_descriptor):
128
+ """Initializes an instance of the service class builder.
129
+
130
+ Args:
131
+ service_descriptor: ServiceDescriptor to use when constructing the
132
+ service class.
133
+ """
134
+ self.descriptor = service_descriptor
135
+
136
+ def BuildService(builder, cls):
137
+ """Constructs the service class.
138
+
139
+ Args:
140
+ cls: The class that will be constructed.
141
+ """
142
+
143
+ # CallMethod needs to operate with an instance of the Service class. This
144
+ # internal wrapper function exists only to be able to pass the service
145
+ # instance to the method that does the real CallMethod work.
146
+ # Making sure to use exact argument names from the abstract interface in
147
+ # service.py to match the type signature
148
+ def _WrapCallMethod(self, method_descriptor, rpc_controller, request, done):
149
+ return builder._CallMethod(self, method_descriptor, rpc_controller,
150
+ request, done)
151
+
152
+ def _WrapGetRequestClass(self, method_descriptor):
153
+ return builder._GetRequestClass(method_descriptor)
154
+
155
+ def _WrapGetResponseClass(self, method_descriptor):
156
+ return builder._GetResponseClass(method_descriptor)
157
+
158
+ builder.cls = cls
159
+ cls.CallMethod = _WrapCallMethod
160
+ cls.GetDescriptor = staticmethod(lambda: builder.descriptor)
161
+ cls.GetDescriptor.__doc__ = 'Returns the service descriptor.'
162
+ cls.GetRequestClass = _WrapGetRequestClass
163
+ cls.GetResponseClass = _WrapGetResponseClass
164
+ for method in builder.descriptor.methods:
165
+ setattr(cls, method.name, builder._GenerateNonImplementedMethod(method))
166
+
167
+ def _CallMethod(self, srvc, method_descriptor,
168
+ rpc_controller, request, callback):
169
+ """Calls the method described by a given method descriptor.
170
+
171
+ Args:
172
+ srvc: Instance of the service for which this method is called.
173
+ method_descriptor: Descriptor that represent the method to call.
174
+ rpc_controller: RPC controller to use for this method's execution.
175
+ request: Request protocol message.
176
+ callback: A callback to invoke after the method has completed.
177
+ """
178
+ if method_descriptor.containing_service != self.descriptor:
179
+ raise RuntimeError(
180
+ 'CallMethod() given method descriptor for wrong service type.')
181
+ method = getattr(srvc, method_descriptor.name)
182
+ return method(rpc_controller, request, callback)
183
+
184
+ def _GetRequestClass(self, method_descriptor):
185
+ """Returns the class of the request protocol message.
186
+
187
+ Args:
188
+ method_descriptor: Descriptor of the method for which to return the
189
+ request protocol message class.
190
+
191
+ Returns:
192
+ A class that represents the input protocol message of the specified
193
+ method.
194
+ """
195
+ if method_descriptor.containing_service != self.descriptor:
196
+ raise RuntimeError(
197
+ 'GetRequestClass() given method descriptor for wrong service type.')
198
+ return method_descriptor.input_type._concrete_class
199
+
200
+ def _GetResponseClass(self, method_descriptor):
201
+ """Returns the class of the response protocol message.
202
+
203
+ Args:
204
+ method_descriptor: Descriptor of the method for which to return the
205
+ response protocol message class.
206
+
207
+ Returns:
208
+ A class that represents the output protocol message of the specified
209
+ method.
210
+ """
211
+ if method_descriptor.containing_service != self.descriptor:
212
+ raise RuntimeError(
213
+ 'GetResponseClass() given method descriptor for wrong service type.')
214
+ return method_descriptor.output_type._concrete_class
215
+
216
+ def _GenerateNonImplementedMethod(self, method):
217
+ """Generates and returns a method that can be set for a service methods.
218
+
219
+ Args:
220
+ method: Descriptor of the service method for which a method is to be
221
+ generated.
222
+
223
+ Returns:
224
+ A method that can be added to the service class.
225
+ """
226
+ return lambda inst, rpc_controller, request, callback: (
227
+ self._NonImplementedMethod(method.name, rpc_controller, callback))
228
+
229
+ def _NonImplementedMethod(self, method_name, rpc_controller, callback):
230
+ """The body of all methods in the generated service class.
231
+
232
+ Args:
233
+ method_name: Name of the method being executed.
234
+ rpc_controller: RPC controller used to execute this method.
235
+ callback: A callback which will be invoked when the method finishes.
236
+ """
237
+ rpc_controller.SetFailed('Method %s not implemented.' % method_name)
238
+ callback(None)
239
+
240
+
241
+ class _ServiceStubBuilder(object):
242
+
243
+ """Constructs a protocol service stub class using a service descriptor.
244
+
245
+ Given a service descriptor, this class constructs a suitable stub class.
246
+ A stub is just a type-safe wrapper around an RpcChannel which emulates a
247
+ local implementation of the service.
248
+
249
+ One service stub builder instance constructs exactly one class. It means all
250
+ instances of that class share the same service stub builder.
251
+ """
252
+
253
+ def __init__(self, service_descriptor):
254
+ """Initializes an instance of the service stub class builder.
255
+
256
+ Args:
257
+ service_descriptor: ServiceDescriptor to use when constructing the
258
+ stub class.
259
+ """
260
+ self.descriptor = service_descriptor
261
+
262
+ def BuildServiceStub(self, cls):
263
+ """Constructs the stub class.
264
+
265
+ Args:
266
+ cls: The class that will be constructed.
267
+ """
268
+
269
+ def _ServiceStubInit(stub, rpc_channel):
270
+ stub.rpc_channel = rpc_channel
271
+ self.cls = cls
272
+ cls.__init__ = _ServiceStubInit
273
+ for method in self.descriptor.methods:
274
+ setattr(cls, method.name, self._GenerateStubMethod(method))
275
+
276
+ def _GenerateStubMethod(self, method):
277
+ return (lambda inst, rpc_controller, request, callback=None:
278
+ self._StubMethod(inst, method, rpc_controller, request, callback))
279
+
280
+ def _StubMethod(self, stub, method_descriptor,
281
+ rpc_controller, request, callback):
282
+ """The body of all service methods in the generated stub class.
283
+
284
+ Args:
285
+ stub: Stub instance.
286
+ method_descriptor: Descriptor of the invoked method.
287
+ rpc_controller: Rpc controller to execute the method.
288
+ request: Request protocol message.
289
+ callback: A callback to execute when the method finishes.
290
+ Returns:
291
+ Response message (in case of blocking call).
292
+ """
293
+ return stub.rpc_channel.CallMethod(
294
+ method_descriptor, rpc_controller, request,
295
+ method_descriptor.output_type._concrete_class, callback)
@@ -0,0 +1,194 @@
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
+ """A database of Python protocol buffer generated symbols.
32
+
33
+ SymbolDatabase is the MessageFactory for messages generated at compile time,
34
+ and makes it easy to create new instances of a registered type, given only the
35
+ type's protocol buffer symbol name.
36
+
37
+ Example usage::
38
+
39
+ db = symbol_database.SymbolDatabase()
40
+
41
+ # Register symbols of interest, from one or multiple files.
42
+ db.RegisterFileDescriptor(my_proto_pb2.DESCRIPTOR)
43
+ db.RegisterMessage(my_proto_pb2.MyMessage)
44
+ db.RegisterEnumDescriptor(my_proto_pb2.MyEnum.DESCRIPTOR)
45
+
46
+ # The database can be used as a MessageFactory, to generate types based on
47
+ # their name:
48
+ types = db.GetMessages(['my_proto.proto'])
49
+ my_message_instance = types['MyMessage']()
50
+
51
+ # The database's underlying descriptor pool can be queried, so it's not
52
+ # necessary to know a type's filename to be able to generate it:
53
+ filename = db.pool.FindFileContainingSymbol('MyMessage')
54
+ my_message_instance = db.GetMessages([filename])['MyMessage']()
55
+
56
+ # This functionality is also provided directly via a convenience method:
57
+ my_message_instance = db.GetSymbol('MyMessage')()
58
+ """
59
+
60
+
61
+ from google.protobuf_ml.internal import api_implementation
62
+ from google.protobuf_ml import descriptor_pool
63
+ from google.protobuf_ml import message_factory
64
+
65
+
66
+ class SymbolDatabase(message_factory.MessageFactory):
67
+ """A database of Python generated symbols."""
68
+
69
+ def RegisterMessage(self, message):
70
+ """Registers the given message type in the local database.
71
+
72
+ Calls to GetSymbol() and GetMessages() will return messages registered here.
73
+
74
+ Args:
75
+ message: A :class:`google.protobuf_ml.message.Message` subclass (or
76
+ instance); its descriptor will be registered.
77
+
78
+ Returns:
79
+ The provided message.
80
+ """
81
+
82
+ desc = message.DESCRIPTOR
83
+ self._classes[desc] = message
84
+ self.RegisterMessageDescriptor(desc)
85
+ return message
86
+
87
+ def RegisterMessageDescriptor(self, message_descriptor):
88
+ """Registers the given message descriptor in the local database.
89
+
90
+ Args:
91
+ message_descriptor (Descriptor): the message descriptor to add.
92
+ """
93
+ if api_implementation.Type() == 'python':
94
+ # pylint: disable=protected-access
95
+ self.pool._AddDescriptor(message_descriptor)
96
+
97
+ def RegisterEnumDescriptor(self, enum_descriptor):
98
+ """Registers the given enum descriptor in the local database.
99
+
100
+ Args:
101
+ enum_descriptor (EnumDescriptor): The enum descriptor to register.
102
+
103
+ Returns:
104
+ EnumDescriptor: The provided descriptor.
105
+ """
106
+ if api_implementation.Type() == 'python':
107
+ # pylint: disable=protected-access
108
+ self.pool._AddEnumDescriptor(enum_descriptor)
109
+ return enum_descriptor
110
+
111
+ def RegisterServiceDescriptor(self, service_descriptor):
112
+ """Registers the given service descriptor in the local database.
113
+
114
+ Args:
115
+ service_descriptor (ServiceDescriptor): the service descriptor to
116
+ register.
117
+ """
118
+ if api_implementation.Type() == 'python':
119
+ # pylint: disable=protected-access
120
+ self.pool._AddServiceDescriptor(service_descriptor)
121
+
122
+ def RegisterFileDescriptor(self, file_descriptor):
123
+ """Registers the given file descriptor in the local database.
124
+
125
+ Args:
126
+ file_descriptor (FileDescriptor): The file descriptor to register.
127
+ """
128
+ if api_implementation.Type() == 'python':
129
+ # pylint: disable=protected-access
130
+ self.pool._InternalAddFileDescriptor(file_descriptor)
131
+
132
+ def GetSymbol(self, symbol):
133
+ """Tries to find a symbol in the local database.
134
+
135
+ Currently, this method only returns message.Message instances, however, if
136
+ may be extended in future to support other symbol types.
137
+
138
+ Args:
139
+ symbol (str): a protocol buffer symbol.
140
+
141
+ Returns:
142
+ A Python class corresponding to the symbol.
143
+
144
+ Raises:
145
+ KeyError: if the symbol could not be found.
146
+ """
147
+
148
+ return self._classes[self.pool.FindMessageTypeByName(symbol)]
149
+
150
+ def GetMessages(self, files):
151
+ # TODO(amauryfa): Fix the differences with MessageFactory.
152
+ """Gets all registered messages from a specified file.
153
+
154
+ Only messages already created and registered will be returned; (this is the
155
+ case for imported _pb2 modules)
156
+ But unlike MessageFactory, this version also returns already defined nested
157
+ messages, but does not register any message extensions.
158
+
159
+ Args:
160
+ files (list[str]): The file names to extract messages from.
161
+
162
+ Returns:
163
+ A dictionary mapping proto names to the message classes.
164
+
165
+ Raises:
166
+ KeyError: if a file could not be found.
167
+ """
168
+
169
+ def _GetAllMessages(desc):
170
+ """Walk a message Descriptor and recursively yields all message names."""
171
+ yield desc
172
+ for msg_desc in desc.nested_types:
173
+ for nested_desc in _GetAllMessages(msg_desc):
174
+ yield nested_desc
175
+
176
+ result = {}
177
+ for file_name in files:
178
+ file_desc = self.pool.FindFileByName(file_name)
179
+ for msg_desc in file_desc.message_types_by_name.values():
180
+ for desc in _GetAllMessages(msg_desc):
181
+ try:
182
+ result[desc.full_name] = self._classes[desc]
183
+ except KeyError:
184
+ # This descriptor has no registered class, skip it.
185
+ pass
186
+ return result
187
+
188
+
189
+ _DEFAULT = SymbolDatabase(pool=descriptor_pool.Default())
190
+
191
+
192
+ def Default():
193
+ """Returns the default SymbolDatabase."""
194
+ return _DEFAULT
@@ -0,0 +1,110 @@
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
+ """Encoding related utilities."""
32
+ import re
33
+
34
+ _cescape_chr_to_symbol_map = {}
35
+ _cescape_chr_to_symbol_map[9] = r'\t' # optional escape
36
+ _cescape_chr_to_symbol_map[10] = r'\n' # optional escape
37
+ _cescape_chr_to_symbol_map[13] = r'\r' # optional escape
38
+ _cescape_chr_to_symbol_map[34] = r'\"' # necessary escape
39
+ _cescape_chr_to_symbol_map[39] = r"\'" # optional escape
40
+ _cescape_chr_to_symbol_map[92] = r'\\' # necessary escape
41
+
42
+ # Lookup table for unicode
43
+ _cescape_unicode_to_str = [chr(i) for i in range(0, 256)]
44
+ for byte, string in _cescape_chr_to_symbol_map.items():
45
+ _cescape_unicode_to_str[byte] = string
46
+
47
+ # Lookup table for non-utf8, with necessary escapes at (o >= 127 or o < 32)
48
+ _cescape_byte_to_str = ([r'\%03o' % i for i in range(0, 32)] +
49
+ [chr(i) for i in range(32, 127)] +
50
+ [r'\%03o' % i for i in range(127, 256)])
51
+ for byte, string in _cescape_chr_to_symbol_map.items():
52
+ _cescape_byte_to_str[byte] = string
53
+ del byte, string
54
+
55
+
56
+ def CEscape(text, as_utf8):
57
+ # type: (...) -> str
58
+ """Escape a bytes string for use in an text protocol buffer.
59
+
60
+ Args:
61
+ text: A byte string to be escaped.
62
+ as_utf8: Specifies if result may contain non-ASCII characters.
63
+ In Python 3 this allows unescaped non-ASCII Unicode characters.
64
+ In Python 2 the return value will be valid UTF-8 rather than only ASCII.
65
+ Returns:
66
+ Escaped string (str).
67
+ """
68
+ # Python's text.encode() 'string_escape' or 'unicode_escape' codecs do not
69
+ # satisfy our needs; they encodes unprintable characters using two-digit hex
70
+ # escapes whereas our C++ unescaping function allows hex escapes to be any
71
+ # length. So, "\0011".encode('string_escape') ends up being "\\x011", which
72
+ # will be decoded in C++ as a single-character string with char code 0x11.
73
+ text_is_unicode = isinstance(text, str)
74
+ if as_utf8 and text_is_unicode:
75
+ # We're already unicode, no processing beyond control char escapes.
76
+ return text.translate(_cescape_chr_to_symbol_map)
77
+ ord_ = ord if text_is_unicode else lambda x: x # bytes iterate as ints.
78
+ if as_utf8:
79
+ return ''.join(_cescape_unicode_to_str[ord_(c)] for c in text)
80
+ return ''.join(_cescape_byte_to_str[ord_(c)] for c in text)
81
+
82
+
83
+ _CUNESCAPE_HEX = re.compile(r'(\\+)x([0-9a-fA-F])(?![0-9a-fA-F])')
84
+
85
+
86
+ def CUnescape(text):
87
+ # type: (str) -> bytes
88
+ """Unescape a text string with C-style escape sequences to UTF-8 bytes.
89
+
90
+ Args:
91
+ text: The data to parse in a str.
92
+ Returns:
93
+ A byte string.
94
+ """
95
+
96
+ def ReplaceHex(m):
97
+ # Only replace the match if the number of leading back slashes is odd. i.e.
98
+ # the slash itself is not escaped.
99
+ if len(m.group(1)) & 1:
100
+ return m.group(1) + 'x0' + m.group(2)
101
+ return m.group(0)
102
+
103
+ # This is required because the 'string_escape' encoding doesn't
104
+ # allow single-digit hex escapes (like '\xf').
105
+ result = _CUNESCAPE_HEX.sub(ReplaceHex, text)
106
+
107
+ return (result.encode('utf-8') # Make it bytes to allow decode.
108
+ .decode('unicode_escape')
109
+ # Make it bytes again to return the proper type.
110
+ .encode('raw_unicode_escape'))