The descriptor.py
module contains message definitions and functions for converting Google Protocol RPC definitions into a transmittable message format.
Describing an Enum instance, Enum class, Field class, or Message class generates an appropriate descriptor object. A descriptor object is an object that describes other Google Protocol RPC definitions such as enums, messages, and services. You can use this message to transmit information to clients wishing to know the description of an enum value, enum, field or message without needing to download the source code. This format is also compatible with other, non-Python languages.
The descriptors are modeled to be binary compatible with https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/descriptor.proto
Note! The names of types and fields defined in descriptor.py
do not necessarily match those defined in descriptor.proto
. Google Protocol RPC is designed this way to make source code files that use these descriptors easier to read. For example, FieldDescriptors need to be prefixed with TYPE
in descriptor.proto
, but not in descriptor.py
.
The following code sample demonstrates use of the descriptor module to describe a class called Pixel
using the MessageDescriptor
class.
from protorpc import descriptor from protorpc import messages class Pixel(messages.Message): x = messages.IntegerField(1, required=True) y = messages.IntegerField(2, required=True) color = messages.BytesField(3) # Describe Pixel class using message descriptor. fields = [ descriptor.FieldDescriptor(name='x', number=1, label=descriptor.FieldDescriptor.Label.REQUIRED, variant=descriptor.FieldDescriptor.Variant.INT64), descriptor.FieldDescriptor(name='y', number=2, label=descriptor.FieldDescriptor.Label.REQUIRED, variant=descriptor.FieldDescriptor.Variant.INT64), descriptor.FieldDescriptor(name='color', number = 3, label=descriptor.FieldDescriptor.Label.OPTIONAL, variant=descriptor.FieldDescriptor.Variant.BYTES)] message = descriptor.MessageDescriptor(name='Pixel', fields=fields) # Describing is the equivalent of building the above message. message == descriptor.describe_message(Pixel)
The protorpc.descriptor
package provides the following functions:
- describe(value)
-
Describes any value as a descriptor. You can use this helper function to describe any object with an appropriate descriptor object.
Arguments- value
- Value to describe as a descriptor.
Returns a descriptor if the object is describable as a descriptor, else None.
- describe_enum(enum_definition)
-
Builds a descriptor from an Enum class.
Arguments- enum_value
- The Enum class to describe.
Returns an initialized EnumDescriptor instance describing the Enum instance.
EnumDescriptor Class fields:
name
- Name of the specified Enum without any qualifications.
values
- Values defined by the Enum class.
- describe_enum_value(enum_value)
-
Builds a descriptor from an Enum instance. Arguments
- enum_value
- The Enum value to describe.
Returns an initialized EnumValueDescriptor instance describing the Enum instance.
EnumValueDescriptor Class fields:
name
- Name of the enumeration value.
number
- Number of the enumeration value.
- describe_field(field_definition)
-
Builds a descriptor from a Field instance.
Arguments- field_definition
- The Field instance to describe.
Returns an initialized FieldDescriptor instance describing the Field instance with a list of Enums and fields.
FieldDescriptor Class Enums:
Variant
- Wire format hint subtypes for the specified field.
Label
- Values for optional, required, and repeated fields.
FieldDescriptor Class fields:
name
- Name of the specified field.
number
- Number of the specified field.
variant
- Variant of the specified field.
type_name
- Type name for the message and enum fields.
default_value
- String representation of the default value.
- describe_file(module)
-
Builds a file from a specified Python module.
Arguments- module
- The Python to describe.
Returns an initialized FileDescriptor instance describing the module.
FileDescriptor Class fields
package
- Fully qualified name of the package that the definitions belong to.
message_types
- Message definitions contained in the file.
enum_types
- Enum definitions contained in the file.
service_types
- Service definitions contained in the file.
- describe_file_set(modules)
-
Builds a file set from the specified Python modules.
Arguments- modules
- Iterable of the Python module to describe.
Returns an initialized FileSet instance describing the module.
FileSet Class fields:
files
- Files in the file set.
- describe_message(message_definition)
-
Builds a descriptor from a Message class.
Arguments- message_definition
- The Message class to describe.
Returns an initialized MessageDescriptor instance describing the Message class.
MessageDescriptor Class fields:
name
- Fully qualified name of the package that the definitions belong to.
fields
- Fields defined for the message.
message_types
- Nested Message classes defined on the message.
enum_types
- Nested Enum classes defined on the message.
- describe_method(method)
-
Builds a descriptor from a remote service method.
Arguments- method
- The remote service method to describe.
Returns an initialized MethodDescriptor instance describing the remote service method.
MethodDescriptor fields:
name
- Name of the service method.
request_type
- Fully qualified name or relative name of the request message type.
response_type
- Fully qualified or relative name of the response message type.
- describe_service(service_class)
-
Builds a descriptor from a Service class.
Arguments- service_class
- The Service class to describe.
Returns an initialized ServiceDescriptor instance describing the service.
ServiceDescriptor fields:
name
- Unqualified name of the Service.
methods
- Remote methods of the Service.
- import_descriptor_loader(definition_name, importer=__import__ )
-
Finds objects by importing modules as needed. A definition loader is a function that resolves a definition name to a descriptor. The import finder resolves definitions to their names by importing modules when necessary.
Arguments- definition_name
- Name of the definition to find.
- importer=__import__ )
- Import function used for importing new modules.
Returns an appropriate descriptor from any describable type located by name.
Raises a DefinitionNotFoundError when a name does not refer to either a definition or a module.