view makeitso/cli.py @ 105:67b50417b8d1

stub out loading api templates; this wont work, however, since one is in the middle of a module trying to load a module that depends on the module you are in
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 11 Jan 2011 19:23:53 -0800
parents 37f92ae8f999
children 9c70669854f4
line wrap: on
line source

"""
command line parser for MakeItSo
"""

from template import Undefined
from optparse import OptionParser

class MakeItSoCLI(object):
  """command line interface to a makeitso template"""
  
  def __init__(self, template_class):
    self.template_class = template_class

  def parser(self):
    """
    return a command line parser for the template
    """
    usage = getattr(self, 'usage', '%prog [options] output')
    description = getattr(self.template_class, 'description', None)
    parser = OptionParser(usage=usage, description=description)

    # add the variables as options
    for variable in self.template_class.vars:
      description = variable.description
      if (variable.default is not None) and (variable.default is not Undefined) and description is not None:
        description += ' [DEFAULT: %s]' % variable.default
      parser.add_option('--%s' % variable.name, dest=variable.name,
                        default=variable.default,
                        help=description)
    return parser

  def get_variables(self, options):
    """
    return variables from (parsed) options
    """
    return dict([(key, value)
                 for key, value in options.__dict__.items()
                 if not key.startswith('_')])

  def parse(self, args=None, parser=None, options=None):

    # parse the command line
    if not parser or not options:
      parser = self.parser()
      options, args = parser.parse_args(args=args)

    # ensure output is given
    if len(args) != 1:
      parser.error("Please specify a single output destination")

    # template variables
    variables = self.get_variables(options)
    
    # return the variables and the output
    return variables, args[0]
    
  def __call__(self, *args):
    variables, output = self.parse(list(args))
    template = self.template_class(variables=variables)
    template.substitute({}, output=output)