view makeitso/template.py @ 41:9956e13558dd

stub out what API templates will look like; put these in a separate file as theres no reason to clutter up the command line entry point any further
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 03 Jan 2011 14:18:59 -0800
parents
children 73dac34d2692
line wrap: on
line source

"""
basic API template class
"""

import sys
from makeitso import ContentTemplate

class Variable(object):
    """variable object for MakeItSo templates"""
    
    def __init__(self, name, default=None, description=None,
                 cast=None):
        self.name = name
        self.default = default
        self.description = description

        # TODO (maybe): get cast from default variable type if not None
        self.cast = cast

        self._set = False

    def set(self, value):
        if self.cast:
            self.value = self.cast(value)
        else:
            self.value = value
        self._set = True

    def read(self, fd=sys.stdout):
        """prompt and read the variable from stdin"""
        fd.write(self.display())
        self.set(raw_input())

    def display(self):
        description = self.description or self.name
        if self.default:
            return 'Enter %s [DEFAULT: %s]:' % (description, repr(self.default))
        else:
            return 'Enter %s:' % description

class MakeItSoTemplate(object):

    def pre(self, **variables):
        """do stuff before interpolation"""

    def post(self, **variables):
        """do stuff after interpolation"""