aboutsummaryrefslogtreecommitdiffstats
path: root/fg21sim/configs
diff options
context:
space:
mode:
authorAaron LI <aaronly.me@outlook.com>2016-10-15 23:06:57 +0800
committerAaron LI <aaronly.me@outlook.com>2016-10-15 23:06:57 +0800
commit3d2d2d3d1e255d5b44e9f5255d2dcd18f54f4bd5 (patch)
tree8542a1fa2b9269394878b34e8cdbe91fec2320f5 /fg21sim/configs
parentf02e823603d95c10b1de176490966d208a245d6d (diff)
downloadfg21sim-3d2d2d3d1e255d5b44e9f5255d2dcd18f54f4bd5.tar.bz2
configs: Update get_path() to take care None and non-string value
* Return None if specified config is None or not exist * Raise ValueError if specified config is non-string * Update comments
Diffstat (limited to 'fg21sim/configs')
-rw-r--r--fg21sim/configs/manager.py27
1 files changed, 24 insertions, 3 deletions
diff --git a/fg21sim/configs/manager.py b/fg21sim/configs/manager.py
index a446057..8d2cab5 100644
--- a/fg21sim/configs/manager.py
+++ b/fg21sim/configs/manager.py
@@ -174,19 +174,40 @@ class ConfigManager:
"/"-separated string specifying the config name of the
file/directory
+ Returns
+ -------
+ path : str
+ The absolute path (if user configuration loaded) or relative
+ path specified by the input key, or `None` if specified config
+ is `None`.
+
+ Raises
+ ------
+ ValueError:
+ If the value of the specified config is not string.
+
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(key))
+ value = self.getn(key)
+ if value is None:
+ logger.warning("Specified config '%s' is None or not exist" % key)
+ return None
+ if not isinstance(value, str):
+ msg = "Specified config '%s' is non-string: %s" % (key, value)
+ logger.error(msg)
+ raise ValueError(msg)
+ #
+ path = os.path.expanduser(value)
if not os.path.isabs(path):
- # relative path
+ # Got relative path, try to convert to the absolute path
if hasattr(self, "userconfig"):
+ # User configuration loaded
path = os.path.join(os.path.dirname(self.userconfig), path)
else:
- # cannot convert to the absolute path
logger.warning("Cannot convert to absolute path: %s" % path)
return os.path.normpath(path)