
    T                     .   S r SSKrSSKrSSKrSSKrSSKJr  SSKJr  SSKJ	r	  SSK
Jr   " S S\	R                  5      r " S	 S
\	R                  5      r " S S\	R                  5      r " S S\	R                  5      r " S S\	R                  5      rS rS rS rS!S jrS rS rS r " S S\R2                  " \R4                  \5      5      r " S S\5      r " S S\5      rS rS r \RB                  RE                  5       \RF                  0r$S  r%g)"aD  Extended protorpc descriptors.

This takes existing protorpc Descriptor classes and adds extra
properties not directly supported in proto itself, notably field and
message descriptions. We need this in order to generate protorpc
message files with comments.

Note that for most of these classes, we can't simply wrap the existing
message, since we need to change the type of the subfields. We could
have a "plain" descriptor attached, but that seems like unnecessary
bookkeeping. Where possible, we purposely reuse existing tag numbers;
for new fields, we start numbering at 100.
    N)
descriptor)message_types)messages)extra_typesc                       \ rS rSrSr\R                  " S5      r\R                  " S\R                  R                  S9r\R                  " S5      rSrg)	ExtendedEnumValueDescriptor*   zEnum value descriptor with additional fields.

Fields:
  name: Name of enumeration value.
  number: Number of enumeration value.
  description: Description of this enum value.
      )variantd    N)__name__
__module____qualname____firstlineno____doc__r   StringFieldnameIntegerFieldVariantINT32numberdescription__static_attributes__r       3lib/third_party/apitools/gen/extended_descriptor.pyr   r   *   sG     "D""1h.>.>.D.DEF&&s+Kr   r   c                       \ rS rSrSr " S S\R                  5      r\R                  " S5      r	\R                  " \SSS9r\R                  " S	5      r\R                  " S
5      r\R                  " SSSS9rSrg)ExtendedEnumDescriptor9   a9  Enum class descriptor with additional fields.

Fields:
  name: Name of Enum without any qualification.
  values: Values defined by Enum class.
  description: Description of this enum class.
  full_name: Fully qualified name of this enum class.
  enum_mappings: Mappings from python to JSON names for enum values.
c                   `    \ rS rSrSr\R                  " S5      r\R                  " S5      rSr	g)&ExtendedEnumDescriptor.JsonEnumMappingE   z8Mapping from a python name to the wire name for an enum.r
   r   r   N
r   r   r   r   r   r   r   python_name	json_namer   r   r   r   JsonEnumMappingr"   E   %    F**1-((+	r   r'   r
   r   Trepeatedr   e   f   r   N)r   r   r   r   r   r   Messager'   r   r   MessageFieldr   valuesr   	full_nameenum_mappingsr   r   r   r   r   r   9   s{    ,(** , "D""#Q7F &&s+K$$S)I))3/Mr   r   c                       \ rS rSrSr\R                  " \R                  S5      r	\R                  " S5      r\R                  " S5      rSrg)ExtendedFieldDescriptorU   zField descriptor with additional fields.

Fields:
  field_descriptor: The underlying field descriptor.
  name: The name of this field.
  description: Description of this field.
r   r+   r,   r   N)r   r   r   r   r   r   r.   protorpc_descriptorFieldDescriptorfield_descriptorr   r   r   r   r   r   r   r3   r3   U   sG      ,,++S2 $D&&s+Kr   r3   c                      \ rS rSrSr " S S\R                  5      r\R                  " S5      r	\R                  " \SSS9r\R                  " S	S
SS9r\R                  " \SSS9r\R                  " S5      r\R                  " S5      r\R                  " SSS9r\R                  " S5      r\R                  " SSSS9rSrg)ExtendedMessageDescriptorr+   a  Message descriptor with additional fields.

Fields:
  name: Name of Message without any qualification.
  fields: Fields defined for message.
  message_types: Nested Message classes defined on message.
  enum_types: Nested Enum classes defined on message.
  description: Description of this message.
  full_name: Full qualified name of this message.
  decorators: Decorators to include in the definition when printing.
      Printed in the given order from top to bottom (so the last entry
      is the innermost decorator).
  alias_for: This type is just an alias for the named type.
  field_mappings: Mappings from python to json field names.
c                   `    \ rS rSrSr\R                  " S5      r\R                  " S5      rSr	g)*ExtendedMessageDescriptor.JsonFieldMappingw   z8Mapping from a python name to the wire name for a field.r
   r   r   Nr$   r   r   r   JsonFieldMappingr;   w   r(   r   r=   r
   r   Tr)   z-extended_descriptor.ExtendedMessageDescriptor      r   r,   g   h   r   N)r   r   r   r   r   r   r-   r=   r   r   r.   r3   fieldsr   r   
