rename dtk -> dcd
This commit is contained in:
parent
e7a35d23e5
commit
139381e7fc
7 changed files with 56 additions and 26 deletions
2
README
2
README
|
@ -1,3 +1,3 @@
|
|||
# dtk
|
||||
# dcd
|
||||
|
||||
Decadic toolkit.
|
|
@ -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.
|
||||
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():
|
||||
|
||||
|
||||
|
||||
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)
|
||||
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
|
||||
|
10
setup.py
10
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 = [
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue