B u `v<@sdZddddgZddlZddlZddlZddlmZmZddlZddl m Z e j d e d ZGd ddeZGd dde jZe eZGd dde jejjZGdddeZdddZddZe j dS)z& Defining a Pyomo-specific task class pyomo_api IPyomoTaskPyomoAPIFactory PyomoAPIDataN) iteritemswith_metaclass)pluginZpyomoz pyomo.commonc@sjeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ dddZdS)rz A generalization of pyutilib.misc.Bunch. This class counts access to attributes, and it generates errors for undefined attributes. cKs,t|||j|t|_|dS)N)dict__init____dict__updateset _declared_clean)selfkwr6/tmp/pip-unpacked-wheel-n62dbgi3/pyomo/common/_task.pyr &s  zPyomoAPIData.__init__cCs t|_dS)N)r _dirty_)rrrrr,szPyomoAPIData.cleanccs8x2|jD]$\}}||jkr |ddkr |Vq WdS)Nr_)r itemsr)rkvrrrunused/szPyomoAPIData.unusedcCs0t|tttfkr |j|n |j|dS)N)typelisttupler rr add)rargsrrrdeclare4szPyomoAPIData.declarecCs|j|dS)N)r r )rdrrrr :szPyomoAPIData.updatecCs|||dS)N) __setattr__)rnamevalrrr __setitem__=szPyomoAPIData.__setitem__cCs ||S)N) __getattr__)rr"rrr __getitem__@szPyomoAPIData.__getitem__cCsX|ddkrJt|jdkr0||jkr0td||j|t|||||j|<dS)NrrzUndeclared attribute '%s')lenrAttributeErrorrrr r$r )rr"r#rrrr!Cs    zPyomoAPIData.__setattr__cCsft|jdkr$||jkr$td|y|j|t||S|ddkr\td|YnXdS)NrzUndeclared attribute '%s'rzUnknown attribute %s)r'rr(rrr r&)rr"rrrr%Ks    zPyomoAPIData.__getattr__cCs t|S)N)r __repr__)rrrrr)VszPyomoAPIData.__repr__rcCsg}|d|}xvt|jD]h\}}|ds||dg}t|trb|d|||dn|dt||d|qW| d|S)Nz r:  r*) rr startswith isinstancerappend__str__strjoinsort)rZnestingindentattrs indentationrrtextrrrr2Ys     zPyomoAPIData.__str__N)rr*)__name__ __module__ __qualname____doc__r rrrr r$r&r!r%r)r2rrrrr s c@seZdZdZdS)rzInterface for Pyomo tasksN)r:r;r<r=rrrrrhsc@seZdZddZdddZdS)PyomoTaskPlugincOs.tjj|f||tjjj|f||dS)N)rPluginr pyutilibworkflowTask)rrkwdsrrrr oszPyomoTaskPlugin.__init__FcCstjj|S)N)r@rArBr))rsimplerrrr)sszPyomoTaskPlugin.__repr__N)F)r:r;r<r r)rrrrr>msr>c@s6eZdZddZd ddZddZdd Zd d Zd S) PyomoTaskcOs$|dd|_tj|f||dS)Nfn)pop_fnr>r )rrkwargsrrrr xszPyomoTask.__init__Fc s|jdkrtd|jdddk rNttkrNt}|||jd<|jddfdd}xV|jD]L}y||j|Wqpt k rtd|Yqptd|YqpXqpWd|j kr|jdd|jf|j}n|j d|jf|j}|dks t t |kr.td|_ nVt|trZt t |ksR|_||_ n*t|tr|t|_ |j |ntddS) NzIThis is a bad definition of a PyomoTask. The '_fn' method is not defineddatacsf|d}||d}dk r8|ddkr8|dx|ddD] }||}qFW|dkrbtdS)N.rrJr-)splitr ValueError)rClookupZlookupsobjkey)rJrr nested_lookups   z(PyomoTask.execute..nested_lookupz*None value found for nested attribute '%s'z3Failed to verify existence of nested attribute '%s')rJz]A Pyomo task function must return either None, a PyomoAPIData object, or an instance of dict.)rH RuntimeError_kwdsgetrr rr _nested_requirementsrM_kwargsrGid_retvalr0rJ)rdebug_datarQr"retvalr)rJrexecute}s>        zPyomoTask.executecCs |dS)N)reset)rrrr _call_startszPyomoTask._call_startcOs\d|krHt|dkr6t|dkr(td|d|d<n|jjjsHtd||_tj|f|S)NrJrr-zLA PyomoTask instance can only be executed with a single non-keyword argumentz=A PyomoTask instance must be executed with at 'data' argument)r'rRinputsrJoptionalrSr> _call_init)roptionsrCrrrras   zPyomoTask._call_initcOsx6|jD],}||jkr"td|t|||j|qWtj|f|||j}|d|_d|_x"|jD]}t||rlt ||qlWx0|j D]&}|j |t||rt ||qW|S)NzHCannot return value '%s' that is not a predefined output of a Pyomo task) rXoutputsrRsetattrr> _call_finir]rShasattrdelattrr_)rrbrCrPr[irrrres"       zPyomoTask._call_finiN)F)r:r;r<r r\r^rarerrrrrEvs  8 rEcs"fdd}|dkr|S||S)Ncs$dkrtddSdkr&jndjddtjrntjdk rtddSn>t j dk rtddSj rtddSj dk rtddSt krtddSGfdd d tj}Gfd d d t|t}|S) Nz-Error applying decorator. No function value!rKrz\Attempting to declare Pyomo task with function '%s' that contains variable keyword argumentszXAttempting to declare Pyomo task with function '%s' that contains keyword-only argumentszTAttempting to declare Pyomo task with function '%s' that contains variable argumentsz.my_decorator..TaskMetacstj|dt||S)NZ PyomoTask_)r PluginMeta__new__r3)clsr"basesr )_namerrrj sz9pyomo_api..my_decorator..TaskMeta.__new__N)r:r;r<rjr)rmrrTaskMeta srncs8eZdZeejeddfddZdS)z6pyomo_api..my_decorator..PyomoTask_tmpF)Zservicecs|d<tj|f||dk rtjdkr8d}n*jdkrNtj}ntjtj}j|d|_|dkrd|jkrtdjdkri}ntt t j|dj}t }d|dkr|j j dddd n|j j ddd xj|dD]v}||dkr<|j j |d|||d|d n@||d krd|j j ||d |d n|dkrtd |qW|jj ddd dkrt |d}n}xD|D]<}||dkr|jj ||d|d ntd|qWg|_x(|d D]}d|kr|j|qWx>|d D]2}d|krDq2||j kr2td|fq2Wx0|dD]$}||j krrtd|fqrWx0|dD]$}||jkrtd|fqWj|_j|_|d|_|d|_|_dS)NrFrr-rJz/A Pyomo functor '%s' must have a 'data argumentr`zA container of labeled data.T)docr`)ro)r`defaultrorequiredz0Argument '%s' is not specified in the docstring!returnz4Return value '%s' is not specified in the docstring!rKz@Unexpected name '%s' in list of required inputs for functor '%s'z@Unexpected name '%s' in list of optional inputs for functor '%s'z8Unexpected name '%s' in list of outputs for functor '%s' short_doclong_doc)rEr r'rdefaultsrVloggererrorr rzipparse_docstringr_rrckeysrUr1r=Z__help__stripZ __short_doc__Z __long_doc__Z __namespace__)rrrInargs _defaultsZdocinfor"Z_outputs)_aliasargspecrF namespacercrrr sj     "        z?pyomo_api..my_decorator..PyomoTask_tmp.__init__N)r:r;r<ralias implementsrr r)r~rrFrrcrr PyomoTask_tmp s r)rvrwr:replacesixPY2inspect getargspeckeywordsgetfullargspecvarkw kwonlyargsvarargsrZservicesrrirrE)rFrnr)rrc)r~rmrrFr my_decorators@        "Kzpyomo_api..my_decoratorr)rFrrcrrr)rrcrrsxcCsi}d|d<d|d<i|d<i|d<i|d<d}t|}|dkrNd|d<|Sx"|dD]}|}|d ksz|d krd}q\|d ks|d krd}q\|d ks|dks|dkrd}q\|dkr |ddkr|dkrd|d<n |d|7<|dd7<n |d|7<|dd7<q\d|krT|dd\}}|d|||<q\||||d7<q\W|ddkrd|d<|S)zYParse a function docstring for information about the function arguments and return valuesr*rsNrtrqr`rrr,z Required:zRequired Arguments:z Optional:zOptimal Arguments:zReturn Values:zReturn:z Returned:r+r-)rgetdocrLr{)rFr[currroliner"descrrrry^sD      ry)NNNN)r=__all__rloggingrrrZpyutilib.workflowr@Z pyomo.commonrZ PluginGlobalsZadd_env getLoggerrvr rZ InterfacerZCreatePluginFactoryrr?rArBr>rErryZpop_envrrrr s"    H  j ~*