ó
ÉÈ÷Xc           @   s6  d  Z  d Z d d l Z d d l m Z d d l Z d d l Z d d l Z d d l Z d d l m	 Z	 d Z
 e j d ƒ Z e j d ƒ Z e e	 j j e	 j j e	 j j e	 j j g ƒ Z e e	 j j e	 j j g ƒ Z e e	 j j e	 j j g ƒ Z e e k re Z n e Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ e  d „ Z! d „  Z" d „  Z# d „  Z$ e  d „ Z% d „  Z& d „  Z' d „  Z( d „  Z) d „  Z* d „  Z+ d „  Z, d „  Z- d „  Z. d „  Z/ d „  Z0 d „  Z1 d „  Z2 d „  Z3 d  „  Z4 d! „  Z5 d" „  Z6 e  d# „ Z7 d$ „  Z8 d% „  Z9 d& „  Z: d S('   s@   Contains routines for printing protocol messages in JSON format.s   jieluo@google.com (Jie Luo)iÿÿÿÿN(   t   datetime(   t
   descriptors   %Y-%m-%dT%H:%M:%Su   [0-9+-][0-9e.+-]*u   [0-9+-]t   SerializeToJsonErrorc           B   s   e  Z d  Z RS(   s&   Thrown if serialization to JSON fails.(   t   __name__t
   __module__t   __doc__(    (    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyR   =   s   t
   ParseErrorc           B   s   e  Z d  Z RS(   s    Thrown in case of parsing error.(   R   R   R   (    (    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyR   A   s   c         C   s"   t  |  | ƒ } t j | d d ƒS(   sØ  Converts protobuf message to JSON format.

  Args:
    message: The protocol buffers message instance to serialize.
    including_default_value_fields: If True, singular primitive fields,
        repeated fields, and map fields will always be serialized.  If
        False, only serialize non-empty fields.  Singular message fields
        and oneof fields are not affected by this option.

  Returns:
    A string containing the JSON formatted protocol buffer message.
  t   indenti   (   t   _MessageToJsonObjectt   jsont   dumps(   t   messaget   including_default_value_fieldst   js(    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyt   MessageToJsonE   s    c         C   sn   |  j  } t | ƒ r t |  ƒ St | ƒ r5 t |  ƒ St | ƒ rK t |  ƒ St | ƒ ra t |  ƒ St	 |  | ƒ S(   sE   Converts message to an object according to Proto3 JSON Specification.(
   t
   DESCRIPTORt   _IsTimestampMessaget   _TimestampMessageToJsonObjectt   _IsDurationMessaget   _DurationMessageToJsonObjectt   _IsFieldMaskMessaget   _FieldMaskMessageToJsonObjectt   _IsWrapperMessaget   _WrapperMessageToJsonObjectt   _RegularMessageToJsonObject(   R   R   t   message_descriptor(    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyR   V   s    	



c         C   s1   |  j  t j j k o0 |  j j o0 |  j j ƒ  j S(   N(   t   typeR   t   FieldDescriptort   TYPE_MESSAGEt   message_typet   has_optionst
   GetOptionst	   map_entry(   t   field(    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyt   _IsMapEntryd   s    c         C   s  i  } |  j  ƒ  } yØx| D]þ \ } } | j } t | ƒ r² i  } x_ | D]W } t | t ƒ rw | rn d }	 q} d }	 n | }	 t | j j d | | | ƒ | |	 <qJ W| | | <q | j t	 j
 j k rg  }
 x' | D] } |
 j t | | | ƒ ƒ qÔ W|
 | | <q t | | | ƒ | | <q W| ré|  j } x¹ | j D]« } | j t	 j
 j k rg| j t	 j
 j k s7| j rvq7n  | j } | | k r‘q7n  t | ƒ rªi  | | <q7| j t	 j
 j k rÌg  | | <q7t | | j ƒ | | <q7Wn  Wn. t k
 r} t d j | j | ƒ ƒ ‚ n X| S(   s?   Converts normal message according to Proto3 JSON Specification.t   truet   falset   values"   Failed to serialize {0} field: {1}(   t
   ListFieldst   camelcase_nameR"   t
   isinstancet   boolt   _ConvertFieldToJsonObjectR   t   fields_by_namet   labelR   R   t   LABEL_REPEATEDt   appendR   t   fieldst   cpp_typet   CPPTYPE_MESSAGEt   containing_oneoft   default_valuet
   ValueErrorR   t   formatt   name(   R   R   R   R/   R!   R%   R6   t   js_mapt   keyt   recorded_keyt   repeatedt   elementR   t   e(    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyR   j   sX    							!c         C   sF  |  j  t j j k r" t | | ƒ S|  j  t j j k rq |  j j j | d ƒ } | d k	 rb | j
 St d ƒ ‚ nÑ |  j  t j j k r¸ |  j t j j k r± t j | ƒ j d ƒ S| SnŠ |  j  t j j k rÞ | r× t St Snd |  j  t k r÷ t | ƒ S|  j  t k rBt j | ƒ r,| d k  r%d Sd Sn  t j | ƒ rBd Sn  | S(   s<   Converts field value according to Proto3 JSON Specification.sK   Enum field contains an integer value which can not mapped to an enum value.s   utf-8g        s	   -Infinityt   Infinityt   NaNN(   R0   R   R   R1   R   t   CPPTYPE_ENUMt	   enum_typet   values_by_numbert   gett   NoneR6   R   t   CPPTYPE_STRINGR   t
   TYPE_BYTESt   base64t	   b64encodet   decodet   CPPTYPE_BOOLt   Truet   Falset   _INT64_TYPESt   strt   _FLOAT_TYPESt   matht   isinft   isnan(   R!   R%   R   t
   enum_value(    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyR*   §   s2    
c         C   s   |  j  d k o |  j j  d k S(   Nt	   Timestamps   google/protobuf/timestamp.proto(   R6   t   file(   R   (    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyR   Ë   s    c         C   sž   |  j  d } t j |  j |  j  | d ƒ } | j ƒ  } | d d k rR | d S| d d k rr | d | d S| d d k r’ | d | d S| d | S(	   sB   Converts Timestamp message according to Proto3 JSON Specification.g    eÍÍAi    t   Zg    €„.As   .%03dZg     @@s   .%06dZs   .%09dZ(   t   nanosR    t   utcfromtimestampt   secondst	   isoformat(   R   RV   t   dtt   result(    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyR   Ð   s    c         C   s   |  j  d k o |  j j  d k S(   Nt   Durations   google/protobuf/duration.proto(   R6   RT   (   R   (    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyR   ä   s    c         C   sö   |  j  d k  s |  j d k  rW d } |  j  t d |  j d ƒ } d |  j d } n- d } |  j  t |  j d ƒ } |  j d } | d | 7} | d d k rª | d S| d d k rÊ | d | d S| d	 d k rê | d
 | d	 S| d | S(   sA   Converts Duration message according to Proto3 JSON Specification.i    t   -g    eÍÍAt    s   %dt   sg    €„.As   .%03dsg     @@s   .%06dss   .%09ds(   RX   RV   t   int(   R   R[   RX   RV   (    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyR   é   s    c         C   s   |  j  d k o |  j j  d k S(   Nt	   FieldMasks    google/protobuf/field_mask.proto(   R6   RT   (   R   (    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyR     s    c         C   sG   d } t  } x4 |  j D]) } | s/ | d 7} n  | | 7} t } q W| S(   sB   Converts FieldMask message according to Proto3 JSON Specification.R^   t   ,(   RJ   t   pathsRK   (   R   R[   t   firstt   path(    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyR     s    

c         C   s   |  j  j d k S(   Ns   google/protobuf/wrappers.proto(   RT   R6   (   R   (    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyR     s    c         C   s   t  |  j j d |  j ƒ S(   NR%   (   R*   R   R+   R%   (   R   (    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyR     s    c         C   sJ   i  } x= |  D]5 \ } } | | k r8 t  d | ƒ ‚ n  | | | <q W| S(   Ns#   Failed to load JSON: duplicate key (   R   (   R   R[   R6   R%   (    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyt   _DuplicateChecker  s    c         C   s˜   t  |  t ƒ s! |  j d ƒ }  n  y: t j d k  rE t j |  ƒ } n t j |  d t ƒ} Wn) t k
 r† } t	 d t
 | ƒ ƒ ‚ n Xt | | ƒ | S(   s  Parses a JSON representation of a protocol message into a message.

  Args:
    text: Message JSON representation.
    message: A protocol beffer message to merge into.

  Returns:
    The same message passed as argument.

  Raises::
    ParseError: On JSON parsing problems.
  s   utf-8i   i   t   object_pairs_hooks   Failed to load JSON: (   i   i   (   R(   t   _UNICODETYPERH   t   syst   version_infoR	   t   loadsRf   R4   R   RM   t   _ConvertFieldValuePair(   t   textR   R   R<   (    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyt   Parse%  s     c         C   s!  g  } | j  } x|  D]} yQ| j j | d ƒ } | sX t d j | j | ƒ ƒ ‚ n  | | k r… t d j | j  j | ƒ ƒ ‚ n  | j | ƒ | j d k	 rê | j j	 } | | k rÚ t d j | j  j | ƒ ƒ ‚ n  | j | ƒ n  |  | } | d k r| j
 | j	 ƒ w n  t | ƒ rE| j
 | j	 ƒ t | | | ƒ n'| j t j j k r| j
 | j	 ƒ t | t ƒ s”t d j | | ƒ ƒ ‚ n  xÕ | D]w } | d k r³q›n  | j t j j k rðt | | j	 ƒ j ƒ  }	 t | |	 ƒ q›t | | j	 ƒ j t | | ƒ ƒ q›WnS | j t j j k rPt | | j	 ƒ }	 t | |	 ƒ n t | | j	 t | | ƒ ƒ Wq t k
 rÄ}
 | r¯| j d k r¯t d j | |
 ƒ ƒ ‚ qt t |
 ƒ ƒ ‚ q t k
 rî}
 t d j | |
 ƒ ƒ ‚ q t k
 r}
 t d j | |
 ƒ ƒ ‚ q Xq Wd S(   sê   Convert field value pairs into regular message.

  Args:
    js: A JSON object to convert the field value pairs.
    message: A regular protocol message to record the data.

  Raises:
    ParseError: In case of problems converting.
  s,   Message type "{0}" has no field named "{1}".s9   Message type "{0}" should not have multiple "{1}" fields.s?   Message type "{0}" should not have multiple "{1}" oneof fields.s-   repeated field {0} must be in [] which is {1}s   Failed to parse {0} field: {1}N(   R   t   fields_by_camelcase_nameRB   RC   R   R5   t	   full_nameR.   R2   R6   t
   ClearFieldR"   t   _ConvertMapFieldValueR,   R   R   R-   R(   t   listR0   R1   t   getattrt   addt   _ConvertMessaget   _ConvertScalarFieldValuet   setattrRM   R4   t	   TypeError(   R   R   t   namesR   R6   R!   t
   oneof_nameR%   t   itemt   sub_messageR<   (    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyRl   ?  sh    
		
	 c         C   sŠ   | j  } t | ƒ r% t |  | ƒ na t | ƒ rA t |  | ƒ nE t | ƒ r] t |  | ƒ n) t | ƒ ry t |  | ƒ n t	 |  | ƒ d S(   sÆ   Convert a JSON object into a message.

  Args:
    value: A JSON object.
    message: A WKT or regular protocol message to record the data.

  Raises:
    ParseError: In case of convert problems.
  N(
   R   R   t   _ConvertTimestampMessageR   t   _ConvertDurationMessageR   t   _ConvertFieldMaskMessageR   t   _ConvertWrapperMessageRl   (   R%   R   R   (    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyRv   ‡  s    
	c         C   sX  |  j  d ƒ } | d k r- |  j  d ƒ } n  | d k rK |  j d ƒ } n  | d k rf t d ƒ ‚ n  |  d | !} | j  d ƒ } | d k r | } d } n | |  } | | d	 } t j | t ƒ } | t d
 d	 d	 ƒ } | j | j d d }	 t | ƒ d k rt d j	 | ƒ ƒ ‚ n  | rBt
 t d | ƒ d ƒ }
 n d }
 |  | d k r‰t |  ƒ | d	 k r6t d j	 |  ƒ ƒ ‚ q6n­ |  | } | j  d ƒ } | d k rÁt d | ƒ ‚ n  | d d k r|	 t | d	 | !ƒ d t | | d	 ƒ d 7}	 n1 |	 t | d	 | !ƒ d t | | d	 ƒ d 8}	 t |	 ƒ | _ t |
 ƒ | _ d S(   s5   Convert a JSON representation into Timestamp message.RU   iÿÿÿÿt   +R]   s9   Failed to parse timestamp: missing valid timezone offset.i    t   .R^   i   i²  i   i  i	   sC   Failed to parse Timestamp: nanos {0} more than 9 fractional digits.s   0.g    eÍÍAs5   Failed to parse timestamp: invalid trailing data {0}.t   :s   Invalid timezone offset value: i<   N(   t   findt   rfindR   R    t   strptimet   _TIMESTAMPFOMATRX   t   dayst   lenR5   t   roundt   floatR`   RV   (   R%   R   t   timezone_offsett
   time_valuet   point_positiont   second_valuet
   nano_valuet   date_objectt   tdRX   RV   t   timezonet   pos(    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyR~   ž  sN    	

41c         C   sŽ   |  d d k r# t  d |  ƒ ‚ n  y t |  d  ƒ } Wn! t k
 rZ t  d |  ƒ ‚ n Xt | ƒ | _ t t | | j d ƒ ƒ | _ d S(   s4   Convert a JSON representation into Duration message.iÿÿÿÿR_   s#   Duration must end with letter "s": s   Couldn't parse duration: g    eÍÍAN(   R   RŒ   R4   R`   RX   R‹   RV   (   R%   R   t   duration(    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyR   Ð  s    c         C   s.   x' |  j  d ƒ D] } | j j | ƒ q Wd S(   s5   Convert a JSON representation into FieldMask message.Rb   N(   t   splitRc   R.   (   R%   R   Re   (    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyR€   Þ  s    c         C   s-   | j  j d } t | d t |  | ƒ ƒ d S(   s3   Convert a JSON representation into Wrapper message.R%   N(   R   R+   Rx   Rw   (   R%   R   R!   (    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyR   ä  s    c         C   sÐ   t  |  t ƒ s- t d j | j |  ƒ ƒ ‚ n  | j j d } | j j d } x| |  D]t } t | | t ƒ } | j	 t
 j j k r¥ t |  | t | | j ƒ | ƒ qT t |  | | ƒ t | | j ƒ | <qT Wd S(   s'  Convert map field value for a message map field.

  Args:
    value: A JSON object to convert the map field value.
    message: A protocol message to record the converted data.
    field: The descriptor of the map field to be converted.

  Raises:
    ParseError: In case of convert problems.
  s*   Map fieled {0} must be in {} which is {1}.R8   R%   N(   R(   t   dictR   R5   R6   R   R+   Rw   RJ   R0   R   R   R1   Rv   Rt   (   R%   R   R!   t	   key_fieldt   value_fieldR8   t	   key_value(    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyRr   ê  s    $c         C   s÷   | j  t k r t |  ƒ S| j  t k r2 t |  ƒ S| j  t j j k rT t |  | ƒ S| j  t j j	 k r’ | j
 t j j k r‹ t j |  ƒ S|  Sna | j  t j j k ró | j j j |  d ƒ } | d k rì t d j | j j |  ƒ ƒ ‚ n  | j Sd S(   sJ  Convert a single scalar field value.

  Args:
    value: A scalar value to convert the scalar field value.
    field: The descriptor of the field to convert.
    require_quote: If True, '"' is required for the field value.

  Returns:
    The converted scalar field value

  Raises:
    ParseError: In case of convert problems.
  sZ   Enum value must be a string literal with double quotes. Type "{0}" has no value named {1}.N(   R0   t
   _INT_TYPESt   _ConvertIntegerRN   t   _ConvertFloatR   R   RI   t   _ConvertBoolRD   R   RE   RF   t	   b64decodeR?   R@   t   values_by_nameRB   RC   R   R5   Rp   t   number(   R%   R!   t   require_quoteRR   (    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyRw     s"    

c         C   sh   t  |  t ƒ r' t d j |  ƒ ƒ ‚ n  t  |  t ƒ r^ t j |  ƒ r^ t d j |  ƒ ƒ ‚ n  t |  ƒ S(   s§   Convert an integer.

  Args:
    value: A scalar value to convert.

  Returns:
    The integer value.

  Raises:
    ParseError: If an integer couldn't be consumed.
  s   Couldn't parse integer: {0}s   Couldn't parse integer: "{0}"(   R(   RŒ   R   R5   Rh   t   _INTEGERt   matchR`   (   R%   (    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyR   '  s
    c         C   s˜   |  d k r t  d ƒ ‚ n  y t |  ƒ SWnh t k
 r“ |  d k rO t d ƒ S|  d k re t d ƒ S|  d k r{ t d ƒ St  d j |  ƒ ƒ ‚ n Xd	 S(
   s!   Convert an floating point number.t   nans-   Couldn't parse float "nan", use "NaN" insteads	   -Infinitys   -infR=   t   infR>   s   Couldn't parse float: {0}N(   R   RŒ   R4   R5   (   R%   (    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyRž   <  s    


c         C   s`   | r> |  d k r t  S|  d k r& t St d j |  ƒ ƒ ‚ n  t |  t ƒ s\ t d ƒ ‚ n  |  S(   sò   Convert a boolean value.

  Args:
    value: A scalar value to convert.
    require_quote: If True, '"' is required for the boolean value.

  Returns:
    The bool parsed.

  Raises:
    ParseError: If a boolean value couldn't be consumed.
  R#   R$   s"   Expect "true" or "false", not {0}.s&   Expected true or false without quotes.(   RJ   RK   R   R5   R(   R)   (   R%   R£   (    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyRŸ   O  s    (;   R   t
   __author__RF   R    R	   RO   t   reRi   t   google.protobufR   Rˆ   t   compilet   _NUMBERR¤   t	   frozensetR   t   CPPTYPE_INT32t   CPPTYPE_UINT32t   CPPTYPE_INT64t   CPPTYPE_UINT64Rœ   RL   t   CPPTYPE_FLOATt   CPPTYPE_DOUBLERN   RM   t   bytest   unicodeRh   t	   ExceptionR   R   RK   R   R   R"   R   R*   R   R   R   R   R   R   R   R   Rf   Rn   Rl   Rv   R~   R   R€   R   Rr   Rw   R   Rž   RŸ   (    (    (    s]   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/google/protobuf/json_format.pyt   <module>   sd   						>#												H		2				$		