Mercurial > eagle-eye
comparison pyikriam/lconf.py @ 158:d4b7d8f51e9f
More exactly parser for $HOME/.eagleeye.pm
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sat, 01 Nov 2008 16:35:24 +0800 |
parents | 19d2a7b175b3 |
children |
comparison
equal
deleted
inserted
replaced
155:1499b0d496b6 | 158:d4b7d8f51e9f |
---|---|
1 import os,string | 1 import os, string |
2 import re | |
3 | |
4 _user_home = os.environ['HOME'] | |
5 dfl_profile = os.path.join(_user_home, '.eagleeye.pm') | |
6 _reo_assign = re.compile('^\\$::([_a-zA-Z][_a-zA-Z0-9]+) *= *([\'"][^"]*[\'"]).*$') | |
7 | |
8 def _real_load_conf(conf_o): | |
9 confs = [_reo_assign.match(line) | |
10 for line in conf_o | |
11 if line.startswith('$::')] | |
12 cd = dict([(mo.group(1), eval(mo.group(2))) | |
13 for mo in confs if mo]) | |
14 return cd | |
15 | |
2 class LoadConfigfile(object): | 16 class LoadConfigfile(object): |
3 def __init__(self): | 17 def __init__(self, profile=dfl_profile): |
4 profile = os.environ["HOME"]+'/.eagleeye.pm' | 18 prof_o = open(profile, 'r') |
5 self.cd={} | 19 self.cd = _real_load_conf(prof_o) |
6 if os.path.isfile(profile): | 20 pass |
7 print "Loading Config file." | 21 pass |
8 cfile=open(profile,'r') | |
9 for line in cfile.xreadlines(): | |
10 if line[0:3]=='$::': | |
11 con=string.split(line[3:-2]) | |
12 self.cd[con[0]]=con[2][1:-1] | |
13 else: | |
14 print "File don't exist." | |
15 | 22 |
23 if __name__ == '__main__': | |
24 from StringIO import StringIO | |
25 conf = ''' | |
26 $::user = "alsfjsf"; #lsfjslf | |
27 $::server = "fkljalfasf"; # sfjslf | |
28 $::pass = "lsfjslfsf"; | |
29 ''' | |
30 conf_o = StringIO(conf) | |
31 cd = _real_load_conf(conf_o) | |
32 print cd | |
33 pass |