add over.cfg.Config - a simple read-only API for JSON configs
This commit is contained in:
parent
88be91445f
commit
43ba8ee6a3
2 changed files with 70 additions and 0 deletions
|
@ -6,6 +6,7 @@ import sys
|
||||||
from . import app
|
from . import app
|
||||||
from . import aux
|
from . import aux
|
||||||
from . import callback
|
from . import callback
|
||||||
|
from . import cfg
|
||||||
from . import cmd
|
from . import cmd
|
||||||
from . import docs
|
from . import docs
|
||||||
from . import file
|
from . import file
|
||||||
|
|
69
over/cfg.py
Normal file
69
over/cfg.py
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
#! /usr/bin/env python3
|
||||||
|
# encoding: utf-8
|
||||||
|
|
||||||
|
# --------------------------------------------------
|
||||||
|
# Library imports
|
||||||
|
import json
|
||||||
|
import jsmin
|
||||||
|
|
||||||
|
# --------------------------------------------------
|
||||||
|
# Local imports
|
||||||
|
|
||||||
|
# --------------------------------------------------
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
def __init__(self, source=None, readonly=False):
|
||||||
|
"""
|
||||||
|
Can be loaded from a JSON file (str path) or from a python dict.
|
||||||
|
"""
|
||||||
|
|
||||||
|
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)
|
||||||
|
object.__setattr__(self, "readonly", readonly)
|
||||||
|
|
||||||
|
def keys(self):
|
||||||
|
return self.raw.keys()
|
||||||
|
|
||||||
|
def __getitem__(self, name):
|
||||||
|
return self.raw[name]
|
||||||
|
|
||||||
|
def __setitem__(self, name, value):
|
||||||
|
if self.readonly:
|
||||||
|
raise AttributeError("object is not writable")
|
||||||
|
|
||||||
|
self.raw[name] = value
|
||||||
|
|
||||||
|
def __setattr__(self, name, value):
|
||||||
|
if self.readonly:
|
||||||
|
raise AttributeError("object is not writable")
|
||||||
|
|
||||||
|
self.raw[name] = value
|
||||||
|
|
||||||
|
def __getattr__(self, name):
|
||||||
|
matches = [key for key in self.raw if key.replace("-", "_") == name]
|
||||||
|
|
||||||
|
if matches:
|
||||||
|
assert len(matches) == 1
|
||||||
|
|
||||||
|
value = self.raw[matches[0]]
|
||||||
|
|
||||||
|
if type(value) == dict:
|
||||||
|
return Config(value)
|
||||||
|
else:
|
||||||
|
return value
|
||||||
|
else:
|
||||||
|
raise KeyError(name)
|
||||||
|
|
||||||
|
def __contains__(self, name):
|
||||||
|
return name in self.raw
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "Config(%s)" %(" ".join("%s=%s" %(k, "…" if type(v) == dict else v) for k, v in self.raw.items()))
|
Loading…
Add table
Add a link
Reference in a new issue