From 7b83d98669962b7dbbb77822baeee82e0a7447f5 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sat, 25 Jul 2020 15:31:56 -0400 Subject: [PATCH 1/4] Start a minimal YamlSource to encapsulate loading Simple refactoring that just simplifies some calls to `yaml_util.load_yaml` at this point. --- confuse/core.py | 16 +++++----------- confuse/sources.py | 11 +++++++++++ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/confuse/core.py b/confuse/core.py index 5015705..301247e 100644 --- a/confuse/core.py +++ b/confuse/core.py @@ -24,7 +24,7 @@ from . import util from . import templates from . import yaml_util -from .sources import ConfigSource +from .sources import ConfigSource, YamlSource from .exceptions import ConfigTypeError, NotFoundError, ConfigError CONFIG_FILENAME = 'config.yaml' @@ -571,9 +571,7 @@ def _add_user_source(self): """ filename = self.user_config_path() if os.path.isfile(filename): - yaml_data = yaml_util.load_yaml(filename, loader=self.loader) \ - or {} - self.add(ConfigSource(yaml_data, filename)) + self.add(YamlSource(filename, loader=self.loader)) def _add_default_source(self): """Add the package's default configuration settings. This looks @@ -584,11 +582,8 @@ def _add_default_source(self): if self._package_path: filename = os.path.join(self._package_path, DEFAULT_FILENAME) if os.path.isfile(filename): - yaml_data = yaml_util.load_yaml( - filename, - loader=self.loader, - ) - self.add(ConfigSource(yaml_data, filename, True)) + self.add(YamlSource(filename, loader=self.loader, + default=True)) def read(self, user=True, defaults=True): """Find and read the files for this configuration and set them @@ -642,8 +637,7 @@ def set_file(self, filename): sources with highest priority. """ filename = os.path.abspath(filename) - yaml_data = yaml_util.load_yaml(filename, loader=self.loader) - self.set(ConfigSource(yaml_data, filename)) + self.set(YamlSource(filename, loader=self.loader)) def dump(self, full=True, redact=False): """Dump the Configuration object to a YAML file. diff --git a/confuse/sources.py b/confuse/sources.py index bba603d..d55cb23 100644 --- a/confuse/sources.py +++ b/confuse/sources.py @@ -1,6 +1,7 @@ from __future__ import division, absolute_import, print_function from .util import BASESTRING +from . import yaml_util __all__ = ['ConfigSource'] @@ -36,3 +37,13 @@ def of(cls, value): return ConfigSource(value) else: raise TypeError(u'source value must be a dict') + + +class YamlSource(ConfigSource): + """A configuration data source that reads from a YAML file. + """ + + def __init__(self, filename=None, default=False, + optional=False, loader=yaml_util.Loader): + value = yaml_util.load_yaml(filename, loader=loader) or {} + super(YamlSource, self).__init__(value, filename, default) From 7dc643c647a0755e65e9d622f96a83c5acdcf364 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sat, 25 Jul 2020 15:37:42 -0400 Subject: [PATCH 2/4] Move optional file check to YamlSource --- confuse/core.py | 8 +++----- confuse/sources.py | 13 ++++++++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/confuse/core.py b/confuse/core.py index 301247e..d9360c8 100644 --- a/confuse/core.py +++ b/confuse/core.py @@ -570,8 +570,7 @@ def _add_user_source(self): exists. """ filename = self.user_config_path() - if os.path.isfile(filename): - self.add(YamlSource(filename, loader=self.loader)) + self.add(YamlSource(filename, loader=self.loader, optional=True)) def _add_default_source(self): """Add the package's default configuration settings. This looks @@ -581,9 +580,8 @@ def _add_default_source(self): if self.modname: if self._package_path: filename = os.path.join(self._package_path, DEFAULT_FILENAME) - if os.path.isfile(filename): - self.add(YamlSource(filename, loader=self.loader, - default=True)) + self.add(YamlSource(filename, loader=self.loader, + optional=True, default=True)) def read(self, user=True, defaults=True): """Find and read the files for this configuration and set them diff --git a/confuse/sources.py b/confuse/sources.py index d55cb23..a06f762 100644 --- a/confuse/sources.py +++ b/confuse/sources.py @@ -2,6 +2,7 @@ from .util import BASESTRING from . import yaml_util +import os __all__ = ['ConfigSource'] @@ -45,5 +46,15 @@ class YamlSource(ConfigSource): def __init__(self, filename=None, default=False, optional=False, loader=yaml_util.Loader): - value = yaml_util.load_yaml(filename, loader=loader) or {} + """Create a YAML data source by reading data from a file. + + May raise a `ConfigReadError`. However, if `optional` is + enabled, this exception will not be raised in the case when the + file does not exist---instead, the source will be silently + empty. + """ + if optional and not os.path.isfile(filename): + value = {} + else: + value = yaml_util.load_yaml(filename, loader=loader) or {} super(YamlSource, self).__init__(value, filename, default) From 753fafec7ff69a32193d31b83d3e742062f72425 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sat, 25 Jul 2020 15:39:49 -0400 Subject: [PATCH 3/4] Remove an __all__ definition This seems to be just fine without it (these are the only names exported here). --- confuse/sources.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/confuse/sources.py b/confuse/sources.py index a06f762..64243a8 100644 --- a/confuse/sources.py +++ b/confuse/sources.py @@ -4,8 +4,6 @@ from . import yaml_util import os -__all__ = ['ConfigSource'] - class ConfigSource(dict): """A dictionary augmented with metadata about the source of the From 81ff221289168b312a8a1e3da86c1f22ace763c3 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sat, 25 Jul 2020 15:44:29 -0400 Subject: [PATCH 4/4] Always absolutify YAML file paths Removes the decision from the YamlSource call sites. --- confuse/core.py | 1 - confuse/sources.py | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/confuse/core.py b/confuse/core.py index d9360c8..c63b1a9 100644 --- a/confuse/core.py +++ b/confuse/core.py @@ -634,7 +634,6 @@ def set_file(self, filename): """Parses the file as YAML and inserts it into the configuration sources with highest priority. """ - filename = os.path.abspath(filename) self.set(YamlSource(filename, loader=self.loader)) def dump(self, full=True, redact=False): diff --git a/confuse/sources.py b/confuse/sources.py index 64243a8..82dc0fd 100644 --- a/confuse/sources.py +++ b/confuse/sources.py @@ -51,6 +51,7 @@ def __init__(self, filename=None, default=False, file does not exist---instead, the source will be silently empty. """ + filename = os.path.abspath(filename) if optional and not os.path.isfile(filename): value = {} else: