implemented draft of config globalization
[findit] / src / config.py
index ffd81c3..8147fb7 100755 (executable)
@@ -3,23 +3,51 @@
 # vim: sw=4 ts=4 expandtab ai
 # main.py --search files -o=table -p ". 7"
 
+CONF = '''
+[DEFAULT]
+search = 'files'
+outtype = 'outtable'
+
+[files]
+ignore_dirs = ['/dev', '/proc', '/sys', '/mnt']
+start_path = '.'
+count = 5
+
+[debs]
+count = 12
+'''
+
+from ConfigParser import ConfigParser, NoOptionError
+
+class FindITConfig(ConfigParser):
+    def __init__(self, config):
+        ConfigParser.__init__(self)
+        if isinstance(config, basestring):
+            self.read(config)
+        else:
+            self.readfp(config)
+        self._section = None
+
+    def __getitem__(self, item):
+        try:
+            return self.get('DEFAULT', item)
+        except NoOptionError:
+            if self.has_section(item):
+                self._section = item
+                return self
+            else:
+                return self.get(self._section, item)
+
+    def __setitem__(self, item, value):
+        return self.set(self._section, item, str(value))
+
+    def get(self, section, option):
+        value = ConfigParser.get(self, section, option)
+        try:
+            return eval(value)
+        except SyntaxError:
+            return value
+
+from StringIO import StringIO
+config = FindITConfig(StringIO(CONF))
 
-# Dummy config
-
-Config = {}
-
-Config['search'] = 'files'
-Config['outtype'] = 'outtable'
-
-# files
-Config['ignore_dirs'] = ['/dev', '/proc', '/sys', '/mnt']
-Config['start_path'] = '.'
-Config['count'] = 5
-
-# # debs
-# Config['count'] = 7
-
-
-# if __name__ == '__main__':
-#     config = Config
-#     print config['search']