41943a9c6e61207dd7e7218a6326dd97ed2856e3
[findit] / src / config.py
1 #!/usr/bin/env python
2 # -*-coding: utf-8 -*-
3 # vim: sw=4 ts=4 expandtab ai
4
5 CONF = '''
6 [DEFAULT]
7 search = 'files'
8 outtype = 'out_diabar'
9
10 [files]
11 ignore_dirs = ['/dev', '/proc', '/sys', '/mnt']
12 start_path = '..'
13 count = 7
14
15 [debs]
16 count = 12
17 '''
18
19 from ConfigParser import ConfigParser, NoOptionError
20
21 class FindITConfig(ConfigParser):
22     def __init__(self, config):
23         ConfigParser.__init__(self)
24         if isinstance(config, basestring):
25             self.read(config)
26         else:
27             self.readfp(config)
28         self._section = None
29
30     def __getitem__(self, item):
31         try:
32             return self.get('DEFAULT', item)
33         except NoOptionError:
34             if self.has_section(item):
35                 self._section = item
36                 return self
37             else:
38                 return self.get(self._section, item)
39
40     def __setitem__(self, item, value):
41         return self.set(self._section, item, str(value))
42
43     def get(self, section, option):
44         value = ConfigParser.get(self, section, option)
45         try:
46             return eval(value)
47         except SyntaxError:
48             return value
49
50 from StringIO import StringIO
51 config = FindITConfig(StringIO(CONF))
52