rename dtk -> dcd

This commit is contained in:
Martin Sekera 2019-10-16 23:26:14 +02:00
parent e7a35d23e5
commit 139381e7fc
7 changed files with 56 additions and 26 deletions

2
README
View file

@ -1,3 +1,3 @@
# dtk # dcd
Decadic toolkit. Decadic toolkit.

View file

@ -5,25 +5,55 @@ import jsmin
import json import json
class Config: class Config:
def __init__(self, source=None, readonly=False): """
""" A hierarchical key-value container that can be loaded from JSON or a Python dict.
Can be loaded from a JSON file (str path) or from a python dict.
The JSON file can contain C++ style comments. Any contained dicts are automatically converted to Config instances as well.
"""
if type(source) == str: Supports sequential overwriting - so one can load a default config and then
with open(source) as f: progressively overwrite it with overlays:
initial = json.loads(jsmin.jsmin(f.read()))
elif source is None:
initial = {}
else:
initial = source
assert type(initial) == dict
object.__setattr__(self, "raw", initial)
>>> 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) 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): def keys(self):
return self.raw.keys() return self.raw.keys()
@ -38,13 +68,13 @@ class Config:
def __setitem__(self, name, value): def __setitem__(self, name, value):
if self.readonly: if self.readonly:
raise AttributeError("object is not writable") raise AttributeError("config is read-only")
self.raw[name] = value self.raw[name] = value
def __setattr__(self, name, value): def __setattr__(self, name, value):
if self.readonly: if self.readonly:
raise AttributeError("object is not writable") raise AttributeError("config is read-only")
self.raw[name] = value self.raw[name] = value

View file

@ -1,11 +1,11 @@
from distutils.core import setup from distutils.core import setup
setup( setup(
name = "dtk", name = "dcd",
version = "0.0.0", version = "0.1.0",
author = "Decadic", author = "Decade",
packages = ["dtk"], packages = ["dcd"],
url = "https://git.decade.cz/decadic/dtk/", url = "https://git.decade.cz/decadic/dcd/",
license = "LICENSE", license = "LICENSE",
description = "Decadic toolkit.", description = "Decadic toolkit.",
install_requires = [ install_requires = [