Package linda :: Package parser :: Module config
[hide private]

Source Code for Module linda.parser.config

 1  import os, re, linda 
 2  from linda.debug import dprint 
 3   
4 -class ConfigFileParser:
5 - def __init__(self):
6 if linda.clparser['config']: 7 self.file = linda.clparser['config'] 8 elif os.path.exists(os.path.expanduser('~/.linda/config')): 9 self.file = os.path.expanduser('~/.linda/config') 10 else: 11 self.file = '/dev/null' 12 self.errors = [] 13 self.data = [] 14 self.read() 15 self.parse() 16 self.check_error()
17
18 - def read(self):
19 f = open(self.file) 20 for k in f: 21 if k.startswith('#') or k == '\n': 22 continue 23 else: 24 self.data.append(k[:-1])
25
26 - def parse(self):
27 for line in self.data: 28 splitted_line = re.split(r'\s', line, 1) 29 if splitted_line[0] in ('info', 'informational', 'no-cull', \ 30 'show-overridden', 'quiet', 'show-tag', 'traceback'): 31 linda.clparser[line] = 1 32 elif splitted_line[0] in ('debug', 'verbose'): 33 if len(splitted_line) > 1: 34 linda.clparser[splitted_line[0]] = int(splitted_line[1]) 35 else: 36 linda.clparser[splitted_line[0]] += 1 37 elif splitted_line[0] in ('checks', 'format', 'lab-root', \ 38 'more-overrides', 'types'): 39 if len(splitted_line) > 1: 40 if splitted_line[0] == 'lab-root': 41 if os.path.isdir(splitted_line[1]): 42 linda.clparser[splitted_line[0]] = splitted_line[1] 43 elif splitted_line[0] == 'types': 44 linda.clparser[splitted_line[0]] = [] 45 for type in re.split(r', ?', splitted_line[1]): 46 if type in ('E', 'W', 'X', 'I'): 47 linda.clparser[splitted_line[0]].append(type) 48 else: 49 self.errors.append("'%s' isn't a valid type" % \ 50 type) 51 else: 52 if splitted_line[1]: 53 if not linda.clparser[splitted_line[0]]: 54 linda.clparser[splitted_line[0]] = \ 55 splitted_line[1] 56 else: 57 self.errors.append('Required argument to %s empty' % \ 58 splitted_line[0]) 59 else: 60 self.errors.append('Required argument to %s missing' % \ 61 splitted_line[0]) 62 else: 63 self.errors.append('Unknown line in config file: %s' % line)
64
65 - def check_error(self):
66 if self.errors: 67 raise ConfigFileParserException, '; '.join(self.errors)
68
69 -class ConfigFileParserException(Exception):
70 pass
71