changeset 159:e2956846ee98

Merge
author Thinker K.F. Li <thinker@branda.to>
date Sat, 01 Nov 2008 16:38:56 +0800
parents d4b7d8f51e9f (diff) 4b5e1d3a5198 (current diff)
children 7551342718b6
files
diffstat 1 files changed, 31 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/pyikriam/lconf.py	Sat Nov 01 05:16:27 2008 +0800
+++ b/pyikriam/lconf.py	Sat Nov 01 16:38:56 2008 +0800
@@ -1,15 +1,33 @@
-import os,string
+import os, string
+import re
+
+_user_home = os.environ['HOME']
+dfl_profile = os.path.join(_user_home, '.eagleeye.pm')
+_reo_assign = re.compile('^\\$::([_a-zA-Z][_a-zA-Z0-9]+) *= *([\'"][^"]*[\'"]).*$')
+
+def _real_load_conf(conf_o):
+    confs = [_reo_assign.match(line)
+             for line in conf_o
+             if line.startswith('$::')]
+    cd = dict([(mo.group(1), eval(mo.group(2)))
+               for mo in confs if mo])
+    return cd
+
 class LoadConfigfile(object):
-    def __init__(self):
-	profile = os.environ["HOME"]+'/.eagleeye.pm'
-	self.cd={}
-	if os.path.isfile(profile):
-	    print "Loading Config file."
-	    cfile=open(profile,'r')
-	    for line in cfile.xreadlines():
-	    	if line[0:3]=='$::':
-		   con=string.split(line[3:-2])
-		   self.cd[con[0]]=con[2][1:-1]	   
-	else:
-	    print "File don't exist."
+    def __init__(self, profile=dfl_profile):
+        prof_o = open(profile, 'r')
+        self.cd = _real_load_conf(prof_o)
+        pass
+    pass
 
+if __name__ == '__main__':
+    from StringIO import StringIO
+    conf = '''
+$::user = "alsfjsf"; #lsfjslf
+$::server = "fkljalfasf"; # sfjslf
+$::pass = "lsfjslfsf";
+'''
+    conf_o = StringIO(conf)
+    cd = _real_load_conf(conf_o)
+    print cd
+    pass