rename dtk -> dcd
This commit is contained in:
parent
e7a35d23e5
commit
139381e7fc
7 changed files with 56 additions and 26 deletions
4
README
4
README
|
@ -1,3 +1,3 @@
|
||||||
# dtk
|
# dcd
|
||||||
|
|
||||||
Decadic toolkit.
|
Decadic toolkit.
|
||||||
|
|
|
@ -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.
|
|
||||||
|
Any contained dicts are automatically converted to Config instances as well.
|
||||||
The JSON file can contain C++ style comments.
|
|
||||||
"""
|
Supports sequential overwriting - so one can load a default config and then
|
||||||
|
progressively overwrite it with overlays:
|
||||||
if type(source) == str:
|
|
||||||
with open(source) as f:
|
|
||||||
initial = json.loads(jsmin.jsmin(f.read()))
|
|
||||||
elif source is None:
|
|
||||||
initial = {}
|
|
||||||
else:
|
|
||||||
initial = source
|
|
||||||
|
>>> raw
|
||||||
assert type(initial) == dict
|
{'a': 1, 'b': {'ba': 2, 'bb': 3, 'bc': {'bca': None}}, 'c': 4}
|
||||||
object.__setattr__(self, "raw", initial)
|
>>> 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
|
||||||
|
|
10
setup.py
10
setup.py
|
@ -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 = [
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue