-
Notifications
You must be signed in to change notification settings - Fork 52
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
Lazy sources - after submodules #96
Open
beasteers
wants to merge
5
commits into
beetbox:main
Choose a base branch
from
beasteers:lazysources3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,146 @@ | ||
from __future__ import division, absolute_import, print_function | ||
|
||
import os | ||
import functools | ||
from .util import BASESTRING | ||
from . import yaml_util | ||
|
||
__all__ = ['ConfigSource'] | ||
__all__ = ['ConfigSource', 'YamlSource'] | ||
|
||
|
||
UNSET = object() # sentinel | ||
|
||
|
||
def _load_first(func): | ||
'''Call self.load() before the function is called - used for lazy source | ||
loading''' | ||
def inner(self, *a, **kw): | ||
self.load() | ||
return func(self, *a, **kw) | ||
|
||
try: | ||
return functools.wraps(func)(inner) | ||
except AttributeError: | ||
# in v2 they don't ignore missing attributes | ||
# v3: https://github.com/python/cpython/blob/3.8/Lib/functools.py | ||
# v2: https://github.com/python/cpython/blob/2.7/Lib/functools.py | ||
inner.__name__ = func.__name__ | ||
return inner | ||
|
||
|
||
class ConfigSource(dict): | ||
"""A dictionary augmented with metadata about the source of the | ||
'''A dictionary augmented with metadata about the source of the | ||
configuration. | ||
""" | ||
def __init__(self, value, filename=None, default=False): | ||
super(ConfigSource, self).__init__(value) | ||
''' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise with the docstring """s. |
||
def __getattribute__(self, k): | ||
x = super(ConfigSource, self).__getattribute__(k) | ||
if k == 'keys': | ||
# HACK: in 2.7, it appears that doing dict(source) only checks for | ||
# the existance of a keys attribute and doesn't actually cast | ||
# to a dict, so we never get the chance to load. My goal | ||
# is to remove this entirely ASAP. | ||
x() | ||
return x | ||
|
||
def __init__(self, value=UNSET, filename=None, default=False, | ||
retry=False): | ||
# track whether a config source has been set yet | ||
self.loaded = value is not UNSET | ||
self.retry = retry | ||
super(ConfigSource, self).__init__(value if self.loaded else {}) | ||
if (filename is not None | ||
and not isinstance(filename, BASESTRING)): | ||
raise TypeError(u'filename must be a string or None') | ||
self.filename = filename | ||
self.default = default | ||
|
||
def __repr__(self): | ||
return 'ConfigSource({0!r}, {1!r}, {2!r})'.format( | ||
super(ConfigSource, self), | ||
self.filename, | ||
self.default, | ||
) | ||
return '{}({}, filename={}, default={})'.format( | ||
self.__class__.__name__, | ||
dict.__repr__(self) | ||
if self.loaded else '[Unloaded]' | ||
if self.exists else "[Source doesn't exist]", | ||
self.filename, self.default) | ||
|
||
@property | ||
def exists(self): | ||
"""Does this config have access to usable configuration values?""" | ||
return self.loaded or self.filename and os.path.isfile(self.filename) | ||
|
||
def load(self): | ||
"""Ensure that the source is loaded.""" | ||
if not self.loaded: | ||
self.config_dir() | ||
self.loaded = self._load() is not False or not self.retry | ||
return self | ||
|
||
def _load(self): | ||
"""Load config from source and update self. | ||
If it doesn't load, return False to keep it marked as unloaded. | ||
Otherwise it will be assumed to be loaded. | ||
""" | ||
|
||
def config_dir(self, create=True): | ||
"""Create the config dir, if there's a filename associated with the | ||
source.""" | ||
if self.filename: | ||
dirname = os.path.dirname(self.filename) | ||
if create and dirname and not os.path.isdir(dirname): | ||
os.makedirs(dirname) | ||
return dirname | ||
return None | ||
|
||
# overriding dict methods so that the configuration is loaded before any | ||
# of them are run | ||
__getitem__ = _load_first(dict.__getitem__) | ||
__iter__ = _load_first(dict.__iter__) | ||
# __len__ = _load_first(dict.__len__) | ||
keys = _load_first(dict.keys) | ||
values = _load_first(dict.values) | ||
|
||
@classmethod | ||
def of(cls, value): | ||
"""Given either a dictionary or a `ConfigSource` object, return | ||
a `ConfigSource` object. This lets a function accept either type | ||
of object as an argument. | ||
def isoftype(cls, value, **kw): | ||
return False | ||
|
||
@classmethod | ||
def of(cls, value, **kw): | ||
"""Try to convert value to a `ConfigSource` object. This lets a | ||
function accept values that are convertable to a source. | ||
""" | ||
# ignore if already a source | ||
if isinstance(value, ConfigSource): | ||
return value | ||
elif isinstance(value, dict): | ||
return ConfigSource(value) | ||
else: | ||
raise TypeError(u'source value must be a dict') | ||
|
||
# if it's a yaml file | ||
if YamlSource.isoftype(value, **kw): | ||
return YamlSource(value, **kw) | ||
|
||
# if it's an explicit config dict | ||
if isinstance(value, dict): | ||
return ConfigSource(value, **kw) | ||
|
||
# none of the above | ||
raise TypeError( | ||
u'ConfigSource.of value unable to cast to ConfigSource.') | ||
|
||
|
||
class YamlSource(ConfigSource): | ||
"""A config source pulled from yaml files.""" | ||
EXTENSIONS = '.yaml', '.yml' | ||
|
||
def __init__(self, filename=None, value=UNSET, optional=False, | ||
loader=yaml_util.Loader, **kw): | ||
self.optional = optional | ||
self.loader = loader | ||
super(YamlSource, self).__init__(value, filename, **kw) | ||
|
||
@classmethod | ||
def isoftype(cls, value, **kw): | ||
return (isinstance(value, BASESTRING) | ||
and os.path.splitext(value)[1] in YamlSource.EXTENSIONS) | ||
|
||
def _load(self): | ||
'''Load the file if it exists.''' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One more. |
||
if self.optional and not os.path.isfile(self.filename): | ||
return False | ||
self.update(yaml_util.load_yaml(self.filename, loader=self.loader)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from __future__ import division, absolute_import, print_function | ||
|
||
import confuse | ||
import confuse.yaml_util | ||
import unittest | ||
|
||
|
||
class ConfigSourceTest(unittest.TestCase): | ||
def _load_yaml(self, file, **kw): | ||
return {'a': 5, 'file': file} | ||
|
||
def setUp(self): | ||
self._orig_load_yaml = confuse.yaml_util.load_yaml | ||
confuse.yaml_util.load_yaml = self._load_yaml | ||
|
||
def tearDown(self): | ||
confuse.yaml_util.load_yaml = self._orig_load_yaml | ||
|
||
def test_source_conversion(self): | ||
# test pure dict source | ||
src = confuse.ConfigSource.of({'a': 5}) | ||
self.assertIsInstance(src, confuse.ConfigSource) | ||
self.assertEqual(src.loaded, True) | ||
# test yaml filename | ||
src = confuse.ConfigSource.of('asdf/asfdd.yml') | ||
self.assertIsInstance(src, confuse.YamlSource) | ||
self.assertEqual(src.loaded, False) | ||
self.assertEqual(src.exists, False) | ||
self.assertEqual(src.config_dir(create=False), 'asdf') | ||
|
||
def test_explicit_load(self): | ||
src = confuse.ConfigSource.of('asdf.yml') | ||
self.assertEqual(src.loaded, False) | ||
src.load() | ||
self.assertEqual(src.loaded, True) | ||
self.assertEqual(src['a'], 5) | ||
|
||
def test_load_getitem(self): | ||
src = confuse.ConfigSource.of('asdf.yml') | ||
self.assertEqual(src.loaded, False) | ||
self.assertEqual(src['a'], 5) | ||
self.assertEqual(src.loaded, True) | ||
|
||
def test_load_cast_dict(self): | ||
src = confuse.ConfigSource.of('asdf.yml') | ||
self.assertEqual(src.loaded, False) | ||
self.assertEqual(dict(src), {'a': 5, 'file': 'asdf.yml'}) | ||
self.assertEqual(src.loaded, True) | ||
|
||
def test_load_keys(self): | ||
src = confuse.ConfigSource.of('asdf.yml') | ||
self.assertEqual(src.loaded, False) | ||
self.assertEqual(set(src.keys()), {'a', 'file'}) | ||
self.assertEqual(src.loaded, True) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's switch this to """ for uniformity.