some stuff is exact
[dana/openbox.git] / 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     for inv in invalid:
63         while 1:
64             i, key, nul = find_key(data, inv)
65             if i >= 0:
66                 out(key + ' is no longer supported.\nRemove (Y/n)? ')
67                 if read_bool():
68                     out('Removing "' + key + '"\n')
69                     data.pop(i)
70             else:
71                 break
72     invalid.append('rootCommand')
73     invalid.append('menu.bullet')
74     invalid.append('menu.frame.justify')
75     for inv in invalid:
76         while 1:
77             i, key, nul = find_key(data, inv, True)
78             if i >= 0:
79                 out(key + ' is no longer supported.\nRemove (Y/n)? ')
80                 if read_bool():
81                     out('Removing "' + key + '"\n')
82                     data.pop(i)
83             else:
84                 break
85
86 def pressed(data):
87     i, nul, nul = find_key(data, 'window.button.pressed', True)
88     if i >= 0:
89         out('The window.button.pressed option has been replaced by ' +
90             'window.button.pressed.focus and ' +
91             'window.button.pressed.unfocus.\nUpdate (Y/n)? ')
92         if read_bool():
93             l = data[i]
94             out('Removing "window.button.pressed"\n')
95             data.pop(i)
96             out('Adding "window.button.pressed.unfocus"\n')
97             data.insert(i, l.replace('window.button.pressed',
98                                              'window.button.pressed.unfocus'))
99             out('Adding "window.button.pressed.focus"\n')
100             data.insert(i, l.replace('window.button.pressed',
101                                      'window.button.pressed.focus'))
102
103 def x_fonts(data):
104     i, nul, nul = find_key(data, 'window.font')
105     if i >= 0:
106         out('You appear to specify fonts using the old X fonts ' +
107             'syntax.\nShall I remove all fonts from the theme (Y/n)? ')
108         if not read_bool():
109             return
110     else: return
111     while 1:
112         i, key = key_find(data, '.font')
113         if i < 0:
114             break
115         out('Removing "' + key + '"\n')
116         data.pop(i)
117
118 def xft_fonts(data):
119     i, nul, nul = find_key(data, '.xft.')
120     if i >= 0:
121         out('You appear to specify fonts using the old Xft fonts ' +
122             'syntax.\nShall I update these to the new syntax (Y/n)? ')
123         if not read_bool():
124             return
125     else: return
126     fonts = {}
127     fonts['window'] = 'window.label.focus.font'
128     fonts['menu.items'] = 'menu.items.font'
129     fonts['menu.title'] = 'menu.title.font'
130     for f in fonts.keys():
131         li, nul, flags = find_key(data, f + '.xft.flags')
132         if li < 0:
133             li, nul, flags = find_key(data, '*.xft.flags')
134         else:
135             out('Removing ' + f + '.xft.flags\n')
136             data.pop(li)
137         oi, nul, offset = find_key(data, f + '.xft.shadow.offset')
138         if oi < 0:
139             oi, nul, offset = find_key(data, '*.xft.shadow.offset')
140         else:
141             out('Removing ' + f + '.xft.shadow.offset\n')
142             data.pop(oi)
143         ti, nul, tint = find_key(data, f + '.xft.shadow.tint')
144         if ti < 0:
145             ti, nul, tint = find_key(data, '*.xft.shadow.tint')
146         else:
147             out('Removing ' + f + '.xft.shadow.tint\n')
148             data.pop(ti)
149         fi, nul, face = find_key(data, f + '.xft.font')
150         if fi < 0:
151             fi, nul, face = find_key(data, '*.xft.font')
152             if fi >= 0: fi = len(data) - 1
153         else:
154             out('Removing ' + f + '.xft.font\n')
155             data.pop(fi)
156
157         if fi >= 0:
158             s = face
159             if li >= 0:
160                 if flags.find('bold'):
161                     s = s + ':bold'
162                 if flags.find('shadow'):
163                     s = s + ':shadow=y'
164             if oi >= 0:
165                 s = s + ':shadowoffset=' + offset
166             if ti >= 0:
167                 s = s + ':shadowtint=' + tint
168         out('Adding ' + fonts[f] + '\n')
169         data.insert(fi, fonts[f] + ': ' + s)
170
171     for stars in ('*.xft.flags', '*.xft.shadow.offset' ,
172                   '*.xft.shadow.tint', '*.xft.font'):
173         i, key, nul = find_key(data, stars)
174         if i >= 0:
175             out('Removing ' + key + '\n')
176             data.pop(i)
177
178 def pixelsize(data):
179     fonts = ('window.label.focus.font',
180              'menu.items.font',
181              'menu.title.font')
182     for f in fonts:
183         i, key, value = find_key(data, f, True)
184         if value:
185             if value.find('pixelsize') == -1:
186                 out('*** ERROR *** The ' + key + ' font size is not being '
187                     'specified by pixelsize. It is recommended that you use '
188                     'pixelsize instead of pointsize for specifying theme '
189                     'fonts. e.g. "sans:pixelsize=12"\n')
190                 global valid
191                 valid = False
192
193 def warn_missing(data):
194     need = ('window.button.hover.focus',  'window.button.hover.unfocus',
195             'menuOverlap')
196     for n in need:
197         i, nul, nul = find_key(data, n)
198         if i < 0:
199             out('The ' + n + ' value was not found in the theme, but it '
200                 'can optionally be set.\n')
201
202 def err_missing(data):
203     need = ('window.button.disabled.focus',  'window.button.disabled.unfocus',
204             'window.frame.focusColor', 'window.frame.unfocusColor')
205     for n in need:
206         i, nul, nul = find_key(data, n)
207         if i < 0:
208             out('*** ERROR *** The ' + n + ' value was not found in the '
209                 'theme, but it is required to be set.\n')
210             global valid
211             valid = False
212
213
214 def usage():
215     out('Usage: themupdate.py /path/to/themerc > newthemerc\n\n')
216     sys.exit()
217
218 try:
219     file = open(sys.argv[1])
220 except IndexError:
221     usage()
222 except IOError:
223     out('Unable to open file "' + sys.argv[1] + '"\n\n')
224     usage()
225
226 data = file.readlines()
227 for i in range(len(data)):
228     data[i] = data[i].strip()
229
230 simple_replace(data)
231 remove(data)
232 pressed(data)
233 x_fonts(data)
234 xft_fonts(data)
235 pixelsize(data)
236 warn_missing(data)
237 err_missing(data)
238
239 for l in data:
240     print l
241
242 sys.exit(not valid)