def get_list(self, key, default=UndefinedKey): """Return list representation of value found at key :param key: key to use (dot separated). E.g., a.b.c :type key: basestring :param default: default value if key not found :type default: list :return: list value :type return: list """ value = self.get(key, default) if isinstance(value, list): return value elif isinstance(value, ConfigTree): lst = [] for k, v in sorted(value.items(), key=lambda kv: kv[0]): if re.match('^[1-9][0-9]*$|0', k): lst.append(v) else: raise ConfigException( u"{key} does not translate to a list".format(key=key)) return lst elif value is None: return None else: raise ConfigException( u"{key} has type '{type}' rather than 'list'".format( key=key, type=type(value).__name__))
def get_bool(self, key, default=UndefinedKey): """Return boolean representation of value found at key :param key: key to use (dot separated). E.g., a.b.c :type key: basestring :param default: default value if key not found :type default: bool :return: boolean value :type return: bool """ # String conversions as per API-recommendations: # https://github.com/typesafehub/config/blob/master/HOCON.md#automatic-type-conversions bool_conversions = { 'true': True, 'yes': True, 'on': True, 'false': False, 'no': False, 'off': False } try: return bool_conversions[self.get_string(key, default)] except KeyError: raise ConfigException( u"{key} does not translate to a Boolean value".format(key=key))
def plain_value(v): if isinstance(v, list): return [plain_value(e) for e in v] elif isinstance(v, ConfigTree): return v.as_plain_ordered_dict() else: if isinstance(v, ConfigValues): raise ConfigException("The config tree contains unresolved elements") return v
def _merge(a, b): if a is None or b is None: return a or b elif isinstance(a, ConfigTree) and isinstance(b, ConfigTree): return ConfigTree.merge_configs(a, b) elif isinstance(a, list) and isinstance(b, list): return a + b else: raise ConfigException('Unable to make such include (merging unexpected types: {a} and {b}', a=type(a), b=type(b))
def include_config(instring, loc, token): url = None file = None required = False if token[0] == 'required': required = True final_tokens = token[1:] else: final_tokens = token if len(final_tokens) == 1: # include "test" value = final_tokens[0].value if isinstance( final_tokens[0], ConfigQuotedString) else final_tokens[0] if value.startswith("http://") or value.startswith( "https://") or value.startswith("file://"): url = value else: file = value elif len(final_tokens) == 2: # include url("test") or file("test") value = final_tokens[1].value if isinstance( final_tokens[1], ConfigQuotedString) else final_tokens[1] if final_tokens[0] == 'url': url = value elif final_tokens[0] == 'package': file = asset.load(value).filename else: file = value if url is not None: logger.debug('Loading config from url %s', url) obj = ConfigFactory.parse_URL(url, resolve=False, required=required, unresolved_value=NO_SUBSTITUTION) elif file is not None: path = file if basedir is None else os.path.join(basedir, file) logger.debug('Loading config from file %s', path) obj = ConfigFactory.parse_file( path, resolve=False, required=required, unresolved_value=NO_SUBSTITUTION) else: raise ConfigException( 'No file or URL specified at: {loc}: {instring}', loc=loc, instring=instring) return ConfigInclude(obj if isinstance(obj, list) else obj.items())
def get_config(self, key): """Return tree config representation of value found at key :param key: key to use (dot separated). E.g., a.b.c :type key: basestring :return: config value :type return: ConfigTree """ value = self.get(key) if isinstance(value, ConfigTree): return value else: raise ConfigException( "{key} has type '{type}' rather than 'config'".format( key=key, type=type(value).__name__))
def get_float(self, key, default=UndefinedKey): """Return float representation of value found at key :param key: key to use (dot separated). E.g., a.b.c :type key: basestring :param default: default value if key not found :type default: float :return: float value :type return: float """ value = self.get(key, default) try: return float(value) if value is not None else None except (TypeError, ValueError): raise ConfigException( u"{key} has type '{type}' rather than 'float'".format(key=key, type=type(value).__name__))
def get_list(self, key, default=UndefinedKey): """Return list representation of value found at key :param key: key to use (dot separated). E.g., a.b.c :type key: basestring :param default: default value if key not found :type default: list :return: list value :type return: list """ value = self.get(key, default) if isinstance(value, list): return value else: raise ConfigException( "{key} has type '{type}' rather than 'list'".format(key=key, type=type(value).__name__))
def get_config(self, key, default=UndefinedKey): """Return tree config representation of value found at key :param key: key to use (dot separated). E.g., a.b.c :type key: basestring :param default: default value if key not found :type default: config :return: config value :type return: ConfigTree """ value = self.get(key, default) if isinstance(value, dict): return value elif value is None: return None else: raise ConfigException( u"{key} has type '{type}' rather than 'config'".format(key=key, type=type(value).__name__))
def include_config(instring, loc, token): url = None file = None required = False if token[0] == 'required': required = True final_tokens = token[1:] else: final_tokens = token if len(final_tokens) == 1: # include "test" value = final_tokens[0].value if isinstance( final_tokens[0], ConfigQuotedString) else final_tokens[0] if value.startswith("http://") or value.startswith( "https://") or value.startswith("file://"): url = value else: file = value elif len(final_tokens) == 2: # include url("test") or file("test") value = final_tokens[1].value if isinstance( final_tokens[1], ConfigQuotedString) else final_tokens[1] if final_tokens[0] == 'url': url = value elif final_tokens[0] == 'package': file = cls.resolve_package_path(value) else: file = value if url is not None: logger.debug('Loading config from url %s', url) obj = ConfigFactory.parse_URL(url, resolve=False, required=required, unresolved_value=NO_SUBSTITUTION) elif file is not None: path = file if basedir is None else os.path.join(basedir, file) def _make_prefix(path): return ('<root>' if path is None else '[%s]' % path).ljust(55).replace('\\', '/') _prefix = _make_prefix(path) def _load(path): _prefix = _make_prefix(path) logger.debug('%s Loading config from file %r', _prefix, path) obj = ConfigFactory.parse_file( path, resolve=False, required=required, unresolved_value=NO_SUBSTITUTION) logger.debug('%s Result: %s', _prefix, obj) return obj if '*' in path or '?' in path: paths = glob(path, recursive=True) obj = None def _merge(a, b): if a is None or b is None: return a or b elif isinstance(a, ConfigTree) and isinstance( b, ConfigTree): return ConfigTree.merge_configs(a, b) elif isinstance(a, list) and isinstance(b, list): return a + b else: raise ConfigException( 'Unable to make such include (merging unexpected types: {a} and {b}', a=type(a), b=type(b)) logger.debug('%s Loading following configs: %s', _prefix, paths) for p in paths: obj = _merge(obj, _load(p)) logger.debug('%s Result: %s', _prefix, obj) else: logger.debug('%s Loading single config: %s', _prefix, path) obj = _load(path) else: raise ConfigException( 'No file or URL specified at: {loc}: {instring}', loc=loc, instring=instring) return ConfigInclude(obj if isinstance(obj, list) else obj.items())