Merge branch 'master' of git.decade.cz:overwatch/over
This commit is contained in:
commit
b083fbc384
3 changed files with 88 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
|
||||||
|
|
|
@ -128,3 +128,15 @@ def integer(arg):
|
||||||
return int(arg, 8)
|
return int(arg, 8)
|
||||||
|
|
||||||
return int(arg)
|
return int(arg)
|
||||||
|
|
||||||
|
def integers(*args):
|
||||||
|
"""
|
||||||
|
@while converting arguments to ints
|
||||||
|
"""
|
||||||
|
|
||||||
|
out = []
|
||||||
|
|
||||||
|
for arg in args:
|
||||||
|
out.append(integer(arg))
|
||||||
|
|
||||||
|
return out
|
||||||
|
|
75
over/cfg.py
Normal file
75
over/cfg.py
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
#! /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 items(self):
|
||||||
|
return self.raw.items()
|
||||||
|
|
||||||
|
def values(self):
|
||||||
|
return self.raw.values()
|
||||||
|
|
||||||
|
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