Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor YAML loading into YamlSource class #106

Merged
merged 4 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions confuse/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -570,10 +570,7 @@ def _add_user_source(self):
exists.
"""
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, optional=True))

def _add_default_source(self):
"""Add the package's default configuration settings. This looks
Expand All @@ -583,12 +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):
yaml_data = yaml_util.load_yaml(
filename,
loader=self.loader,
)
self.add(ConfigSource(yaml_data, filename, 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
Expand Down Expand Up @@ -641,9 +634,7 @@ 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)
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.
Expand Down
25 changes: 23 additions & 2 deletions confuse/sources.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import division, absolute_import, print_function

from .util import BASESTRING

__all__ = ['ConfigSource']
from . import yaml_util
import os


class ConfigSource(dict):
Expand Down Expand Up @@ -36,3 +36,24 @@ 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):
"""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.
"""
filename = os.path.abspath(filename)
if optional and not os.path.isfile(filename):
value = {}
else:
value = yaml_util.load_yaml(filename, loader=loader) or {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in its own load method so it can be called later.

super(YamlSource, self).__init__(value, filename, default)