Package google :: Package protobuf :: Module text_format
[hide private]
[frames] | no frames]

Module text_format

source code

Contains routines for printing protocol messages in text format.

Simple usage example:

  # Create a proto object and serialize it to a text proto string.
  message = my_proto_pb2.MyMessage(foo='bar')
  text_proto = text_format.MessageToString(message)

  # Parse a text proto string.
  message = text_format.Parse(text_proto, my_proto_pb2.MyMessage())


Author: kenton@google.com (Kenton Varda)

Classes [hide private]
  long
int(x=0) -> int or long int(x, base=10) -> int or long
  Error
Top-level module error for text_format.
  ParseError
Thrown in case of text parsing or tokenizing error.
  TextWriter
  _Printer
Text format printer for protocol message.
  _Parser
Text format parser for protocol message.
  Tokenizer
Protocol buffer text representation tokenizer.
  _Tokenizer
Protocol buffer text representation tokenizer.
Functions [hide private]
 
MessageToString(message, as_utf8=False, as_one_line=False, use_short_repeated_primitives=False, pointy_brackets=False, use_index_order=False, float_format=None, double_format=None, use_field_number=False, descriptor_pool=None, indent=0, message_formatter=None, print_unknown_fields=False)
Convert protobuf message to text format.
source code
 
MessageToBytes(message, **kwargs)
Convert protobuf message to encoded text format.
source code
 
_IsMapEntry(field) source code
 
PrintMessage(message, out, indent=0, as_utf8=False, as_one_line=False, use_short_repeated_primitives=False, pointy_brackets=False, use_index_order=False, float_format=None, double_format=None, use_field_number=False, descriptor_pool=None, message_formatter=None, print_unknown_fields=False) source code
 
PrintField(field, value, out, indent=0, as_utf8=False, as_one_line=False, use_short_repeated_primitives=False, pointy_brackets=False, use_index_order=False, float_format=None, double_format=None, message_formatter=None, print_unknown_fields=False)
Print a single field name/value pair.
source code
 
PrintFieldValue(field, value, out, indent=0, as_utf8=False, as_one_line=False, use_short_repeated_primitives=False, pointy_brackets=False, use_index_order=False, float_format=None, double_format=None, message_formatter=None, print_unknown_fields=False)
Print a single field value (not including name).
source code
 
_BuildMessageFromTypeName(type_name, descriptor_pool)
Returns a protobuf message instance.
source code
 
Parse(text, message, allow_unknown_extension=False, allow_field_number=False, descriptor_pool=None, allow_unknown_field=False)
Parses a text representation of a protocol message into a message.
source code
 
Merge(text, message, allow_unknown_extension=False, allow_field_number=False, descriptor_pool=None, allow_unknown_field=False)
Parses a text representation of a protocol message into a message.
source code
 
ParseLines(lines, message, allow_unknown_extension=False, allow_field_number=False, descriptor_pool=None, allow_unknown_field=False)
Parses a text representation of a protocol message into a message.
source code
 
MergeLines(lines, message, allow_unknown_extension=False, allow_field_number=False, descriptor_pool=None, allow_unknown_field=False)
Parses a text representation of a protocol message into a message.
source code
 
_SkipFieldContents(tokenizer)
Skips over contents (value or message) of a field.
source code
 
_SkipField(tokenizer)
Skips over a complete field (name and value/message).
source code
 
_SkipFieldMessage(tokenizer)
Skips over a field message.
source code
 
_SkipFieldValue(tokenizer)
Skips over a field value.
source code
 
_ConsumeInt32(tokenizer)
Consumes a signed 32bit integer number from tokenizer.
source code
 
_ConsumeUint32(tokenizer)
Consumes an unsigned 32bit integer number from tokenizer.
source code
 
_TryConsumeInt64(tokenizer) source code
 
_ConsumeInt64(tokenizer)
Consumes a signed 32bit integer number from tokenizer.
source code
 
_TryConsumeUint64(tokenizer) source code
 
_ConsumeUint64(tokenizer)
Consumes an unsigned 64bit integer number from tokenizer.
source code
 
_TryConsumeInteger(tokenizer, is_signed=False, is_long=False) source code
 
_ConsumeInteger(tokenizer, is_signed=False, is_long=False)
Consumes an integer number from tokenizer.
source code
 
ParseInteger(text, is_signed=False, is_long=False)
Parses an integer.
source code
 
_ParseAbstractInteger(text, is_long=False)
Parses an integer without checking size/signedness.
source code
 
ParseFloat(text)
Parse a floating point number.
source code
 
ParseBool(text)
Parse a boolean value.
source code
 
