]> Dogcows Code - chaz/openbox/blob - tools/themeupdate/themeupdate.py
a6d7cd074d361b1828e63a7e5cbdca65aff85884
[chaz/openbox] / tools / themeupdate / themeupdate.py
1 #! /usr/bin/python
2
3 import sys
4
5 data = []
6 valid = True
7
8 def out(str):
9 sys.stderr.write(str)
10 sys.stderr.flush()
11
12 def read_bool():
13 while True:
14 inp = sys.stdin.readline(1).strip()
15 if inp == 'y' or inp == '': return True
16 if inp == 'n': return False
17
18 def getkeyval(line):
19 key = line[:line.find(':')].strip()
20 value = line[line.find(':') + 1:].strip()
21 if not (key and value):
22 key = value = None
23 return key, value
24
25 def find_key(data, keysubstr, exact = False):
26 i = 0
27 n = len(data)
28 while i < n:
29 l = data[i]
30 key, value = getkeyval(l)
31 if key and value:
32 if (exact and key == keysubstr) or \
33 (not exact and key.find(keysubstr) != -1):
34 return i, key, value
35 i += 1
36 return -1, None, None
37
38 def simple_replace(data):
39 pairs = {}
40 pairs['window.focus.font'] = 'window.label.focus.font'
41 pairs['window.unfocus.font'] = 'window.label.unfocus.font'
42 pairs['window.justify'] = 'window.label.justify'
43 pairs['menu.frame.disableColor'] = 'menu.disabled.textColor'
44 pairs['menu.frame'] = 'menu.items'
45 pairs['menu.hilite'] = 'menu.selected'
46 pairs['.picColor'] = '.imageColor'
47
48 for k in pairs.keys():
49 while 1:
50 i, key, nul = find_key(data, k);
51 if i >= 0:
52 newl = data[i].replace(k, pairs[k])
53 out('Updating "' + key +
54 '" to "' + key.replace(k, pairs[k]) + '"\n')
55 data[i] = newl
56 else:
57 break
58
59 def remove(data):
60 invalid = []
61 invalid.append('toolbar')
62 invalid.append('menu.bullet')
63 invalid.append('rootCommand')
64 invalid.append('menu.frame.justify')
65 for inv in invalid:
66 while 1:
67 i, key, nul = find_key(data, inv)
68 if i >= 0:
69 out(key + ' is no longer supported.\nRemove (Y/n)? ')
70 if read_bool():
71 out('Removing "' + key + '"\n')
72 data.pop(i)
73 else:
74 break
75
76 def pressed(data):
77 i, nul, nul = find_key(data, 'window.button.pressed', True)
78 if i >= 0:
79 out('The window.button.pressed option has been replaced by ' +
80 'window.button.pressed.focus and ' +
81 'window.button.pressed.unfocus.\nUpdate (Y/n)? ')
82 if read_bool():
83 l = data[i]
84 out('Removing "window.button.pressed"\n')
85 data.pop(i)
86 out('Adding "window.button.pressed.unfocus"\n')
87 data.insert(i, l.replace('window.button.pressed',
88 'window.button.pressed.unfocus'))
89 out('Adding "window.button.pressed.focus"\n')
90 data.insert(i, l.replace('window.button.pressed',
91 'window.button.pressed.focus'))
92
93 def x_fonts(data):
94 i, nul, nul = find_key(data, 'window.font')
95 if i >= 0:
96 out('You appear to specify fonts using the old X fonts ' +
97 'syntax.\nShall I remove all fonts from the theme (Y/n)? ')
98 if not read_bool():
99 return
100 else: return
101 while 1:
102 i, key = key_find(data, '.font')
103 if i < 0:
104 break
105 out('Removing "' + key + '"\n')
106 data.pop(i)
107
108 def xft_fonts(data):
109 i, nul, nul = find_key(data, '.xft.')
110 if i >= 0:
111 out('You appear to specify fonts using the old Xft fonts ' +
112 'syntax.\nShall I update these to the new syntax (Y/n)? ')
113 if not read_bool():
114 return
115 else: return
116 fonts = {}
117 fonts['window'] = 'window.label.focus.font'
118 fonts['menu.items'] = 'menu.items.font'
119 fonts['menu.title'] = 'menu.title.font'
120 for f in fonts.keys():
121 li, nul, flags = find_key(data, f + '.xft.flags')
122 if li < 0:
123 li, nul, flags = find_key(data, '*.xft.flags')
124 else:
125 out('Removing ' + f + '.xft.flags\n')
126 data.pop(li)
127 oi, nul, offset = find_key(data, f + '.xft.shadow.offset')
128 if oi < 0:
129 oi, nul, offset = find_key(data, '*.xft.shadow.offset')
130 else:
131 out('Removing ' + f + '.xft.shadow.offset\n')
132 data.pop(oi)
133 ti, nul, tint = find_key(data, f + '.xft.shadow.tint')
134 if ti < 0:
135 ti, nul, tint = find_key(data, '*.xft.shadow.tint')
136 else:
137 out('Removing ' + f + '.xft.shadow.tint\n')
138 data.pop(ti)
139 fi, nul, face = find_key(data, f + '.xft.font')
140 if fi < 0:
141 fi, nul, face = find_key(data, '*.xft.font')
142 if fi >= 0: fi = len(data) - 1
143 else:
144 out('Removing ' + f + '.xft.font\n')
145 data.pop(fi)
146
147 if fi >= 0:
148 s = face
149 if li >= 0:
150 if flags.find('bold'):
151 s = s + ':bold'
152 if flags.find('shadow'):
153 s = s + ':shadow=y'
154 if oi >= 0:
155 s = s + ':shadowoffset=' + offset
156 if ti >= 0:
157 s = s + ':shadowtint=' + tint
158 out('Adding ' + fonts[f] + '\n')
159 data.insert(fi, fonts[f] + ': ' + s)
160
161 for stars in ('*.xft.flags', '*.xft.shadow.offset' ,
162 '*.xft.shadow.tint', '*.xft.font'):
163 i, key, nul = find_key(data, stars)
164 if i >= 0:
165 out('Removing ' + key + '\n')
166 data.pop(i)
167
168 def pixelsize(data):
169 fonts = ('window.label.focus.font',
170 'menu.items.font',
171 'menu.title.font')
172 for f in fonts:
173 i, key, value = find_key(data, f, True)
174 if value:
175 if value.find('pixelsize') == -1:
176 out('*** ERROR *** The ' + key + ' font size is not being '
177 'specified by pixelsize. It is recommended that you use '
178 'pixelsize instead of pointsize for specifying theme '
179 'fonts. e.g. "sans:pixelsize=12"\n')
180 global valid
181 valid = False
182
183 def warn_missing(data):
184 need = ('window.button.hover.focus', 'window.button.hover.unfocus',
185 'menuOverlap')
186 for n in need:
187 i, nul, nul = find_key(data, n)
188 if i < 0:
189 out('The ' + n + ' value was not found in the theme, but it '
190 'can optionally be set.\n')
191
192 def err_missing(data):
193 need = ('window.button.disabled.focus', 'window.button.disabled.unfocus',
194 'window.frame.focusColor', 'window.frame.unfocusColor')
195 for n in need:
196 i, nul, nul = find_key(data, n)
197 if i < 0:
198 out('*** ERROR *** The ' + n + ' value was not found in the '
199 'theme, but it is required to be set.\n')
200 global valid
201 valid = False
202
203
204 def usage():
205 out('Usage: themupdate.py /path/to/themerc > newthemerc\n\n')
206 sys.exit()
207
208 try:
209 file = open(sys.argv[1])
210 except IndexError:
211 usage()
212 except IOError:
213 out('Unable to open file "' + sys.argv[1] + '"\n\n')
214 usage()
215
216 data = file.readlines()
217 for i in range(len(data)):
218 data[i] = data[i].strip()
219
220 simple_replace(data)
221 remove(data)
222 pressed(data)
223 x_fonts(data)
224 xft_fonts(data)
225 pixelsize(data)
226 warn_missing(data)
227 err_missing(data)
228
229 for l in data:
230 print l
231
232 sys.exit(not valid)
This page took 0.048231 seconds and 3 git commands to generate.