enum_typesr   r0   
decorators	alias_forfield_mappingsr   r   r   r   r9   r9   e   s     ,8++ , "D""#:AMF))7TKM&&D2J &&s+K$$S)I%%cD9J$$S)I**C$0Nr   r9   c                       \ rS rSrSr\R                  " S5      r\R                  " \	SSS9r
\R                  " \SSS9r\R                  " S5      r\R                  " S	SS9rS
rg)ExtendedFileDescriptor   aE  File descriptor with additional fields.

Fields:
  package: Fully qualified name of package that definitions belong to.
  message_types: Message definitions contained in file.
  enum_types: Enum definitions contained in file.
  description: Description of this file.
  additional_imports: Extra imports used in this package.
r   r?   Tr)      r   r+   r   N)r   r   r   r   r   r   r   packager.   r9   r   r   rC   r   additional_importsr   r   r   r   rH   rH      sl     ""1%G))!1t5M&&D2J &&s+K!--cDAr   rH   c                 "   UR                  XU 5        [        X0R                  5        [        X0R                  5        [        U R                  5      nUR                  [        U R                  5      5        U H  nUR                  U5        M     g)z8Write the given extended file descriptor to the printer.N)PrintPreamble_PrintEnumsrC   _PrintMessagesr   _FetchCustomMappingsextendPrintCustomJsonMapping)file_descriptorrK   versionproto_printercustom_json_mappingsmappings         r   
_WriteFilerY      ss    /B99:="?"?@/0J0JK_::;=',,W5 (r   c                 0    [        XU[        U5      5        g)zBWrite the given extended file descriptor to out as a message file.N)rY   _Proto2PrinterrT   rK   rU   printers       r   WriteMessagesFiler^      s    g&(r   c                 0    [        XU[        U5      5        g)z0Write the given extended file descriptor to out.N)rY   _ProtoRpcPrinterr\   s       r   WritePythonFilera      s    (*r   c           
         U(       a  U R                  US9   U R                  5          U R                  5       [        U5      -
  nU " 5         U " US-   5        U HE  nUR                  < SUR
                  < 3n[        R                  " XdSSS9 H  nU " U5        M     MG     S S S 5        S S S 5        g g ! , (       d  f       N= f! , (       d  f       g = f)N)indent:z: z      )initial_indentsubsequent_indent)IndentCommentContextCalculateWidthlenr   r   textwrapwrap)r]   lsr   prefixwidthxr   lines           r   PrintIndentedDescriptionsrs      s    	^^6^*'')..03v;>	s
