cfgtools
- Configuration file fun
Functions for working with configuration file selection and reading.
The which()
convenience function returns the first file that should be used as configuration file from a list.
- torxtools.cfgtools.candidates(cfgname: str = None) List[str]
Convenience function to return list of candidates paths for a configuration file.
- torxtools.cfgtools.which(cfgfile: str = None, candidates: ~typing.List[str] = None, default: str = '/dev/null', verify: ~typing.Callable = <function exists>) str | None
Convenience function to return the configuration file to read.
- Parameters:
cfgfile (str, None) – a single path. If not None, then it will be returned without calling the verify function.
candidates (t.List[str], None) –
a list of paths. If cfgfile is None, then the verify function will be called for each of these paths and the first one for which verify returns True will be returned.
If no file is matched, then
/dev/null
will be returned.verify (t.Callable) – callable that will be called for every file in candidate. Defaults to os.path.exists.
default (str, None) – value to be returned if cfgfile is None and no match from candidates was found
- Returns:
cfgfile, a value from candidates, or default
- Return type:
Example
import argparse, sys from torxtools import cfgtools, pathtools defaults = pathtools.expandpath(["~/.my-cfgfile"], ["/etc/cfgfile"]) parser = argparse.ArgumentParser() parser.add_argument("--cfgfile") args = parser.parse_args(sys.argv[:1]) print(cfgtools.which(args.cfgfile, defaults))