Rev 545 | Rev 549 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 545 | Rev 547 | ||
---|---|---|---|
Line 173... | Line 173... | ||
173 | if res: |
173 | if res: |
174 | print data |
174 | print data |
175 | raise EOFError |
175 | raise EOFError |
176 | return data |
176 | return data |
177 | 177 | ||
178 | def read_defaults(fname): |
178 | def read_defaults(fname,defaults): |
179 | defaults = {} |
- | |
180 | f = file(fname,'r') |
179 | f = file(fname,'r') |
181 | for line in f: |
180 | for line in f: |
182 | res = re.match(r'^([^#]\w*)\s*=\s*(.*?)\s*$', line) |
181 | res = re.match(r'^(?:#!# )?([^#]\w*)\s*=\s*(.*?)\s*$', line) |
183 | if res: |
182 | if res: |
184 | defaults[res.group(1)] = res.group(2) |
183 | defaults[res.group(1)] = res.group(2) |
185 | f.close() |
184 | f.close() |
- | 185 | ||
- | 186 | def check_condition(text, defaults): |
|
- | 187 | result = False |
|
- | 188 | conds = text.split('|') |
|
- | 189 | for cond in conds: |
|
- | 190 | condname,condval = cond.split('=') |
|
- | 191 | if not defaults.has_key(condname): |
|
- | 192 | raise RuntimeError("Condition var %s does not exist: %s" % \ |
|
- | 193 | (condname,line)) |
|
- | 194 | # None means wildcard |
|
- | 195 | if defaults[condname] is None: |
|
- | 196 | return True |
|
- | 197 | if condval == defaults[condname]: |
|
- | 198 | return True |
|
186 | return defaults |
199 | return False |
187 | 200 | ||
188 | def parse_config(input, output, dlg, defaults={}): |
201 | def parse_config(input, output, dlg, defaults={}): |
189 | f = file(input, 'r') |
202 | f = file(input, 'r') |
190 | outf = file(output, 'w') |
203 | outf = file(output, 'w') |
191 | 204 | ||
Line 196... | Line 209... | ||
196 | comment = '' |
209 | comment = '' |
197 | default = None |
210 | default = None |
198 | choices = [] |
211 | choices = [] |
199 | for line in f: |
212 | for line in f: |
200 | if line.startswith('!'): |
213 | if line.startswith('!'): |
201 | res = re.search(r'!\s*([^\s]+)\s*\((.*)\)\s*$', line) |
214 | res = re.search(r'!\s*(?:\[(.*?)\])?\s*([^\s]+)\s*\((.*)\)\s*$', line) |
202 | if not res: |
215 | if not res: |
203 | raise RuntimeError("Weird line: %s" % line) |
216 | raise RuntimeError("Weird line: %s" % line) |
204 | varname = res.group(1) |
217 | varname = res.group(2) |
205 | vartype = res.group(2) |
218 | vartype = res.group(3) |
206 | 219 | ||
207 | default = defaults.get(varname,None) |
220 | default = defaults.get(varname,None) |
208 | 221 | ||
- | 222 | if res.group(1): |
|
- | 223 | if not check_condition(res.group(1), defaults): |
|
- | 224 | if default is not None: |
|
- | 225 | outf.write('#!# %s = %s\n' % (varname, default)) |
|
- | 226 | continue |
|
- | 227 | ||
209 | if vartype == 'y/n': |
228 | if vartype == 'y/n': |
210 | result = dlg.yesno(comment, default) |
229 | result = dlg.yesno(comment, default) |
211 | elif vartype == 'n/y': |
230 | elif vartype == 'n/y': |
212 | result = dlg.noyes(comment, default) |
231 | result = dlg.noyes(comment, default) |
213 | elif vartype == 'choice': |
232 | elif vartype == 'choice': |
Line 219... | Line 238... | ||
219 | break |
238 | break |
220 | result = dlg.choice(comment, choices, defopt) |
239 | result = dlg.choice(comment, choices, defopt) |
221 | else: |
240 | else: |
222 | raise RuntimeError("Bad method: %s" % vartype) |
241 | raise RuntimeError("Bad method: %s" % vartype) |
223 | outf.write('%s = %s\n' % (varname, result)) |
242 | outf.write('%s = %s\n' % (varname, result)) |
- | 243 | # Remeber the selected value |
|
- | 244 | defaults[varname] = result |
|
224 | # Clear cumulated values |
245 | # Clear cumulated values |
225 | comment = '' |
246 | comment = '' |
226 | default = None |
247 | default = None |
227 | choices = [] |
248 | choices = [] |
228 | continue |
249 | continue |
229 | 250 | ||
230 | if line.startswith('@'): |
251 | if line.startswith('@'): |
231 | res = re.match(r'@\s*"(.*?)"\s*(.*)$', line) |
252 | res = re.match(r'@\s*(?:\[(.*?)\])?\s*"(.*?)"\s*(.*)$', line) |
232 | if not res: |
253 | if not res: |
233 | raise RuntimeError("Bad line: %s" % line) |
254 | raise RuntimeError("Bad line: %s" % line) |
- | 255 | if res.group(1): |
|
- | 256 | if not check_condition(res.group(1),defaults): |
|
- | 257 | continue |
|
234 | choices.append((res.group(1), res.group(2))) |
258 | choices.append((res.group(2), res.group(3))) |
235 | continue |
259 | continue |
236 | 260 | ||
237 | outf.write(line) |
261 | outf.write(line) |
238 | if re.match(r'^#[^#]', line): |
262 | if re.match(r'^#[^#]', line): |
239 | comment = line[1:].strip() |
263 | comment = line[1:].strip() |
Line 242... | Line 266... | ||
242 | 266 | ||
243 | outf.close() |
267 | outf.close() |
244 | f.close() |
268 | f.close() |
245 | 269 | ||
246 | def main(): |
270 | def main(): |
247 | defaults = {} |
271 | defaults = {'ARCH':None} |
248 | try: |
272 | try: |
249 | dlg = Dialog() |
273 | dlg = Dialog() |
250 | except NotImplementedError: |
274 | except NotImplementedError: |
251 | dlg = NoDialog() |
275 | dlg = NoDialog() |
252 | 276 | ||
253 | # Default run will update the configuration file |
277 | # Default run will update the configuration file |
254 | # with newest options |
278 | # with newest options |
- | 279 | if len(sys.argv) >= 2: |
|
- | 280 | defaults['ARCH'] = sys.argv[1] |
|
255 | if len(sys.argv) == 2 and sys.argv[1]=='default': |
281 | if len(sys.argv) == 3 and sys.argv[2]=='default': |
256 | dlg = DefaultDialog(dlg) |
282 | dlg = DefaultDialog(dlg) |
257 | 283 | ||
258 | if os.path.exists(OUTPUT): |
284 | if os.path.exists(OUTPUT): |
259 | defaults = read_defaults(OUTPUT) |
285 | read_defaults(OUTPUT, defaults) |
260 | 286 | ||
261 | parse_config(INPUT, TMPOUTPUT, dlg, defaults) |
287 | parse_config(INPUT, TMPOUTPUT, dlg, defaults) |
262 | if os.path.exists(OUTPUT): |
288 | if os.path.exists(OUTPUT): |
263 | os.unlink(OUTPUT) |
289 | os.unlink(OUTPUT) |
264 | os.rename(TMPOUTPUT, OUTPUT) |
290 | os.rename(TMPOUTPUT, OUTPUT) |