示例#1
0
def _PrintUnknownFields(self, unknown_fields):
    out = self.out
    for field in unknown_fields:
        out.write(' ' * self.indent)
        out.write(str(field.field_number))
        if field.wire_type == wire_format.WIRETYPE_START_GROUP:
            if self.as_one_line:
                out.write(' < ')
            else:
                out.write(' <\n')
                self.indent += 2

            self._PrintUnknownFields(field.data)

            if self.as_one_line:
                out.write('> ')
            else:
                self.indent -= 2
                out.write(' ' * self.indent + '>\n')
        elif field.wire_type == wire_format.WIRETYPE_LENGTH_DELIMITED:
            try:
                msg, pos = decoder._DecodeUnknownFieldSet(
                    memoryview(field.data), 0, len(field.data))
            except Exception:
                pos = 0
            if pos == len(field.data):
                if self.as_one_line:
                    out.write(' { ')
                else:
                    out.write(' {\n')
                    self.indent += 2
                self._PrintUnknownFields(msg)

                if self.as_one_line:
                    out.write('} ')
                else:
                    self.indent -= 2
                    out.write(' ' * self.indent + '}\n')
            else:
                out.write(': \"')
                out.write(text_encoding.CEscape(field.data, False))
                out.write('\" ' if self.as_one_line else '\"\n')
        else:
            out.write(' (')
            out.write({
                wire_format.WIRETYPE_VARINT: 'int',
                wire_format.WIRETYPE_FIXED64: 'f64',
                wire_format.WIRETYPE_FIXED32: 'f32',
            }[field.wire_type])
            out.write('): ')
            if field.wire_type == wire_format.WIRETYPE_VARINT:
                out.write(str(field.data))
            elif field.wire_type == wire_format.WIRETYPE_FIXED32:
                out.write(
                    str(struct.unpack('<f', struct.pack('<I', field.data))[0]))
            elif field.wire_type == wire_format.WIRETYPE_FIXED64:
                out.write(
                    str(struct.unpack('<d', struct.pack('<Q', field.data))[0]))
            out.write(' ' if self.as_one_line else '\n')
示例#2
0
def PrintFieldValue(field, value, out, indent=0, as_utf8=False,
                    as_one_line=False, pointy_brackets=False,
                    use_index_order=False,
                    float_format=None):
  """Print a single field value (not including name).  For repeated fields,
  the value should be a single element."""

  if pointy_brackets:
    openb = '<'
    closeb = '>'
  else:
    openb = '{'
    closeb = '}'

  if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
    if as_one_line:
      out.write(' %s ' % openb)
      PrintMessage(value, out, indent, as_utf8, as_one_line,
                   pointy_brackets=pointy_brackets,
                   use_index_order=use_index_order,
                   float_format=float_format)
      out.write(closeb)
    else:
      out.write(' %s\n' % openb)
      PrintMessage(value, out, indent + 2, as_utf8, as_one_line,
                   pointy_brackets=pointy_brackets,
                   use_index_order=use_index_order,
                   float_format=float_format)
      out.write(' ' * indent + closeb)
  elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM:
    enum_value = field.enum_type.values_by_number.get(value, None)
    if enum_value is not None:
      out.write(enum_value.name)
    else:
      out.write(str(value))
  elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_STRING:
    out.write('\"')
    if isinstance(value, six.text_type):
      out_value = value.encode('utf-8')
    else:
      out_value = value
    if field.type == descriptor.FieldDescriptor.TYPE_BYTES:
      # We need to escape non-UTF8 chars in TYPE_BYTES field.
      out_as_utf8 = False
    else:
      out_as_utf8 = as_utf8
    out.write(text_encoding.CEscape(out_value, out_as_utf8))
    out.write('\"')
  elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_BOOL:
    if value:
      out.write('true')
    else:
      out.write('false')
  elif field.cpp_type in _FLOAT_TYPES and float_format is not None:
    out.write('{1:{0}}'.format(float_format, value))
  else:
    out.write(str(value))
示例#3
0
  def PrintFieldValue(self, field, value):
    """Print a single field value (not including name).

    For repeated fields, the value should be a single element.

    Args:
      field: The descriptor of the field to be printed.
      value: The value of the field.
    """
    out = self.out
    if self.pointy_brackets:
      openb = '<'
      closeb = '>'
    else:
      openb = '{'
      closeb = '}'

    if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
      if self.as_one_line:
        out.write(' %s ' % openb)
        self.PrintMessage(value)
        out.write(closeb)
      else:
        out.write(' %s\n' % openb)
        self.indent += 2
        self.PrintMessage(value)
        self.indent -= 2
        out.write(' ' * self.indent + closeb)
    elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM:
      enum_value = field.enum_type.values_by_number.get(value, None)
      if enum_value is not None:
        out.write(enum_value.name)
      else:
        out.write(str(value))
    elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_STRING:
      out.write('\"')
      if isinstance(value, six.text_type):
        out_value = value.encode('utf-8')
      else:
        out_value = value
      if field.type == descriptor.FieldDescriptor.TYPE_BYTES:
        # We need to escape non-UTF8 chars in TYPE_BYTES field.
        out_as_utf8 = False
      else:
        out_as_utf8 = self.as_utf8
      out.write(text_encoding.CEscape(out_value, out_as_utf8))
      out.write('\"')
    elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_BOOL:
      if value:
        out.write('true')
      else:
        out.write('false')
    elif field.cpp_type in _FLOAT_TYPES and self.float_format is not None:
      out.write('{1:{0}}'.format(self.float_format, value))
    else:
      out.write(str(value))
