7690c6812777eb5762b2f8b043800f18504ff2fd
[dana/openbox.git] / tools / themeupdate / themeupdate.py
1 #! /usr/bin/python
2
3 # themeupdate.py for the Openbox window manager
4 # Copyright (c) 2003        Ben Jansens
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # See the COPYING file for a copy of the GNU General Public License.
17
18 import sys
19
20 data = []
21 valid = True
22
23 def out(str):
24     sys.stderr.write(str)
25     sys.stderr.flush()
26
27 def read_bool():
28     while True:
29         inp = sys.stdin.readline(1).strip()
30         if inp == 'y' or inp == '': return True
31         if inp == 'n': return False
32
33 def getkeyval(line):
34     key = line[:line.find(':')].strip()
35     value = line[line.find(':') + 1:].strip()
36     if not (key and value):
37         key = value = None
38     return key, value
39
40 def find_key(data, keysubstr, exact = False):
41     i = 0
42     n = len(data)
43     while i < n:
44         l = data[i]
45         key, value = getkeyval(l)
46         if key and value:
47             if (exact and key == keysubstr) or \
48                (not exact and key.find(keysubstr) != -1):
49                 return i, key, value
50         i += 1
51     return -1, None, None
52
53 def simple_replace(data):
54     pairs = {}
55     pairs['.picColor'] = '.imageColor'
56     pairs['menu.frame'] = 'menu.items'
57     pairs['menu.hilite'] = 'menu.selected'
58     pairs['borderColor'] = 'border.color'
59     pairs['imageColor'] = 'image.color'
60     pairs['textColor'] = 'text.color'
61     pairs['interlaceColor'] = 'interlace.color'
62
63     for k in pairs.keys():
64         while 1:
65             i, key, nul = find_key(data, k);
66             if i >= 0:
67                 newl = data[i].replace(k, pairs[k])
68                 out('Updating "' + key +
69                     '" to "' + key.replace(k, pairs[k]) + '"\n')
70                 data[i] = newl
71             else:
72                 break
73
74     pairs = {}
75     pairs['window.focus.font'] = 'window.active.label.text.font'
76     pairs['window.unfocus.font'] = 'window.inactive.label.text.font'
77     pairs['window.justify'] = 'window.label.justify'
78     pairs['menu.frame.disableColor'] = 'menu.disabled.textColor'
79     pairs['window.label.focus.font'] = 'window.active.label.text.font'
80     pairs['window.label.unfocus.font'] = 'window.inactive.label.text.font'
81     pairs['window.label.justify'] = 'window.label.text.justify'
82     pairs['menu.title.font'] = 'menu.title.text.font'
83     pairs['menu.title.justify'] = 'menu.title.text.justify'
84     pairs['menuOverlap'] = 'menu.overlap'
85     pairs['handleWidth'] = 'window.handle.width'
86     pairs['borderWidth'] = 'border.width'
87     pairs['bevelWidth'] = 'padding.width'
88     pairs['frameWidth'] = 'window.client.padding.width'
89     pairs['window.frame.focusColor'] = 'window.active.client.color'
90     pairs['window.frame.unfocusColor'] = 'window.inactive.client.color'
91     pairs['window.title.focus'] = 'window.active.title.bg'
92     pairs['window.title.unfocus'] = 'window.inactive.title.bg'
93     pairs['window.label.focus'] = 'window.active.label.bg'
94     pairs['window.label.unfocus'] = 'window.inactive.label.bg'
95     pairs['window.handle.focus'] = 'window.active.handle.bg'
96     pairs['window.handle.unfocus'] = 'window.inactive.handle.bg'
97     pairs['window.grip.focus'] = 'window.active.grip.bg'
98     pairs['window.grip.unfocus'] = 'window.inactive.grip.bg'
99     pairs['menu.items'] = 'menu.items.bg'
100     pairs['menu.title'] = 'menu.title.bg'
101     pairs['menu.selected'] = 'menu.items.active.bg'
102     pairs['window.title.focus'] = 'window.active.title.bg'
103     pairs['window.label.focus'] = 'window.active.label.bg'
104     pairs['window.title.unfocus'] = 'window.inactive.title.bg'
105     pairs['window.label.unfocus'] = 'window.inactive.label.bg'
106     pairs['window.button.disabled.focus'] = 'window.active.button.disabled.bg'
107     pairs['window.button.disabled.unfocus'] = \
108         'window.inactive.button.disabled.bg'
109     pairs['window.button.pressed.focus'] = 'window.active.button.pressed.bg'
110     pairs['window.button.pressed.unfocus'] = \
111         'window.inactive.button.pressed.bg'
112     pairs['window.button.toggled.focus'] = 'window.active.button.toggled.bg'
113     pairs['window.button.toggled.unfocus'] = \
114         'window.inactive.button.toggled.bg'
115     pairs['window.button.focus'] = 'window.active.button.unpressed.bg'
116     pairs['window.button.unfocus'] = 'window.inactive.button.unpressed.bg'
117     pairs['window.button.hover.focus'] = 'window.active.button.hover.bg'
118     pairs['window.button.hover.unfocus'] = 'window.inactive.button.hover.bg'
119
120     for k in pairs.keys():
121         while 1:
122             i, key, nul = find_key(data, k, True);
123             if i >= 0:
124                 newl = data[i].replace(k, pairs[k])
125                 out('Updating "' + key +
126                     '" to "' + key.replace(k, pairs[k]) + '"\n')
127                 data[i] = newl
128             else:
129                 break
130
131     pairs = {}
132     pairs['window.title.focus'] = 'window.active.title'
133     pairs['window.title.unfocus'] = 'window.inactive.title'
134     pairs['window.label.focus'] = 'window.active.label'
135     pairs['window.label.unfocus'] = 'window.inactive.label'
136     pairs['window.handle.focus'] = 'window.active.handle'
137     pairs['window.handle.unfocus'] = 'window.inactive.handle'
138     pairs['window.grip.focus'] = 'window.active.grip'
139     pairs['window.grip.unfocus'] = 'window.inactive.grip'
140     pairs['menu.selected'] = 'menu.items.active'
141     pairs['window.title.focus'] = 'window.active.title'
142     pairs['window.label.focus'] = 'window.active.label'
143     pairs['window.title.unfocus'] = 'window.inactive.title'
144     pairs['window.label.unfocus'] = 'window.inactive.label'
145     pairs['window.button.disabled.focus'] = 'window.active.button.disabled'
146     pairs['window.button.disabled.unfocus'] = \
147         'window.inactive.button.disabled'
148     pairs['window.button.pressed.focus'] = 'window.active.button.pressed'
149     pairs['window.button.pressed.unfocus'] = \
150         'window.inactive.button.pressed'
151     pairs['window.button.toggled.focus'] = 'window.active.button.toggled'
152     pairs['window.button.toggled.unfocus'] = \
153         'window.inactive.button.toggled'
154     pairs['window.button.focus'] = 'window.active.button'
155     pairs['window.button.unfocus'] = 'window.inactive.button'
156     pairs['window.button.hover.focus'] = 'window.active.button.hover'
157     pairs['window.button.hover.unfocus'] = 'window.inactive.button.hover'
158     pairs['window.label.unfocus'] = 'window.inactive.label'
159     pairs['window.label.focus'] = 'window.active.label'
160     pairs['window.button.focus'] = 'window.active.button.unpressed'
161     pairs['menu.disabled'] = 'menu.items.disabled'
162     pairs['menu.selected'] = 'menu.items.active'
163     pairs['window.button.unfocus'] = 'window.inactive.button.unpressed'
164     pairs['window.button.pressed.focus'] = 'window.active.button.pressed'
165     pairs['window.button.pressed.unfocus'] = 'window.inactive.button.pressed'
166     pairs['window.button.disabled.focus'] = 'window.active.button.disabled'
167     pairs['window.button.disabled.unfocus'] = 'window.inactive.button.disabled'
168     pairs['window.button.hover.focus'] = 'window.active.button.hover'
169     pairs['window.button.hover.unfocus'] = 'window.inactive.button.hover'
170     pairs['window.button.toggled.focus'] = 'window.active.button.toggled'
171     pairs['window.button.toggled.unfocus'] = 'window.inactive.button.toggled'
172
173     for k in pairs.keys():
174         while 1:
175             i, key, nul = find_key(data, k);
176             if i >= 0:
177                 newl = data[i].replace(k, pairs[k])
178                 out('Updating "' + key +
179                     '" to "' + key.replace(k, pairs[k]) + '"\n')
180                 data[i] = newl
181             else:
182                 break
183
184 def replace_colors(data):
185     i = 0
186     n = len(data)
187     while i < n:
188         l = data[i]
189         key, value = getkeyval(l)
190         if key and value:
191             if key.find('.color') != -1:
192                 if key.find('client.color') == -1 \
193                        and key.find('image.color') == -1 \
194                        and key.find('bg.color') == -1 \
195                        and key.find('border.color') == -1 \
196                        and key.find('interlace.color') == -1 \
197                        and key.find('text.color') == -1:
198                     newl = data[i].replace('.color', '.bg.color')
199                     out('Updating "' + key +
200                         '" to "' + key.replace('.color', '.bg.color') + '"\n')
201                     data[i] = newl
202             if key.find('.border.color') != -1 \
203                and key.find('bg.border.color') == -1:
204                 newl = data[i].replace('.border.color', '.bg.border.color')
205                 out('Updating "' + key +
206                     '" to "' + key.replace('.border.color',
207                                            '.bg.border.color') + '"\n')
208                 data[i] = newl
209             if key.find('.interlace.color') != -1 \
210                and key.find('bg.interlace.color') == -1:
211                 newl = data[i].replace('.interlace.color',
212                                        '.bg.interlace.color')
213                 out('Updating "' + key +
214                     '" to "' + key.replace('.interlace.color',
215                                            '.bg.interlace.color') + '"\n')
216                 data[i] = newl
217         i += 1
218
219 def remove(data):
220     invalid = []
221     invalid.append('toolbar')
222     for inv in invalid:
223         while 1:
224             i, key, nul = find_key(data, inv)
225             if i >= 0:
226                 out(key + ' is no longer supported.\nRemove (Y/n)? ')
227                 if read_bool():
228                     out('Removing "' + key + '"\n')
229                     data.pop(i)
230             else:
231                 break
232     invalid.append('rootCommand')
233     invalid.append('menu.bullet')
234     invalid.append('menu.bullet.image.color')
235     invalid.append('menu.bullet.selected.image.color')
236     invalid.append('menu.frame.justify')
237     for inv in invalid:
238         while 1:
239             i, key, nul = find_key(data, inv, True)
240             if i >= 0:
241                 out(key + ' is no longer supported.\nRemove (Y/n)? ')
242                 if read_bool():
243                     out('Removing "' + key + '"\n')
244                     data.pop(i)
245             else:
246                 break
247
248 def pressed(data):
249     i, nul, nul = find_key(data, 'window.button.pressed', True)
250     if i >= 0:
251         out('The window.button.pressed option has been replaced by ' +
252             'window.button.pressed.focus and ' +
253             'window.button.pressed.unfocus.\nUpdate (Y/n)? ')
254         if read_bool():
255             l = data[i]
256             out('Removing "window.button.pressed"\n')
257             data.pop(i)
258             out('Adding "window.button.pressed.unfocus"\n')
259             data.insert(i, l.replace('window.button.pressed',
260                                              'window.button.pressed.unfocus'))
261             out('Adding "window.button.pressed.focus"\n')
262             data.insert(i, l.replace('window.button.pressed',
263                                      'window.button.pressed.focus'))
264
265 def x_fonts(data):
266     i, nul, nul = find_key(data, 'window.font')
267     if i >= 0:
268         out('You appear to specify fonts using the old X fonts ' +
269             'syntax.\nShall I remove all fonts from the theme (Y/n)? ')
270         if not read_bool():
271             return
272     else: return
273     while 1:
274         i, key = find_key(data, '.font')
275         if i < 0:
276             break
277         out('Removing "' + key + '"\n')
278         data.pop(i)
279
280 def xft_fonts(data):
281     i, nul, nul = find_key(data, '.xft.')
282     if i >= 0:
283         out('You appear to specify fonts using the old Xft fonts ' +
284             'syntax.\nShall I update these to the new syntax (Y/n)? ')
285         if not read_bool():
286             return
287     else: return
288     fonts = {}
289     fonts['window'] = 'window.label.focus.font'
290     fonts['menu.items'] = 'menu.items.font'
291     fonts['menu.title'] = 'menu.title.font'
292     for f in fonts.keys():
293         li, nul, flags = find_key(data, f + '.xft.flags')
294         if li < 0:
295             li, nul, flags = find_key(data, '*.xft.flags')
296         else:
297             out('Removing ' + f + '.xft.flags\n')
298             data.pop(li)
299         oi, nul, offset = find_key(data, f + '.xft.shadow.offset')
300         if oi < 0:
301             oi, nul, offset = find_key(data, '*.xft.shadow.offset')
302         else:
303             out('Removing ' + f + '.xft.shadow.offset\n')
304             data.pop(oi)
305         ti, nul, tint = find_key(data, f + '.xft.shadow.tint')
306         if ti < 0:
307             ti, nul, tint = find_key(data, '*.xft.shadow.tint')
308         else:
309             out('Removing ' + f + '.xft.shadow.tint\n')
310             data.pop(ti)
311         fi, nul, face = find_key(data, f + '.xft.font')
312         if fi < 0:
313             fi, nul, face = find_key(data, '*.xft.font')
314             if fi >= 0: fi = len(data) - 1
315         else:
316             out('Removing ' + f + '.xft.font\n')
317             data.pop(fi)
318
319         if fi >= 0:
320             s = face
321             if li >= 0:
322                 if flags.find('bold'):
323                     s = s + ':bold'
324                 if flags.find('shadow'):
325                     s = s + ':shadow=y'
326             if oi >= 0:
327                 s = s + ':shadowoffset=' + offset
328             if ti >= 0:
329                 s = s + ':shadowtint=' + tint
330         out('Adding ' + fonts[f] + '\n')
331         data.insert(fi, fonts[f] + ': ' + s)
332
333     for stars in ('*.xft.flags', '*.xft.shadow.offset' ,
334                   '*.xft.shadow.tint', '*.xft.font'):
335         i, key, nul = find_key(data, stars)
336         if i >= 0:
337             out('Removing ' + key + '\n')
338             data.pop(i)
339
340 def pixelsize(data):
341     fonts = ('window.label.focus.font',
342              'menu.items.font',
343              'menu.title.font')
344     for f in fonts:
345         i, key, value = find_key(data, f, True)
346         if value:
347             if value.find('pixelsize') == -1:
348                 out('*** ERROR *** The ' + key + ' font size is not being '
349                     'specified by pixelsize. It is recommended that you use '
350                     'pixelsize instead of pointsize for specifying theme '
351                     'fonts. e.g. "sans:pixelsize=12"\n')
352                 global valid
353                 valid = False
354
355 def warn_missing(data):
356     need = ('window.active.button.hover',  'window.inactive.button.hover',
357             'menu.overlap')
358     for n in need:
359         i, nul, nul = find_key(data, n)
360         if i < 0:
361             out('The ' + n + ' value was not found in the theme, but it '
362                 'can optionally be set.\n')
363
364 def err_missing(data):
365     need = ('window.active.button.disabled',
366             'window.inactive.button.disabled',
367             'window.active.client.color',
368             'window.inactive.client.color')
369     for n in need:
370         i, nul, nul = find_key(data, n)
371         if i < 0:
372             out('*** ERROR *** The ' + n + ' value was not found in the '
373                 'theme, but it is required to be set.\n')
374             global valid
375             valid = False
376
377
378 def usage():
379     out('Usage: themupdate.py /path/to/themerc > newthemerc\n\n')
380     sys.exit()
381
382 try:
383     file = open(sys.argv[1])
384 except IndexError:
385     usage()
386 except IOError:
387     out('Unable to open file "' + sys.argv[1] + '"\n\n')
388     usage()
389
390 data = file.readlines()
391 for i in range(len(data)):
392     data[i] = data[i].strip()
393
394 simple_replace(data)
395 replace_colors(data)
396 remove(data)
397 pressed(data)
398 x_fonts(data)
399 xft_fonts(data)
400 pixelsize(data)
401 warn_missing(data)
402 err_missing(data)
403
404 for l in data:
405     print l
406
407 sys.exit(not valid)