Skip to content

Commit

Permalink
Initial data value sketch.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjw296 committed Jul 15, 2022
1 parent 1d69fe8 commit 1453838
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
7 changes: 7 additions & 0 deletions configurator/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class DataValue:

def __init__(self, value):
self.value = value

def get(self):
return self.value
3 changes: 3 additions & 0 deletions configurator/node.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pprint import pformat

from .data import DataValue
from .path import parse_text, NotPresent


Expand All @@ -23,6 +24,8 @@ def __init__(self, data=None, container=None, accessor=None):
self._accessor = accessor

def _wrap(self, accessor, value):
if isinstance(value, DataValue):
value = value.get()
if isinstance(value, (dict, list)):
value = ConfigNode(value, self.data, accessor)
return value
Expand Down
31 changes: 31 additions & 0 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from testfixtures import compare
from configurator import Config
from configurator.data import DataValue
from configurator.mapping import target, convert
import pytest

Expand Down Expand Up @@ -87,6 +88,36 @@ def test_overlay(self, dir):
compare(config.user, expected=2)
compare(config.file, expected=3)

def test_lazy_load(self, dir):
yaml = pytest.importorskip("yaml")

class LazyVault(DataValue):
def __init__(self, key):
self.key = key

@classmethod
def from_yaml(cls, loader, node):
return cls(loader.construct_mapping(node)['key'])

def get(self):
assert self.key == 'someuser'
return {'name': 'Some User', 'password': '...'}

class Loader(yaml.Loader):
pass

Loader.add_constructor('!from_vault', LazyVault.from_yaml)

path = dir.write('lazy.yml', '''
users:
- !from_vault {key: someuser}
''')
config = Config.from_path(path, parser=lambda p: yaml.load(p, Loader))
compare(config.users[0].data, expected={'name': 'Some User', 'password': '...'})

# make sure clone still works:
config.clone()
compare(config.users.data[0], expected=LazyVault('someuser'))

def test_fake_fs(fs):
fs.create_file('/foo/bar.yml', contents='foo: 1\n')
Expand Down

0 comments on commit 1453838

Please sign in to comment.