示例#4
0
  def link_typespace(self, typespace):
    """Links and initializes a given schema typespace.

    Args:
      typespace: Uninitialized typespace object

    Raises:
      InvalidType:
      MissingArgument:
    """

    if not isinstance(typespace, schema.Typespace):
      raise exception.InvalidType('Expecting an typespace')

    if typespace.desc.fields:
      raise exception.InvalidType(
          'Typespace {} contains properties, typespaces should not contain '
          'properties.'.format(typespace.full_name))

    options = typespace.desc.options.Extensions[wdl_options_pb2.typespace]

    vendor = self.get_vendor(typespace.desc.full_name)
    vendor.typespace_list.append(typespace)

    # Used to set the file descriptor in dev_*
    typespace.nwv_pb_desc = text_encoding.CEscape(
        typespace.desc.SerializeToString(), False)

    typespace.stability = schema.Stability(options.stability)
    typespace.version = options.version
    typespace.version_map = schema.VersionMap(options.version_map)

    for nested_msg_desc in _order_messages_by_dependency(
        typespace.desc.messages.values(), typespace.full_name):
      if nested_msg_desc.is_map_entry:
        continue  # ignore map entries
      nested_msg = self.get_obj(nested_msg_desc.full_name)
      if nested_msg:
        if isinstance(nested_msg, schema.Command):
          typespace.command_list.append(nested_msg)
        elif isinstance(nested_msg, schema.Event):
          typespace.event_list.append(nested_msg)
        elif isinstance(nested_msg, schema.Struct):
          typespace.struct_list.append(nested_msg)
        else:
          raise exception.InvalidType('Unexpected type in typespace')

    for nested_enum_desc in typespace.desc.enums.values():
      nested_enum = self.get_obj(nested_enum_desc.full_name)
      constant_type = nested_enum_desc.options.Extensions[
          wdl_options_pb2.enumopts].constant_type
      if constant_type:
        typespace.constant_group_list.append(nested_enum)
      else:
        typespace.enum_list.append(nested_enum)
示例#5
0
def _text_format_field(value: Any, field: descriptor.FieldDescriptor,
                       indent: str, remove_value: bool) -> str:
    """Returns a text formated proto field."""
    if field.type == TYPE_MESSAGE:
        output = [
            indent + field.name + ' {',
            _text_format_message(value, indent + '  ', remove_value),
            indent + '}'
        ]
        return '\n'.join(output)
    elif field.type == TYPE_ENUM:
        value_name = field.enum_type.values_by_number[value].name
        return indent + field.name + ': ' + value_name
    elif field.type in [TYPE_STRING, TYPE_BYTES]:
        return (indent + field.name + ': "' +
                text_encoding.CEscape(value, False) + '"')
    else:
        return indent + field.name + ': ' + str(value)
示例#6
0
 def testCEscape(self):
     for escaped, escaped_utf8, unescaped in TEST_VALUES:
         self.assertEqual(escaped,
                          text_encoding.CEscape(unescaped, as_utf8=False))
         self.assertEqual(escaped_utf8,
                          text_encoding.CEscape(unescaped, as_utf8=True))
示例#7
0
  def link_trait(self, trait):
    """Links a given trait and inserts it into the schema.

    Args:
      trait: An uninitialized schema trait object

    Raises:
      InvalidType:
      MissingArgument:
    """

    if not isinstance(trait, schema.Trait):
      raise exception.InvalidType('Expecting an trait')

    options = trait.desc.options.Extensions[wdl_options_pb2.trait]

    vendor = self.get_vendor(trait.desc.full_name)
    vendor.trait_list.append(trait)

    # Used to set the file descriptor in dev_*
    trait.nwv_pb_desc = text_encoding.CEscape(trait.desc.SerializeToString(),
                                              False)

    trait.stability = schema.Stability(options.stability)
    trait.version = options.version
    trait.version_map = schema.VersionMap(options.version_map)

    for field_desc in trait.desc.fields.values():
      trait.state_list.append(self.get_obj(field_desc.full_name))

    for nested_msg_desc in _order_messages_by_dependency(
        trait.desc.messages.values(), trait.full_name):
      if nested_msg_desc.is_map_entry:
        continue  # ignore map entries
      nested_msg = self.get_obj(nested_msg_desc.full_name)
      if isinstance(nested_msg, schema.Command):
        trait.command_list.append(nested_msg)
      elif isinstance(nested_msg, schema.Event):
        trait.event_list.append(nested_msg)
      elif isinstance(nested_msg, schema.CommandResponse):
        pass  # ignore, it will be handled inside link_command
      elif isinstance(nested_msg, schema.Struct):
        trait.struct_list.append(nested_msg)
      else:
        raise exception.InvalidType('Unexpected nested type in trait')

    for nested_enum_desc in trait.desc.enums.values():
      nested_enum = self.get_obj(nested_enum_desc.full_name)
      constant_type = nested_enum_desc.options.Extensions[
          wdl_options_pb2.enumopts].constant_type
      if constant_type:
        trait.constant_group_list.append(nested_enum)
      else:
        trait.enum_list.append(nested_enum)

    trait.extends = self.get_extends(
        trait.desc.options.Extensions[wdl_options_pb2.properties].extends.trait)

    self.validate_extendable(
        trait.desc.options.Extensions[wdl_options_pb2.properties],
        trait.state_list, trait.extends.desc.fields if trait.extends else {})