B @`V @sdZddddddddd g Zd d lZd d lZd d lZd d lmZd d lmZGddde Z Gddde Z Gddde Z ddZ Gddde ZGdddeZGdddeZGdddeZGddde ZGdd d eZe d dZd S)z&Definitions for workflow task objects.Task EmptyTask ComponentPortPorts InputPorts OutputPorts ConnectorDirectConnectorN)Options)globalsc@seZdZdZd:ddZddZddZd d Zd d Zd dZ ddZ d;ddZ ddZ ddZ ddZddZddZddZd d!Zd"d#Zd$d%Zd&d'ZdrzA A Task object represents a single action in a workflow. NcCs|dk r||_n t|_|dkr4dt|j|_n||_t||_|j|jdt||_ |j |jdi|_ g|_ | |t||_ |j |jdt||_|j|jdd|_dS)z Constructor.Nrz-inputsz-outputsz-input-controlsz-output-controlsF)idr Z unique_idstrnamerinputsset_nameroutputs _resources _predecessors_create_parserinput_controlsoutput_controlsdebug)selfr rparserr:/tmp/pip-unpacked-wheel-bvjqrktv/pyutilib/workflow/task.py__init__s$      z Task.__init__cCs||j|j<dS)z9Add a resource that is required for this task to execute.N)rr)rresourcerrr add_resource1szTask.add_resourcecCs |j|S)z%Return the specified resource object.)r)rrrrrr5sz Task.resourcecs0tfddjDtfddjDBS)z?Return the set of tasks that succeed this task in the workflow.c3s*|]"}j|jD]}|jVqqdS)N)routput_connectionsto_porttask).0rt)rrr ;sz"Task.next_tasks..c3s*|]"}j|jD]}|jVqqdS)N)rr r!r")r#rr$)rrrr%>s)setrr)rr)rr next_tasks9s    zTask.next_taskscsDtfddjDtddjDBtfddjDBS)z?Return the set of tasks that precede this task in the workflow.cs0g|](}j|D]}|jtjkr|qqSr)r from_tasksr NoTask)r#rr")rrr Dsz#Task.prev_tasks..css|] }|VqdS)Nr)r#r"rrrr%Hsz"Task.prev_tasks..cs0g|](}j|D]}|jtjkr|qqSr)rr(r r))r#rr")rrrr*Is)r&rrr)rr)rr prev_tasksBs, zTask.prev_taskscCstdd|DS)zGReturn the set of ids for tasks that succeed this task in the workflow.css|] }|jVqdS)N)r )r#r"rrrr%Psz%Task.next_task_ids..)r&r')rrrr next_task_idsNszTask.next_task_idscCstdd|DS)zGReturn the set of ids for tasks that precede this task in the workflow.css|] }|jVqdS)N)r )r#r"rrrr%Tsz%Task.prev_task_ids..)r&r+)rrrr prev_task_idsRszTask.prev_task_idsFcCstd|dS)zExecute this task.zBThere is no default execution for an abstract Task object! Task=%sN) ValueError_name)rrrrrexecuteVsz Task.executecsfddjDS)z;Return the list of resources that this task is waiting for.csg|]}j|s|qSr)r available)r#r)rrrr*^sz'Task.busy_resources..)r)rr)rrbusy_resources\szTask.busy_resourcescCsT|r dSx |jD]}|j|sdSqWx |jD]}|j|s6dSq6WdS)NFT)busyrreadyr)rrrrrr4as  z Task.readycCs t|S)z;Return the list of resources that this task is waiting for.)lenr2)rrrrr3rsz Task.busycOs |j||||j||S)zSetup inputs and output parameters and execute this task. Copy the inputs into this Task's dictionary, then execute the task, then copy the outputs out of the dictionary. ) _call_initr0 _call_fini)roptionskwdsrrr__call__vs z Task.__call__cOs||}t|dkr2td|jt|fx|D]}||q8W||x|jD]\}}| q`Wx|j D]}t ||dq|Wx2|j D](}|j | t |||j |qWdS)Nr z+Cannot execute task %s. Busy resources: %s) _call_startr2r5IOErrorrr _set_inputsritemslockrsetattrr compute_value get_value)rr8r9r3optrresirrrr6s      zTask._call_initcOsx$|jD]}|j|t||qWx|jD]\}}|q2W||t}x&|jD]}t ||t|j| qdW|S)N) r set_valuegetattrrr>Zunlock _call_finish set_readyr r@rB)rr8r9rErrDrCrrrr7s   zTask._call_finicCs`|j|\|_}i}x:|jjD].}yt|j|j}|||j<Wq Yq Xq W||dS)z;Use a list of command-line options to initialize this task.N)_parserparse_known_argsr8_actionsrGdestr=)rargstmpactionvalrrr set_optionss zTask.set_optionscCsdS)z1This method is executed when the task is started.Nr)rrrrr;szTask._call_startcCsdS)z2This method is executed when the task is finished.Nr)rrrrrHszTask._call_finishcCs&x |D]}|j|||qWdS)z!Set the inputs from a dictionary.N)rrF)rr8keyrrrr=s zTask._set_inputscCs>|dkr dSx,|jD]"}|d}|d}|jj||qWdS)Nr ) _parser_argrJ add_argument)rrargrNkwargsrrr set_argumentss  zTask.set_argumentscOs"|j||g|jj||dS)N)rUappendrJrV)rrNrXrrrrVszTask.add_argumentcCsT|dkrt|_n||_g|_i|_|x |jD]}|j|j|q6WdS)zBCreate the OptionParser object and populate it with option groups.N)argparseArgumentParserrJrU _parser_group_create_parser_groupsadd_argument_group)rrrSrrrrs  zTask._create_parsercCsdS)zxThis method is called by the _create_parser method to setup the parser groups that are registered for this task.Nr)rrrrr^szTask._create_parser_groupscCs\i}d|d<|j|d<|j|d<|j|d<|j|d<|j|d<|j|d<|S) NrA_TYPENameZIdInputsOutputsZ InputControlsZOutputControls)rr r_repn_rrr)rrOrrrrds  z Task._repn_cCst|dS)z-Return a string representation for this task.)pprintpformatrd)rrrr__str__sz Task.__str__cCsDdt|jttt|ttt|tt|jfS)Nz"%s prev: %s next: %s resources: %s)rrsortedlistr-r,rkeys)rrrrr/sz Task._namecCs@x|jD]}|j|qWx|jD]}|j|q&WdS)N)rresetr)rrErrrrls  z Task.resetcCs"x|jD]}|j|qWdS)N)rrI)rrErrrrIs zTask.set_ready)NNN)F)N)N)__name__ __module__ __qualname____doc__rrrr'r+r,r-r0r2r4r3r:r6r7rRr;rHr=rYrVrr^rdrhr/rlrIrrrrrs8        c@seZdZdZddZdS)rz# Alias for the Task class. cOstj|f||dS)z Constructor.N)rr)rrNr9rrrrszComponent.__init__N)rmrnrorprrrrrrsc@s<eZdZddZddZddZddZd d Zd d Zd S)rcOsdS)zEmpty task execution.Nr)rrNr9rrrr: szEmptyTask.__call__cCsdS)zEmpty task initialization.Nr)rrNrrrrRszEmptyTask.set_optionscOstdS)N)NotImplementedError)rrNr9rrrrYszEmptyTask.set_argumentscOstdS)N)rq)rrNr9rrrrVszEmptyTask.add_argumentcOsd|_g|_i|_dS)N)rJrUr])rrNr9rrrrszEmptyTask._create_parsercCstdS)N)rq)rrrrr^!szEmptyTask._create_parser_groupsN) rmrnror:rRrYrVrr^rrrrr s c Cs|jdkrXt|jdkrXtd|j|j|j|j|jdjj|jdjjf|||d}|j||j|dS)z8Define a connection by constructing the specified class.storerTzkCannot connect to task %s port %s from task %s port %s. This port is already connected from task %s port %sr ) from_portr!N) rPr5input_connectionsr.r"rrsrZr )clsrsr!Z connectorrrrdefine_connection%s  rvc@s|eZdZdZdddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ d ddZddZddZdS)!rz:A class that represents an input or output port on a task.FNc Cs`||_t||_|dkr"d|_n||_||_||_g|_g|_| |d|_ ||_ ||_ dS)z Constructor.NrrF) rweakrefrefr"rPoptionalconstantrtr rF_readydefaultdoc) rrr"ryvaluerPrzr|r}rrrr=s   z Port.__init__cCsd|_d|_dS)NF)r{r~)rrrrrlWsz Port.resetcCstt||ddS)z,Define a connection with the specified port.)rsr!N)rvr )rportrrrconnect[sz Port.connectcCsdd|jDS)z$Return the id of the preceding task.cSs$g|]}|jdkr|jqS)N)rsr")r#crrrr*asz#Port.from_tasks..)rt)rrrrr(_szPort.from_taskscCs|jdkr|jS|jS)zGet the value of this port.N)r~r|)rrrrrBds zPort.get_valuecCs ||_dS)zSet the value of this port.N)r~)rr~rrrrFjszPort.set_valuecCs |jdkr6t|jdkr4|jd}|dk r4||_n|jdkrtx|jD]&}|sVqH|}|dk rH||_PqHWn|jdkrg}x(|jD]}|}|dk r||qWt|dkr||_nT|jdkri}x.|jD]$}|}|dk r|||jj <qWt|dkr||_| dS)z-Compute the value from the input connections.rrrTr N store_any)rZ append_any)mapmap_any) rPr5rtrBr~r4rZrsr"r validate)rrQ connectionrOrrrrAns<        zPort.compute_valuecCs|jdkr:|js|dkrtdt|j|jfnr|jdkrt|js|dkrtdt|j|jfn8|jdkr|js|dkrtdt|j|jfdS)N)rrrzFTask %s Port %s requires a nontrivial value. Value specified is None.)rZrzTTask %s Port %s requires a nontrivial value. All input connections have value None.)rr)rPryrBr.rr"r r)rrrrrs   z Port.validatecCsi}d|d<|j|d<t|j|d<t|j|d<t|j|d<t|j|d<t||d<|j|d <i|d <g|d d <x$|j D]}|d d  t |qWg|d d <x$|j D]}|d d  t |qW|S) Nrr`rarOptionalZConstantValueZReadyActionZ Connectionsrbrc) rrr"r ryrzr~r4rPrtrZreprr )rrOrrrrrds"      z Port._repn_cCst|S)N)r)rrrr__repr__sz Port.__repr__cCst|dS)Nre)rfrgrd)r indentationrrrrhsz Port.__str__cCs|dk rdSt|jdkr$|jS|jdkr<|jdS|jdkrfx|jD]}|rNdSqNWdS|jdkrx|jD]}|sxdSqxWdStd|jdS)NTr rr)rrrF)rZrzWARNING: unknown action: )rBr5rtr{rPr4r<)rrrrrr4s$      z Port.readycCs d|_dS)NT)r{)rrrrrIszPort.set_ready)FNNFNN)r)rmrnrorprrlrr(rBrFrArrdrrhr4rIrrrrr:s& & c@sdeZdZdZddZddZddd Zd d Zd d ZddZ ddZ ddZ ddZ dddZ dS)rz&A class that specifies a set of ports.cCs"d|_t||_d|_d|_dS)z Constructor.rFN)_name_rwrx_task_inputs_outputs)rr"rrrrs zPorts.__init__cCs ||_dS)z$Set the name of this class instance.N)r)rrrrrrszPorts.set_nameFNc Cs*t|||||||d}t||||S)zDeclare a port.)ryrPrzr|r})rrr@)rrryrPrzr|r}rrrrdeclares z Ports.declarecCs|||dS)zCOverload this operator to set an attribute with the specified name.N) __setattr__)rrrQrrr __setitem__szPorts.__setitem__cCs ||S)zDOverload this operator to get the attribute with the specified name.) __getattr__)rrrrr __getitem__szPorts.__getitem__cCs|ddkr||j|<dS||jkrTt|ts:td|t|||||j|<n,t|tsp|j||n|j||dS)z-Overload this operator to setup a connection.r _Nz/Error declaring port '%s' without a Port object)__dict__ isinstancer TypeErrordictrrFr)rrrQrrrrs       zPorts.__setattr__cCs*y |j|Std|YnXdS)z-Overload this operator to setup a connection.zUnknown attribute '%s'N)rrAttributeError)rrrrrrs zPorts.__getattr__cCs~i}d|d<x,|jD]\}}|ds|||<qW|j|d<|jrRd|d<|jr`d|d<|jdk rz||d<|S) Nrr`rrarZModerZOwner) rr> startswithrdrrrrr/)rrOkvrrrrd&s   z Ports._repn_cCs.tdd|jD}d|jjd|fS)z4Return a string representation of these connections.css(|] \}}|dsd||fVqdS)rz%s = %rN)r)r#rrrrrr%9sz!Ports.__repr__..z%s(%s)z, )rirr> __class__rmjoin)rattrsrrrr7szPorts.__repr__rTrTcCstj|dddS)Nre)width)rfrgrd)rZnestingindentZ print_namerrrrh=sz Ports.__str__)FNFNN)rTrT)rmrnrorprrrrrrrrdrrhrrrrrs c@seZdZdZddZdS)rz2A class that is used to manage set of input ports.cCst||d|_dS)z Constructor.TN)rrr)rr"rrrrDs zInputPorts.__init__N)rmrnrorprrrrrrAsc@seZdZdZddZdS)rz3A class that is used to manage set of output ports.cCst||d|_dS)z Constructor.TN)rrr)rr"rrrrMs zOutputPorts.__init__N)rmrnrorprrrrrrJsc@s6eZdZd ddZddZddZdd Zd d ZdS) rNcCs0|dkrt|_n||_|dkr&t|_n||_dS)z Constructor.N)r)rsr!)rrsr!rrrrUs zConnector.__init__cCs tddS)Nz9There is no value to get in an abstract Connector object!)r.)rrrrrB`szConnector.get_valuecCs |jS)N)rsr4)rrrrr4dszConnector.readycCst|S)z3Return a string representation for this connection.)r)rrrrrgszConnector.__repr__cCs4dt|jjt|jjt|jj|fS)z3Return a string representation for this connection.z%s: from=(%s) to=(%s) %s)rrrmrsr"r r!r4)rrrrrhkszConnector.__str__)NN)rmrnrorrBr4rrhrrrrrSs  c@seZdZdddZddZdS)r NcCstj|||ddS)N)rsr!)rr)rrsr!rrrrtszDirectConnector.__init__cCs|r|jSdS)N)r4rsrB)rrrrrBws zDirectConnector.get_value)NN)rmrnrorrBrrrrr rs )r )rp__all__r[rfrwZ pyutilib.miscr Zpyutilib.workflowr objectrrrrvrrrrrrr r)rrrr s(    l "f