ParseEnum(field, value)
Parse an enum value.
source code
Variables [hide private]
  _INTEGER_CHECKERS = (<google.protobuf.internal.type_checkers.U...
  _FLOAT_INFINITY = re.compile(r'(?i)-?inf(?:inity)?f?$')
  _FLOAT_NAN = re.compile(r'(?i)nanf?$')
  _QUOTES = frozenset(['"', '\''])
  _ANY_FULL_TYPE_NAME = 'google.protobuf.Any'
  WIRETYPE_LENGTH_DELIMITED = 2
  WIRETYPE_START_GROUP = 3
  __package__ = 'google.protobuf'
Function Details [hide private]

MessageToString(message, as_utf8=False, as_one_line=False, use_short_repeated_primitives=False, pointy_brackets=False, use_index_order=False, float_format=None, double_format=None, use_field_number=False, descriptor_pool=None, indent=0, message_formatter=None, print_unknown_fields=False)

source code 
Convert protobuf message to text format.

Double values can be formatted compactly with 15 digits of
precision (which is the most that IEEE 754 "double" can guarantee)
using double_format='.15g'. To ensure that converting to text and back to a
proto will result in an identical value, double_format='.17g' should be used.

Args:
  message: The protocol buffers message.
  as_utf8: Return unescaped Unicode for non-ASCII characters.
      In Python 3 actual Unicode characters may appear as is in strings.
      In Python 2 the return value will be valid UTF-8 rather than only ASCII.
  as_one_line: Don't introduce newlines between fields.
  use_short_repeated_primitives: Use short repeated format for primitives.
  pointy_brackets: If True, use angle brackets instead of curly braces for
    nesting.
  use_index_order: If True, fields of a proto message will be printed using
    the order defined in source code instead of the field number, extensions
    will be printed at the end of the message and their relative order is
    determined by the extension number. By default, use the field number
    order.
  float_format: If set, use this to specify float field formatting
    (per the "Format Specification Mini-Language"); otherwise, 8 valid digits
    is used (default '.8g'). Also affect double field if double_format is
    not set but float_format is set.
  double_format: If set, use this to specify double field formatting
    (per the "Format Specification Mini-Language"); if it is not set but
    float_format is set, use float_format. Otherwise, use str()
  use_field_number: If True, print field numbers instead of names.
  descriptor_pool: A DescriptorPool used to resolve Any types.
  indent: The initial indent level, in terms of spaces, for pretty print.
  message_formatter: A function(message, indent, as_one_line): unicode|None
    to custom format selected sub-messages (usually based on message type).
    Use to pretty print parts of the protobuf for easier diffing.
  print_unknown_fields: If True, unknown fields will be printed.

Returns:
  A string of the text formatted protocol buffer message.

MessageToBytes(message, **kwargs)

source code 

Convert protobuf message to encoded text format. See MessageToString.

_BuildMessageFromTypeName(type_name, descriptor_pool)

source code 
Returns a protobuf message instance.

Args:
  type_name: Fully-qualified protobuf  message type name string.
  descriptor_pool: DescriptorPool instance.

Returns:
  A Message instance of type matching type_name, or None if the a Descriptor
  wasn't found matching type_name.

Parse(text, message, allow_unknown_extension=False, allow_field_number=False, descriptor_pool=None, allow_unknown_field=False)

source code 
Parses a text representation of a protocol message into a message.

NOTE: for historical reasons this function does not clear the input
message. This is different from what the binary msg.ParseFrom(...) does.

Example
  a = MyProto()
  a.repeated_field.append('test')
  b = MyProto()

  text_format.Parse(repr(a), b)
  text_format.Parse(repr(a), b) # repeated_field contains ["test", "test"]

  # Binary version:
  b.ParseFromString(a.SerializeToString()) # repeated_field is now "test"

Caller is responsible for clearing the message as needed.

Args:
  text: Message text representation.
  message: A protocol buffer message to merge into.
  allow_unknown_extension: if True, skip over missing extensions and keep
    parsing
  allow_field_number: if True, both field number and field name are allowed.
  descriptor_pool: A DescriptorPool used to resolve Any types.
  allow_unknown_field: if True, skip over unknown field and keep
    parsing. Avoid to use this option if possible. It may hide some
    errors (e.g. spelling error on field name)

Returns:
  The same message passed as argument.

Raises:
  ParseError: On text parsing problems.

Merge(text, message, allow_unknown_extension=False, allow_field_number=False, descriptor_pool=None, allow_unknown_field=False)

source code 
Parses a text representation of a protocol message into a message.

Like Parse(), but allows repeated values for a non-repeated field, and uses
the last one.

Args:
  text: Message text representation.
  message: A protocol buffer message to merge into.
  allow_unknown_extension: if True, skip over missing extensions and keep
    parsing
  allow_field_number: if True, both field number and field name are allowed.
  descriptor_pool: A DescriptorPool used to resolve Any types.
  allow_unknown_field: if True, skip over unknown field and keep
    parsing. Avoid to use this option if possible. It may hide some
    errors (e.g. spelling error on field name)

Returns:
  The same message passed as argument.

Raises:
  ParseError: On text parsing problems.

ParseLines(lines, message, allow_unknown_extension=False, allow_field_number=False, descriptor_pool=None, allow_unknown_field=False)

source code 
Parses a text representation of a protocol message into a message.

Args:
  lines: An iterable of lines of a message's text representation.
  message: A protocol buffer message to merge into.
  allow_unknown_extension: if True, skip over missing extensions and keep
    parsing
  allow_field_number: if True, both field number and field name are allowed.
  descriptor_pool: A DescriptorPool used to resolve Any types.
  allow_unknown_field: if True, skip over unknown field and keep
    parsing. Avoid to use this option if possible. It may hide some
    errors (e.g. spelling error on field name)

Returns:
  The same message passed as argument.

Raises:
  ParseError: On text parsing problems.

MergeLines(lines, message, allow_unknown_extension=False, allow_field_number=False, descriptor_pool=None, allow_unknown_field=False)

source code 
Parses a text representation of a protocol message into a message.

Like ParseLines(), but allows repeated values for a non-repeated field, and
uses the last one.

Args:
  lines: An iterable of lines of a message's text representation.
  message: A protocol buffer message to merge into.
  allow_unknown_extension: if True, skip over missing extensions and keep
    parsing
  allow_field_number: if True, both field number and field name are allowed.
  descriptor_pool: A DescriptorPool used to resolve Any types.
  allow_unknown_field: if True, skip over unknown field and keep
    parsing. Avoid to use this option if possible. It may hide some
    errors (e.g. spelling error on field name)

Returns:
  The same message passed as argument.

Raises:
  ParseError: On text parsing problems.

_SkipFieldContents(tokenizer)

source code 
Skips over contents (value or message) of a field.

Args:
  tokenizer: A tokenizer to parse the field name and values.

_SkipField(tokenizer)

source code 
Skips over a complete field (name and value/message).

Args:
  tokenizer: A tokenizer to parse the field name and values.

_SkipFieldMessage(tokenizer)

source code 
Skips over a field message.

Args:
  tokenizer: A tokenizer to parse the field name and values.

_SkipFieldValue(tokenizer)

source code 
Skips over a field value.

Args:
  tokenizer: A tokenizer to parse the field name and values.

Raises:
  ParseError: In case an invalid field value is found.

_ConsumeInt32(tokenizer)

source code 
Consumes a signed 32bit integer number from tokenizer.

Args:
  tokenizer: A tokenizer used to parse the number.

Returns:
  The integer parsed.

Raises:
  ParseError: If a signed 32bit integer couldn't be consumed.

_ConsumeUint32(tokenizer)

source code 
Consumes an unsigned 32bit integer number from tokenizer.

Args:
  tokenizer: A tokenizer used to parse the number.

Returns:
  The integer parsed.

Raises:
  ParseError: If an unsigned 32bit integer couldn't be consumed.

_ConsumeInt64(tokenizer)

source code 
Consumes a signed 32bit integer number from tokenizer.

Args:
  tokenizer: A tokenizer used to parse the number.

Returns:
  The integer parsed.

Raises:
  ParseError: If a signed 32bit integer couldn't be consumed.

_ConsumeUint64(tokenizer)

source code 
Consumes an unsigned 64bit integer number from tokenizer.

Args:
  tokenizer: A tokenizer used to parse the number.

Returns:
  The integer parsed.

Raises:
  ParseError: If an unsigned 64bit integer couldn't be consumed.

_ConsumeInteger(tokenizer, is_signed=False, is_long=False)

source code 
Consumes an integer number from tokenizer.

Args:
  tokenizer: A tokenizer used to parse the number.
  is_signed: True if a signed integer must be parsed.
  is_long: True if a long integer must be parsed.

Returns:
  The integer parsed.

Raises:
  ParseError: If an integer with given characteristics couldn't be consumed.

ParseInteger(text, is_signed=False, is_long=False)

source code 
Parses an integer.

Args:
  text: The text to parse.
  is_signed: True if a signed integer must be parsed.
  is_long: True if a long integer must be parsed.

Returns:
  The integer value.

Raises:
  ValueError: Thrown Iff the text is not a valid integer.

_ParseAbstractInteger(text, is_long=False)

source code 
Parses an integer without checking size/signedness.

Args:
  text: The text to parse.
  is_long: True if the value should be returned as a long integer.

Returns:
  The integer value.

Raises:
  ValueError: Thrown Iff the text is not a valid integer.

ParseFloat(text)

source code 
Parse a floating point number.

Args:
  text: Text to parse.

Returns:
  The number parsed.

Raises:
  ValueError: If a floating point number couldn't be parsed.

ParseBool(text)

source code 
Parse a boolean value.

Args:
  text: Text to parse.

Returns:
  Boolean values parsed

Raises:
  ValueError: If text is not a valid boolean.

ParseEnum(field, value)

source code 
Parse an enum value.

The value can be specified by a number (the enum value), or by
a string literal (the enum name).

Args:
  field: Enum field descriptor.
  value: String value.

Returns:
  Enum value number.

Raises:
  ValueError: If the enum value could not be parsed.


Variables Details [hide private]

_INTEGER_CHECKERS

Value:
(<google.protobuf.internal.type_checkers.Uint32ValueChecker object>,
 <google.protobuf.internal.type_checkers.Int32ValueChecker object>,
 <google.protobuf.internal.type_checkers.Uint64ValueChecker object>,
 <google.protobuf.internal.type_checkers.Int64ValueChecker object>)