Mercurial > hg > MakeItSo
changeset 103:a6aff990985b
do rudimentary interpretation of config files
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 11 Jan 2011 19:12:22 -0800 |
parents | ad5fd3eb6674 |
children | e059a58ea23c |
files | makeitso/makeitso.py |
diffstat | 1 files changed, 42 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/makeitso/makeitso.py Tue Jan 11 16:30:52 2011 -0800 +++ b/makeitso/makeitso.py Tue Jan 11 19:12:22 2011 -0800 @@ -14,6 +14,7 @@ import urllib # TODO: may have to use urllib2.urlopen to get reasonable timeouts +from ConfigParser import RawConfigParser from optparse import OptionParser # URL of -this file- @@ -372,6 +373,18 @@ ### command line interface +def read_config(config_files, templates=()): + """read variables from a set of .ini files""" + retval = {} + parser = RawConfigParser() + parser.read(config_files) + retval.update(dict(parser.items('DEFAULT'))) + for template in templates: + if template in parser.sections(): + retval.update(dict(parser.items(template))) + + return retval + def invocation(url, **variables): """returns a string appropriate for TTW invocation""" variables_string = ' '.join(['%s=%s' % (i,j) for i,j in variables.items()]) @@ -383,6 +396,13 @@ usage = '%prog [options] template <template> <...>' parser = OptionParser(usage, description=__doc__) + # find dotfile + dotfile = None + if 'HOME' in os.environ: + dotfile = os.path.join(os.environ['HOME'], '.makeitso') + if not (os.path.exists(dotfile) and os.path.isfile(dotfile)): + dotfile = None + # delimeters # XXX needs tempita trunk if has_delimeters: @@ -397,12 +417,14 @@ # TODO # options for (.ini) variables -# parser.add_option('-c', '--config', dest='config' -# default=[], action='append', -# help='.ini config files to read variables from') -# parser.add_option('--no-defaults', dest='use_defaults', -# default=True, action='store_false', -# help="don't read ~/.makeitso") # XXX should only be displayed if ~/.makeitso exists + parser.add_option('-c', '--config', dest='config', + default=[], action='append', + help='.ini config files to read variables from') + if dotfile: + parser.add_option('--no-defaults', dest='use_defaults', + default=True, action='store_false', + help="don't read ~/.makeitso") + # parser.add_option('-u', '--update', dest='update', # help="update the specified .ini file for variables read from this run") # parser.add_option('-U', '--Update', dest='Update', @@ -441,13 +463,24 @@ # template variables variables = {} _args = [] + + # read variables from configuration + config_files = options.config + if dotfile and options.use_defaults: + config_files.insert(0, dotfile) + if config_files: + variables.update(read_config(config_files)) + + # override with variables from the command line + _variables = {} for arg in args: if '=' in arg: key, value = arg.split('=') - variables[key] = value + _variables[key] = value else: _args.append(arg) args = _args + variables.update(_variables) # print TTW commandline for invocation if options.commandline: @@ -458,11 +491,12 @@ print invocation('[URI]', **variables) return + # get the content if args: template = PolyTemplate(templates=args, variables=variables) - template.substitute(output=options.output) + template.substitute({}, output=options.output) else: template = ContentTemplate(sys.stdin.read(), variables=variables) print template.substitute()