U C^X@s6ddlmZddlmZddlmZddlmZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZddlZddlZddlZe jddZedkrddlmZnddlZd d Ze jd krd\d d ZednedZd]ddZzeWne k re!ZYnXddZ"eddZ#ddZ$ddZ%ddZ&ddZ'e j(dkZ)Gdd d e*Z+d!d"Z,d^d$d%Z-d&d'Z.Gd(d)d)e/Z0Gd*d+d+e/Z1Gd,d-d-ej2Z3d.d/Z4e4ej5_4d_d0d1Z6ze j78d2d39d4Z:Wne;ed5e j7YnXd6d7Ze>dgdDZ?Gd?d@d@e>Z@GdAdBdBe>ZAdCdDZBGdEdFdFe>ZCGdGdHdHe/ZDGdIdJdJe jEZFGdKdLdLe/ZGGdMdNdNeGZHGdOdPdPeGZIddlJZJddlKZKddlLZLGdQdRdReKjMZNGdSdTdTeJjOZPGdUdVdVe/ZQGdWdXdXe/ZRd`dZd[ZSdS)a)with_statement)contextmanager) attrgetter)gettextN)) load_sourcecCs*tj||}tj|}|j||SN) importlibutilspec_from_file_locationmodule_from_specloader exec_module)Zdotnamepathspecmodr*/tmp/pip-install-6_kvzl1k/plac/plac_ext.pyr s  r 3cCsB|dkr*td}|j}|dkr&|j}~n |dkr6|}tddS)Nzexec _code_ in _globs_, _locs_)sys _getframe f_globalsf_localsexec)Z_code_Z_globs_Z_locs_framerrrexec_#s rz> def raise_(tp, value=None, tb=None): raise tp, value, tb rcCsL|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_5s   r(cCs.z |dWStk r(t|YSXdS)z: Decode an object assuming the encoding is UTF-8. zutf-8N)decodeAttributeErrorstr)valrrrr)Ks r)ccs$tj}|t_z dVW5|t_XdS)z8usage: with stdout(file('out.txt', 'a')): do_something()N)rstdout)fileobj orig_stdoutrrrr-Ys  r-cCstjt|tjdS)z2Write str(x) on stdout and flush, no newline addedN)rr-writer+flush)xrrrr0dsr0ccs |VdS)z/Return a generator object with a single elementNr)r%rrrgen_valjsr3ccst|||dVdS)z.Return a generator object raising an exceptionN)r()etyper'r&rrrgen_excos r5cCsPtjddgtjd}z|j|Wntk r8YnX|j|dS)zSend a text to less via a pipeless-c)stdinN) subprocessPopenPIPEr8r0IOErrorclosewait)textZporrrr6us r6win32c@s eZdZdS)TerminatedProcessN__name__ __module__ __qualname__rrrrrAsrAcCstdSr )rA)signumrrrrterminatedProcesssrGcCs(t|tr||St||SdS)z4Read a line from stdin, using readline when possibleN)r ReadlineInputreadliner0)r8promptrrr read_lines  rLccsdg}|}|sdS|}|s$qq|d|krF||ddqRq||qd|VqdS)z Read multiple lines from stdin until the terminator character is found, then yield a single space-separated long line. N )rJstripappendjoin)r8 terminatorlineslinerrrread_long_lines  rUc@sDeZdZdZdddZddZdd Zd d Zdd dZddZ dS)rIzA 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_sensitivehistfilerJrlparse_and_bind set_completercomplete)selfrZr[r\rJrrr__init__s zReadlineInput.__init__cCs>|j|_z|jr"|j|jWntk r8YnX|Sr )r]Z get_completer old_completerr\read_history_filer<rarrr __enter__s zReadlineInput.__enter__cCs&|j|j|jr"|j|jdSr )r]r_rcr\write_history_file)rar4r'r&rrr__exit__szReadlineInput.__exit__csD|jszfdd|jD|WStk r>YdSXdS)Ncsg|]}|r|qSr startswith)rWkkwrrrYs z*ReadlineInput.complete..)r[rVrZ IndexError)rarmstaterrlrr`s zReadlineInput.completerHcCs*zt|dWStk r$YdSXdS)N rH) raw_inputEOFError)rarKrrrrJszReadlineInput.readlinecCs t|jdSNrH)iterrJrerrr__iter__szReadlineInput.__iter__)TN)rH) rCrDrE__doc__rbrfrhr`rJrurrrrrIs   rIc@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)r-rpspecial commandsPzcustom commands"commands run in external processesthreaded commands) plac_core parser_fromcmdZCmdr-r0Z print_topicssortedcommands mpcommands thcommandsr+ helpsummary)clsobjspecialcommandsprXrrradds0  zHelpSummary.addcCs g|_dSr )_lsrerrrrbszHelpSummary.__init__cCs|j|dSr )rrP)rasrrrr0szHelpSummary.writecCs d|jSrs)rQrrerrr__str__szHelpSummary.__str__N) rCrDrErv classmethodrrbr0rrrrrrws  rwcseZdZfddZZS) PlacFormattercs4|jpi}tdd|D|_tt|||S)z.Remove special commands from the usage messagecss$|]\}}|ds||fVqdS).Nri)rWnrXrrr s z3PlacFormatter._metavar_formatter..)choicesdictitemssuperr_metavar_formatter)raactiondefault_metavarr __class__rrrs   z PlacFormatter._metavar_formatter)rCrDrEr __classcell__rrrrrsrcCs2z|jWStk r,ttj|YSXdS)z:Attached to plac_core.ArgumentParser for plac interpretersN)rr*rr}ArgumentParser format_helprerrrrsrccst|}|dkr |VdS|jj|}|dkrFtd|Vnt|ddr|}||_ | |j dd|j D|j ||j|jD]:}||j||j|dd|jD|q|Vn |VdS) z3The default help functionality in plac interpretersNzUnknown command %s _interact_FcSsg|]}|jdkr|qS)helpdestrWarrrrYs z default_help..css|]}|jdkr|VqdS)rNrrrrrr!s zdefault_help..)r}r~rZ 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)rrparserZsubp formatter action_grouprrr default_helps.         rZPLACPATHr: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)r}r~Zargspecdefaultsvarargsvarkwr!rQargsrradd_helpinspectisclassrrbrcall)rarglistrZ required_argscodeZdicrrrr partial_call0s4      rc Gsd|kr|d\}}nd}tj|sbtD]"}tj||}tj|r,qfq,ttd|n|}tj tj |\}}t ||}|rt t |||}n|j}|S)zt An utility to import the main function of a plac tool. It also works with command container factories. rNzCannot find %s)splitosrisabsPLACDIRSrQexists ImportErrorrsplitextbasenamer rrmain) rrZ factory_nameZplacdirfullpathnameextmoduleZtoolrrr import_mainMs    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)rHNNNr) nor_wrap_genobjr+r4r'r&statusoutlistrarrgenobjrrrrb|s  zBaseTask.__init__cCsdS)z2Notifies the underlying monitor. To be implementedNrramsgrrrnotifyszBaseTask.notifyFc csd|_zD|D]:}|jdkrt|dk r@|j||t|dVq Wntjk rj|Ynltt t fk rd|_YnNt k rt \|_|_}|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. rrNrrHr)r GeneratorExitrrPrr) InterpreterExit _regular_exitrAKeyboardInterruptr rexc_infor4r'rQ traceback format_tbr&)rar stringify_tbr%r&rrrrs&     zBaseTask._wrapcCs>d|_zdtt|j|_Wntk r8d|_YnXdS)Nrrpz no result)rrQmapr)rr+rnrerrrrs zBaseTask._regular_exitcCs|jD]}qdS)zRun the inner generatorN)r)ranonerrrruns z BaseTask.runcCs d|_dS)zSet a TOBEKILLED statusrNrrerrrkillsz BaseTask.killcCsdS)z-Wait for the task to finish: to be overriddenNrrerrrr>sz BaseTask.waitcCs:|jdkrdSt|jttfr$|jSdt|jSdS)z1Return the traceback as a (possibly empty) stringNrH)r&rr+bytesrQrrrerrrrs  zBaseTask.tracebackcCsV||jrBt|jttfr,||jnt|j|j|jp>d|jsLdS|jdS)NrM) r>r'rr&r+rr4r(rrerrrresultszBaseTask.resultcCs d|jj|jd|j|jfS)zDString representation containing class name, number, arglist, statusz<%s %d [%s] %s>rN)rrCrrQrrrerrr__repr__s  zBaseTask.__repr__N)F)rCrDrErvSTATESrbrrrrrr>propertyrrrrrrrrks    rccs|] }dVqdS)skipNr)rWdummyrrrrsr)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: %srpN)r4rCr'rQrr+rrerrrrszSynTask.__str__N)rCrDrErvrrrrrrsrcs0eZdZdZfddZddZddZZS) ThreadedTaskz/ A task running in a separated thread. cs*t||||tjtt|jd|_dS)Ntarget)rrb threadingThreadrrrthreadrrrrrbszThreadedTask.__init__cCs|jdS)zRun the task into a threadN)rstartrerrrrszThreadedTask.runcCs|jdS)zBlock until the thread endsN)rrQrerrrr>szThreadedTask.wait)rCrDrErvrbrr>rrrrrrs rcs$fdd}fdd}t||S)z-Return a property to be attached to an MPTaskcs$zt|jWSYSXdSr )rnsreron_errorrrrszsharedattr..getcs$zt|j|Wn YnXdSr )setattrr )rar%)rrrsetszsharedattr..set)r)rr rr rr r 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+rHr4Nr'r&rrcCsz|jWSgYSXdSr )_outlistrerrrrszMPTask.outlistcCs|j|j|dSr )mannotify_listenerrrrrrrsz 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)NNNrN)rrrrrmplistr Namespacer rr4r'r&reprr+multiprocessingProcessrrrproc)rarrrmanagerrrrrbs   zMPTask.__init__cCs|jdS)z%Run the task into an external processN)rrrerrrr+sz MPTask.runcCs|jdS)z2Block until the external process ends or is killedN)rrQrerrrr>/sz MPTask.waitcCs|jdS)z^Kill the process with a SIGTERM inducing a TerminatedProcess exception in the childrenN)r terminatererrrr3sz MPTask.kill)rCrDrErvrr+r4r'r&rrrrrbrr>rrrrrrrs       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. rz.last_tbcCs||_i|_|js|jr*|jdddgt|dd}tj||rDdndt d|_ t ||j|jrjt nd|_ttjtdS)Nz.killz.listz.outputrFrH)progformatter_class)rregistryrrrupdaterr}r~rrrwrManagerrsignalSIGTERMrG)rarrrrrrbCs   zTaskManager.__init__cCsP|jD]0}z|jdkr*||Wq Yq Xq |jrL|jdS)zKill all the running tasksrN)rvaluesrrr>rstop)rataskrrrr=Os  zTaskManager.closerMNcsn|dkstdr.fdd|jD}ndd|jD}|jtddt|t|krj||SdS) z/Get the latest submitted task from the registryrzYou must pass a negative numbercsg|]}|jkr|qSrrrWtrrrrY_s z+TaskManager._get_latest..cSsg|]}|qSrrr'rrrrYbsr)keyN)AssertionErrorrr$sortrlenabs)ratasknortasksrrr _get_latest[szTaskManager._get_latestz task to kill positional)r.ccsv|dkr*|j|dd}|dkrLdVdSn"||jkrBd|VdS|j|}|jdkrdd|VdS||VdS) z8kill the given task (-1 to kill the latest running task)rrrNzNothing to killUnknown task %d)rrrzAlready finished %s)r0rrrrar.r&rrrris     zTaskManager.killrHrrccs$|jD]}|j|kr |Vq dS)zlist tasks with a given statusN)rr$r)rarr&rrrr}s 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)rNNothing to showr2rpwzsaved output of %d into %s) r0rrQrr+ropenr0r,use_lessr6)rar.fnamer&Zoutstrrrroutputs$     zTaskManager.outputccs"||}|r|jVndVdS)z*show the traceback of a given task, if anyr4N)r0rr3rrrlast_tbs  zTaskManager.last_tb)rMN)rM)r)rMN)rM)rCrDrErvZ cmdprefixr rrbr=r0r} annotationsintrr+rrrr:r;rrrrr;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-ur7)r8r-) r"SIGPIPESIG_DFLr9r:rbr executabler;rr!r)raparamsrrrrrbs  zProcess.__init__cCs"|j|j|jdS)zClose stdin and stdoutN)r8r=r-rshutdownrerrrr=s  z Process.closecCs@g}||j|ddkrd|}|dddSqdS)zBReturn the output of the subprocess, line-by-line until the promptrMzi> rHNrN)rPr-rJrQ)rarSoutrrrrecvs   z Process.recvcCs|j|tj|S)z[Send a line (adding a newline) to the underlying subprocess and wait for the answer)r8r0rlineseprD)rarTrrrsendsz Process.sendN)rCrDrErvrbr=rDrFrrrrrs   rc@s eZdZdZddZddZdS)StartStopObjectFcCsdSr rrerrrrszStartStopObject.startcCsdSr rrerrrr%szStartStopObject.stopN)rCrDrEstartedrr%rrrrrGsrGc@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|_dSr )rrQueuequeue)rarrKrrrrbszMonitor.__init__cCsdSr rrar.rrr add_listenerszMonitor.add_listenercCsdSr rrLrrr del_listenerszMonitor.del_listenercCsdSr r)rar.rrrrrszMonitor.notify_listenercCsdSr rrerrrrsz Monitor.startcCsdSr rrerrrr%sz Monitor.stopcCsdSr rrerrr read_queueszMonitor.read_queue)N) rCrDrErvrbrMrNrrr%rOrrrrrIs rIc@sHeZdZdZddZddZddZdd Zd d Zd d Z ddZ dS)r!z 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|_dSNF)rrHrrerrrrbszManager.__init__cCs*td|j|j}|j|_||j|j<dS)z(Add or replace a monitor in the registryN)rrrrrKr)ramonitorrrrrrsz Manager.addcCs |j|=dS)z(Remove a named monitor from the registryN)rrarrrrdeleteszManager.deletecCs6|jdkrt|_|jD] }|qd|_dS)NT)rrr!rr$rrHrarQrrrr s    z Manager.startcCsB|jD]}|j|q |jr8|jd|_d|_dSrP)rr$rKr=rrrBrHrTrrrr%s   z Manager.stopcCs&|jD]}|jd||fq dS)Nrrr$rKput)rar.rrQrrrrszManager.notify_listenercCs$|jD]}|jd|fq dS)NrMrU)rarrQrrrrM szManager.add_listenerN) rCrDrErvrbrrSrr%rrMrrrrr!s r!c@s8eZdZdZdZdZddZddZdd Zd d Z d S) _AsynHandlerzDasynchat handler starting a new interpreter loop for each connectionz i> cCs@tj||||j||_|jg|_||j dSr ) asynchat async_chatrbZset_terminatorrRrrfdatar0rK)rasocket interpreterrrrrb1s   z_AsynHandler.__init__cGsB|r ||;}|dr4||js4|dd|j}||dS)z Push a string back to the clientrpNrM)endswithrRpush)rar[rrrrr09s z_AsynHandler.writecCs|j|dS)z!Collect one character at the timeN)r[rP)rar[rrrcollect_incoming_dataAsz"_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 clientrHzReceived line %r from %sEOFNz%s: %s Received: %srN)rQr[logaddrrrhZ handle_closesubmitrr4rCr'rZlog_inforr0rRr+rK)rarTr&errorrrrfound_terminatorEs$    z_AsynHandler.found_terminatorN) rCrDrErvrRrKrbr0r`rfrrrrrW+srWc@s"eZdZdZdddZddZdS) _AsynServerz+asyncore-based server spawning AsynHandlersrcCsJ||_||_||_tj||tjtj | d|f| |dSrs) r] newhandlerportasyncore dispatcherrbZ create_socketr\AF_INET SOCK_STREAMbindlisten)rar]rhrirorrrrb]s z_AsynServer.__init__cCs>|\}}|dt||j|jj}|||dS)NzConnected from %s)acceptrbr+r]rrrh)raZ clientsockZ clientaddrrrrr handle_acceptfs z_AsynServer.handle_acceptN)r)rCrDrErvrbrqrrrrrgZs rgc@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.ExitNrBrrrrrtsr#cCs||_z |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)NZplacrx)rr{r|cSs t|Sr )rexit)rrrrz&Interpreter.__init__..)rrDrr* commentcharr _set_commandsrtmrrrZaddsubcommandsrrrre _interpreter)rarrvrrrrrbws>       zInterpreter.__init__c CsdD]6}t||tt|j|gt||tt||gq|j|_|j|j|j|j|jsr|jsr|jrt|dst ||j|_ |j ddS)z=Make sure obj has the right command attributes as Python sets)rrrrN) r r rrrrr rhasattrr__get__rr)rarattrnamerrrrwszInterpreter._set_commandscCs||_|jd|S)z Start the inner interpreter loopN)_make_interpreterryrFrerrrrfs  zInterpreter.__enter__cCs||||dS)z0Close the inner interpreter and the task managerN)r=raexctyper'r&rrrrhszInterpreter.__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)ry RuntimeErrorrrr+rrrvnulltaskrxrrHrrFr}Z _match_cmdrrrrM)rarTrmr&rrrrds$    zInterpreter.submitcCs||}t||S)zNSend a line to the underlying interpreter and return the finished task)rdrrrarTr&rrrrFs  zInterpreter.sendcCs |jjS)z%The full lists of the submitted tasks)rxrr$rerrrr/szInterpreter.tasksNcCs2|j|dk r$|j|||n |jdS)z2Can be called to close the interpreter prematurelyN)rxr=rythrowr~rrrr=s zInterpreter.closec cs|t|jddd}t|jddd}|d}z tdD]}|V}z|j|\}}Wnvtk r}z6|jdkr|g} n t|g} t ||t | }WYq.rhcSsdSr r)etexr&rrrrtruNr)r)rr itertoolscountrconsume SystemExitrr+rrtr5rrr}iterabler3rrrxrrrr) raZenterrsr&rrrreZerrlistrrrr}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)rFr+r*)raZ given_inputZexpected_outputr:okrrrrchecks zInterpreter.checkc Csdd|D}g}g}t|D].\}}|dr||dd||q|t|dg}t|ddD]0\}}||d} |d||d| qtt|||S) zDReturns the lines of input, the lines of output, and the line numbercSsg|] }|qSr)rO)rWrTrrrrYsz.Interpreter._parse_doctest..rXrNrrMrp) enumeraterjrPr,rQzip) ralineiterrSinputsZ positionsrrToutputsrendrrr_parse_doctests   zInterpreter._parse_doctestFc Cs|z||D]p\}}}|r8td|td|||}t||ksd|d|||f}t||jrt|j|j|jqWn|jk rYnXW5QRXdS)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) rr0rFr+r'r(r4r&r)rarverboseinputr:rr&rrrrdoctest s     zInterpreter.doctestc Csz|lzN|D]D}|r td|||}|jrBt|j|j|jtd|jq Wn|jk rjYnXW5QRXdS)z@Execute a lineiter of commands in a context and print the outputrX%s N)r0rFr4r(r'r&r+r)rarrrTr&rrrexecute"s  zInterpreter.execute;c Cst|fzHt||D]8}||}|td|j|r|jrt|jqWn|jk rdYnXW5QRXdS)zd|j_|jtjkr|n|j|W5QRXW5QRXdS) zEStarts an interactive command loop reading commands from the consollerNTFz ~/.%s.history)r\rHrp)rJrrr8rr expanduserrrrrrrxrrIrKrrrvr0r _manage_input) rar8rKrrJZreadline_presentr\rZZintrorrrr=s0      zInterpreter.interactcs~zbtfdddD]J}|}|s&q|}|tt|djr|jrt|jqWnj k rxYnXdS)z5Convert input lines into task which are then executedcstjjSr )rLr8rKrrerrrtZruz+Interpreter._manage_input..rHrpN) rtrOrdrr0r+rr4rrrrrerrWs  zInterpreter._manage_inputc KsJt|t|z.ztjf|Wnttfk r6YnXW5tXdS)zqStarts an asyncore server reading commands for clients and opening a new interpreter for each connection.N)rgrWrjZ close_allZlooprrA)rarirmrrr start_serverfs   zInterpreter.start_servercCs|j|dSr )rr)ramonrrr add_monitorqszInterpreter.add_monitorcCs|j|dSr )rrSrRrrr del_monitortszInterpreter.del_monitorrc Cst||}||||} | jjrj| @| | jj} | jrJt| j| j| jt| } | r^t | W5QRXn"| 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)rrZ_args_rFr'r(r4r&r+printrrr print_usage) rrrrvrr8rKrrrr&rCrrrrws  zInterpreter.call)NNN)F)F)r) rCrDrErvr rshlexrrbrwrfrhrdrFr/r=r}rrrrrr8rrrrrrrargvrrrrrros:  #      rc@s eZdZdZddZddZdS) _TaskLauncherzHelper for runpcCs(|dkrdg|_ndg|_t||_dS)Nrrungen)rrrgenlist)ragenseqmoderrrrbs z_TaskLauncher.__init__ccs"|jt|dD] }|VqdS)Nr)rr=)rarrCrrrrsz_TaskLauncher.rungenN)rCrDrErvrbrrrrrrsrrc Cs|dkst|t||}g}t||}tt|jD]}|d|dq6|D]B}z| |j WqZt k r}z| |W5d}~XYqZXqZW5QRX|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) r*rrranger,rrdrr/rPrr )rrlauncherresZinterrr&rrrrrunps   (r)NN)NN)rH)N)r)T __future__r contextlibroperatorrrrrrrrrr9argparserrrr"rr} version_infoversionimpr importlib.utilr rrevalr(rq NameErrorrr)r-r0r3r5r6platformr8r rArGrLrUobjectrIrwRawDescriptionHelpFormatterrrrrenvironrrr ValueErrorrrrrrrrrrr:rrGrIr!rjrYr\rZrWrkrgrrrrrrrs           2  g 4p% 0/%