Don't let ListView call a non-existent method on its superclass.
[matthijs/upstream/mobilegtd.git] / src / config / config.py
1 import os,re
2 from defaultconfig import *
3 from inout import io
4 from log.logging import logger
5 configuration_regexp = re.compile('(?P<key>[^:]*):(?P<value>.*)',re.U)
6
7 class odict(dict):
8     def __init__(self):
9         self._keys = []
10         dict.__init__(self)
11
12     def __setitem__(self, key, item):
13         dict.__setitem__(self, key, item)
14         if key not in self._keys: self._keys.append(key)
15
16     def items(self):
17         return zip(self._keys, self.values())
18
19     def keys(self):
20         return self._keys
21     def __repr__(self):
22         return repr(self.items())
23
24     def values(self):
25         return map(self.get, self._keys)
26
27
28
29
30
31
32
33
34 class Configuration(odict):
35     def __init__(self,complete_file_path,defaults={}):
36         odict.__init__(self)
37         self.file_path=complete_file_path
38
39         self.read()
40         if self.merge(defaults):
41             self.write()
42             self.read()
43     def read(self):
44         encoded_path = io.os_encode(self.file_path)
45         if not os.path.isfile(encoded_path):
46             logger.log(u'Configuration file %s does not exist'%os.path.abspath(encoded_path))
47             return
48         for line in io.parse_file_to_line_list(self.file_path):
49             if len(line)<1:continue
50             if line[0] == '#': continue
51             matching = configuration_regexp.match(line)
52             key = matching.group('key')
53             value = matching.group('value').rstrip(u' \r\n')
54     
55             self[key]=self.parse_value(value)
56     def parse_value(self,value):
57         if ',' in value:
58             value=value.split(',')
59         return value
60
61     def merge(self, other):
62         changed = False
63         for key in other:
64             if key not in self:
65                 self[key] = other[key]
66                 changed = True
67         return changed
68
69                 
70     def write(self):
71         content = u'\n'.join([u'%s:%s'%(key,self.format_value(value)) for (key,value) in self.items()])
72         io.write(self.file_path,content)
73     def format_value(self,value):
74         if isinstance(value,list):
75             return ','.join(value)
76         else:
77             return value
78
79
80
81 COMMON_CONFIG = Configuration(main_config_file,default_configuration)
82 ABBREVIATIONS =  {} #Configuration("abbreviations.cfg",default_abbreviations)
83
84 def read_configurations():
85     global ABBREVIATIONS
86     ABBREVIATIONS = Configuration("abbreviations.cfg",default_abbreviations)
87
88 gtd_directory = COMMON_CONFIG['path']
89 inactivity_threshold = int(COMMON_CONFIG['inactivity_threshold'])
90 read_sms = int(COMMON_CONFIG['read_sms'])
91
92 __all__=["Configuration","read_sms","inactivity_threshold","COMMON_CONFIG"]