Mercurial > hg > MakeItSo
annotate makeitso/template.py @ 72:f44af8f30147
add function to assemble variables
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 07 Jan 2011 18:09:43 -0800 |
parents | 95d1bb85ab3c |
children | 3db48fdc6be3 |
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""" |
65
0152741621c1
check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents:
64
diff
changeset
|
12 def __nonzero__(self): |
0152741621c1
check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents:
64
diff
changeset
|
13 return False |
44
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
14 Undefined = Undefined() # singleton |
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
15 |
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
|
16 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
|
17 """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
|
18 |
58
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
56
diff
changeset
|
19 def __init__(self, name, description=None, default=Undefined, |
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
|
20 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
|
21 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
|
22 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
|
23 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
|
24 |
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 # 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
|
26 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
|
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 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
|
29 |
71
95d1bb85ab3c
add a copy method to a variable to get a non-futzed copy
Jeff Hammel <jhammel@mozilla.com>
parents:
70
diff
changeset
|
30 def copy(self): |
95d1bb85ab3c
add a copy method to a variable to get a non-futzed copy
Jeff Hammel <jhammel@mozilla.com>
parents:
70
diff
changeset
|
31 """returns a copy of the variable""" |
95d1bb85ab3c
add a copy method to a variable to get a non-futzed copy
Jeff Hammel <jhammel@mozilla.com>
parents:
70
diff
changeset
|
32 return Variable(self.name, self.description, self.default, self.cast) |
95d1bb85ab3c
add a copy method to a variable to get a non-futzed copy
Jeff Hammel <jhammel@mozilla.com>
parents:
70
diff
changeset
|
33 |
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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 |
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 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
|
42 """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
|
43 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
|
44 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
|
45 |
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 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
|
47 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
|
48 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
|
49 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
|
50 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
|
51 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
|
52 |
72
f44af8f30147
add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents:
71
diff
changeset
|
53 def assemble(*args): |
f44af8f30147
add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents:
71
diff
changeset
|
54 names = set() |
f44af8f30147
add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents:
71
diff
changeset
|
55 retval = [] |
f44af8f30147
add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents:
71
diff
changeset
|
56 for arg in args: |
f44af8f30147
add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents:
71
diff
changeset
|
57 if isinstance(arg, MakeItSoTemplate): |
f44af8f30147
add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents:
71
diff
changeset
|
58 arg = arg.vars |
f44af8f30147
add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents:
71
diff
changeset
|
59 for variable in args: |
f44af8f30147
add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents:
71
diff
changeset
|
60 if variable.name in names: |
f44af8f30147
add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents:
71
diff
changeset
|
61 continue |
f44af8f30147
add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents:
71
diff
changeset
|
62 retval.append(variable.copy()) |
f44af8f30147
add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents:
71
diff
changeset
|
63 return retval |
f44af8f30147
add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents:
71
diff
changeset
|
64 |
42
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
65 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
|
66 """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
|
67 |
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
68 # 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
|
69 name = '' |
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
70 |
56
728cae02a6ed
* fix another variable-related bug
Jeff Hammel <jhammel@mozilla.com>
parents:
44
diff
changeset
|
71 # description of the template |
728cae02a6ed
* fix another variable-related bug
Jeff Hammel <jhammel@mozilla.com>
parents:
44
diff
changeset
|
72 description = '' |
728cae02a6ed
* fix another variable-related bug
Jeff Hammel <jhammel@mozilla.com>
parents:
44
diff
changeset
|
73 |
42
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
74 # 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
|
75 # 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
|
76 templates = [] |
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
77 |
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
78 # variables |
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
79 vars = [] |
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
80 |
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
81 # 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
|
82 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
|
83 |
58
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
56
diff
changeset
|
84 def __init__(self, output=None, interactive=True, usedefaults=True, |
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
56
diff
changeset
|
85 variables=None): |
44
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
86 """ |
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
87 - 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
|
88 - 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
|
89 - 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
|
90 """ |
63
b91133e3b02d
override get_variables for API template; could instead do this in the ctor, alternately
Jeff Hammel <jhammel@mozilla.com>
parents:
62
diff
changeset
|
91 |
b91133e3b02d
override get_variables for API template; could instead do this in the ctor, alternately
Jeff Hammel <jhammel@mozilla.com>
parents:
62
diff
changeset
|
92 # boilerplate |
65
0152741621c1
check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents:
64
diff
changeset
|
93 variables = variables or {} |
42
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
94 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
|
95 self.interactive = interactive |
66
7821c82772f5
determine the location in a better way
Jeff Hammel <jhammel@mozilla.com>
parents:
65
diff
changeset
|
96 _file = sys.modules[self.__class__.__module__].__file__ |
7821c82772f5
determine the location in a better way
Jeff Hammel <jhammel@mozilla.com>
parents:
65
diff
changeset
|
97 self.location = os.path.dirname(os.path.abspath(_file)) |
58
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
56
diff
changeset
|
98 self.defaults = variables.copy() |
63
b91133e3b02d
override get_variables for API template; could instead do this in the ctor, alternately
Jeff Hammel <jhammel@mozilla.com>
parents:
62
diff
changeset
|
99 self.usedefaults = usedefaults |
42
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
100 |
58
112bf081148c
make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents:
56
diff
changeset
|
101 # make a dictionary of the variables for lookup convenience |
44
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
102 self.vardict = {} |
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
103 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
|
104 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
|
105 |
42
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
106 # ensure all of these templates exist |
65
0152741621c1
check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents:
64
diff
changeset
|
107 self._templates = [] |
42
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
108 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
|
109 if template.startswith('http://') or template.startswith('https://'): |
65
0152741621c1
check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents:
64
diff
changeset
|
110 self._templates.append(template) |
42
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
111 continue |
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
112 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
|
113 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
|
114 else: |
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
115 path = os.path.join(self.location, template) |
65
0152741621c1
check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents:
64
diff
changeset
|
116 assert os.path.exists(path), "%s does not exist" % path |
0152741621c1
check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents:
64
diff
changeset
|
117 self._templates.append(path) |
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
|
118 |
63
b91133e3b02d
override get_variables for API template; could instead do this in the ctor, alternately
Jeff Hammel <jhammel@mozilla.com>
parents:
62
diff
changeset
|
119 def get_variables(self, **variables): |
b91133e3b02d
override get_variables for API template; could instead do this in the ctor, alternately
Jeff Hammel <jhammel@mozilla.com>
parents:
62
diff
changeset
|
120 # XXX could do this in the ctor |
b91133e3b02d
override get_variables for API template; could instead do this in the ctor, alternately
Jeff Hammel <jhammel@mozilla.com>
parents:
62
diff
changeset
|
121 vars = ContentTemplate.get_variables(self, **variables) |
b91133e3b02d
override get_variables for API template; could instead do this in the ctor, alternately
Jeff Hammel <jhammel@mozilla.com>
parents:
62
diff
changeset
|
122 if self.usedefaults: |
b91133e3b02d
override get_variables for API template; could instead do this in the ctor, alternately
Jeff Hammel <jhammel@mozilla.com>
parents:
62
diff
changeset
|
123 for variable in self.vars: |
b91133e3b02d
override get_variables for API template; could instead do this in the ctor, alternately
Jeff Hammel <jhammel@mozilla.com>
parents:
62
diff
changeset
|
124 if variable.name in vars: |
b91133e3b02d
override get_variables for API template; could instead do this in the ctor, alternately
Jeff Hammel <jhammel@mozilla.com>
parents:
62
diff
changeset
|
125 continue |
b91133e3b02d
override get_variables for API template; could instead do this in the ctor, alternately
Jeff Hammel <jhammel@mozilla.com>
parents:
62
diff
changeset
|
126 if variable.default is not Undefined: |
b91133e3b02d
override get_variables for API template; could instead do this in the ctor, alternately
Jeff Hammel <jhammel@mozilla.com>
parents:
62
diff
changeset
|
127 vars[variable.name] = variable.default |
b91133e3b02d
override get_variables for API template; could instead do this in the ctor, alternately
Jeff Hammel <jhammel@mozilla.com>
parents:
62
diff
changeset
|
128 return vars |
b91133e3b02d
override get_variables for API template; could instead do this in the ctor, alternately
Jeff Hammel <jhammel@mozilla.com>
parents:
62
diff
changeset
|
129 |
44
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
130 def missing(self, **variables): |
63
b91133e3b02d
override get_variables for API template; could instead do this in the ctor, alternately
Jeff Hammel <jhammel@mozilla.com>
parents:
62
diff
changeset
|
131 vars = self.get_variables(**variables) |
62
30100690ad3f
display defaults with command line --help option
Jeff Hammel <jhammel@mozilla.com>
parents:
58
diff
changeset
|
132 missing = set([]) |
64 | 133 |
134 # get known needed variables | |
135 for var in self.vars: | |
136 if var.name not in vars: | |
137 missing.add(var) | |
138 | |
44
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
139 if self.look: |
64 | 140 # scan templates for other variables |
141 raise NotImplementedError | |
142 | |
143 return missing | |
44
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
144 |
69
a75138a952d0
using **kw doesnt update the actual dict; fix this
Jeff Hammel <jhammel@mozilla.com>
parents:
67
diff
changeset
|
145 def pre(self, variables): |
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
|
146 """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
|
147 |
44
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
148 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
|
149 """do the substitution""" |
65
0152741621c1
check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents:
64
diff
changeset
|
150 |
44
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
151 vars = self.get_variables(**variables) |
69
a75138a952d0
using **kw doesnt update the actual dict; fix this
Jeff Hammel <jhammel@mozilla.com>
parents:
67
diff
changeset
|
152 self.pre(vars) |
44
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
153 self.check_missing(vars) |
64 | 154 |
155 # do the substitution | |
67
a0f7bfa98755
API templates now hobble along on their own two feet
Jeff Hammel <jhammel@mozilla.com>
parents:
66
diff
changeset
|
156 template = PolyTemplate(self._templates, |
a0f7bfa98755
API templates now hobble along on their own two feet
Jeff Hammel <jhammel@mozilla.com>
parents:
66
diff
changeset
|
157 output=self.output, |
a0f7bfa98755
API templates now hobble along on their own two feet
Jeff Hammel <jhammel@mozilla.com>
parents:
66
diff
changeset
|
158 interactive=self.interactive, |
a0f7bfa98755
API templates now hobble along on their own two feet
Jeff Hammel <jhammel@mozilla.com>
parents:
66
diff
changeset
|
159 variables=vars) |
a0f7bfa98755
API templates now hobble along on their own two feet
Jeff Hammel <jhammel@mozilla.com>
parents:
66
diff
changeset
|
160 template.substitute() |
64 | 161 |
69
a75138a952d0
using **kw doesnt update the actual dict; fix this
Jeff Hammel <jhammel@mozilla.com>
parents:
67
diff
changeset
|
162 self.post(vars) |
44
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
163 |
69
a75138a952d0
using **kw doesnt update the actual dict; fix this
Jeff Hammel <jhammel@mozilla.com>
parents:
67
diff
changeset
|
164 def post(self, variables): |
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
|
165 """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
|
166 |
44
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
167 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
|
168 """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
|
169 retval = {} |
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
170 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
|
171 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
|
172 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
|
173 else: |
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
174 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
|
175 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
|
176 |
42
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
177 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
|
178 """template for backwards compatability with PasteScript""" |