1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
#!/usr/bin/env python3
import argparse
import os
import re
import sys
from configparser import ConfigParser
FF_PATH = os.path.expanduser('~/.mozilla/firefox')
UP_REGEX = re.compile(r'^user_pref\("(?P<key>[^"]+)", (?P<value>.+)\);$')
def scan_profiles():
f = os.path.join(FF_PATH, 'profiles.ini')
if not os.path.exists(f):
return
ini = ConfigParser()
ini.read(f)
for section in ini.sections():
if section.startswith('Profile'):
yield (ini.get(section, 'Name'), ini.get(section, 'Path'))
def read_prefs(profile):
f = os.path.join(FF_PATH, profile, 'prefs.js')
assert os.path.exists(f)
prefs = dict()
with open(f, 'r') as p:
for line in p:
match = UP_REGEX.match(line.strip())
if match:
prefs[match.groupdict()['key']] = match.groupdict()['value']
return prefs
def diff(a, b):
removed = dict()
added = dict()
changed = dict()
for key, value in a.items():
if key not in b:
removed[key] = value
elif b[key] != value:
changed[key] = b[key]
for key, value in b.items():
if key not in a:
added[key] = value
return removed, added, changed
def pentadactylrc(removed, added, changed):
def stripval(value):
if value.startswith('"') and value.endswith('"'):
svalue = value[1:-1]
if '"' not in svalue and "=" not in svalue and " " not in svalue:
return svalue
return value
print('" reset to default deleted values')
for key, value in removed.items():
print('set! %s&' % key)
print('" added')
for key, value in added.items():
print('set! %s=%s' % (key, stripval(value)))
print('" changed')
for key, value in changed.items():
print('set! %s=%s' % (key, stripval(value)))
def main(argv):
profiles = dict(scan_profiles())
default_profile = profiles.get('default')
if default_profile is None and len(profiles) == 1:
default_profile = profiles.values()[0]
parser = argparse.ArgumentParser(description="Live diff Firefox settings.")
parser.add_argument('-p', '--profile', nargs=1,
required=default_profile is None,
default=default_profile, choices=profiles.values(),
help="Profile name")
args = parser.parse_args(argv[1:])
print("Watching settings of %s." % args.profile)
oldprefs = read_prefs(args.profile)
input('Press enter when ready to show the differences.')
newprefs = read_prefs(args.profile)
removed, added, changed = diff(oldprefs, newprefs)
# TODO Add support for other outputs
pentadactylrc(removed, added, changed)
if __name__ == '__main__':
sys.exit(main(sys.argv))
|