annotate python/prefs.py @ 771:c1f314c518c9

more characters
author Jeff Hammel <k0scist@gmail.com>
date Thu, 10 Mar 2016 13:13:14 -0800
parents 31a41264d51f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
143
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 import re
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 comment = re.compile('/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/', re.MULTILINE)
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 def read(filename):
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 token = '##//' # magical token
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 lines = [i.strip() for i in file(filename).readlines() if i.strip()]
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 retval = {}
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 _lines = []
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 for line in lines:
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 if line.startswith('#'):
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 continue
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 if '//' in line:
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 line = line.replace('//', token)
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 _lines.append(line)
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 string = '\n'.join(_lines)
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 string = re.sub(comment, '', string)
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 def pref(a, b):
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 retval[a] = b
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 lines = [i.strip().rstrip(';') for i in string.split('\n') if i.strip()]
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 for line in lines:
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 try:
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 _globals = {'retval': retval, 'pref': pref, 'user_pref': pref, 'true': True, 'false': False}
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 eval(line, _globals, {})
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 except SyntaxError:
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 print line
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 import pdb; pdb.set_trace()
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 for key in retval:
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 if isinstance(retval[key], basestring) and token in retval[key]:
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 retval[key] = retval[key].replace(token, '//')
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 return retval
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 def write(filename, prefs, pref_string='user_pref("%s", %s);'):
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 f = file(filename, 'w')
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 for key, value in prefs.items():
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 if value is True:
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 print >> f, pref_string % (key, 'true')
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 elif value is False:
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 print >> f, pref_string % (key, 'true')
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 elif isinstance(value, basestring):
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 print >> f, pref_string % (key, repr(string(value)))
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 else:
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 print >> f, pref_string % (key, value) # should be numeric!
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 f.close()
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 if __name__ == '__main__':
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 import sys
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 if not sys.argv[1:]:
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 sys.exit(1)
31a41264d51f add a naive prefs reader
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 print read(sys.argv[1])