n3Xc@@stddlmZmZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z yddlZWnek rddlZnXddlZddlmZmZddlmZmZddlmZddlmZddlmZddlmZm Z d e!fd YZ"d Z#d Z$d dZ%dS(i(tabsolute_importtprint_functionN(tArgtMutuallyExclusiveArgList(t ConfigDatat ConfigView(t ArgumentError(tconfigure_root_logger(tRequestBuilder(tadd_default_routestaggregate_subclass_fieldst BaseCommandcB@seZdZdZdZgZdZeZ e Z dde dZ e dZdZdZdZdZdZd Zd Zd Zd Zd Ze dZedZdZdZedZdZdZ RS(s3 The basis for a command line tool. To invoke this as a command line tool, call the run() method on the class. Arguments will be parsed from the command line. To invoke this in another context, such as from inside another command, pass keyword arguments to __init__() with names that match those stored by the argument parser and then call main() to retrieve a result. The general workflow of a command involves two methods: main(), which inspects the arguments stored in self.args, does something, and returns a result; and print_result(), which takes the output of main() and prints it to stdout. By default, both of these methods do nothing. It is up to you to implement them to do what the tool is designed to do. Important members of this class include: - DESCRIPTION: a string that describes the tool, which is re-wrapped and used as part of the command line help string. If it is not supplied, the class's doc string, if any, will be used instead. - USAGE: a usage message for the command line help string. If this is None, one will be generated automatically. - ARGS: a list of Arg and/or MutuallyExclusiveArgGroup objects are used to generate command line arguments. Inheriting classes needing to add command line arguments should contain their own ARGS lists, which are combined with those of their parent classes. tcK@s||_||_d|_|j|_i|_d|_t|_ ||_ |j ||j |j rt j r|jtt _n|j|j r|jn|jy|jWnBtk r }|j r|j r|jjt|q nXdS(N(targstconfigtNonetlogtSUITEtsuitet _arg_routest _cli_parsertFalset_BaseCommand__debugt_BaseCommand__do_clit_configure_loggingt_process_configfilesR t%_BaseCommand__CONFIGURED_GLOBAL_STATEt_configure_global_loggingtTruet _build_parsertprocess_cli_argstdistribute_argst configureRterrortstr(tselfRtloglevelt_do_clitkwargsterr((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pyt__init__Ns.               cK@s/|jd|jj|d|j|}|S(NR$R(t setdefaultRtlevelR(tclstotherR&tnew((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pyt from_other|scC@sWtj|j|_|dk r4|jj|n|jrS|jjtjndS(N(tloggingt getLoggertnameRRtsetLeveltdebugtDEBUG(R#R$((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pyRs   cC@su|jdkr<|jj}t|}t||_n|jrq|jjt j |jjjt j ndS(N( RRRtlist_config_filesRRR3RR2R/R4(R#t config_filest config_data((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pyRs  cC@s3|jjddkr(tdtntdS(NR3tcolortcolourt use_color(R8R9(Rtget_global_optionRR(R#((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pyRsc C@sIt|jp|jpd}tjd|d|jdtdtj}|j}|j |||j dddd d d tj d d |j dddd dd tj d d|j dddd|j j d dtd|Dr|j ddd d tj d dn%|j dddd d tj d d||_dS(NR t descriptiontusagetadd_helptformatter_classs--debugtactiont store_truetdestt_debugtdefaultthelpsshow debugging outputs --debuggert _debuggers$launch interactive debugger on errors --versiontversions#show the program's version and exitcs@s-|]#}t|trd|jkVqdS(s-hN(t isinstanceRtpargs(t.0targ_obj((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pys ss--helpsshow this help message and exits-h(t_rewrap_descriptiont DESCRIPTIONt__doc__targparsetArgumentParsertUSAGERtRawDescriptionHelpFormattertcollect_arg_objst_populate_parsert add_argumenttSUPPRESSRtformat_versiontanyR(R#R<tparsertarg_objs((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pyRs0       cC@s&t|jd}t||j|S(NtARGS(R t __class__R tDEFAULT_ROUTES(R#RZ((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pyRSscC@s%x|D]}|j||qWdS(N(t#_BaseCommand__add_arg_to_cli_parser(R#RYRZRK((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pyRTs cC@sIt|tr|jjdtjkr.gS|j|j|j}|jdkrhd|j |j ((R#((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pytst__repr__tpasswordtsecretsparsed arguments from CLI: %s((tvarsRt parse_argstpopRRRt_debugger_except_hooktsyst excepthooktsignaltSIGUSR1t_debugger_usr1_handlerR tupdatettypeRetkeystlowerRRR3R"(R#tcli_argstredactedtkey((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pyRs    $cC@sxtj|jD]\}}||jkrFtdj|n|j|}xB|D]:}|dk rZt|r||}n|||"ss--debugs --debuggeri(s--debugs --debugger(Rt ExceptionRtostpathtbasenameRwRRHtEnvironmentErrortgetattrRtappendRRtprinttjointstderrtlenR R"RXtexittmaint print_resultthandle_cli_exception(R+tcmdR't msg_prefixterr_bitstresult((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pytrun s." "  cC@s |jjS(N(R\Rh(R#((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pyR1+scC@sdS(s} The main processing method. main() is expected to do something with self.args and return a result. N((R#((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pyR/scC@sdS(sJ Take a result produced by main() and print it to stdout. N((R#tdata((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pyR6scC@s;|jrtS|jrtStddDr7tStS(Ncs@s|]}|tjkVqdS(N(RwR(RJRj((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pys Bss--debugs --debugger(s--debugs --debugger(t&_BaseCommand__config_enables_debuggingRRRXR(R#((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pyR3<s  cC@sldjtjjtjd}t|tr]|jr]|j r]|j j t |nt|t r|g}t|ddr|j|jnt|ddr|dcd7<|j|jntdj|dtjn^t|jdkr0|jdr0t||jddtjnt|t |dtj|j r[ntjd dS( Ns {0}: error:iRRiRRRi(RRRRRwRRHRRR3RR!R"RRRRRRRRRRR R(R#R'RR((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pyRGs""" "  cC@sU|jdkrtS|jjddkr/tS|jj|jjdrQtStS(NR3R8R9(scolorscolour(RRRR;Rtconvert_to_bool(R#((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pyt__config_enables_debugging`sN((!Rht __module__RNRMRRQR[R]RRRRR(t classmethodR.RRRRRSRTR^RRR RtpropertyR1RRR3RR(((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pyR *s4.         !   cC@s|tjkrtjdntjt_tjjr~tjjr~dtj krnt j |||qt j |nt j|tjddS(s\ Launch epdb (or pdb if epdb is unavailable) when an uncaught exception occurs. itepdbN(tbdbtBdbQuitRwRt__excepthook__RxtstdouttisattytstdintmodulesRt post_mortemtpdbt tracebacktprint_tb(ttype_tvaluettracebk((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pyRvjs  cC@sVi|d6}|j|j|j|jtdjtj|dtjdS(s Show a traceback and local variables when sent SIGUSR1. Note that this could cause exceptions due to interrupted system calls. t_frameR RN( R|t f_globalstf_localsRRRt format_stackRwR(t_tframet frame_dict((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pyR{}s iFc @sidd6dd6dd6dd6dd6fdfdd fd }|td tjjtjd ;}tj|jj d j }g}g}d }x|D]}|j s|j ||d||2|j dd}q|}|} |rE|j ||d||2n,| |krq|j ||d||2n| }|j |qW|r|j ||d|nxVt t|dd dD]8} || || dkodknr|| =qqWd j|S(s Split lines into paragraphs based on blank lines and changes in indentation level, rewrap the text in each of those paragraphs, and yield each of those paragraphs. In paragraphs that start with common shell prompt characters (#, $, and %) or bullets (* and -) followed by whitespace, those characters do not count against the paragraph's indentation level. s\1 \2s ^(\s*)\#(\s+)s ^(\s*)\$(\s+)s ^(\s*)\%(\s+)s ^(\s*)\*(\s+)s ^(\s*)\-(\s+)c@s\|jsdSx/jD]!\}}tj|||}qWt|t|jS(Ni(tstripRtretsubRtlstrip(tlinetpatternR(tbullets(s:/usr/lib/python2.7/site-packages/requestbuilder/command.pytmeasure_margins  c@s1x*D]"}|rtj||r|SqWdS(N(Rtmatch(RR(R(s:/usr/lib/python2.7/site-packages/requestbuilder/command.pytfind_bullet_patterns iFc@s`|s dS|d|d}|rt|dt|dj}|d|!}tj|||dd|dstwidthRc3@s|]}d|VqdS(RN((RJR(tmargin(s:/usr/lib/python2.7/site-packages/requestbuilder/command.pys s(RRRRttextwraptwrapR(tlinesRRtfirstline_margint bullet_textt rewrappedRtcombined(RRR(Rs:/usr/lib/python2.7/site-packages/requestbuilder/command.pytreformat_paragraphs$$' )!tprogis RR ii(tdictRRRRwRRtdedenttrstripRt splitlinesRRtxrangeRR( ttextRRtin_linest paragraphst current_linestcurrent_marginRRRti((RRRs:/usr/lib/python2.7/site-packages/requestbuilder/command.pyRLsL  &$          #((&t __future__RRRORR/tos.pathRRRyRwRRtwarningsRt ImportErrorRRtrequestbuilderRRtrequestbuilder.configRRtrequestbuilder.exceptionsRtrequestbuilder.loggingRtrequestbuilder.suiteRtrequestbuilder.utilR R tobjectR RvR{RL(((s:/usr/lib/python2.7/site-packages/requestbuilder/command.pyts4            A