view makeitso/main.py @ 1:c2f8464e0395

use correct method
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 03 Nov 2010 08:35:16 -0700
parents 7a76836b50a7
children 825922315ce6
line wrap: on
line source

#!/usr/bin/env python
"""
filesystem template interpreter
"""

import os
import subprocess
import sys

from optparse import OptionParser
from tempita import Template

def call(command, *args, **kw):
    code = subprocess.call(command, *args, **kw)
    if code:
        if isinstance(command, basestring):
            cmdstr = command
        else:
            cmdstr = ' '.join(command)
        raise SystemExit("Command `%s` exited with code %d" % (cmdstr, code))

def template_variables(template):
    """return the variables needed for a template"""
    raise NotImplementedError

def main(args=sys.argv[1:]):

    # create option parser
    usage = '%prog [options]'
    parser = OptionParser(usage, description=__doc__)
    parser.add_option('--variables', dest='variables',
                      help='print the variables in a template')
    options, args = parser.parse_args(args)

    # template variables
    variables = {}
    _vars = []
    _args = []
    for arg in args:
        if '=' in arg:
            key, value = arg.split('=')
            variables[key] = value
        else:
            _args.append(arg)
    args = _args

    # get the content
    content = sys.stdin.read()
    template = Template(content)
    print template.substitute(**variables)
        

if __name__ == '__main__':
    main()