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

Make Config objects immutable #250

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
49 changes: 16 additions & 33 deletions emanate/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
from pathlib import Path
from collections.abc import Iterable

from frozendict import frozendict


PATHS = frozenset(('destination', 'source',))
PATH_SETS = frozenset(('ignore',))
PATH_KEYS = PATHS.union(PATH_SETS)

class Config(dict):

class Config(frozendict):
"""Simple wrapper around dict, allowing accessing values as attributes."""

def __getattr__(self, name):
Expand All @@ -27,10 +30,6 @@ def __getattr__(self, name):

return self[name]

def copy(self):
"""Return a new Config, with the same contents as self."""
return Config(self)

@classmethod
def defaults(cls, src):
"""Return Emanate's default configuration.
Expand Down Expand Up @@ -65,25 +64,15 @@ def resolve(self, rel_to):
"""
assert isinstance(rel_to, Path)
assert rel_to.is_absolute()
result = self.copy()

for key in PATHS:
if key not in result:
continue

assert isinstance(result[key], (str, Path))
result[key] = rel_to / Path(result[key]).expanduser()

for key in PATH_SETS:
if key not in result:
continue

assert isinstance(result[key], Iterable)
assert all((isinstance(p, (Path, str)) for p in result[key]))
result[key] = frozenset((rel_to / Path(p).expanduser() for p in result[key]))

return result
def _resolve(path):
return rel_to / Path(path).expanduser()

return self.copy(**{
key: _resolve(v) if key in PATHS else frozenset(map(_resolve, v))
for (key, v) in self.items()
if key in PATH_KEYS
})

def merge(*configs, strict_resolve=True): # pylint: disable=no-method-argument,no-self-argument
"""Merge several Config objects.
Expand All @@ -101,17 +90,11 @@ def _merge_one(config, other):
if strict_resolve and not other.resolved:
raise ValueError("Merging a non-resolved configuration")

config = config.copy()
for key, value in other.items():
if value is None:
continue

if key == 'ignore':
config[key] = config.get(key, frozenset()).union(value)
else:
config[key] = value

return config
return config.copy(**other, **{
key: config[key].union(other[key])
for key in PATH_SETS
if key in config and key in other
})

return functools.reduce(_merge_one, filter(None, configs), Config())

Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ packages = find:

python_requires = >=3.8
install_requires =
frozendict~=2.3


[options.extras_require]
Expand Down