comparison makeitso/makeitso.py @ 103:a6aff990985b

do rudimentary interpretation of config files
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 11 Jan 2011 19:12:22 -0800
parents 80a57bf2b7f4
children e059a58ea23c
comparison
equal deleted inserted replaced
102:ad5fd3eb6674 103:a6aff990985b
12 import sys 12 import sys
13 import tempfile 13 import tempfile
14 import urllib 14 import urllib
15 # TODO: may have to use urllib2.urlopen to get reasonable timeouts 15 # TODO: may have to use urllib2.urlopen to get reasonable timeouts
16 16
17 from ConfigParser import RawConfigParser
17 from optparse import OptionParser 18 from optparse import OptionParser
18 19
19 # URL of -this file- 20 # URL of -this file-
20 location = 'http://k0s.org/mozilla/hg/MakeItSo/raw-file/tip/makeitso/makeitso.py' 21 location = 'http://k0s.org/mozilla/hg/MakeItSo/raw-file/tip/makeitso/makeitso.py'
21 22
370 for template in self.templates: 371 for template in self.templates:
371 template.substitute(vars, output) 372 template.substitute(vars, output)
372 373
373 ### command line interface 374 ### command line interface
374 375
376 def read_config(config_files, templates=()):
377 """read variables from a set of .ini files"""
378 retval = {}
379 parser = RawConfigParser()
380 parser.read(config_files)
381 retval.update(dict(parser.items('DEFAULT')))
382 for template in templates:
383 if template in parser.sections():
384 retval.update(dict(parser.items(template)))
385
386 return retval
387
375 def invocation(url, **variables): 388 def invocation(url, **variables):
376 """returns a string appropriate for TTW invocation""" 389 """returns a string appropriate for TTW invocation"""
377 variables_string = ' '.join(['%s=%s' % (i,j) for i,j in variables.items()]) 390 variables_string = ' '.join(['%s=%s' % (i,j) for i,j in variables.items()])
378 return 'python <(curl %s) %s %s' % (location, url, variables_string) 391 return 'python <(curl %s) %s %s' % (location, url, variables_string)
379 392
380 def main(args=sys.argv[1:]): 393 def main(args=sys.argv[1:]):
381 394
382 # create option parser 395 # create option parser
383 usage = '%prog [options] template <template> <...>' 396 usage = '%prog [options] template <template> <...>'
384 parser = OptionParser(usage, description=__doc__) 397 parser = OptionParser(usage, description=__doc__)
398
399 # find dotfile
400 dotfile = None
401 if 'HOME' in os.environ:
402 dotfile = os.path.join(os.environ['HOME'], '.makeitso')
403 if not (os.path.exists(dotfile) and os.path.isfile(dotfile)):
404 dotfile = None
385 405
386 # delimeters 406 # delimeters
387 # XXX needs tempita trunk 407 # XXX needs tempita trunk
388 if has_delimeters: 408 if has_delimeters:
389 parser.add_option('-[', '--start-braces', dest='start_braces', 409 parser.add_option('-[', '--start-braces', dest='start_braces',
395 parser.add_option('-o', '--output', dest='output', 415 parser.add_option('-o', '--output', dest='output',
396 help='where to put the output (filename or directory)') 416 help='where to put the output (filename or directory)')
397 417
398 # TODO 418 # TODO
399 # options for (.ini) variables 419 # options for (.ini) variables
400 # parser.add_option('-c', '--config', dest='config' 420 parser.add_option('-c', '--config', dest='config',
401 # default=[], action='append', 421 default=[], action='append',
402 # help='.ini config files to read variables from') 422 help='.ini config files to read variables from')
403 # parser.add_option('--no-defaults', dest='use_defaults', 423 if dotfile:
404 # default=True, action='store_false', 424 parser.add_option('--no-defaults', dest='use_defaults',
405 # help="don't read ~/.makeitso") # XXX should only be displayed if ~/.makeitso exists 425 default=True, action='store_false',
426 help="don't read ~/.makeitso")
427
406 # parser.add_option('-u', '--update', dest='update', 428 # parser.add_option('-u', '--update', dest='update',
407 # help="update the specified .ini file for variables read from this run") 429 # help="update the specified .ini file for variables read from this run")
408 # parser.add_option('-U', '--Update', dest='Update', 430 # parser.add_option('-U', '--Update', dest='Update',
409 # help="same as -u, but update the global [DEFAULTS] section") 431 # help="same as -u, but update the global [DEFAULTS] section")
410 # parser.add_option('--dry-run', dest='dry_run', 432 # parser.add_option('--dry-run', dest='dry_run',
439 return 461 return
440 462
441 # template variables 463 # template variables
442 variables = {} 464 variables = {}
443 _args = [] 465 _args = []
466
467 # read variables from configuration
468 config_files = options.config
469 if dotfile and options.use_defaults:
470 config_files.insert(0, dotfile)
471 if config_files:
472 variables.update(read_config(config_files))
473
474 # override with variables from the command line
475 _variables = {}
444 for arg in args: 476 for arg in args:
445 if '=' in arg: 477 if '=' in arg:
446 key, value = arg.split('=') 478 key, value = arg.split('=')
447 variables[key] = value 479 _variables[key] = value
448 else: 480 else:
449 _args.append(arg) 481 _args.append(arg)
450 args = _args 482 args = _args
483 variables.update(_variables)
451 484
452 # print TTW commandline for invocation 485 # print TTW commandline for invocation
453 if options.commandline: 486 if options.commandline:
454 if args: 487 if args:
455 for arg in args: 488 for arg in args:
456 print invocation(arg, **variables) 489 print invocation(arg, **variables)
457 else: 490 else:
458 print invocation('[URI]', **variables) 491 print invocation('[URI]', **variables)
459 return 492 return
460 493
494
461 # get the content 495 # get the content
462 if args: 496 if args:
463 template = PolyTemplate(templates=args, 497 template = PolyTemplate(templates=args,
464 variables=variables) 498 variables=variables)
465 template.substitute(output=options.output) 499 template.substitute({}, output=options.output)
466 else: 500 else:
467 template = ContentTemplate(sys.stdin.read(), variables=variables) 501 template = ContentTemplate(sys.stdin.read(), variables=variables)
468 print template.substitute() 502 print template.substitute()
469 503
470 # cleanup 504 # cleanup