view makeitso/template.py @ 42:73dac34d2692

more stubbing of API class; first get something off the ground; then rewrite
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 04 Jan 2011 07:38:49 -0800
parents 9956e13558dd
children 6e08cca7d656
line wrap: on
line source

"""
basic API template class
"""

import os
import sys
from makeitso import ContentTemplate
from makeitso import PolyTemplate

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(ContentTemplate):
    """API template for MakeItSo"""

    # name of the template
    name = ''

    # templates to interpolate
    # paths are relative to __file__ unless absolute or URIs
    templates = []

    # variables
    vars = []

    # inspect the templates for more variables
    look = False

    def __init__(self, output=None, interactive=True, **variables):
        assert self.templates
        self.output = output
        self.interactive = interactive
        self.location = os.path.dirnme(os.path.abspath(__file__))
        self.defaults = variables.copy

        # ensure all of these templates exist
        for template in self.templates:
            if template.startswith('http://') or template.startswith('https://'):
                continue
            if os.path.isabs(template):
                path = template
            else:
                path = os.path.join(self.location, template)
            assert os.path.exists(template)

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

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

class PasteScriptTemplate(MakeItSoTemplate):
    """template for backwards compatability with PasteScript"""