
    t)                        S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSK	r	SSK
r\R                  r\R                  " SSS5        \R                  " SSS5        \R                  " SSS	5        / rS
r " S S\5      r " S S\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
qS rS rS!S jrS rS rS"S jr " S S\ 5      r!S  r"g)#a  Generic entry point for Google applications.

To use this module, simply define a 'main' function with a single
'argv' argument and add the following to the end of your source file:

if __name__ == '__main__':
  app.run()

TODO(user): Remove silly main-detection logic, and force all clients
of this module to check __name__ explicitly.  Fix all current clients
that don't check __name__.
    Nrun_with_pdbzSet to true for PDB debug moderun_with_profilingzmSet to true for profiling the script. Execution will be slower, and the output format might change over time.use_cprofile_for_profilingTzpUse cProfile instead of the profile module for profiling. This has no effect unless --run_with_profiling is set.Fc                       \ rS rSrSrg)Error9    N)__name__
__module____qualname____firstlineno____static_attributes__r	       .platform/bq/third_party/google/apputils/app.pyr   r   9   s    r   r   c                   "    \ rS rSrSrSS jrSrg)
UsageError=   ao  The arguments supplied by the user are invalid.

Raise this when the arguments supplied are invalid from the point of
view of the application. For example when two mutually exclusive
flags have been supplied or when there are not enough non-flag
arguments. It is distinct from flags.FlagsError which covers the lower
level of parsing and validating individual flags.
c                 :    [         R                  X5        X l        g N)r   __init__exitcode)selfmessager   s      r   r   UsageError.__init__G   s    	NN4!Mr   )r   N)   )r
   r   r   r   __doc__r   r   r	   r   r   r   r   =   s    r   r   c                   $    \ rS rSrSrS rS rSrg)HelpFlagL   z?Special boolean flag that displays usage and raises SystemExit.c           	      H    [         R                  R                  U SSSSSS9  g )Nhelpr   zshow this help?r   )
