summaryrefslogtreecommitdiff
path: root/src/config.py
diff options
context:
space:
mode:
authorJacob <andqso@gmail.com>2013-07-28 09:54:54 +0100
committerJacob <andqso@gmail.com>2013-07-28 09:54:54 +0100
commitf343459d6198352964dbb6779f15c352fe2d5794 (patch)
tree07d50c9a8269e3892ccea8f5680b3e2bac984fce /src/config.py
init
Diffstat (limited to 'src/config.py')
-rw-r--r--src/config.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/config.py b/src/config.py
new file mode 100644
index 0000000..138a28d
--- /dev/null
+++ b/src/config.py
@@ -0,0 +1,39 @@
+import yaml
+import logging
+
+class Config:
+
+ def __init__(self, settings_path, dependencies_path):
+ with open(settings_path) as settings_file:
+ settings = yaml.load(settings_file)
+ with open(dependencies_path) as dependencies_file:
+ dependencies = yaml.load(dependencies_file)
+
+ self.entries = {}
+
+ for config in (settings, dependencies):
+ for key, value in config.items():
+ self.put(key, value)
+
+ def __hasattr__(self, key):
+ return key in self.entries
+
+ def __getattr__(self, key):
+ if key not in self.entries:
+ raise Exception('No such key: %s' % key)
+ return self.entries[key]
+
+ def put(self, key, value):
+ if key in self.entries:
+ logging.warn('changing value of %s' % key)
+ self.entries[key] = value
+
+ def __repr__(self):
+ return '%s(%d items)' % (self.__class__, len(self.keys))
+
+ def __str__(self):
+ s = []
+ s.append('%s:' % self.__class__.__name__)
+ for key in sorted(self.entries.keys()):
+ s.append(' %s: %s' % (key, getattr(self, key)))
+ return '\n'.join(s)