#A./ffamm"DK (k=A@F!H  !H 	 * +* 
)) +*s#   CA:B4"C4
C	>C
Cc                   ^ / nU  H  m[        T[        5      (       a'  UR                  U4S jTR                   5       5        M?  [        T[        5      (       d  MV  UR                  U4S jTR
                   5       5        UR                  [        TR                  5      5        UR                  [        TR                  5      5        M     U$ )zEFind and return all custom mappings for descriptors in descriptor_ls.c              3   >   >#    U  H  n[        S UT5      v   M     g7f)EnumN_FormatCustomJsonMapping.0mr   s     r   	<genexpr>'_FetchCustomMappings.<locals>.<genexpr>   s$      #31A )J??1   c              3   >   >#    U  H  n[        S UT5      v   M     g7f)FieldNrw   ry   s     r   r|   r}      s$      #42A )!Z@@2r~   )	
isinstancer   rR   r1   r9   rF   rQ   rC   r   )descriptor_lscustom_mappingsr   s     @r   rQ   rQ      s    O#
j"899"" #3#11#3 3 
$=>>"" #4#22#4 4 ""$Z%:%:;=""$Z%=%=>@ $ r   c           
          SR                  SU -  SUR                  < SUR                  < SUR                  < S345      $ )N
z encoding.AddCustomJson%sMapping(re   z, 'z', 'z'))joinr0   r%   r&   )mapping_typerX   r   s      r   rx   rx      s?    99*\9!+!5!5w7J7J!(!2!2	4  r   c                 d    [        U R                  U R                  U R                  45      (       + $ N)anyrC   r   rB   )message_types    r   _EmptyMessager      s3    L++ .. '') * * *r   c                       \ rS rSrSr\R                  S 5       r\R                  S 5       r\R                  S 5       r	Sr
g)ProtoPrinter   zInterface for proto printers.c                     g)z*Print the file docstring and import lines.Nr   )selfrK   rU   rT   s       r   rN   ProtoPrinter.PrintPreamble       r   c                     g)z!Print the given enum declaration.Nr   )r   	enum_types     r   	PrintEnumProtoPrinter.PrintEnum   r   r   c                     g)z$Print the given message declaration.Nr   r   r   s     r   PrintMessageProtoPrinter.PrintMessage   r   r   r   N)r   r   r   r   r   abcabstractmethodrN   r   r   r   r   r   r   r   r      sQ    '9 9 	0 0 	3 3r   r   c                   T    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rSrg)r[      zPrinter for proto2 definitions.c                     Xl         g r   )_Proto2Printer__printerr   r]   s     r   __init___Proto2Printer.__init__        r   c                    UR                   =(       d    SUR                  -  n[        R                  " UU R                  R                  5       S-
  5       H  nU R	                  SU5        M     [        U R                  UR                  SSS9  g )N%s enum type.r>   // %sValues// ro   )r   r   rl   rm   r   rj   rs   r/   r   r   r   rr   s       r   __PrintEnumCommentLines&_Proto2Printer.__PrintEnumCommentLines  sr    ++O/OMM+"&.."?"?"AA"EGDNN7D)G 	"$..)2B2BH).	0r   c                     UR                   (       aX  U R                  R                  5       S-
  n[        R                  " UR                   U5       H  nU R                  SU5        M     g g Nr>   r   )r   r   rj   rl   rm   )r   
enum_valuerp   rr   s       r   __PrintEnumValueCommentLines+_Proto2Printer.__PrintEnumValueCommentLines
  sQ    !!NN113a7E j&<&<eDw- E "r   c                    U R                  U5        U R                  SUR                  5        U R                  R                  5          [	        UR
                  [        R                  " S5      S9nU HK  nU R                  5         U R                  U5        U R                  SUR                  UR                  5        MM     S S S 5        U R                  S5        U R                  5         g ! , (       d  f       N0= f)Nz	enum %s {r   keyz%s = %s;})
%_Proto2Printer__PrintEnumCommentLinesr   r   rh   sortedr/   operator
attrgetter*_Proto2Printer__PrintEnumValueCommentLinesr   r   r   enum_valuesr   s       r   r   _Proto2Printer.PrintEnum  s    $$Y/{INN3^^""$   h&9&9(&CEK)
 11*=z:??J<M<MN * % 	s %$s   A:C,,
C:c                 l   U R                  SX5        U R                  S5        [        R                  " UR                  S5      nU(       a,  U R                  S5        U H  nU R                  SU5        M     U R                  5         U R                  S5        U R                  SUR                  5        g )Nz/// Generated message classes for %s version %s.zE// NOTE: This file is autogenerated and should not be edited by hand.K   z//r   zsyntax = "proto2";zpackage %s;)r   rl   rm   r   rK   )r   rK   rU   rT   description_linesrr   s         r   rN   _Proto2Printer.PrintPreamble  s    H	) ) 	*$MM/*E*ErJNN4 )w- *+,}o&=&=>r   c                    UR                   =(       d    SUR                  -  nU R                  R                  5       S-
  n[        R
                  " X#5       H  nU R                  SU5        M     [        U R                  UR                  SSS9  [        U R                  UR                  SSS9  [        U R                  UR                  SSS9  g	)
z&Print the description of this message.%s message type.r>   r   Enumsr   r   MessagesFieldsN)
r   r   r   rj   rl   rm   rs   rC   r   rB   )r   r   r   rp   rr   s        r   __PrintMessageCommentLines)_Proto2Printer.__PrintMessageCommentLines+  s    ".. 2D3--/!3MM+5DNN7D) 6!$..,2I2I")%	9!$..,2L2L",U	<!$..,2E2E"*5	:r   c                     [         R                  " UU R                  R                  5       S-
  5       H  nU R                  SU5        M     g r   )rl   rm   r   rj   )r   r   rr   s      r   __PrintFieldDescription&_Proto2Printer.__PrintFieldDescription9  s<    MM+"&.."?"?"AA"EGDNN7D)Gr   c           	      J   U GH  nUR                   n[        R                  R                  UR                  5      nU R                  5         U R                  UR                  5        [        UR                  5      R                  5       nU[        R                  [        R                  4;   a  UR                  nO#[        UR                  5      R                  5       nSnUR                  (       a  U[        R                  [        R                   4;   a  SUR                  -  nOLU[        R"                  L a$  [        UR                  5      R                  5       nO[        UR                  5      nSU-  nU R                  SXVUR$                  UR&                  U5        GM     g )N z"%s"z [default = %s]z%s %s %s = %d%s;)r7   r   r   lookup_field_type_by_variantr   r   %_Proto2Printer__PrintFieldDescriptionr   strlabellower	EnumFieldr.   	type_namedefault_value
