# HG changeset patch # User Jeff Hammel # Date 1306881276 25200 # Node ID 31a41264d51f6e1df3d5141bc5b81b506d4763f5 # Parent 09c10061838ca623810774c41f7a932a4fd7bbeb add a naive prefs reader diff -r 09c10061838c -r 31a41264d51f python/prefs.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/prefs.py Tue May 31 15:34:36 2011 -0700 @@ -0,0 +1,49 @@ +import re +comment = re.compile('/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/', re.MULTILINE) + +def read(filename): + token = '##//' # magical token + lines = [i.strip() for i in file(filename).readlines() if i.strip()] + retval = {} + _lines = [] + for line in lines: + if line.startswith('#'): + continue + if '//' in line: + line = line.replace('//', token) + _lines.append(line) + string = '\n'.join(_lines) + string = re.sub(comment, '', string) + def pref(a, b): + retval[a] = b + lines = [i.strip().rstrip(';') for i in string.split('\n') if i.strip()] + for line in lines: + try: + _globals = {'retval': retval, 'pref': pref, 'user_pref': pref, 'true': True, 'false': False} + eval(line, _globals, {}) + except SyntaxError: + print line + import pdb; pdb.set_trace() + for key in retval: + if isinstance(retval[key], basestring) and token in retval[key]: + retval[key] = retval[key].replace(token, '//') + return retval + +def write(filename, prefs, pref_string='user_pref("%s", %s);'): + f = file(filename, 'w') + for key, value in prefs.items(): + if value is True: + print >> f, pref_string % (key, 'true') + elif value is False: + print >> f, pref_string % (key, 'true') + elif isinstance(value, basestring): + print >> f, pref_string % (key, repr(string(value))) + else: + print >> f, pref_string % (key, value) # should be numeric! + f.close() + +if __name__ == '__main__': + import sys + if not sys.argv[1:]: + sys.exit(1) + print read(sys.argv[1])