예제 #1
0
    def definition_package(cls):
        """Get package for service.

    Returns:
      Package name for service.
    """
        try:
            return cls.__definition_package
        except AttributeError:
            cls.__definition_package = util.get_package_for_module(cls.__module__)

        return cls.__definition_package
예제 #2
0
def describe_file(module):
  """Build a file from a specified Python module.

  Args:
    module: Python module to describe.

  Returns:
    Initialized FileDescriptor instance describing the module.
  """
  # May not import remote at top of file because remote depends on this
  # file
  # TODO(rafek): Straighten out this dependency.  Possibly move these functions
  # from descriptor to their own module.
  import remote

  descriptor = FileDescriptor()
  descriptor.package = util.get_package_for_module(module)

  if not descriptor.package:
    descriptor.package = None

  message_descriptors = []
  enum_descriptors = []
  service_descriptors = []

  # Need to iterate over all top level attributes of the module looking for
  # message, enum and service definitions.  Each definition must be itself
  # described.
  for name in sorted(dir(module)):
    value = getattr(module, name)

    if isinstance(value, type):
      if issubclass(value, messages.Message):
        message_descriptors.append(describe_message(value))

      elif issubclass(value, messages.Enum):
        enum_descriptors.append(describe_enum(value))

      elif issubclass(value, remote.Service):
        service_descriptors.append(describe_service(value))

  if message_descriptors:
    descriptor.message_types = message_descriptors

  if enum_descriptors:
    descriptor.enum_types = enum_descriptors

  if service_descriptors:
    descriptor.service_types = service_descriptors

  return descriptor