BytesFieldr   BooleanFieldr   r   )	r   rB   extended_fieldfield
field_typer   
proto_typedefault_statementr   s	            r   __PrintFields_Proto2Printer.__PrintFields>  s<   $N"33E!DDJNN(()C)CD$**,Eh00(2G2GHH"__
 /557
 """("5"5x7K7K!LL$*U-@-@$@M8#8#88$'(;(;$<$B$B$DM$'(;(;$<M$5$E!NN"5::u||=NP+ %r   c                    U R                  5         U R                  U5        [        U5      (       a  U R                  SUR                  5        g U R                  SUR                  5        U R                   R	                  5          [        XR                  5        [        XR                  5        U R                  UR                  5        S S S 5        U R                  S5        g ! , (       d  f       N = f)Nzmessage %s {}zmessage %s {r   )r   (_Proto2Printer__PrintMessageCommentLinesr   r   rh   rO   rC   rP   r   _Proto2Printer__PrintFieldsrB   r   s     r   r   _Proto2Printer.PrintMessageX  s    ''5&&NN?L,=,=>~|'8'89^^""$5564!;!;<|223 % 	s	 %$s   AC%%
C3c                     [        S5      e)Nz-Custom JSON encoding not supported for proto2)NotImplementedError)r   mapping_liness     r   rS   %_Proto2Printer.PrintCustomJsonMappinge  s    !;= 	=r   	__printerN)r   r   r   r   r   r   r   r   r   rN   r   r   r   r   rS   r   r   r   r   r[   r[      s8    )!0.?:*
P4=r   r[   c                   N    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rSrg)r`   ij  z!Printer for ProtoRPC definitions.c                     Xl         g r   _ProtoRpcPrinter__printerr   s     r   r   _ProtoRpcPrinter.__init__n  r   r   c                 |    U R                  5         U R                   R                  (       d  U R                  5         g g r   )r   rc   )r   s    r   __PrintClassSeparator&_ProtoRpcPrinter.__PrintClassSeparatorq  s(    ~~$$NN %r   c                 @   UR                   =(       d    SUR                  -  n[        R                  " SU-  U R                  R                  5       5       H  nU R	                  U5        M     [        U R                  UR                  S5        U R	                  S5        g )Nr   r"""%sr   """)r   r   rl   rm   r   rj   rs   r/   r   s       r   __PrintEnumDocstringLines*_ProtoRpcPrinter.__PrintEnumDocstringLinesv  sw    ++O/OMM(["8"&.."?"?"ACDNN4 C 	"$..)2B2BHMur   c                    U R                  SUR                  5        U R                   R                  5          U R                  U5        [	        UR
                  [        R                  " S5      S9nU H*  nU R                  SUR                  UR                  5        M,     UR
                  (       d  U R                  S5        S S S 5        U R                  5         g ! , (       d  f       N= f)Nzclass %s(_messages.Enum):r   r   %s = %spass)
r   r   rh   )_ProtoRpcPrinter__PrintEnumDocstringLinesr   r/   r   r   r   %_ProtoRpcPrinter__PrintClassSeparatorr   s       r   r   _ProtoRpcPrinter.PrintEnum~  s    2INNC^^""$**95   h&9&9(&CEK)
y*//:;L;LM *##v& % 	""$ %$s   BC
C*c                 j   U Vs/ s H  nSU;   d  M  UPM     nnU Vs/ s H  nSU;  d  M  UPM     nnU(       a3  [        U5       H  nU R                  U5        M     U R                  5         U(       a4  [        U5       H  nU R                  U5        M     U R                  5         ggs  snf s  snf )z-Print additional imports needed for protorpc.googleN)r   r   )r   importsrq   google_importsother_importsimport_s         r   __PrintAdditionalImports)_ProtoRpcPrinter.__PrintAdditionalImports  s    %,>WA!W>$+AGqxq/@GA!-0w' 1NN !.1w' 2NN  ?As   
B+B+
B0B0c                 .   U R                  SX5        U R                  5         [        R                  " UR                  S5       H  nU R                  U5        M     U R                  S5        U R                  S5        U R                  5         U R                  S5        U R                  5         U R	                  UR
                  5        U R                  5         U R                  SUR                  5        U R                  5         U R                  5         g )Nz/"""Generated message classes for %s version %s.N   r   zD# NOTE: This file is autogenerated and should not be edited by hand.z&from __future__ import absolute_importzpackage = '%s')r   rl   rm   r   (_ProtoRpcPrinter__PrintAdditionalImportsrL   rK   )r   rK   rU   rT   rr   s        r   rN   _ProtoRpcPrinter.PrintPreamble  s    H	)MM/"="=rBDNN4  Cu ) 	*?@%%o&H&HI')@)@Ar   c                    UR                   =(       d    SUR                  -  n[        U5      =(       a)    [        U5      U R                  R                  5       S-
  :  nU R                  R                  5          U(       a  U R	                  SU-  5         SSS5        g[        R                  " SU-  U R                  R                  5       5       H  nU R	                  U5        M     [        U R                  UR                  S5        [        U R                  UR                  S5        [        U R                  UR                  S5        U R	                  S	5        U R	                  5         SSS5        g! , (       d  f       g= f)
z%Print the docstring for this message.r      z	r"""%s"""Nr   r   r   r   r   )r   r   r   rk   r   rj   ri   rl   rm   rs   rC   r   rB   )r   r   r   short_descriptionrr   s        r   __PrintMessageDocstringLines-_ProtoRpcPrinter.__PrintMessageDocstringLines  s4   ".. 2D3 ,' E = = ?! CD 	 ^^**,  {[89 -, !h&<&*nn&C&C&EGt$G &dnnl6M6M&-/% : :JH% 3 3X?NN5!NN# -,,s   8E5CE55
Fc                 V   UR                   (       a8  U R                  SUR                  UR                   5        U R                  5         g UR                   H  nU R                  SU5        M     U R                  SUR                  5        U R                  R                  5          U R                  U5        [        XR                  5        [        XR                  5        [        UR                  U R                  5        S S S 5        U R                  5         g ! , (       d  f       N= f)Nr   z@%szclass %s(_messages.Message):)rE   r   r   r   rD   rh   ,_ProtoRpcPrinter__PrintMessageDocstringLinesrO   rC   rP   r   _PrintFieldsrB   )r   r   	decorators      r   r   _ProtoRpcPrinter.PrintMessage  s    !!NN<,,l.D.DF&&(%00INN5), 15|7H7HI^^""$--l;5564!;!;<,,dnn=	 %
 	""$ %$s   %AD
D(c                 &    U R                  U5        g r   r   )r   rX   s     r   rS   '_ProtoRpcPrinter.PrintCustomJsonMapping  s    wr   r   N)r   r   r   r   r   r   r   r   r   r  rN   r  r   rS   r   r   r   r   r`   r`   j  s2    +!

%$4%  r   r`   c                 t    [        U[        R                  " S5      S9nU H  nU R                  U5        M     g)z+Print all enums to the given proto_printer.r   r   N)r   r   r   r   )rV   rC   r   s      r   rO   rO     s1    
(;(;F(CDJ		*  r   c                 t    [        U[        R                  " S5      S9nU H  nU R                  U5        M     g )Nr   r   )r   r   r   r   )rV   message_listr   s      r   rP   rP     s1    ,H,?,?,GHL$""<0 %r   c           
         U  GHF  nUR                   nUR                  SSSUR                  SSSS.n[        R	                  UR
                  5      nU(       a  SUS'   UnOOUR
                  S:X  a  SUS'   [        R                  nO)[        R                  R                  UR                  5      nU[        R                  [        R                  4;   a  SUR
                  -  US	'   UR                  [        R                   R"                  R$                  :X  a  S
US'   O7UR                  [        R                   R"                  R&                  :X  a  SUS'   UR(                  UR                  :w  a  SUR                  -  US'   UR*                  (       a  U[        R,                  [        R.                  4;   a  [1        UR*                  5      nO?U[        R                  L a    [3        [5        UR*                  5      5      nOUR*                  nSU< 3US'   UR8                  US'   SR;                  S S 5       5      nSU-  n	U" X-  5        GMI     g ! [6         a    [1        UR*                  5      n Nff = f)N	_messagesr   )r   moduler   type_formatr   label_formatvariant_formatdefault_format_message_typesr  zextra_types.DateFieldr   z'%s', r  z, required=Truer  z, repeated=Truez, variant=_messages.Variant.%sr  z
, default=r  r   c              3   ,   #    U  H
  nS U-  v   M     g7f)z%%(%s)sNr   )rz   r   s     r   r|   _PrintFields.<locals>.<genexpr>   s       7Uy5( 7s   )r  r   r  r  r  z*%%(name)s = %%(module)s.%%(type_name)s(%s))r7   r   r   _MESSAGE_FIELD_MAPgetr   r   	DateFieldr   r   r   r   r   r.   r   r5   r6   LabelREQUIREDREPEATEDDEFAULT_VARIANTr   r   r   reprr   int
ValueErrorr   r   )
rB   r]   r   r   printed_field_infomessage_fieldr   r   args
format_strs
             r   r  r    s=    //JJ!ll  	
 +..u?+;x(&J__ 77+8x($..J!DDJ (,,h.C.CDD085??0J}-;;-==CCLLL1B~.[[/??EENNN1B~.%%605==@ /0 h1183G3GHH $U%8%8 9x111>$'E,?,?(@$AM !& 3 3 7D$E  " +5*=*=;'ww  7   BDH

/0u !T " >$()<)<$=M>s   II10I1)r   )&r   r   r   rl   sixapitools.base.protorpcliter   r5   r   r   apitools.base.pyr   r-   r   r   r3   r9   rH   rY   r^   ra   rs   rQ   rx   r   with_metaclassABCMetaobjectr   r[   r`   rO   rP   DateTimeMessagedefinition_nameDateTimeFieldr#  r  r   r   r   <module>r:     s  "    
 H 4 / (,("2"2 ,/X-- /8,h.. , $0 0 0 $0NBX-- B,	6(*&&*33%%ckk6: 3"l=\ l=^l | l ^+1 !!113## ;1r   