comparison makeitso/template.py @ 44:6e08cca7d656

do API variable reading and stubbing a bit for control flow
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 04 Jan 2011 18:07:18 -0800
parents 73dac34d2692
children 728cae02a6ed
comparison
equal deleted inserted replaced
43:554f916cef13 44:6e08cca7d656
5 import os 5 import os
6 import sys 6 import sys
7 from makeitso import ContentTemplate 7 from makeitso import ContentTemplate
8 from makeitso import PolyTemplate 8 from makeitso import PolyTemplate
9 9
10 class Undefined(object):
11 """marker class for variables"""
12 Undefined = Undefined() # singleton
13
10 class Variable(object): 14 class Variable(object):
11 """variable object for MakeItSo templates""" 15 """variable object for MakeItSo templates"""
12 16
13 def __init__(self, name, default=None, description=None, 17 def __init__(self, name, default=Undefined, description=None,
14 cast=None): 18 cast=None):
15 self.name = name 19 self.name = name
16 self.default = default 20 self.default = default
17 self.description = description 21 self.description = description
18 22
54 vars = [] 58 vars = []
55 59
56 # inspect the templates for more variables 60 # inspect the templates for more variables
57 look = False 61 look = False
58 62
59 def __init__(self, output=None, interactive=True, **variables): 63 def __init__(self, output=None, interactive=True, usedefaults=False, **variables):
64 """
65 - output : output file or directory
66 - interactive : whether tointeractively get variables
67 - usedefaults : try to use the default values if not specified
68 """
69
60 assert self.templates 70 assert self.templates
61 self.output = output 71 self.output = output
62 self.interactive = interactive 72 self.interactive = interactive
63 self.location = os.path.dirnme(os.path.abspath(__file__)) 73 self.location = os.path.dirnme(os.path.abspath(__file__))
64 self.defaults = variables.copy 74 self.defaults = variables.copy
75
76 # make a dictionary of the variables
77 self.vardict = {}
78 for i in self.vars:
79 self.vardict[i.name] = i
65 80
66 # ensure all of these templates exist 81 # ensure all of these templates exist
67 for template in self.templates: 82 for template in self.templates:
68 if template.startswith('http://') or template.startswith('https://'): 83 if template.startswith('http://') or template.startswith('https://'):
69 continue 84 continue
71 path = template 86 path = template
72 else: 87 else:
73 path = os.path.join(self.location, template) 88 path = os.path.join(self.location, template)
74 assert os.path.exists(template) 89 assert os.path.exists(template)
75 90
91 def missing(self, **variables):
92 if self.look:
93 pass
94 else:
95 if self.usedefaults:
96 pass
97
76 def pre(self, **variables): 98 def pre(self, **variables):
77 """do stuff before interpolation""" 99 """do stuff before interpolation"""
100
101 def substitute(self, **variables):
102 """do the substitution"""
103 vars = self.get_variables(**variables)
104 self.pre(**variables)
105 self.check_missing(vars)
106 self.post(**variables)
78 107
79 def post(self, **variables): 108 def post(self, **variables):
80 """do stuff after interpolation""" 109 """do stuff after interpolation"""
81 110
111 def read_variables(self, variables):
112 """read variables from stdin"""
113 retval = {}
114 for i in variables:
115 if i in self.vardict:
116 self.vardict[i].read()
117 else:
118 retval.update(ContentTemplate.read_variables(self, (i,)))
119 return retval
82 120
83 class PasteScriptTemplate(MakeItSoTemplate): 121 class PasteScriptTemplate(MakeItSoTemplate):
84 """template for backwards compatability with PasteScript""" 122 """template for backwards compatability with PasteScript"""