summaryrefslogtreecommitdiff
path: root/config.py
diff options
context:
space:
mode:
authormikeos <mike.osipov@gmail.com>2013-07-18 20:55:53 +0400
committermikeos <mike.osipov@gmail.com>2013-07-18 20:55:53 +0400
commit5aaf7bd127ee70b90f26dd3dfbb8feea2d9c0c89 (patch)
tree60ef625fca42b42ffb44a8234dd4a5751b08d47d /config.py
parent3ff3ddd16357d96987026e770ffe197ab7286ede (diff)
add config file support
Diffstat (limited to 'config.py')
-rw-r--r--config.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/config.py b/config.py
new file mode 100644
index 0000000..bb6be97
--- /dev/null
+++ b/config.py
@@ -0,0 +1,49 @@
+from utils import is_python_v2, to_unicode
+import os
+
+if is_python_v2():
+ import ConfigParser as configparser
+else:
+ import configparser
+
+ConfigParserClass = configparser.RawConfigParser
+
+def with_default(func, msg = None):
+ def method(cls, section, option, default = None):
+ try:
+ return func(cls.parser, section, option)
+ except configparser.NoSectionError:
+ return default
+ except configparser.NoOptionError:
+ return default
+ except ValueError as err:
+ raise Exception("%s::%s: %s" % (section, option, msg or err))
+ return method
+
+class CfgParser:
+ def __init__(self):
+ self.parser = ConfigParserClass()
+
+ __get = with_default(ConfigParserClass.get)
+
+ if not is_python_v2():
+ get = __get
+ else:
+ def get(self, *args):
+ return to_unicode(self.__get(*args))
+
+ getint = with_default(ConfigParserClass.getint, "invalid number")
+ getbool = with_default(ConfigParserClass.getboolean, "invalid bool")
+
+ def __getattr__(self, attr):
+ return getattr(self.parser, attr)
+
+cfg = CfgParser()
+cfg.read(os.path.expanduser("~/.cutter.cfg"))
+
+DIR = to_unicode(cfg.get("encoding", "dir", "."))
+COMPRESSION = cfg.getint("encoding", "compression")
+
+SAMPLE_RATE = cfg.getint("output", "sample_rate")
+CHANNELS = cfg.getint("output", "channels")
+BITS_PER_SAMPLE = cfg.getint("output", "bits_per_sample")