PathManager
(class from pyomo.common.fileutils)
- class pyomo.common.fileutils.PathManager(finder, dataClass)[source]
Bases:
objectThe PathManager defines a registry class for path locations
The
PathManagerdefines a class very similar to theCachedFactoryclass; however it does not register type constructors. Instead, it registers instances ofPathData(orExecutableData). These contain the resolved path to the directory object under which thePathDataobject was registered. We do not use the PyUtilibregister_executableandregistered_executablefunctions so that we can automatically include Pyomo-specific locations in the search path (namely thePYOMO_CONFIG_DIR).Users will generally interact with this class through global instances of this class (
pyomo.common.Executableandpyomo.common.Library).Users are not required or expected to register file names with the
PathManager; they will be automatically registered upon first use. Generally, users interact through thepath()andavailable()methods:>>> from pyomo.common import Executable >>> if Executable('demo_exec_file').available(): ... loc = Executable('demo_exec_file').path() ... print(os.path.isfile(loc)) True >>> print(os.access(loc, os.X_OK)) True
For convenience,
available()andpath()are available by casting thePathDataobject requrned fromExecutableorLibraryto either aboolorstr:>>> if Executable('demo_exec_file'): ... cmd = "%s --help" % Executable('demo_exec_file')
The
PathManagercaches the location / existence of the target directory entry. If something in the environment changes (e.g., the PATH) or the file is created or removed after the first time a client queried the location or availability, the PathManager will return incorrect information. You can cause thePathManagerto refresh its cache by callingrehash()on either thePathData(for the single file) or thePathManagerto refresh the cache for all files:>>> # refresh the cache for a single file >>> Executable('demo_exec_file').rehash() >>> # or all registered files >>> Executable.rehash()
The
Executablesingleton looks for executables in the systemPATHand in the list of directories specified by thepathlistattribute.Executable.pathlistdefaults to a list containing theos.path.join(pyomo.common.envvar.PYOMO_CONFIG_DIR, 'bin').The
Librarysingleton looks for executables in the systemLD_LIBRARY_PATH,PATHand in the list of directories specified by thepathlistattribute.Library.pathlistdefaults to a list containing theos.path.join(pyomo.common.envvar.PYOMO_CONFIG_DIR, 'lib').Users may also override the normal file resolution by explicitly setting the location using
set_path():>>> Executable('demo_exec_file').set_path(os.path.join( ... pyomo.common.envvar.PYOMO_CONFIG_DIR, 'bin', 'demo_exec_file'))
Explicitly setting the path is an absolute operation and will set the location whether or not that location points to an actual file. Additionally, the explicit location will persist through calls to
rehash(). If you wish to remove the explicit executable location, callset_path(None):>>> Executable('demo_exec_file').set_path(None)
The
Executablesingleton usesExecutableData, an extended form of thePathDataclass, which provides theexecutableproperty as an alias forpath()andset_path():>>> loc = Executable('demo_exec_file').executable >>> print(os.path.isfile(loc)) True >>> Executable('demo_exec_file').executable = os.path.join( ... pyomo.common.envvar.PYOMO_CONFIG_DIR, 'bin', 'demo_exec_file') >>> Executable('demo_exec_file').executable = None
Methods
__init__(finder, dataClass)rehash()Requery the location of all registered executables
Member Documentation