From 139381e7fc6d3285ca37695cfcad1959db5490ce Mon Sep 17 00:00:00 2001 From: Martin Sekera Date: Wed, 16 Oct 2019 23:26:14 +0200 Subject: [PATCH] rename dtk -> dcd --- README | 4 +-- {dtk => dcd}/__init__.py | 0 {dtk => dcd}/argv.py | 0 {dtk => dcd}/aux.py | 0 {dtk => dcd}/cfg.py | 68 +++++++++++++++++++++++++++++----------- {dtk => dcd}/dt.py | 0 setup.py | 10 +++--- 7 files changed, 56 insertions(+), 26 deletions(-) rename {dtk => dcd}/__init__.py (100%) rename {dtk => dcd}/argv.py (100%) rename {dtk => dcd}/aux.py (100%) rename {dtk => dcd}/cfg.py (52%) rename {dtk => dcd}/dt.py (100%) diff --git a/README b/README index 609a90d..706c571 100644 --- a/README +++ b/README @@ -1,3 +1,3 @@ -# dtk +# dcd -Decadic toolkit. \ No newline at end of file +Decadic toolkit. diff --git a/dtk/__init__.py b/dcd/__init__.py similarity index 100% rename from dtk/__init__.py rename to dcd/__init__.py diff --git a/dtk/argv.py b/dcd/argv.py similarity index 100% rename from dtk/argv.py rename to dcd/argv.py diff --git a/dtk/aux.py b/dcd/aux.py similarity index 100% rename from dtk/aux.py rename to dcd/aux.py diff --git a/dtk/cfg.py b/dcd/cfg.py similarity index 52% rename from dtk/cfg.py rename to dcd/cfg.py index 979ca88..4f0053a 100644 --- a/dtk/cfg.py +++ b/dcd/cfg.py @@ -5,25 +5,55 @@ import jsmin import json class Config: - def __init__(self, source=None, readonly=False): - """ - Can be loaded from a JSON file (str path) or from a python dict. - - The JSON file can contain C++ style comments. - """ - - if type(source) == str: - with open(source) as f: - initial = json.loads(jsmin.jsmin(f.read())) - elif source is None: - initial = {} - else: - initial = source - - assert type(initial) == dict - object.__setattr__(self, "raw", initial) + """ + A hierarchical key-value container that can be loaded from JSON or a Python dict. + + 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(): + + + + """ + + def __init__(self, d, readonly=False): + 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) + + def update(self, d): + + def keys(self): return self.raw.keys() @@ -38,13 +68,13 @@ class Config: def __setitem__(self, name, value): if self.readonly: - raise AttributeError("object is not writable") + raise AttributeError("config is read-only") self.raw[name] = value def __setattr__(self, name, value): if self.readonly: - raise AttributeError("object is not writable") + raise AttributeError("config is read-only") self.raw[name] = value diff --git a/dtk/dt.py b/dcd/dt.py similarity index 100% rename from dtk/dt.py rename to dcd/dt.py diff --git a/setup.py b/setup.py index c7d0393..d1ecc0e 100644 --- a/setup.py +++ b/setup.py @@ -1,11 +1,11 @@ from distutils.core import setup setup( - name = "dtk", - version = "0.0.0", - author = "Decadic", - packages = ["dtk"], - url = "https://git.decade.cz/decadic/dtk/", + name = "dcd", + version = "0.1.0", + author = "Decade", + packages = ["dcd"], + url = "https://git.decade.cz/decadic/dcd/", license = "LICENSE", description = "Decadic toolkit.", install_requires = [