
Q,Qc           @   s  d  Z  d Z d d l Z d d l Z d d l m Z d d l m Z d d l m	 Z	 d d d	 d
 d g Z
 e j   e j   e j   e j   f Z e j d e j  Z e j d e j  Z d e f d     YZ e e d  Z d e e d  Z d e e d  Z d e e d  Z d   Z d   Z d   Z d e f d     YZ d   Z  e j d  Z! d   Z" e e d  Z# d   Z$ d   Z% d    Z& d S(!   s@   Contains routines for printing protocol messages in text format.s    kenton@google.com (Kenton Varda)iN(   t   deque(   t   type_checkers(   t
   descriptort   MessageToStringt   PrintMessaget
   PrintFieldt   PrintFieldValuet   Merges   -?inf(?:inity)?f?s   nanf?t
   ParseErrorc           B   s   e  Z d  Z RS(   s&   Thrown in case of ASCII parsing error.(   t   __name__t
   __module__t   __doc__(    (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyR   6   s   c         C   sO   t  j   } t |  | d | d | | j   } | j   | rK | j   S| S(   Nt   as_utf8t   as_one_line(   t	   cStringIOt   StringIOR   t   getvaluet   closet   rstrip(   t   messageR   R   t   outt   result(    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyR   :   s    

i    c      	   C   s|   xu |  j    D]g \ } } | j t j j k r[ xC | D] } t | | | | | |  q5 Wq t | | | | | |  q Wd  S(   N(   t
   ListFieldst   labelR   t   FieldDescriptort   LABEL_REPEATEDR   (   R   R   t   indentR   R   t   fieldt   valuet   element(    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyR   D   s
     c      
   C   sK  | j  d |  |  j r | j  d  |  j j   j r |  j t j j k r |  j	 |  j
 k r |  j t j j k r | j  |  j	 j  n | j  |  j  | j  d  n; |  j t j j k r | j  |  j	 j  n | j  |  j  |  j t j j k r| j  d  n  t |  | | | | |  | r:| j  d  n | j  d  d S(   sc   Print a single field name/value pair.  For repeated fields, the value
  should be a single element.t    t   [t   ]s   : s   
N(   t   writet   is_extensiont   containing_typet
   GetOptionst   message_set_wire_formatt   typeR   R   t   TYPE_MESSAGEt   message_typet   extension_scopeR   t   LABEL_OPTIONALt	   full_namet
   TYPE_GROUPt   namet   cpp_typet   CPPTYPE_MESSAGER   (   R   R   R   R   R   R   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyR   M   s&    	c         C   s  |  j  t j j k r | rN | j d  t | | | | |  | j d  q| j d  t | | | d | |  | j d | d  n,|  j  t j j k r |  j j j	 | d
  } | d
 k	 r | j | j  q| j t |   n |  j  t j j k rk| j d  t |  t k rE| j t | j d  |   n | j t | |   | j d  nN |  j  t j j k r| r| j d  q| j d	  n | j t |   d
 S(   sn   Print a single field value (not including name).  For repeated fields,
  the value should be a single element.s    { t   }s    {
i   R   s   "s   utf-8t   truet   falseN(   R.   R   R   R/   R!   R   t   CPPTYPE_ENUMt	   enum_typet   values_by_numbert   gett   NoneR-   t   strt   CPPTYPE_STRINGR&   t   unicodet   _CEscapet   encodet   CPPTYPE_BOOL(   R   R   R   R   R   R   t
   enum_value(    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyR   n   s0    "c         C   s0   t  |   } x | j   s+ t | |  q Wd S(   s   Merges an ASCII representation of a protocol message into a message.

  Args:
    text: Message ASCII representation.
    message: A protocol buffer message to merge into.

  Raises:
    ParseError: On ASCII parsing problems.
  N(   t
   _Tokenizert   AtEndt   _MergeField(   t   textR   t	   tokenizer(    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyR      s    
c         C   s  | j  } |  j d  r |  j   g } x& |  j d  rO | j |  j    q* Wd j |  } | j s |  j d | j   n  | j j	 |  } | s |  j d |   n. | | j
 k r |  j d | | j f   n  |  j d  n |  j   } | j j | d  } | sV| j j | j   d  } | rV| j t j j k rVd } qVn  | r| j t j j k r| j j | k rd } n  | s|  j d | j | f   n  | j t j j k r|  j d  |  j d	  rd
 } n |  j d  d } | j t j j k rM| j r2| j | j   } qt | | j  j   } n5 | j rf| j | } n t | | j  } | j   xU |  j |  s|  j   r|  j d |   n  t |  |  qWn t  |  | |  d S(   s   Merges a single protocol message field into a message.

  Args:
    tokenizer: A tokenizer to parse the field name and values.
    message: A protocol message to record the data.

  Raises:
    ParseError: In case of ASCII parsing problems.
  R   t   .s+   Message type "%s" does not have extensions.s   Extension "%s" not registered.s1   Extension "%s" does not extend message type "%s".R    s*   Message type "%s" has no field named "%s".t   :t   <t   >t   {R0   s   Expected "%s".N(!   t
   DESCRIPTORt
   TryConsumet   ConsumeIdentifiert   appendt   joint   is_extendablet   ParseErrorPreviousTokenR+   t
   Extensionst   _FindExtensionByNameR#   t   Consumet   fields_by_nameR6   R7   t   lowerR&   R   R   R,   R(   R-   R.   R/   R   R   R"   t   addt   getattrt   SetInParentR@   RA   t   _MergeScalarField(   RC   R   t   message_descriptorR-   R   t	   end_tokent   sub_message(    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyRA      sf    
						
c         C   s<  |  j  d  d } | j t j j t j j t j j f k rL |  j   } no| j t j j	 t j j
 t j j f k r |  j   } n6| j t j j t j j f k r |  j   } n| j t j j t j j f k r |  j   } n | j t j j t j j f k r|  j   } n | j t j j k r9|  j   } n | j t j j k r]|  j   } n^ | j t j j k r|  j   } n: | j t j j k r|  j |  } n t d | j   | j t j j  k r| j! r| j" | j# |  q8t$ | | j%  j# |  n, | j! r%| | j" | <n t& | | j% |  d S(   sJ  Merges a single protocol message scalar field into a message.

  Args:
    tokenizer: A tokenizer to parse the field value.
    message: A protocol message to record the data.
    field: The descriptor of the field to be merged.

  Raises:
    ParseError: In case of ASCII parsing problems.
    RuntimeError: On runtime errors.
  RE   s   Unknown field type %dN('   RR   R7   R&   R   R   t
   TYPE_INT32t   TYPE_SINT32t   TYPE_SFIXED32t   ConsumeInt32t
   TYPE_INT64t   TYPE_SINT64t   TYPE_SFIXED64t   ConsumeInt64t   TYPE_UINT32t   TYPE_FIXED32t   ConsumeUint32t   TYPE_UINT64t   TYPE_FIXED64t   ConsumeUint64t
   TYPE_FLOATt   TYPE_DOUBLEt   ConsumeFloatt	   TYPE_BOOLt   ConsumeBoolt   TYPE_STRINGt   ConsumeStringt
   TYPE_BYTESt   ConsumeByteStringt	   TYPE_ENUMt   ConsumeEnumt   RuntimeErrorR   R   R"   RP   RL   RV   R-   t   setattr(   RC   R   R   R   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyRX      sF    				R?   c           B   s   e  Z d  Z e j d e j  Z e j d  Z e j d  Z d   Z	 d   Z
 d   Z d   Z d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s   Protocol buffer ASCII representation tokenizer.

  This class handles the lower level string parsing by splitting it into
  meaningful tokens.

  It was directly ported from the Java protocol buffer API.
  s   (\s|(#.*$))+s`   [a-zA-Z_][0-9a-zA-Z_+-]*|[0-9+-][0-9a-zA-Z_.+-]*|"([^"
\\]|\\.)*("|\\?$)|'([^'
\\]|\\.)*('|\\?$)s   \w+c         C   s   | |  _  d |  _ d |  _ d |  _ d  |  _ d |  _ t | j d   |  _	 d |  _
 d |  _ d |  _ |  j   |  j   d  S(   Ni    it    s   
(   t   _text_messaget	   _positiont   _linet   _columnR7   t   _token_startt   tokenR    t   splitt   _linest   _current_linet   _previous_linet   _previous_columnt   _SkipWhitespacet	   NextToken(   t   selft   text_message(    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyt   __init__9  s    									
c         C   s   |  j  d k S(   s^   Checks the end of the text was reached.

    Returns:
      True iff the end was reached.
    Rw   (   R}   (   R   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyR@   H  s    c         C   sc   x\ t  |  j  |  j k r^ |  j s1 d |  _ d  S|  j d 7_ d |  _ |  j j   |  _ q Wd  S(   NRw   i   i    (   t   lenR   R{   R   Rz   t   popleft(   R   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyt   _PopLineP  s    			c         C   sd   x] t  r_ |  j   |  j j |  j |  j  } | s8 Pn  t | j d   } |  j | 7_ q Wd  S(   Ni    (   t   TrueR   t   _WHITESPACEt   matchR   R{   R   t   group(   R   R   t   length(    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyR   Y  s    	
c         C   s!   |  j  | k r |  j   t St S(   s   Tries to consume a given piece of text.

    Args:
      token: Text to consume.

    Returns:
      True iff the text was consumed.
    (   R}   R   R   t   False(   R   R}   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyRJ   b  s    	
c         C   s)   |  j  |  s% |  j d |   n  d S(   s   Consumes a piece of text.

    Args:
      token: Text to consume.

    Raises:
      ParseError: If the text couldn't be consumed.
    s   Expected "%s".N(   RJ   t   _ParseError(   R   R}   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyRR   p  s    	c         C   s;   |  j  } |  j j |  s- |  j d   n  |  j   | S(   s   Consumes protocol message field identifier.

    Returns:
      Identifier string.

    Raises:
      ParseError: If an identifier couldn't be consumed.
    s   Expected identifier.(   R}   t   _IDENTIFIERR   R   R   (   R   R   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyRK   |  s
    		
c         C   sX   y t  |  j d t d t } Wn( t k
 rI } |  j t |    n X|  j   | S(   s   Consumes a signed 32bit integer number.

    Returns:
      The integer parsed.

    Raises:
      ParseError: If a signed 32bit integer couldn't be consumed.
    t	   is_signedt   is_long(   t   ParseIntegerR}   R   R   t
   ValueErrorR   R8   R   (   R   R   t   e(    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyR_     s    	
c         C   sX   y t  |  j d t d t } Wn( t k
 rI } |  j t |    n X|  j   | S(   s   Consumes an unsigned 32bit integer number.

    Returns:
      The integer parsed.

    Raises:
      ParseError: If an unsigned 32bit integer couldn't be consumed.
    R   R   (   R   R}   R   R   R   R8   R   (   R   R   R   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyRf     s    	
c         C   sX   y t  |  j d t d t } Wn( t k
 rI } |  j t |    n X|  j   | S(   s   Consumes a signed 64bit integer number.

    Returns:
      The integer parsed.

    Raises:
      ParseError: If a signed 64bit integer couldn't be consumed.
    R   R   (   R   R}   R   R   R   R8   R   (   R   R   R   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyRc     s    	
c         C   sX   y t  |  j d t d t } Wn( t k
 rI } |  j t |    n X|  j   | S(   s   Consumes an unsigned 64bit integer number.

    Returns:
      The integer parsed.

    Raises:
      ParseError: If an unsigned 64bit integer couldn't be consumed.
    R   R   (   R   R}   R   R   R   R   R8   R   (   R   R   R   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyRi     s    	
c         C   sL   y t  |  j  } Wn( t k
 r= } |  j t |    n X|  j   | S(   s   Consumes an floating point number.

    Returns:
      The number parsed.

    Raises:
      ParseError: If a floating point number couldn't be consumed.
    (   t
   ParseFloatR}   R   R   R8   R   (   R   R   R   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyRl     s    	
c         C   sL   y t  |  j  } Wn( t k
 r= } |  j t |    n X|  j   | S(   s   Consumes a boolean value.

    Returns:
      The bool parsed.

    Raises:
      ParseError: If a boolean value couldn't be consumed.
    (   t	   ParseBoolR}   R   R   R8   R   (   R   R   R   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyRn     s    	
c         C   sF   |  j    } y t | d  SWn" t k
 rA } |  j |   n Xd S(   s   Consumes a string value.

    Returns:
      The string parsed.

    Raises:
      ParseError: If a string value couldn't be consumed.
    s   utf-8N(   Rr   R:   t   UnicodeDecodeErrort   _StringParseError(   R   t   bytesR   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyRp     s
    	c         C   s^   |  j    g } x? t |  j  d k rP |  j d d k rP | j |  j     q Wd j |  S(   s   Consumes a byte array value.

    Returns:
      The array parsed (as a string).

    Raises:
      ParseError: If a byte array value couldn't be consumed.
    i    s   't   "Rw   (   s   'R   (   t   _ConsumeSingleByteStringR   R}   RL   RM   (   R   t   list(    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyRr     s    	+c         C   s   |  j  } t |  d k  s+ | d d	 k r= |  j d   n  t |  d k  sc | d | d k ru |  j d   n  y t | d d ! } Wn( t k
 r } |  j t |    n X|  j   | S(
   s   Consume one token of a string literal.

    String literals (whether bytes or text) can come in multiple adjacent
    tokens which are automatically concatenated, like in C or Python.  This
    method only consumes one token.
    i   i    s   'R   s   Expected string.i   is   String missing ending quote.(   s   'R   (   R}   R   R   t
   _CUnescapeR   R8   R   (   R   RB   R   R   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyR     s    	"&
c         C   sO   y t  | |  j  } Wn( t k
 r@ } |  j t |    n X|  j   | S(   N(   t	   ParseEnumR}   R   R   R8   R   (   R   R   R   R   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyRt     s    
c         C   s%   t  d |  j d |  j d | f  S(   s   Creates and *returns* a ParseError for the previously read token.

    Args:
      message: A message to set for the exception.

    Returns:
      A ParseError instance.
    s
   %d:%d : %si   (   R   R   R   (   R   R   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyRO   %  s    	c         C   s%   t  d |  j d |  j d | f  S(   s9   Creates and *returns* a ParseError for the current token.s
   %d:%d : %si   (   R   Rz   R{   (   R   R   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyR   1  s    c         C   s   |  j  d t |   S(   Ns   Couldn't parse string: (   R   R8   (   R   R   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyR   6  s    c         C   s   |  j  |  _ |  j |  _ |  j t |  j  7_ |  j   |  j ri t |  j  |  j k ri d |  _ d S|  j	 j
 |  j |  j  } | r | j d  } | |  _ n |  j |  j |  _ d S(   s    Reads the next meaningful token.Rw   Ni    (   Rz   R   R{   R   R   R}   R   R   R   t   _TOKENR   R   (   R   R   R}   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyR   9  s    
"	(   R	   R
   R   t   ret   compilet	   MULTILINER   R   R   R   R@   R   R   RJ   RR   RK   R_   Rf   Rc   Ri   Rl   Rn   Rp   Rr   R   Rt   RO   R   R   R   (    (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyR?   (  s4   																							c            s5     f d   } d j  g  |  D] } | |  ^ q  S(   Nc            s   t  |   } | d k r d S| d k r, d S| d k r< d S| d k rL d S| d	 k r\ d
 S| d k rl d S  r | d k s | d k  r d | S|  S(   Ni
   s   \ni   s   \ri	   s   \ti'   s   \'i"   s   \"i\   s   \\i   i    s   \%03o(   t   ord(   t   ct   o(   R   (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyt   escapeS  s            Rw   (   RM   (   RB   R   R   R   (    (   R   s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyR;   R  s    s"   (\\+)x([0-9a-fA-F])(?![0-9a-fA-F])c         C   s(   d   } t  j | |   } | j d  S(   Nc         S   sD   t  |  j d   d @r7 |  j d  d |  j d  S|  j d  S(   Ni   t   x0i   i    (   R   R   (   t   m(    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyt
   ReplaceHexg  s    t   string_escape(   t   _CUNESCAPE_HEXt   subt   decode(   RB   R   R   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyR   f  s    		c         C   sf   y t  |  d  } Wn! t k
 r6 t d |    n Xt d t  |  t  |  } | j |  | S(   s  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.
  i    s   Couldn't parse integer: %si   (   t   intR   t   _INTEGER_CHECKERSt
   CheckValue(   RB   R   R   R   t   checker(    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyR   t  s    c         C   s   y t  |   SWn t k
 r t j |   rT |  d d k rG t  d  St  d  Sq t j |   rm t  d  Sy t  |  j d   SWq t k
 r t d |    q Xn Xd S(	   s   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.
  i    t   -s   -inft   inft   nant   fs   Couldn't parse float: %sN(   t   floatR   t   _FLOAT_INFINITYR   t
   _FLOAT_NANR   (   RB   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyR     s    

c         C   s0   |  d	 k r t  S|  d
 k r  t St d   d S(   s   Parse a boolean value.

  Args:
    text: Text to parse.

  Returns:
    Boolean values parsed

  Raises:
    ValueError: If text is not a valid boolean.
  R1   t   tt   1R2   R   t   0s   Expected "true" or "false".N(   s   trueR   R   (   s   falseR   R   (   R   R   R   (   RB   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyR     s
    c         C   s   |  j  } y t | d  } WnN t k
 rl | j j | d  } | d k r t d | j | f   q n> X| j j | d  } | d k r t d | j | f   n  | j S(   s&  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.
  i    s%   Enum type "%s" has no value named %s.s+   Enum type "%s" has no value with number %d.N(	   R4   R   R   t   values_by_nameR6   R7   R+   R5   t   number(   R   R   t   enum_descriptorR   R>   (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyR     s    	('   R   t
   __author__R   R   t   collectionsR    t   google.protobuf.internalR   t   google.protobufR   t   __all__t   Uint32ValueCheckert   Int32ValueCheckert   Uint64ValueCheckert   Int64ValueCheckerR   R   t
   IGNORECASER   R   t	   ExceptionR   R   R   R   R   R   R   RA   RX   t   objectR?   R;   R   R   R   R   R   R   (    (    (    s?   /usr/lib/python2.7/site-packages/google/protobuf/text_format.pyt   <module>   s@   				
	!#		P	7 +			 	