B @`DI @sddlZddlZddlZddlZddlmZddlmZGdddeZ Gddde Z Gdd d e Z Gd d d e Z Gd d d e ZGddde ZGddde ZddZd1ddZiZddZddZddZddZeded \ZZed!ed \ZZed"d#gd$\ZZed%ed&did'\ZZed(d)gd$\Z Z!ed*d+gd$\Z"Z#ed,\Z$Z%ed-ed.d/giee&fd0\Z'Z(y ddl)Z*Wnek rddl*Z*YnXdS)2N) iteritems)deprecation_warningc@s eZdZdS)DeferredImportErrorN)__name__ __module__ __qualname__rr=/tmp/pip-unpacked-wheel-bi3529v6/pyomo/common/dependencies.pyrsrc@s0eZdZdZdhZddZddZd dd Zd 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 Z__sphinx_mock__cCs ||_dS)N)_error_message_)selfmessagerrr __init__)szModuleUnavailable.__init__cCs.|tjkr tdt|j|ft|jdS)Nz!'%s' object has no attribute '%s')r _getattr_raises_attributeerrorAttributeErrortyperrr )r attrrrr __getattr__,s zModuleUnavailable.__getattr__ pyomo.commoncCst||jdS)N)logging getLoggerwarningr )r loggerrrr generate_import_warning2s z)ModuleUnavailable.generate_import_warningN)r)rrr__doc__rrrrrrrr r 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_namerrr r@s zDeferredImportModule.__init__cCsJ|j|jj}|jr@x&|jdddD]}t||}q.Wt||S)Nrr)rresolve_moduler splitgetattr)r r_mod_subrrr rRs  z DeferredImportModule.__getattr__N)rrrrrrrrrr r6s rc@s4eZdZddZddZddZddZd d Zd S) _DeferredImportIndicatorBasecCs|S)N) __nonzero__)r rrr __bool__\sz%_DeferredImportIndicatorBase.__bool__cCs t||S)N) _DeferredAnd)r otherrrr __and___sz$_DeferredImportIndicatorBase.__and__cCs t||S)N) _DeferredOr)r r1rrr __or__bsz#_DeferredImportIndicatorBase.__or__cCs t||S)N)r0)r r1rrr __rand__esz%_DeferredImportIndicatorBase.__rand__cCs t||S)N)r3)r r1rrr __ror__hsz$_DeferredImportIndicatorBase.__ror__N)rrrr/r2r4r5r6rrrr r-[s 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_catch_exceptions_minimum_version_original_globals _callback _importerr( _available_deferred_submodules) r r& alt_names error_messagecatch_exceptionsminimum_versionoriginal_globalscallbackimporterr%Z_nrrr rvsz 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&rFrGrHrJrK defer_checkz"Exception raised when importing %srr)r(attempt_importr9r=r>r?rArBrCr rDrr r)r*rr#replace_self_in_globalsr@inspect currentframef_back f_globalsglobals)r errsubmodZrefmodr&Z_framerrr r'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)NrCZhas_Zhave_rrr8) r9 isinstancerrr r(rCrDrr)extendr*) r Z_globalsr&Z flag_namerUrE _mod_pathr9r+r,rrr rNs2      z/DeferredImportIndicator.replace_self_in_globalscCs||jS)N)r'rC)r rrr r.sz#DeferredImportIndicator.__nonzero__N)rrrrrr'rNr.rrrr r7ls -r7c@seZdZddZddZdS)r0cCs||_||_dS)N)_a_b)r abrrr rsz_DeferredAnd.__init__cCst|jot|jS)N)boolrYrZ)r rrr r.sz_DeferredAnd.__nonzero__N)rrrrr.rrrr r0sr0c@seZdZddZddZdS)r3cCs||_||_dS)N)rYrZ)r r[r\rrr rsz_DeferredOr.__init__cCst|jpt|jS)N)r]rYrZ)r rrr r.sz_DeferredOr.__nonzero__N)rrrrr.rrrr r3sr3cCs~t|tr*|j}||jr&|j}ndSyddlm}|j}Wn t k r`ddl m }YnXt |dd}||||kS)NFr)version) parse_version __version__z0.0.0) rVrrr'rCr( packagingr^parse ImportError pkg_resourcesr_r*)module min_versionr$_version_parserr^rrr check_min_versions    riTc  Cs|dk r8tddd| dk r$td|r0tf} nttf} | dkrFtf} |r |ri} xrt|D]f\} } | ddkrzd| } | d} x6tt| D]&}d| d|}|| krd| |<qW| | | <q^W| ddnd} t |||| |t j j||| d }t|| d|fS|rtd y|dkr2t|}n|}|dksNt||rj|dk rb||d |d fS|rt|d d }|d||f7}nt|d d }d|||f}Wn| k rYnX|sd|f}t|}|dk r||d|dfS)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` Nzconly_catch_importerror is deprecated. Pass exceptions to catch using the catch_exceptions argumentz5.7.3)r^z?Cannot specify both only_catch_importerror and catch_exceptionsrrr) r&rErFrGrHrIrJrKr%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)r ValueErrorrc Exceptionrr)ranger"joinpopr7rOrPrQrRr importlib import_modulerir*r )r&rFZonly_catch_importerrorrHrErJrKrLr%rGdeferredZ_submodZ_altrXiZ _test_modr$rer^rrr rMsX               rMcCs|rt|dr|jtd<dS)N SafeLoaderLoader)hasattrrtyaml_load_args)re availablerrr _finalize_yamlsrycCs |rddl}ddl}ddl}dS)Nr)Z scipy.statsZ scipy.sparseZ scipy.spatial)rerxscipyrrr _finalize_scipysr{cCs|r ddl}dS)Nr)Z pympler.muppy)rerxpymplerrrr _finalize_pymplersr}cCs0|sdStdddDr$|dddl}dS)Ncss|]}|tjkVqdS)N)sysmodules).0modrrr sz'_finalize_matplotlib..)ZnoseZnose2ZsphinxZAggr)anyZuseZmatplotlib.pyplot)rerx matplotlibrrr _finalize_matplotlibs  ryaml)rJr|numpynp)rErzstats)rJr%networkxZnxpandaspddillrZpyplotZplt)rJr%rG) NNNNNNTNN)+rOrprr~sixrZpyomo.common.deprecationrrcrobjectr rr-r7r0r3rirMrwryr{r}rrZyaml_availabler|Zpympler_availablerZnumpy_availablerzZscipy_availablerZnetworkx_availablerZpandas_availablerZdill_available RuntimeErrorrZmatplotlib_availablecPicklepicklerrrr  sR   %j   .