Rewritten dcd.cfg.Config
Config now supports full hierarchical and overlay-capable usage. A generic tree overlay function is in aux.
This commit is contained in:
parent
139381e7fc
commit
d72659088f
3 changed files with 54 additions and 31 deletions
56
dcd/cfg.py
56
dcd/cfg.py
|
@ -4,6 +4,18 @@
|
|||
import jsmin
|
||||
import json
|
||||
|
||||
from . import aux
|
||||
|
||||
def _read_json(path):
|
||||
"""
|
||||
Reads a data structure from a JSON file pointed to by `path`.
|
||||
|
||||
Removes any C/JS-like comments from it.
|
||||
"""
|
||||
|
||||
with open(path) as f:
|
||||
return json.loads(jsmin.jsmin(f.read()))
|
||||
|
||||
class Config:
|
||||
"""
|
||||
A hierarchical key-value container that can be loaded from JSON or a Python dict.
|
||||
|
@ -11,48 +23,30 @@ class Config:
|
|||
Any contained dicts are automatically converted to Config instances as well.
|
||||
|
||||
Supports sequential overwriting - so one can load a default config and then
|
||||
progressively overwrite it with overlays:
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
>>> raw
|
||||
{'a': 1, 'b': {'ba': 2, 'bb': 3, 'bc': {'bca': None}}, 'c': 4}
|
||||
>>> overlay
|
||||
{'b': {'bc': {'bca': 42}}}
|
||||
|
||||
|
||||
|
||||
while stack:
|
||||
current = stack[-1]
|
||||
|
||||
...
|
||||
|
||||
stack.pop()
|
||||
|
||||
for key in overlay.keys():
|
||||
|
||||
|
||||
progressively overlay more on top:
|
||||
|
||||
>>> c = Config.from_file("/usr/lib/app/default.cfg")
|
||||
>>> c.update_from_file("/etc/app/config.cfg")
|
||||
>>> c.update({"key", value, "subtree": {"key": "value"}})
|
||||
"""
|
||||
|
||||
def __init__(self, d, readonly=False):
|
||||
def __init__(self, d=None, readonly=False):
|
||||
if d is None:
|
||||
d = {}
|
||||
|
||||
assert type(d) == dict
|
||||
object.__setattr__(self, "raw", d)
|
||||
object.__setattr__(self, "readonly", readonly)
|
||||
|
||||
@classmethod
|
||||
def from_file(cls, path, readonly=False):
|
||||
with open(path) as f:
|
||||
initial = json.loads(jsmin.jsmin(f.read()))
|
||||
|
||||
return cls(initial, readonly)
|
||||
return cls(_read_json(path), readonly)
|
||||
|
||||
def update(self, d):
|
||||
|
||||
aux.overlay_tree(self.raw, d)
|
||||
|
||||
def update_from_file(self, path):
|
||||
self.update(_read_json(path))
|
||||
|
||||
def keys(self):
|
||||
return self.raw.keys()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue