B u `D@sddlZddlZddlZddlmZGdddeZGdddeZGdddeZ Gd d d eZ Gd d d e Z Gd dde Z Gddde Z ddZd1ddZiZddZddZddZddZeded\ZZed ed\ZZed!d"gd#\ZZed$ed%did&\ZZed'd(gd#\ZZed)d*gd#\ZZ ed+\Z!Z"ed,d-ed.d/gid0\Z#Z$y ddl%Z&Wnek rddl&Z&YnXdS)2N) iteritemsc@s eZdZdS)DeferredImportErrorN)__name__ __module__ __qualname__rr=/tmp/pip-unpacked-wheel-n62dbgi3/pyomo/common/dependencies.pyrsrc@s*eZdZdZddZddZd ddZd S) ModuleUnavailableaDummy object that raises a DeferredImportError upon attribute access This object is returned by attempt_import() in lieu of the module in the case that the module import fails. Any attempts to access attributes on this object will raise a DeferredImportError exception. Parameters ---------- message: str The string message to return in the raised exception cCs ||_dS)N)_error_message_)selfmessagerrr__init__ szModuleUnavailable.__init__cCst|jdS)N)rr )r attrrrr __getattr__#szModuleUnavailable.__getattr__ pyomo.commoncCst||jdS)N)logging getLoggerwarningr )r loggerrrrgenerate_import_warning&s z)ModuleUnavailable.generate_import_warningN)r)rrr__doc__r rrrrrrr s r c@s eZdZdZddZddZdS)DeferredImportModuleaDummy object that serves as a module placeholder until the first time getattr is called, at which point it imports the module and returns the module attribute. This object is returned by attempt_import() in lieu of the module in the case that the module import fails. Any attempts to access attributes on this object will raise a DeferredImportError exception. c Cs|||_||_|sdS|dkr d}xV|D]N}||ds:q&|dt|d}d|krXq&t||t|||d|q&WdS)N.)_indicator_flag_submodule_name startswithlensetattrr)r indicatordeferred_submodulesZsubmodule_namenameZ _local_namerrrr 4s zDeferredImportModule.__init__cCsJ|j|jj}|jr@x&|jdddD]}t||}q.Wt||S)Nrr)rresolve_modulersplitgetattr)r r_mod_subrrrrFs  z DeferredImportModule.__getattr__N)rrrrr rrrrrr*s rc@s4eZdZddZddZddZddZd d Zd S) _DeferredImportIndicatorBasecCs|S)N) __nonzero__)r rrr__bool__Psz%_DeferredImportIndicatorBase.__bool__cCs t||S)N) _DeferredAnd)r otherrrr__and__Ssz$_DeferredImportIndicatorBase.__and__cCs t||S)N) _DeferredOr)r r-rrr__or__Vsz#_DeferredImportIndicatorBase.__or__cCs t||S)N)r,)r r-rrr__rand__Ysz%_DeferredImportIndicatorBase.__rand__cCs t||S)N)r/)r r-rrr__ror__\sz$_DeferredImportIndicatorBase.__ror__N)rrrr+r.r0r1r2rrrrr)Os r)c@s0eZdZdZddZddZddZdd Zd S) DeferredImportIndicatoraPlaceholder indicating if an import was successful. This object serves as a placeholder for the Boolean indicator if a deferred module import was successful. Casting this instance to bool will cause the import to be attempted. The actual import logic is here and not in the DeferredImportModule to reduce the number of attributes on the DeferredImportModule. c Cs|g|_|r|jt|7_x0t|jD]"} d| kr*|j| ddq*W||_||_||_||_||_ ||_ d|_ d|_ | |_ dS)Nr)_nameslisttupleappendr%_error_message_only_catch_importerror_minimum_version_original_globals _callback _importerr$ _available_deferred_submodules) r r" alt_names error_messageonly_catch_importerrorminimum_versionoriginal_globalscallbackimporterr!Z_nrrrr jsz DeferredImportIndicator.__init__c Cs8|jdkry2t|jd|j|j|j|j|jdd\|_|_Wn*t d|jdf|_d|_YnX|j rt |jt kr|jj }xn|j D]d}|j}xX| dddD]B}yt||}Wqtk rt||t |t||}YqXqWqW||jtj}x|jtkr&|j}qW||jdS)NrF)r"rBrCrDrFrG defer_checkz"Exception raised when importing %srr)r$attempt_importr5r9r:r;r=r>r?r r@typer r%r&rrreplace_self_in_globalsr<inspect currentframef_back f_globalsglobals)r errsubmodZrefmodr"Z_framerrrr#}s>     zDeferredImportIndicator.resolvec CsBx|jD]~}||krHt||trH||j|krH||jdkrH|j||<x<|dd|d|fD]"}||kr`|||kr`|j||<q`WqW|jsdSxt|jD]\}}| ddd}|dg}|r| |xf|D]^}||krt||tr||j|kr||j|kr|j}x|D]} t || }qW|||<qWqWdS)Nr?Zhas_Zhave_rrr4) r5 isinstancerrrr$r?r@rr%extendr&) r Z_globalsr"Z flag_namerRrA _mod_pathr5r'r(rrrrKs2      z/DeferredImportIndicator.replace_self_in_globalscCs||jS)N)r#r?)r rrrr*sz#DeferredImportIndicator.__nonzero__N)rrrrr r#rKr*rrrrr3`s -r3c@seZdZddZddZdS)r,cCs||_||_dS)N)_a_b)r abrrrr sz_DeferredAnd.__init__cCst|jot|jS)N)boolrVrW)r rrrr*sz_DeferredAnd.__nonzero__N)rrrr r*rrrrr,sr,c@seZdZddZddZdS)r/cCs||_||_dS)N)rVrW)r rXrYrrrr sz_DeferredOr.__init__cCst|jpt|jS)N)rZrVrW)r rrrr*sz_DeferredOr.__nonzero__N)rrrr r*rrrrr/sr/cCs~t|tr*|j}||jr&|j}ndSyddlm}|j}Wn t k r`ddl m }YnXt |dd}||||kS)NFr)version) parse_version __version__z0.0.0) rSrrr#r?r$ packagingr[parse ImportError pkg_resourcesr\r&)module min_versionr _version_parserr[rrrcheck_min_versions    rfTc  Cs|r|ri} xrt|D]f\} } | ddkr2d| } | d} x6tt| D]&} d| d| }|| krJd| |<qJW| | | <qW| ddnd} t|||||tj j ||| d }t || d|fS|rt dy|dkrt |}n|}|dkst||r|dk r||d|dfS|rBt|dd }|d ||f7}nt|dd }d |||f}Wn(tk rtYn|rYnX|sd |f}t|}|dk r||d |d fS)a Attempt to import the specified module. This will attempt to import the specified module, returning a (module, available) tuple. If the import was successful, `module` will be the imported module and `available` will be True. If the import results in an exception, then `module` will be an instance of :py:class:`ModuleUnavailable` and `available` will be False The following is equivalent to ``import numpy as np``: .. doctest:: >>> from pyomo.common.dependencies import attempt_import >>> numpy, numpy_available = attempt_import('numpy') The import can be "deferred" until the first time the code either attempts to access the module or checks the boolean value of the available flag. This allows optional dependencies to be declared at the module scope but not imported until they are actually used by the module (thereby speeding up the initial package import). Deferred imports are handled by two helper classes (DeferredImportModule and DeferredImportIndicator). Upon actual import, DeferredImportIndicator.resolve() attempts to replace those objects (in both the local and original global namespaces) with the imported module and boolean flag so that subsequent uses of the module do not incur any overhead due to the delayed import. Parameters ---------- name: str The name of the module to import error_message: str, optional The message for the exception raised by ModuleUnavailable only_catch_importerror: bool, optional If True (the default), exceptions other than ImportError raised during module import will be reraised. If False, any exception will result in returning a ModuleUnavailable object. minimum_version: str, optional The minimum acceptable module version (retrieved from module.__version__) alt_names: list, optional A list of common alternate names by which to look for this module in the globals() namespaces. For example, the alt_names for NumPy would be ['np'] callback: function, optional A function with the signature "`fcn(module, available)`" that will be called after the import is first attempted. importer: function, optional A function that will perform the import and return the imported module (or raise an ImportError). This is useful for cases where there are several equivalent modules and you want to import/return the first one that is available. defer_check: bool, optional If True (the default), then the attempted import is deferred until the first use of either the module or the availability flag. The method will return instances of DeferredImportModule and DeferredImportIndicator. deferred_submodules: dict, optional If provided, a mapping of submodules to within this module that can be accessed without triggering a deferred import of this module, to a list of alternate names by which to look for the submodule in the globals() namespaces. For example, the deferred_submodules for matplotlib is {'pyplot': ['plt']} Returns ------- : module the imported module, or an instance of :py:class:`ModuleUnavailable`, or an instance of :py:class:`DeferredImportModule` : bool Boolean indicating if the module import succeeded or an instance of "py:class:`DeferredImportIndicator` rrNr) r"rArBrCrDrErFrGr!z6deferred_submodules is only valid if defer_check==TrueTr]UNKNOWNz5 (version %s does not satisfy the minimum version %s)z@The %s module version %s does not satisfy the minimum version %sz=The %s module (an optional Pyomo dependency) failed to importF)rr%rangerjoinpopr3rLrMrNrOr ValueError importlib import_modulerfr&r`r )r"rBrCrDrArFrGrHr!deferredZ_submodZ_altrUiZ _test_modr rbr[rrrrIspY              rIcCs|rt|dr|jtd<dS)N SafeLoaderLoader)hasattrrpyaml_load_args)rb availablerrr_finalize_yamlsrucCs |rddl}ddl}ddl}dS)Nr)Z scipy.statsZ scipy.sparseZ scipy.spatial)rbrtscipyrrr_finalize_scipysrwcCs|r ddl}dS)Nr)Z pympler.muppy)rbrtpymplerrrr_finalize_pymplersrycCs2|sdSdtjksdtjkr&tdddl}dS)NZnoseZnose2ZAggr)sysmodulesZ matplotlobZuseZmatplotlib.pyplot)rbrt matplotlibrrr_finalize_matplotlibs  r}yaml)rFrxnumpynp)rArvstats)rFr!networkxZnxpandaspddillr|FZpyplotZplt)rCrFr!)NTNNNNTN)'rLrlrsixrr`robjectr rr)r3r,r/rfrIrsrurwryr}r~Zyaml_availablerxZpympler_availablerZnumpy_availablervZscipy_availablerZnetworkx_availablerZpandas_availablerZdill_availabler|Zmatplotlib_availablecPicklepicklerrrr sH %j   #