B e]@sddlmZddlmZddlmZddlmZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZddlZddlZddlZddlZe jdkrdWddZed ned ZdXd d Zd dZeddZddZddZddZddZ e j!dkZ"Gddde#Z$ddZ%dYdd Z&d!d"Z'Gd#d$d$e(Z)Gd%d&d&e(Z*Gd'd(d(ej+Z,d)d*Z-e-ej._-dZd+d,Z/ye j01d-d.2d/Z3Wne4ed0e j0YnXd1d2Z5d3d4Z6Gd5d6d6e(Z7e7dgd7d8d9DZ8Gd:d;d;e7Z9Gdd?Z;Gd@dAdAe7ZZ?GdFdGdGe(Z@GdHdIdIe@ZAGdJdKdKe@ZBddlCZCddlDZDddlEZEGdLdMdMeDjFZGGdNdOdOeCjHZIGdPdQdQe(ZJGdRdSdSe(ZKd[dUdVZLdS)\)with_statement)contextmanager) attrgetter)gettextN3cCsB|dkr*td}|j}|dkr&|j}~n |dkr6|}tddS)Nzexec _code_ in _globs_, _locs_)sys _getframe f_globalsf_localsexec)Z_code_Z_globs_Z_locs_framerk/home/app_decipher_dev_19-4/dev/decipher-analysis/serverless-application/helper/df_spacy/python/plac_ext.pyexec_s rz> def raise_(tp, value=None, tb=None): raise tp, value, tb r cCsL|dk rt|trtd|dk r,||}n|}|j|k rD|||dS)z A function that matches the Python 2.x ``raise`` statement. This allows re-raising exceptions with the cls value and traceback on Python 2 and 3. Nz0instance exception may not have a separate value) isinstance Exception TypeError __traceback__with_traceback)tpvaluetbexcrrrraise_(s   rcCs$y |dStk r|SXdS)z: Decode an object assuming the encoding is UTF-8. zutf-8N)decodeAttributeError)valrrrr9s rccs$tj}|t_z dVWd|t_XdS)z8usage: with stdout(file('out.txt', 'a')): do_something()N)rstdout)fileobjZ orig_stdoutrrrrGs  rcCstjt|tjdS)z2Write str(x) on stdout and flush, no newline addedN)rrwritestrflush)xrrrr Rsr ccs |VdS)z/Return a generator object with a single elementNr)rrrrgen_valXsr$ccst|||dVdS)z.Return a generator object raising an exceptionN)r)etyperrrrrgen_exc]s r&cCsPtjddgtjd}y|j|Wntk r8YnX|j|dS)zSend a text to less via a pipelessz-c)stdinN) subprocessPopenPIPEr(r IOErrorclosewait)textZporrrr'cs r'win32c@s eZdZdS)TerminatedProcessN)__name__ __module__ __qualname__rrrrr1qsr1cCstdS)N)r1)signumr rrrterminatedProcessusr6cCs(t|tr||St||SdS)z4Read a line from stdin, using readline when possibleN)r ReadlineInputreadliner )r(promptrrr read_line{s  r;ccslxfg}xP|}|sdS|}|s(qq|d|krJ||ddPq||qWd|VqWdS)z Read multiple lines from stdin until the terminator character is found, then yield a single space-separated long line. N )r9stripappendjoin)r( terminatorlineslinerrrread_long_lines rDc@sDeZdZdZdddZddZdd Zd d Zdd dZddZ dS)r8zA An iterable with a .readline method reading from stdin. TNcCsN||_||_||_|s&dd|D|_ddl}||_|d||jdS)NcSsg|] }|qSr)upper).0crrr sz*ReadlineInput.__init__..rz tab: complete) completionscase_sensitivehistfiler9rlparse_and_bind set_completercomplete)selfrIrJrKr9rrr__init__s zReadlineInput.__init__cCs>|j|_y|jr"|j|jWntk r8YnX|S)N)rLZ get_completer old_completerrKread_history_filer,)rPrrr __enter__s zReadlineInput.__enter__cCs&|j|j|jr"|j|jdS)N)rLrNrRrKwrite_history_file)rPr%rrrrr__exit__szReadlineInput.__exit__cs@|jsyfdd|jD|Stk r:dSXdS)Ncsg|]}|r|qSr) startswith)rFk)kwrrrHsz*ReadlineInput.complete..)rJrErI IndexError)rPrYstater)rYrrOs zReadlineInput.completer7cCs&y t|dStk r dSXdS)N r7)Z raw_inputEOFError)rPr:rrrr9s zReadlineInput.readlinecCs t|jdS)Nr7)iterr9)rPrrr__iter__szReadlineInput.__iter__)TN)r7) r2r3r4__doc__rQrTrVrOr9r_rrrrr8s   r8c@s4eZdZdZeddZddZddZdd Zd S) HelpSummaryz7Build the help summary consistently with the cmd modulecCst|}tj|d}|jd|dt|dd|dt|jdd|dt|j dd|dt|j ddt |j|_ dS) N)rr\zspecial commandsPzcustom commandsz"commands run in external processeszthreaded commands) plac_core parser_fromcmdZCmdrr Z print_topicssortedcommands mpcommands thcommandsr! helpsummary)clsobjspecialcommandsprGrrradds  zHelpSummary.addcCs g|_dS)N)_ls)rPrrrrQszHelpSummary.__init__cCs|j|dS)N)rqr?)rPsrrrr szHelpSummary.writecCs d|jS)Nr7)r@rq)rPrrr__str__szHelpSummary.__str__N) r2r3r4r` classmethodrprQr rsrrrrras  racseZdZfddZZS) PlacFormattercs4|jpi}tdd|D|_tt|||S)z.Remove special commands from the usage messagecss$|]\}}|ds||fVqdS).N)rW)rFnrGrrr sz3PlacFormatter._metavar_formatter..)choicesdictitemssuperru_metavar_formatter)rPactiondefault_metavarry) __class__rrr}s  z PlacFormatter._metavar_formatter)r2r3r4r} __classcell__rr)rrrusrucCs,y|jStk r&ttj|SXdS)z:Attached to plac_core.ArgumentParser for plac interpretersN)rkrr|rdArgumentParser format_help)rPrrrrsrccst|}|dkr |VdS|jj|}|dkrFtd|Vnt|ddr|}||_ | |j dd|j D|j ||jxD|jD]:}||j||j|dd|jD|qW|Vn |VdS) z3The default help functionality in plac interpretersNzUnknown command %s _interact_FcSsg|]}|jdkr|qS)help)dest)rFarrrrHsz default_help..css|]}|jdkr|VqdS)rN)r)rFrrrrrxszdefault_help..)rdrer subparsers_name_parser_mapget_getattr_get_formatter_prog add_usageusage_actions_mutually_exclusive_groupsadd_text description_action_groups start_sectiontitle add_arguments_group_actions end_section)rmrfparsersubp formatter action_grouprrr default_helps*         rZPLACPATHrv:z#Ill-formed PLACPATH: got %PLACPATHscCst|j}|js|js|jr&tdd|j}|r>|d7}d||f}t |d}t |||d}d|_ t |rt|jdi|_nt|di|_d |jd <t||S) zBCall a container factory with the arglist and return a plac objectzJInterpreter.call must be invoked on factories with required arguments onlyz, ,zzdef makeobj(interact, %s *args): obj = factory(%s) obj._interact_ = interact obj._args_ = args return obj )factorymakeobjF__annotations__)zstart interactive interpreterflagiinteract)rdreargspecdefaultsvarargsvarkwrr@argsrzradd_helpinspectisclassrrQrcall)rarglistrZ required_argscodeZdicrrrr partial_calls$        rc Gsd|kr|d\}}nd}tj|sdx>tD] }tj||}tj|r.Pq.Wttd|n|}tj tj |\}}t |t |||ddf}|rtt|||}n|j}|S)zt An utility to import the main function of a plac tool. It also works with command container factories. rNzCannot find %sUr)splitospathisabsPLACDIRSr@exists ImportErrorrsplitextbasenameimp load_moduleopenrrmain) rrZ factory_nameZplacdirfullpathnameextmoduleZtoolrrr import_main:s    rc@sneZdZdZdZddZddZddd Zd d Zd d Z ddZ ddZ e ddZ e ddZddZdS)BaseTaskz A task is a wrapper over a generator object with signature Task(no, arglist, genobj), attributes .no .arglist .outlist .str .etype .exc .tb .status and methods .run and .kill. ) SUBMITTEDRUNNING TOBEKILLEDKILLEDFINISHEDABORTEDcCs<||_||_|||_d\|_|_|_|_d|_g|_ dS)N)r7NNNr) nor_wrap_genobjr!r%rrstatusoutlist)rPrrgenobjrrrrQis  zBaseTask.__init__cCsdS)z2Notifies the underlying monitor. To be implementedNr)rPmsgrrrnotifyqszBaseTask.notifyFc csd|_yHxB|D]:}|jdkr t|dk rB|j||t|dVqWWnztjk rn|Yndtt t fk rd|_YnFt \|_ |_}|rdt|n||_d|_Yn X|dS)z Wrap the genobj into a generator managing the exceptions, populating the .outlist, setting the .status and yielding None. stringify_tb must be True if the traceback must be sent to a process. rrNrr7r)r GeneratorExitrr?rr InterpreterExit _regular_exitr1KeyboardInterruptrexc_infor%rr@ traceback format_tbr)rPr stringify_tbrrrrrrts&     zBaseTask._wrapcCs>d|_ydtt|j|_Wntk r8d|_YnXdS)Nrr\z no result)rr@maprrr!rZ)rPrrrrs zBaseTask._regular_exitcCsx|jD]}qWdS)zRun the inner generatorN)r)rPZnonerrrruns z BaseTask.runcCs d|_dS)zSet a TOBEKILLED statusrN)r)rPrrrkillsz BaseTask.killcCsdS)z-Wait for the task to finish: to be overriddenNr)rPrrrr.sz BaseTask.waitcCs:|jdkrdSt|jttfr$|jSdt|jSdS)z1Return the traceback as a (possibly empty) stringNr7)rrr!bytesr@rr)rPrrrrs  zBaseTask.tracebackcCsV||jrBt|jttfr,||jnt|j|j|jp>d|jsLdS|jdS)Nr<) r.rrrr!rr%rr)rPrrrresultszBaseTask.resultcCs d|jj|jd|j|jfS)zDString representation containing class name, number, arglist, statusz<%s %d [%s] %s>r=)rr2rr@rr)rPrrr__repr__s zBaseTask.__repr__N)F)r2r3r4r`STATESrQrrrrrr.propertyrrrrrrrrXs   rccs|] }dVqdS)skipNr)rFdummyrrrrxsrx)rc@seZdZdZddZdS)SynTaskzn Synchronous task running in the interpreter loop and displaying its output as soon as available. cCs.|jrd|jj|jfSdtt|jSdS)z-Return the output string or the error messagez%s: %sr\N)r%r2rr@rr!r)rPrrrrsszSynTask.__str__N)r2r3r4r`rsrrrrrsrcs0eZdZdZfddZddZddZZS) ThreadedTaskz/ A task running in a separated thread. cs*t||||tjtt|jd|_dS)N)target)rrQ threadingThreadr|rrthread)rPrrr)rrrrQszThreadedTask.__init__cCs|jdS)zRun the task into a threadN)rstart)rPrrrrszThreadedTask.runcCs|jdS)zBlock until the thread endsN)rr@)rPrrrr.szThreadedTask.wait)r2r3r4r`rQrr.rrr)rrrs rcs$fdd}fdd}t||S)z-Return a property to be attached to an MPTaskcsy t|jSSdS)N)rns)rP)ron_errorrrrs zsharedattr..getcs$yt|j|Wn YnXdS)N)setattrr)rPr)rrrsetszsharedattr..set)r)rrrrr)rrr sharedattrs rcs~eZdZdZeddZeddZeddZeddZedd Z e d d Z d d Z fddZ ddZddZddZZS)MPTaskz A task running as an external process. The current implementation only works on Unix-like systems, where multiprocessing use forks. r!r7r%NrrrrcCsy|jSgSdS)N)_outlist)rPrrrrszMPTask.outlistcCs|j|j|dS)N)mannotify_listenerr)rPrrrrrsz MPTask.notifycst||_||_|j|dd|_||_|j|_|j|_ d|_ d\|_ |_ |_ t||_tjtt|jd|_dS)zS The monitor has a .send method and a .man multiprocessing.Manager T)rr)NNN)rN)rrrrrmplistr Namespacerrr%rrreprr!multiprocessingProcessr|rrproc)rPrrrmanager)rrrrQ s   zMPTask.__init__cCs|jdS)z%Run the task into an external processN)rr)rPrrrrsz MPTask.runcCs|jdS)z2Block until the external process ends or is killedN)rr@)rPrrrr.sz MPTask.waitcCs|jdS)z^Kill the process with a SIGTERM inducing a TerminatedProcess exception in the childrenN)r terminate)rPrrrr sz MPTask.kill)r2r3r4r`rr!r%rrrrrrrQrr.rrrr)rrrs       rc@seZdZdZdZedgZddZddZdd d Z e j d d d e fddddZ e j dd d eejfddddZe j dd d e fddddZe j dd d e fddddZd S) TaskManagerzl Store the given commands into a task registry. Provides methods to manage the submitted tasks. rvz.last_tbcCs||_i|_|js|jr*|jdddgt|dd}tj||rDdndt d|_ t ||j|jrjt nd|_ttjtdS)Nz.killz.listz.outputrFr7)progformatter_class)rmregistryrirjrnupdaterrdrerurrarpManagerrsignalSIGTERMr6)rPrmrrrrrQ0s  zTaskManager.__init__cCsTx>|jD]0}y|jdkr,||Wq Yq Xq W|jrP|jdS)zKill all the running tasksrN)rvaluesrrr.rstop)rPtaskrrrr-<s   zTaskManager.closer<Ncsn|dkstdr.fdd|jD}ndd|jD}|jtddt|t|krj||SdS) z/Get the latest submitted task from the registryrzYou must pass a negative numbercsg|]}|jkr|qSr)r)rFt)rrrrHLsz+TaskManager._get_latest..cSsg|]}|qSrr)rFr rrrrHOsr)keyN)AssertionErrorrr sortrlenabs)rPtasknortasksr)rr _get_latestHszTaskManager._get_latestz task to kill positional)rccsv|dkr*|j|dd}|dkrLdVdSn"||jkrBd|VdS|j|}|jdkrdd|VdS||VdS) z8kill the given task (-1 to kill the latest running task)rr)rNzNothing to killzUnknown task %d)rrrzAlready finished %s)rrrr)rPrr rrrrVs     zTaskManager.killr7)rrccs(x"|jD]}|j|kr |Vq WdS)zlist tasks with a given statusN)rr r)rPrr rrrrjs zTaskManager.listz task numberccs|dkr&||}|dkrHdVdSn"||jkr>d|VdS|j|}dtt|j}|rt|d|d||fVdS|Vt|jdkrt rt |n|VdS) zBshow the output of a given task (and optionally save it to a file)rNzNothing to showzUnknown task %dr\wzsaved output of %d into %s) rrr@rr!rrr ruse_lessr')rPrfnamer Zoutstrrrroutputrs$     zTaskManager.outputccs"||}|r|jVndVdS)z*show the traceback of a given task, if anyzNothing to showN)rr)rPrr rrrlast_tbs  zTaskManager.last_tb)r<N)r<)r)r<N)r<)r2r3r4r` cmdprefixrrnrQr-rrd annotationsintrr!rrrrrrrrrr(s$      rc@s0eZdZdZddZddZddZdd Zd S) rz=Start the interpreter specified by the params in a subprocesscCsPttjtjd|d|f}tjj|tjdd|gtjtjdt |_ dS)Nzfimport plac, sys sys.argv[0] = '<%s>' plac.Interpreter(plac.import_main(*%s)).interact(prompt='i>\n') rz-uz-c)r(r) rSIGPIPESIG_DFLr)r*rQr executabler+rrr)rPparamsrrrrrQs zProcess.__init__cCs"|j|j|jdS)zClose stdin and stdoutN)r(r-rrshutdown)rPrrrr-s  z Process.closecCsDg}x:||j|ddkrd|}|dddSqWdS)zBReturn the output of the subprocess, line-by-line until the promptr<zi> r7Nr=)r?rr9r@)rPrBoutrrrrecvs   z Process.recvcCs|j|tj|S)z[Send a line (adding a newline) to the underlying subprocess and wait for the answer)r(r rlinesepr&)rPrCrrrsendsz Process.sendN)r2r3r4r`rQr-r&r(rrrrrs   rc@s eZdZdZddZddZdS)StartStopObjectFcCsdS)Nr)rPrrrrszStartStopObject.startcCsdS)Nr)rPrrrr szStartStopObject.stopN)r2r3r4startedrr rrrrr)sr)c@sJeZdZdZdddZddZddZd d Zd d Zd dZ ddZ dS)Monitorzv Base monitor class with methods add_listener/del_listener/notify_listener read_queue and and start/stop. NcCs||_|pt|_dS)N)rrQueuequeue)rPrr-rrrrQszMonitor.__init__cCsdS)Nr)rPrrrr add_listenerszMonitor.add_listenercCsdS)Nr)rPrrrr del_listenerszMonitor.del_listenercCsdS)Nr)rPrrrrrrszMonitor.notify_listenercCsdS)Nr)rPrrrrsz Monitor.startcCsdS)Nr)rPrrrr sz Monitor.stopcCsdS)Nr)rPrrr read_queueszMonitor.read_queue)N) r2r3r4r`rQr.r/rrr r0rrrrr+s r+c@sHeZdZdZddZddZddZdd Zd d Zd d Z ddZ dS)rz The plac Manager contains a multiprocessing.Manager and a set of slave monitor processes to which we can send commands. There is a manager for each interpreter with mpcommands. cCsi|_d|_d|_dS)NF)rr*r)rPrrrrQszManager.__init__cCs*td|j|j}|j|_||j|j<dS)z(Add or replace a monitor in the registryN)rrrrr-r)rPmonitorrrrrrpsz Manager.addcCs |j|=dS)z(Remove a named monitor from the registryN)r)rPrrrrdeleteszManager.deletecCs:|jdkrt|_x|jD] }|q Wd|_dS)NT)rrrrr rr*)rPr1rrrrs    z Manager.startcCsFx$|jD]}|j|q W|jr<|jd|_d|_dS)NF)rr r-r-rrr$r*)rPr1rrrr s   z Manager.stopcCs*x$|jD]}|jd||fq WdS)Nr)rr r-put)rPrrr1rrrr szManager.notify_listenercCs(x"|jD]}|jd|fq WdS)Nr.)rr r-r3)rPrr1rrrr. szManager.add_listenerN) r2r3r4r`rQrpr2rr rr.rrrrrs rc@s8eZdZdZdZdZddZddZdd Zd d Z d S) _AsynHandlerzDasynchat handler starting a new interpreter loop for each connectionz zi> cCs@tj||||j||_|jg|_||j dS)N) asynchat async_chatrQZset_terminatorrArrTdatar r:)rPsocket interpreterrrrrQs   z_AsynHandler.__init__cGsB|r ||;}|dr4||js4|dd|j}||dS)z Push a string back to the clientr\Nr<)endswithrApush)rPr7rrrrr &s z_AsynHandler.writecCs|j|dS)z!Collect one character at the timeN)r7r?)rPr7rrrcollect_incoming_data.sz"_AsynHandler.collect_incoming_datacCsd|j}|d||jf|dkrB|jddd|n||j|}||j rd|j j |j d|j f}| |j||||jn||j|jg|_||jdS)z2Put in the queue the line received from the clientr7zReceived line %r from %sZEOFNz%s: %s Received: %sr=)r@r7logaddrrrVZ handle_closesubmitrr%r2rrZlog_inforr rAr!r:)rPrCr errorrrrfound_terminator2s   z_AsynHandler.found_terminatorN) r2r3r4r`rAr:rQr r<rArrrrr4sr4c@s"eZdZdZdddZddZdS) _AsynServerz+asyncore-based server spawning AsynHandlerscCsJ||_||_||_tj||tjtj | d|f| |dS)Nr7) r9 newhandlerportasyncore dispatcherrQZ create_socketr8AF_INET SOCK_STREAMbindlisten)rPr9rDrErKrrrrQJs z_AsynServer.__init__cCs>|\}}|dt||j|jj}|||dS)NzConnected from %s)acceptr=r!r9rrmrD)rPZ clientsockZ clientaddrrrrr handle_acceptSs z_AsynServer.handle_acceptN)rC)r2r3r4r`rQrMrrrrrBGs rBc@seZdZdZGdddeZdejfddZddZ d d Z d d Z d dZ ddZ ddZd3ddZddZddZddZd4ddZd5dd Zejd!dfd"d#Zejd$dfd%d&Zd'd(Zd6d*d+Zd,d-Zd.d/Zeejd0ddejejd$dfd1d2ZdS)7rzi A context manager with a .send method and a few utility methods: execute, test and doctest. c@s eZdZdS)zInterpreter.ExitN)r2r3r4rrrrrasr#cCs||_y |j|_Wntk r,d|_YnX||_||_||t||_|jj |_ |jj |_ |j r|j j |jj |jdd|jr|j j |j|dd|jr|j j |j|dddd|j _d|_dS)Nplaczspecial commands)rz"commands run in external processeszthreaded commandscSs t|S)N)rexit)rrrrzz&Interpreter.__init__..)rmr3rr commentcharr _set_commandsrtmrrrhaddsubcommandsrnrirjr@ _interpreter)rPrmrSrrrrrQds.       zInterpreter.__init__c Csx>dD]6}t||tt|j|gt||tt||gqW|j|_|j|j|j|j|jsv|jsv|jrt|dst ||j|_ |j ddS)z=Make sure obj has the right command attributes as Python sets)rhrirjrN) rrrrrhrirrjhasattrr__get__rrp)rPrmattrnamerrrrT}s  zInterpreter._set_commandscCs||_|jd|S)z Start the inner interpreter loopN)_make_interpreterrWr()rPrrrrTs  zInterpreter.__enter__cCs||||dS)z0Close the inner interpreter and the task managerN)r-)rPexctyperrrrrrVszInterpreter.__exit__cCs|jdkrttd|t|ttfr8|||j}n|}|sDtS|j j }|r^|j s^| |j |}t|d|j js||j j|j<|r||j|S)zBSend a line to the underlying interpreter and return a task objectNzA%r not initialized: probably you forgot to use the with statementr)rW RuntimeErrorrrr!rrrSnulltaskrUrr*rr(rd _match_cmdrnrrr.)rPrCrmr rrrr?s"    zInterpreter.submitcCs||}t||S)zNSend a line to the underlying interpreter and return the finished task)r?rr)rPrCr rrrr(s  zInterpreter.sendcCs |jjS)z%The full lists of the submitted tasks)rUrr )rPrrrrszInterpreter.tasksNcCs2|j|dk r$|j|||n |jdS)z2Can be called to close the interpreter prematurelyN)rUr-rWthrow)rPr\rrrrrr-s zInterpreter.closec cszt|jddd}t|jddd}|d}y xtdD]}|V}y|j|\}}Wnntk r}z0|jdkrg} n t|g} t ||t | }w@Wdd}~XYn"t ||t t }w@YnXt|st ||t|}q@||jjkr t||||jj}q@||jjkr(t|||}q@t |||}q@WWn:tk r\|dddYn|t YnXdS) zBThe interpreter main loop, from lists of arguments to task objectsrTcSsdS)NrrrrrrQrRz/Interpreter._make_interpreter..rVcSsdS)Nr)etexrrrrrQrRNr)r)rrm itertoolscountrconsume SystemExitrr!rr^r&rrrditerabler$rirrUrrjrr) rPZenterrPr rrrfreZerrlistrrrr[s<    zInterpreter._make_interpretercCs2||j}||k}|s.d|||f}t|dS)z:Make sure you get the expected_output from the given_inputz!input: %s output: %s expected: %sN)r(r!r)rPZ given_inputZexpected_outputrokrrrrchecks   zInterpreter.checkc Csdd|D}g}g}x:t|D].\}}|dr ||dd||q W|t|dg}xDt|ddD]0\}}||d} |d||d| qzWt|||S) zDReturns the lines of input, the lines of output, and the line numbercSsg|] }|qSr)r>)rFrCrrrrHsz.Interpreter._parse_doctest..zi> Nrr<r\) enumeraterWr?rr@zip) rPlineiterrBinputs positionsrrCoutputsrendrrr_parse_doctests   zInterpreter._parse_doctestFc Cs|yx~||D]p\}}}|r:td|td|||}t||ksd|d|||f}t||jrt|j|j|jqWWn|jk rYnXWdQRXdS)z Parse a text containing doctests in a context and tests of all them. Raise an error even if a single doctest if broken. Use this for sequential tests which are logically grouped. zi> %s z-> %s z+line %d: input: %s output: %s expected: %s rN) rtr r(r!rrr%rr)rProverboseinputrrr rrrrdoctests    zInterpreter.doctestc Cs~|pyRxL|D]D}|r"td|||}|jrDt|j|j|jtd|jqWWn|jk rnYnXWdQRXdS)z@Execute a lineiter of commands in a context and print the outputzi> z%s N)r r(r%rrrr!r)rProrurCr rrrexecutes   zInterpreter.execute;c Csx|jyLxFt||D]8}||}|td|j|r|jrt|jqWWn|jk rhYnXWdQRXdS)z c Csyddl}d}Wntk r(d}YnX|tjkr|rtjd|j}t|j t|j t|j t|j j }t||d|_n||_||_||_|jjpd}t|d|>d|j_|jtjkr|n|j|WdQRXWdQRXdS) zEStarts an interactive command loop reading commands from the consollerNTFz ~/.%s.history)rKr7r\)r9rrr(rr expanduserrrrhrirjrUrnr8r:rurmr`r r _manage_input) rPr(r:rur9Zreadline_presentrKrIZintrorrrr*s( *    zInterpreter.interactcsyfx`tfdddD]J}|}|s(q|}|tt|djr|jrt|jqWWnj k r|YnXdS)z5Convert input lines into task which are then executedcstjjS)N)r;r(r:r)rPrrrQGrRz+Interpreter._manage_input..r7r\N) r^r>r?rr r!rur%rr)rPrCr r)rPrr|Ds  zInterpreter._manage_inputc KsJt|t|z.ytjf|Wnttfk r6YnXWdtXdS)zqStarts an asyncore server reading commands for clients and opening a new interpreter for each connection.N)rBr4rFZlooprr1Z close_all)rPrErYrrr start_serverSs   zInterpreter.start_servercCs|j|dS)N)rrp)rPmonrrr add_monitor^szInterpreter.add_monitorcCs|j|dS)N)rr2)rPrrrr del_monitoraszInterpreter.del_monitorrc Cst||}||||} | jjrj| @| | jj} | jrJt| j| j| jt| } | r^t | WdQRXn"| jj r| |||n | j dS)z Call a container factory with the arglist and instantiate an interpreter object. If there are remaining arguments, send them to the interpreter, else start an interactive session. N)rrmZ_args_r(rrr%rr!printrrr print_usage) rlrrrSrr(r:rurmrr r%rrrrds  zInterpreter.call)NNN)F)F)r}) r2r3r4r`rrshlexrrQrTrTrVr?r(rr-r[rkrtrwrxrr(rzrr|r~rrrtargvrrrrrr\s2  #      rc@s eZdZdZddZddZdS) _TaskLauncherzHelper for runpcCs(|dkrdg|_ndg|_t||_dS)Nrorungen)rirjrgenlist)rPgenseqmoderrrrQs z_TaskLauncher.__init__ccs&x |jt|dD] }|VqWdS)Nr)rr)rPrr%rrrrsz_TaskLauncher.rungenN)r2r3r4r`rQrrrrrrsrroc Cs|dkst|t||}g}t|}x,tt|jD]}|d|dq8WxN|D]B}y| |j Wq`t k r}z| |Wdd}~XYq`Xq`WWdQRX|S)zRun a sequence of generators in parallel. Mode can be 'p' (use processes) or 't' (use threads). After all of them are finished, return a list of task objects. ptz rungen %drN) rrrrangerrr?rrr?rr)rrZlauncherresZinterrr rirrrrunps  *r)NN)NN)r7)N)ro)M __future__r contextlibroperatorrrrrrrrrfrr)argparserdrrrrrdversionrr evalrrrr r$r&r'platformrrr1r6r;rDobjectr8raRawDescriptionHelpFormatterrurrrenvironrrr ValueErrorrrrr^rrrrrr*rr)r+rrFr5r8r6r4rGrBrrrrrrrs          2  g 4p% 0/%