3
SÉLjÇÆ ã @ s d Z dZddlZddlZddlZejr*eZddlm Z ddl
mZ ddl
mZ ddd d
dgZ
e jƒ e jƒ e jƒ e jƒ fZejdejƒZejd
ejƒZeejjejjgƒZedOƒZdZG dd„ deƒZG dd„ deƒZ G dd„ de!ƒZ"dPdd„Z#dd„ Z$dQdd„Z%dRdd „Z&dSdd
„Z'dd„ Z(G d d!„ d!e!ƒZ)dTd"d#„Z*dUd$d„Z+dVd%d&„Z,dWd'd(„Z-G d)d*„ d*e!ƒZ.d+d,„ Z/d-d.„ Z0d/d0„ Z1d1d2„ Z2G d3d4„ d4e!ƒZ3e3Z4d5d6„ Z5d7d8„ Z6d9d:„ Z7d;d<„ Z8d=d>„ Z9d?d@„ Z:dXdAdB„Z;dYdCdD„ZdIdJ„ Z?dKdL„ Z@dMdN„ ZAdS )\ab 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())
z kenton@google.com (Kenton Varda)é N)Ú
type_checkers)Ú
descriptor)Ú
text_encodingÚMessageToStringÚPrintMessageÚ
PrintFieldÚPrintFieldValueÚMergez-?inf(?:inity)?f?znanf?ú'ú"zgoogle.protobuf.Anyc @ s e Zd ZdZdS )ÚErrorz'Top-level module error for text_format.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r r ú!/usr/lib/python3.6/text_format.pyr I s r c s2 e Zd ZdZd ‡ fdd„ Zdd„ Zdd„ Z‡ ZS )
Ú
ParseErrorz3Thrown in case of text parsing or tokenizing error.Nc sr |d k r:|d k r:t |ƒ}|d k r.|dj|ƒ7 }dj||ƒ}|d k rTtt| ƒj|ƒ ntt| ƒjƒ || _|| _d S )Nz:{0}z {0} : {1})ÚstrÚformatÚsuperr Ú__init__Ú_lineÚ_column)ÚselfÚmessageÚlineÚcolumnÚloc)Ú __class__r r r P s zParseError.__init__c C s | j S )N)r )r r r r ÚGetLine] s zParseError.GetLinec C s | j S )N)r )r r r r Ú GetColumn` s zParseError.GetColumn)NNN)r
r r r r r r! Ú
__classcell__r r )r r r M s
r c @ s, e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd S )
Ú
TextWriterc C s t jrtjƒ | _n
tjƒ | _d S )N)ÚsixÚPY2ÚioÚBytesIOÚ_writerÚStringIO)r Úas_utf8r r r r f s zTextWriter.__init__c C s( t jrt|t jƒr|jdƒ}| jj|ƒS )Nzutf-8)r$ r% Ú
isinstanceÚ text_typeÚencoder( Úwrite)r Úvalr r r r. l s
zTextWriter.writec C s
| j jƒ S )N)r( Úclose)r r r r r0 r s zTextWriter.closec C s
| j jƒ S )N)r( Úgetvalue)r r r r r1 u s zTextWriter.getvalueN)r
r r r r. r0 r1 r r r r r# d s r# Fc
C sL t |ƒ}
t|
||||||||| ƒ
}|j| ƒ |
jƒ }|
jƒ |rH|jƒ S |S )a” Convert protobuf message to text format.
Floating point values can be formatted compactly with 15 digits of
precision (which is the most that IEEE 754 "double" can guarantee)
using float_format='.15g'. To ensure that converting to text and back to a
proto will result in an identical value, float_format='.17g' should be used.
Args:
message: The protocol buffers message.
as_utf8: Produce text output in UTF8 format.
as_one_line: Don't introduce newlines between fields.
pointy_brackets: If True, use angle brackets instead of curly braces for
nesting.
use_index_order: If True, print fields of a proto message using the order
defined in source code instead of the field number. By default, use the
field number order.
float_format: If set, use this to specify floating point number formatting
(per the "Format Specification Mini-Language"); otherwise, str() is used.
use_field_number: If True, print field numbers instead of names.
descriptor_pool: A DescriptorPool used to resolve Any types.
indent: The 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.
Returns:
A string of the text formatted protocol buffer message.
)r# Ú_Printerr r1 r0 Úrstrip)
r r* Úas_one_lineÚpointy_bracketsÚuse_index_orderÚfloat_formatÚuse_field_numberÚdescriptor_poolÚindentÚmessage_formatterÚoutÚprinterÚresultr r r r y s &
c C s" | j tjjko | jjo | jjƒ jS )N)Útyper ÚFieldDescriptorÚTYPE_MESSAGEÚmessage_typeZhas_optionsÚ
GetOptionsZ map_entry)Úfieldr r r Ú_IsMapEntry« s rE c C s( t ||||||||| |
ƒ
}|j| ƒ d S )N)r2 r )r r<