short_nameallow_overrideflagsBooleanFlagr   r   s    r   r   HelpFlag.__init__O   s*    	tVQ0@*-a  Ar   c                 R    U(       a   [        SS9  [        R                  " S5        g g )Nr   )writeto_stdoutusagesysexitr   args     r   ParseHelpFlag.ParseS   s    
1	hhqk r   r	   Nr
   r   r   r   r   r   r2   r   r	   r   r   r   r   L   s    GAr   r   c                   $    \ rS rSrSrS rS rSrg)HelpXMLFlagY   z8Similar to HelpFlag, but generates output in XML format.c                 F    [         R                  R                  U SSSSS9  g )NhelpxmlFz%like --help, but generates XML outputr   r$   r%   r(   s    r   r   HelpXMLFlag.__init__\   s'    	tYF./  1r   c                     U(       aD  [         R                  R                  [        R                  5        [        R
                  " S5        g g )Nr   )r&   FLAGSWriteHelpInXMLFormatr.   stdoutr/   r0   s     r   r2   HelpXMLFlag.Parsea   s+    
kk&&szz2	hhqk r   r	   Nr4   r	   r   r   r6   r6   Y   s    @1
r   r6   c                   $    \ rS rSrSrS rS rSrg)HelpshortFlagg   zFSpecial bool flag that calls usage(shorthelp=1) and raises SystemExit.c                 F    [         R                  R                  U SSSSS9  g )N	helpshortr   zshow usage only for this moduler   r:   r%   r(   s    r   r   HelpshortFlag.__init__j   s'    	t[!@./  1r   c                 T    U(       a!  [        SSS9  [        R                  " S5        g g )Nr   )	shorthelpr+   r,   r0   s     r   r2   HelpshortFlag.Parseo   s    
a*	hhqk r   r	   Nr4   r	   r   r   rB   rB   g   s    N1
r   rB   c                   $    \ rS rSrSrS rS rSrg)BuildDataFlagu   z8Boolean flag that writes build data to stdout and exits.c                 H    [         R                  R                  U SSS5        g )Nshow_build_datar   zshow build data and exitr%   r(   s    r   r   BuildDataFlag.__init__y   s     	t%69;r   c                     U(       aH  [         R                  R                  [        R	                  5       5        [         R
                  " S5        g g )Nr   )r.   r?   write
build_data	BuildDatar/   r0   s     r   r2   BuildDataFlag.Parse}   s0    
	jjz++-.	hhqk r   r	   Nr4   r	   r   r   rK   rK   u   s    @;r   rK   c                     [        U 5      nU$ ! [        R                   aa  n[        R                  R                  SU-  5        [        R                  R                  S5        [        R                  " S5         SnAgSnAff = f)zATry parsing the flags, printing usage and exiting if unparseable.zFATAL Flags parsing error: %s
z1Pass --help or --helpshort to see help on flags.
r   N)r=   r&   
FlagsErrorr.   stderrrQ   r/   )argsargverrors      r   parse_flags_with_usager[      sc    ;DK			 JJ6>?JJIJHHQKKs    BAA??Bc                     [         (       d{  [        R                  " [        5       5        [        R                  " [	        5       5        [        R                  " [        5       5        [        R                  " [        5       5        Sq gg)z Register help flags. Idempotent.TN)_define_help_flags_calledr&   DEFINE_flagr   r6   rB   rK   r	   r   r   DefineHelpFlagsr_      sU     
#	"	hj!	km$	mo&	mo& $ 
#r   c                  L    [        5         [        [        R                  5      n U $ )zxRegister help flags, parse arguments and show usage if appropriate.

Returns:
  remaining arguments after flags parsing
)r_   r[   r.   rY   )rY   s    r   RegisterAndParseFlagsWithUsagera      s     		)$	+r   c                 |   [        5       nU c  [        R                  S   R                  n  [        R
                  (       a+  [        R                  " [        R                  " X5      5        g[        R                  (       au  SSK
n[        R                  (       a  SSKnOSSKnUR                  5       nUR                  UR                   5        UR                  X5      n[        R                  " U5        g[        R                  " U " U5      5        g! ["         a  n[%        SXfR&                  S9   SnAgSnAff = f)a1  Initializes flag values, and calls main with non-flag arguments.

Only non-flag arguments are passed to main().  The return value of main() is
used as the exit status.

Args:
  main: Main function to run with the list of non-flag arguments, or None
    so that sys.modules['__main__'].main is to be used.
N__main__r   r   )rH   detailed_errorr   )ra   r.   modulesmainr=   r   r/   pdbruncallr   atexitr   cProfileprofileProfileregisterprint_statsr   r-   r   )rf   rY   ri   rk   profilerretvalrZ   s          r   really_startrq      s     
(	)$	\;;z"''DF	hhs{{4&'		!	! 	++
$
??$,,-!!$-d	 F	AennEFs$   ?D ,B	D 6D 
D;D66D;c                      [        5       $ )zBegin executing the program.

- Parses command line flags with the flag module.
- If there are any errors, print usage().
- Calls main() with the remaining arguments.
- If main() raises a UsageError, print usage and the error message.
)_actual_startr	   r   r   runrt      s     
r   c                     Sn  [        S5      e! [          a    [        R                  " 5       S   n  Of = fU (       d   eU R                  R                  R                  nU(       d  gUR
                  R                  SS5      nUS:w  a  UR                  S5      (       d  gA [        R                  " 5          [        5         g! [         a  ne SnAf[         au  n[         He  n UR                  U5      (       a  UR                  U5        M-  M/  !   [        R                  R!                  ["        R$                  " 5       5        e = f   e SnAff = f)z$Another layer in the starting stack.N    r
   rc   z.appcommands)ZeroDivisionErrorr.   exc_infotb_framef_back	f_globalsgetendswith	exc_clearrq   
SystemExit	ExceptionEXCEPTION_HANDLERSWantsHandlerW   rQ   	traceback
format_exc)tbprev_prev_frameprev_prev_nameehandlers        r   rs   rs      s    "
B
	 		B	)
 KK&&--/	
",,00TB.
"%%n55
--/
N	 
		 
 &==
..
  	

--/0 & 

sH    "448
C 
ECE	E%'DE5EEEc                    U(       a  [         R                  nO[         R                  n[         R                  S   R                  nU(       d-  S[         R
                  S   -  n[        R                  " USSS9nOvUR                  S5      SUR                  S	5      -  -
  n U[         R
                  S   4U-  -  n[        (       a*  [        R                  " [        R                  " U5      5      nU (       a  [        R                  5       nO[!        [        5      n UR#                  U5        U(       a"  UR#                  S
5        UR#                  U5        UR#                  S5        Ub  UR#                  SU-  5        Ub  [         R*                  " U5        gg! [        [        [        4 a     Nf = f! [$         a)  nUR&                  [&        R(                  :w  a  e  SnANdSnAff = f)aZ  Write __main__'s docstring to stderr with some help text.

Args:
  shorthelp: print only flags from this module, rather than all flags.
  writeto_stdout: write help message to stdout, rather than to stderr.
  detailed_error: additional detail about why usage info was presented.
  exitcode: if set, exit with this status code after writing help.
rc   z
USAGE: %s [flags]
r   z       rv   )indentfirstline_indent%rw   z%%z
flags:

Nz
%s
)r.   r?   rW   re   r   rY   r&   TextWrapcountOverflowError	TypeError
ValueErrorhelp_text_wrap	DocToHelpr=   MainModuleHelpstrrQ   IOErrorerrnoEPIPEr/   )	rH   r+   rd   r   stdfiledocnum_specifiersflag_strr   s	            r   r-   r-     sy    jjGjjGJ''#	
!CHHQK
/C
..Y
DC YYs^a#))D/&99N	chhqk^n,,c ~NN5??3/0c##%H5zHMM#mmL!mmHMM$!mmH~-. HHX - 9j1 
" 
  	ww%++ s+   !F A"F4 F10F14
G'>G""G'c                   $    \ rS rSrSrS rS rSrg)ExceptionHandleri8  z4Base exception handler from which other may inherit.c                     g)zCheck if this exception handler want to handle this exception.

Args:
  unused_exc: Exception, the current exception

Returns:
  boolean

This base handler wants to handle all exceptions, override this
method if you want to be more selective.
Tr	   )r   
unused_excs     r   r   ExceptionHandler.Wants;  s     r   c                     [        5       e)zyDo something with the current exception.

Args:
  exc: Exception, the current exception

This method must be overridden.
)NotImplementedError)r   excs     r   r   ExceptionHandler.HandleI  s     
r   r	   N)r
   r   r   r   r   r   r   r   r	   r   r   r   r   8  s    < r   r   c                     [        U [        5      (       d  [        S[        U 5      -  5      e[        R                  U 5        g)aQ  Install an exception handler.

Args:
  handler: an object conforming to the interface defined in ExceptionHandler

Raises:
  TypeError: handler was not of the correct type

All installed exception handlers will be called if main() exits via
an abnormal exception, i.e. not one of SystemExit, KeyboardInterrupt,
FlagsError or UsageError.
z9handler of type %s does not inherit from ExceptionHandlerN)
isinstancer   r   typer   append)r   s    r   InstallExceptionHandlerr   T  s<     
G-	.	.
O7m$ % %G$r   r   )r   r   NN)#r   r   osrg   socketstatstructr.   timer   gflagsr&   r=   DEFINE_booleanr   r   r   r   r   r'   r   r6   rB   rK   r[   r]   r_   ra   rq   rt   rs   r-   objectr   r   r	   r   r   <module>r      s%     	 
    
      ^Q(H I   )1)*   1445  I  
u   
%## E%% E%%  " %	"FJ)
X.b v  8%r   