RuntimeContext¶
- class tolteca.utils.RuntimeContext(config)[source]¶
Bases:
object
A class to manage configurations.
This class manages configurations in a coherent way for different scenarios. The constructor could take a file path that points to a YAML config file, or a tolteca working directory path containing multiple config files (most recommended, see below), or an python dict.
Depending on the type of config source (file/dir/dict), the respective
ConfigBackend
object (config_backend
) is created and the handling of the config is done through it.Among the three, the
DirConf
backend provides the most complete features and capabilities, via the notion of “tolteca workdir”. Using the workdir allows saving a snapshot of the full config in thesetup_info
into thesetup.yaml
file in the workdir. This setup info config is then used to check any incompatibility in the software or pipeline versions to ensure repeatability of the work. The factory methodfrom_dir()
can be used to create a tolteca workdir from an empty directory. The directory will be populated with pre-defined content as outlined inDirConf._contents
.From a runtime context object, the full config dict can be accessed using the
config
property. The propertiesruntime_info
,config_info
, andseup_info
can be used to access the information that are related to the current config and setup.The class provides a registry that maps itself to subclasses of
RuntimeBase
, a consumer class of this class. Objects ofRuntimeBase
subclasses can be constructed conveniently using the__get_item__
interface:>>> from tolteca.simu import SimulatorRuntime >>> from tolteca.reduce import PipelineRuntime >>> rc = RuntimeContext('/path/to/workdir') >>> simrt, plrt = rc[SimulatorRuntime, PipelineRuntime]
- Parameters
- configstr,
pathlib.Path
, dict The config source, can be a file path, a directory path or a python dict.
- configstr,
Attributes Summary
The bin directory.
The cal directory.
The config dict.
The config backend of this runtime context.
The config info.
The doc directory, available only for
DirConf
backend.True if the runtime context is created from a file system path.
The log directory.
The path from which the runtime context is created.
The runtime info of the context.
The setup info.
Methods Summary
from_dir
(dirpath[, init_config])Create
RuntimeContext
instance fromdirpath
.Return the runtime context constructed from the setup info.
setup
([overwrite, runtime_info_only, ...])Save the config dict to the setup info dict.
yaml_dump
(config[, output])Dump
config
as YAML tooutput
yaml_load
(stream)Attributes Documentation
- bindir¶
The bin directory.
- caldir¶
The cal directory.
- config¶
The config dict.
- config_backend¶
The config backend of this runtime context.
- config_info¶
The config info.
- docdir¶
The doc directory, available only for
DirConf
backend.
- is_persistent¶
True if the runtime context is created from a file system path.
- logdir¶
The log directory.
- logger = <Logger RuntimeContext (DEBUG)>¶
- rootpath¶
The path from which the runtime context is created.
- runtime_info¶
The runtime info of the context.
- setup_info¶
The setup info.
For runtime context created from a tolteca workdir, it makes use of the
setup_file
(the setup.yaml file) to store a copy of the config dict.This config dict is consulted in the
check()
function to detect any changes in the runtime context which may affect the correctness of the program.
Methods Documentation
- classmethod from_dir(dirpath, init_config=None, **kwargs)[source]¶
Create
RuntimeContext
instance fromdirpath
.This factory method is preferred when the given directory may not be already setup as tolteca workdir, in which case the setup can be done by passing
create=True
to this method.For paths that have already been setup as a tolteca workdir previously, use the constructor with
config=dirpath
is more convenient.- Parameters
- dirpath
pathlib.Path
, str The path to the work directory.
- init_configdict, optional
The dict to add to the setup_file.
- **kwargsdict
Additional arguments passed to the underlying
DirConfMixin.populate_dir()
.
- dirpath
- setup(overwrite=False, runtime_info_only=False, setup_filepath=None, overwrite_setup_file=False, backup_setup_file=True)[source]¶
Save the config dict to the setup info dict.
This will behave differently for different type of config backend.
For
DirConf
, the config dict is saved to thesetup.yaml
file, so subsequent creation of theRuntimeContext
from the workdir will be able to access the config dict via thesetup_info
.For
FileConf
andDirConf
, this will only update the in-memory setup info dict and it won’t get saved persistently, unless thesetup_filepath
is specified, in which case the dict is saved in the specified path. The setup file can be subsequently used together with other config files using-c
option for the tolteca CLI so the fullcheck()
can be performed.- Parameters
- overwritebool
Set to True to force overwrite if previous setup config exists. Otherwise a
RuntimeContextError
is raised. Note that this is not related to whether to overwrite thesetup_filepath
, which can be specified byoverwrite_setup_file
.- runtime_info_onlybool
If True, only dump the runtime info dict to the setup config.
- setup_filepathstr,
pathlib.Path
, optional If set, and use this filepath to save the config dict. For
DirConf
config backend, this will be used instead of the defaultsetup.yaml
file in the workdir if specified. ForFileConf
andDictConf
, this has to be a valid path in order to save the config dict.- overwrite_setup_filebool
If True, the setup file is overwritten if exists. Otherwise
RuntimeContextError
is raised. This is not to be confused with theoverwrite
option, which determines whether allowing to overwrite the setup config dict itself.- backup_setup_filebool
If True, a copy of the setup file is created. This applies to all three types of the config backends.
- classmethod yaml_dump(config, output=None)¶
Dump
config
as YAML tooutput
- Parameters
- configdict
The config to write.
- outputio.StringIO, optional
The object to write to. If None, return the YAML as string.
- classmethod yaml_load(stream)¶