annotate makeitso/template.py @ 55:b087a14a664b

fix several issues involving variable interpolation
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 06 Jan 2011 14:15:19 -0800
parents 6e08cca7d656
children 728cae02a6ed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 """
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 basic API template class
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4
42
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
5 import os
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 import sys
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 from makeitso import ContentTemplate
42
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
8 from makeitso import PolyTemplate
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9
44
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
10 class Undefined(object):
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
11 """marker class for variables"""
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
12 Undefined = Undefined() # singleton
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
13
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 class Variable(object):
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 """variable object for MakeItSo templates"""
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16
44
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
17 def __init__(self, name, default=Undefined, description=None,
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 cast=None):
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 self.name = name
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 self.default = default
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 self.description = description
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 # TODO (maybe): get cast from default variable type if not None
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 self.cast = cast
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 self._set = False
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 def set(self, value):
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 if self.cast:
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 self.value = self.cast(value)
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 else:
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 self.value = value
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 self._set = True
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 def read(self, fd=sys.stdout):
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 """prompt and read the variable from stdin"""
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 fd.write(self.display())
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 self.set(raw_input())
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 def display(self):
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 description = self.description or self.name
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 if self.default:
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 return 'Enter %s [DEFAULT: %s]:' % (description, repr(self.default))
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 else:
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 return 'Enter %s:' % description
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46
42
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
47 class MakeItSoTemplate(ContentTemplate):
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
48 """API template for MakeItSo"""
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
49
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
50 # name of the template
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
51 name = ''
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
52
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
53 # templates to interpolate
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
54 # paths are relative to __file__ unless absolute or URIs
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
55 templates = []
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
56
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
57 # variables
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
58 vars = []
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
59
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
60 # inspect the templates for more variables
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
61 look = False
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
62
44
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
63 def __init__(self, output=None, interactive=True, usedefaults=False, **variables):
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
64 """
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
65 - output : output file or directory
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
66 - interactive : whether tointeractively get variables
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
67 - usedefaults : try to use the default values if not specified
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
68 """
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
69
42
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
70 assert self.templates
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
71 self.output = output
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
72 self.interactive = interactive
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
73 self.location = os.path.dirnme(os.path.abspath(__file__))
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
74 self.defaults = variables.copy
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
75
44
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
76 # make a dictionary of the variables
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
77 self.vardict = {}
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
78 for i in self.vars:
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
79 self.vardict[i.name] = i
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
80
42
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
81 # ensure all of these templates exist
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
82 for template in self.templates:
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
83 if template.startswith('http://') or template.startswith('https://'):
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
84 continue
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
85 if os.path.isabs(template):
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
86 path = template
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
87 else:
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
88 path = os.path.join(self.location, template)
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
89 assert os.path.exists(template)
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
90
44
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
91 def missing(self, **variables):
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
92 if self.look:
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
93 pass
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
94 else:
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
95 if self.usedefaults:
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
96 pass
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
97
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
98 def pre(self, **variables):
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
99 """do stuff before interpolation"""
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
100
44
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
101 def substitute(self, **variables):
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
102 """do the substitution"""
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
103 vars = self.get_variables(**variables)
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
104 self.pre(**variables)
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
105 self.check_missing(vars)
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
106 self.post(**variables)
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
107
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
108 def post(self, **variables):
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
109 """do stuff after interpolation"""
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
110
44
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
111 def read_variables(self, variables):
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
112 """read variables from stdin"""
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
113 retval = {}
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
114 for i in variables:
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
115 if i in self.vardict:
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
116 self.vardict[i].read()
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
117 else:
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
118 retval.update(ContentTemplate.read_variables(self, (i,)))
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
119 return retval
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
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
120
42
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
121 class PasteScriptTemplate(MakeItSoTemplate):
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
122 """template for backwards compatability with PasteScript"""