From 0f4b966c3e111b65422ce29a24b64bdb8ba75405 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Tue, 4 Oct 2016 13:26:55 +0800 Subject: configs/manager.py: Add method "get_path()" --- fg21sim/configs/manager.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'fg21sim') 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 -- cgit v1.2.2