From 3d2d2d3d1e255d5b44e9f5255d2dcd18f54f4bd5 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Sat, 15 Oct 2016 23:06:57 +0800 Subject: 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 --- fg21sim/configs/manager.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'fg21sim/configs') 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) -- cgit v1.2.2