B _(2@s,ddlZddlZddlZGdddeZGdddeZGdddeZGdd d eZGd d d eZ Gd d d eZ GdddeZ ddZ d'ddZ iZddZddZddZe ded\ZZe ded\ZZe ddgd \ZZe d!ed\ZZe d"d#gd \ZZe d$d%gd \ZZe d&\ZZdS)(Nc@s eZdZdS)DeferredImportErrorN)__name__ __module__ __qualname__rr=/tmp/pip-unpacked-wheel-d4p3hk07/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 rrrrrrrs rc@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. cCs ||_dS)N)_indicator_flag)r indicatorrrrr 3szDeferredImportModule.__init__cCs|jt|jj|S)N)rresolvegetattr_module)r r rrrr6s z DeferredImportModule.__getattr__N)rrrrr rrrrrr)s 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 r rrr__or__Bsz#_DeferredImportIndicatorBase.__or__cCs t||S)N)r)r r rrr__rand__Esz%_DeferredImportIndicatorBase.__rand__cCs t||S)N)r")r r rrr__ror__Hsz$_DeferredImportIndicatorBase.__ror__N)rrrrr!r#r$r%rrrrr;s rc@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)N.) _nameslisttupleappendsplit_error_message_only_catch_importerror_minimum_version_original_globals _callback _importerr _available) r name alt_names error_messageonly_catch_importerrorminimum_versionoriginal_globalscallbackimporterZ_nrrrr Vsz DeferredImportIndicator.__init__c Cs|jdkrty2t|jd|j|j|j|j|jdd\|_|_Wn*t d|jdf|_d|_YnX| |j t jjj}| |dS)NrF)r5r7r8r9r;r< defer_checkz"Exception raised when importing %s)rattempt_importr)r.r/r0r2r3r4rreplace_self_in_globalsr1inspect currentframef_back f_globals)r _globalsrrrrgs$  zDeferredImportIndicator.resolvecCsxz|jD]p}||kr:t||tr:||j|kr:|j||<x<|dd|d|fD]"}||krR|||krR|j||<qRWqWdS)Nr4Zhas_Zhave_)r) isinstancerrrr4)r rDr5Z flag_namerrrr?s  z/DeferredImportIndicator.replace_self_in_globalscCs||jS)N)rr4)r rrrrsz#DeferredImportIndicator.__nonzero__N)rrrrr rr?rrrrrr&Ls  r&c@seZdZddZddZdS)rcCs||_||_dS)N)_a_b)r abrrrr sz_DeferredAnd.__init__cCst|jot|jS)N)boolrFrG)r rrrrsz_DeferredAnd.__nonzero__N)rrrr rrrrrrsrc@seZdZddZddZdS)r"cCs||_||_dS)N)rFrG)r rHrIrrrr sz_DeferredOr.__init__cCst|jpt|jS)N)rJrFrG)r rrrrsz_DeferredOr.__nonzero__N)rrrr rrrrrr"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) rErrrr4r packagingrKparse ImportError pkg_resourcesrLr)module min_versionr_version_parserrKrrrcheck_min_versions    rVTc Cs|r0t|||||tjj||d}t||fSy|dkrFt|} n|} |dks^t| |rx|dk rp|| d| dfS|rt | dd} |d| |f7}nt | dd} d|| |f}Wn$t k rYn|rւYnX|sd|f}t |} |dk r|| d | d fS) aX 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. 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` )r5r6r7r8r9r:r;r<NTrMUNKNOWNz5 (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&r@rArBrCr importlib import_modulerVrrPr) r5r7r8r9r6r;r<r=rrRrKrrrr>sNR         r>cCs|rt|dr|jtd<dS)N SafeLoaderLoader)hasattrrZyaml_load_args)rR availablerrr_finalize_yaml?sr_cCs |rddl}ddl}ddl}dS)Nr)Z scipy.sparseZ scipy.spatialZ scipy.stats)rRr^scipyrrr_finalize_scipyEsracCs|r ddl}dS)Nr)Z pympler.muppy)rRr^pymplerrrr_finalize_pymplerLsrcyaml)r;rbnumpynp)r6r`networkxZnxpandaspddill)NTNNNNT) r@rXrrProbjectrrrr&rr"rVr>r]r_rarcrdZyaml_availablerbZpympler_availablereZnumpy_availabler`Zscipy_availablergZnetworkx_availablerhZpandas_availablerjZdill_availablerrrr s4G