diff options
author | Aaron LI <aaronly.me@outlook.com> | 2016-10-04 13:26:55 +0800 |
---|---|---|
committer | Aaron LI <aaronly.me@outlook.com> | 2016-10-04 13:26:55 +0800 |
commit | 0f4b966c3e111b65422ce29a24b64bdb8ba75405 (patch) | |
tree | bcdbaf1fb37b061d22376469f3a8adf944b97397 /fg21sim/configs/manager.py | |
parent | 9d63da1b5947965d38a860b6cb5bcfc3deb982a5 (diff) | |
download | fg21sim-0f4b966c3e111b65422ce29a24b64bdb8ba75405.tar.bz2 |
configs/manager.py: Add method "get_path()"
Diffstat (limited to 'fg21sim/configs/manager.py')
-rw-r--r-- | fg21sim/configs/manager.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/fg21sim/configs/manager.py b/fg21sim/configs/manager.py index caf0bc4..6e8545b 100644 --- a/fg21sim/configs/manager.py +++ b/fg21sim/configs/manager.py @@ -147,6 +147,27 @@ class ConfigManager: keys = keys.split(sep) return reduce(dict.get, keys, self._config) + def get_path(self, keys): + """Return the absolute path of the file/directory specified by the + config keys. + + NOTE + ---- + - The "~" (tilde) inside path is expanded to the user home directory. + - The relative path (with respect to the user configuration file) + is converted to absolute path if `self.userconfig` presents. + """ + path = os.path.expanduser(self.getn(keys)) + if not os.path.isabs(path): + # relative path + if hasattr(self, "userconfig"): + path = os.path.join(os.path.dirname(self.userconfig), path) + else: + # cannot convert to the absolute path + print("WARNING: cannot convert to the absolute path!", + file=sys.stderr) + return path + @property def frequencies(self): """Get (calculate if necessary) )the frequencies at which to |