Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View config.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
__all__ = ['Config']
 
import json, sys
 
class _Config():
def __init__(self):
self.filename = 'PROJECT.cfg'
self.load()
self.upgrade()
def load(self):
raw = ''
try:
f = open(self.filename, 'rt')
raw = f.read().split("\n")[1:]
f.close()
except Exception, e:
raw = ['{}']
try:
raw = "\n".join(raw)
self._c = json.loads(raw)
except Exception as e:
print "Error reading configuration:", repr(e)
raise
def upgrade(self):
self._upgrade(u'database', {u'connectstring': u'sqlite:///PROJECT.db'})
def _upgrade(self, toplevel, defaults):
if not toplevel in self._c:
self._c[toplevel] = {}
defaults.update(self._c[toplevel])
self._c[toplevel] = defaults
def save(self):
f = open(self.filename, 'wt')
f.write(" -*- mode: javascript -*- PROJECT config\n")
f.write(self.dump())
f.close()
def __getitem__(self, name):
v = self._c
for k in name.split('.'):
v = v[k]
return v
def dump(self):
return json.dumps(self._c, indent=2)
 
Config = _Config()
 
if __name__ == "__main__":
print "Configuration filename:", Config.filename
Config.save()
print Config.dump()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.