2 from defaultconfig import *
4 from log.logging import logger
5 configuration_regexp = re.compile('(?P<key>[^:]*):(?P<value>.*)',re.U)
12 def __setitem__(self, key, item):
13 dict.__setitem__(self, key, item)
14 if key not in self._keys: self._keys.append(key)
17 return zip(self._keys, self.values())
22 return repr(self.items())
25 return map(self.get, self._keys)
34 class Configuration(odict):
35 def __init__(self,complete_file_path,defaults={}):
37 self.file_path=complete_file_path
40 if self.merge(defaults):
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))
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')
55 self[key]=self.parse_value(value)
56 def parse_value(self,value):
58 value=value.split(',')
61 def merge(self, other):
65 self[key] = other[key]
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)
81 COMMON_CONFIG = Configuration(main_config_file,default_configuration)
82 ABBREVIATIONS = {} #Configuration("abbreviations.cfg",default_abbreviations)
84 def read_configurations():
86 ABBREVIATIONS = Configuration("abbreviations.cfg",default_abbreviations)
88 gtd_directory = COMMON_CONFIG['path']
89 inactivity_threshold = int(COMMON_CONFIG['inactivity_threshold'])
90 read_sms = int(COMMON_CONFIG['read_sms'])
92 __all__=["Configuration","read_sms","inactivity_threshold","COMMON_CONFIG"]