Skip to content

Commit

Permalink
Merge #249
Browse files Browse the repository at this point in the history
249: Config.resolved: Major bugfix(es) r=duckinator a=nbraud



Co-authored-by: nicoo <nicoo@mur.at>
  • Loading branch information
bors[bot] and nbraud authored Apr 28, 2021
2 parents dd0c19c + 05a073d commit 324be52
Showing 1 changed file with 38 additions and 24 deletions.
62 changes: 38 additions & 24 deletions emanate/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
from collections.abc import Iterable


CONFIG_PATHS = ('destination', 'source', 'ignore')

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

class Config(dict):
"""Simple wrapper around dict, allowing accessing values as attributes."""
Expand Down Expand Up @@ -66,15 +67,20 @@ def resolve(self, rel_to):
assert rel_to.is_absolute()
result = self.copy()

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

if isinstance(result[key], (str, Path)):
result[key] = rel_to / Path(result[key]).expanduser()
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

elif isinstance(result[key], Iterable):
result[key] = [rel_to / Path(p).expanduser() for p in result[key]]
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

Expand Down Expand Up @@ -122,25 +128,33 @@ def from_json(cls, path):
@property
def resolved(self):
"""Check that all path options in a configuration object are absolute."""
for key in CONFIG_PATHS:
for key in PATHS:
if key not in self:
continue

if isinstance(self[key], Path):
return self[key].is_absolute()
if isinstance(self[key], Iterable):
for path in self[key]:
if not isinstance(path, Path):
raise TypeError(
f"Configuration key '{key}' should contain Paths, "
f"got a '{type(path).__name__}': '{path!r}'"
)
if not path.is_absolute():
return False

raise TypeError(
f"Configuration key '{key}' should be a (list of) Path(s), "
f"got a '{type(key).__name__}': '{self[key]!r}'"
)
path = self[key]
if not isinstance(path, Path):
raise TypeError(
f"Configuration key '{key}' should contain a Path, "
f"got a '{type(path).__name__}': '{path!r}'"
)

if not self[key].is_absolute():
return False

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

assert isinstance(self[key], Iterable)
for path in self[key]:
if not isinstance(path, Path):
raise TypeError(
f"Configuration key '{key}' should be a set of Paths, "
f"got a '{type(path).__name__}': '{path!r}'"
)

if not path.is_absolute():
return False

return True

0 comments on commit 324be52

Please sign in to comment.