annotate makeitso/template.py @ 263:d6fb50be391d

[packaging] add our other template
author Jeff Hammel <k0scist@gmail.com>
date Sun, 13 Aug 2017 15:53:01 -0700
parents f10f5beb8ad1
children
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"""
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
122
b2152efec89a get the description from the docstring if applicable
Jeff Hammel <jhammel@mozilla.com>
parents: 113
diff changeset
16
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
17 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
18 """variable object for MakeItSo templates"""
159
Jeff Hammel <jhammel@mozilla.com>
parents: 122
diff changeset
19
58
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 56
diff changeset
20 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
21 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
22 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
23 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
24 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
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 # 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
27 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
28
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 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
30
71
95d1bb85ab3c add a copy method to a variable to get a non-futzed copy
Jeff Hammel <jhammel@mozilla.com>
parents: 70
diff changeset
31 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
32 """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
33 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
34
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
35 def set(self, value):
256
f10f5beb8ad1 pytest now actually runs the tests
Jeff Hammel <k0scist@gmail.com>
parents: 159
diff changeset
36 self.value = self.cast(value) if self.cast else value
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
37 self._set = True
99
d9c6e26a42ff fix up interactivity of API template a bit. still doesnt quite work
Jeff Hammel <jhammel@mozilla.com>
parents: 90
diff changeset
38 return self.value
d9c6e26a42ff fix up interactivity of API template a bit. still doesnt quite work
Jeff Hammel <jhammel@mozilla.com>
parents: 90
diff changeset
39
d9c6e26a42ff fix up interactivity of API template a bit. still doesnt quite work
Jeff Hammel <jhammel@mozilla.com>
parents: 90
diff changeset
40 def isset(self):
d9c6e26a42ff fix up interactivity of API template a bit. still doesnt quite work
Jeff Hammel <jhammel@mozilla.com>
parents: 90
diff changeset
41 """whether the variable has been set or not"""
d9c6e26a42ff fix up interactivity of API template a bit. still doesnt quite work
Jeff Hammel <jhammel@mozilla.com>
parents: 90
diff changeset
42 return self._set
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
43
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 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
45 """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
46 fd.write(self.display())
100
b54898f7d8a9 now API template variables seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 99
diff changeset
47 return self.set(raw_input())
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
48
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 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
50 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
51 if self.default:
99
d9c6e26a42ff fix up interactivity of API template a bit. still doesnt quite work
Jeff Hammel <jhammel@mozilla.com>
parents: 90
diff changeset
52 return 'Enter %s [DEFAULT: %s]: ' % (description, repr(self.default))
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
53 else:
99
d9c6e26a42ff fix up interactivity of API template a bit. still doesnt quite work
Jeff Hammel <jhammel@mozilla.com>
parents: 90
diff changeset
54 return 'Enter %s: ' % description
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
55
75
ec35e5763b52 * fix with assemble function
Jeff Hammel <jhammel@mozilla.com>
parents: 74
diff changeset
56 def __repr__(self):
ec35e5763b52 * fix with assemble function
Jeff Hammel <jhammel@mozilla.com>
parents: 74
diff changeset
57 return "Variable(name='%s')" % self.name
ec35e5763b52 * fix with assemble function
Jeff Hammel <jhammel@mozilla.com>
parents: 74
diff changeset
58
72
f44af8f30147 add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents: 71
diff changeset
59 def assemble(*args):
f44af8f30147 add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents: 71
diff changeset
60 names = set()
f44af8f30147 add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents: 71
diff changeset
61 retval = []
f44af8f30147 add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents: 71
diff changeset
62 for arg in args:
74
d16823fd8c4a check for subclassing, not instance of
Jeff Hammel <jhammel@mozilla.com>
parents: 73
diff changeset
63 if issubclass(arg, MakeItSoTemplate):
72
f44af8f30147 add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents: 71
diff changeset
64 arg = arg.vars
73
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
65 for variable in arg:
72
f44af8f30147 add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents: 71
diff changeset
66 if variable.name in names:
f44af8f30147 add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents: 71
diff changeset
67 continue
f44af8f30147 add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents: 71
diff changeset
68 retval.append(variable.copy())
75
ec35e5763b52 * fix with assemble function
Jeff Hammel <jhammel@mozilla.com>
parents: 74
diff changeset
69 names.add(variable.name)
72
f44af8f30147 add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents: 71
diff changeset
70 return retval
f44af8f30147 add function to assemble variables
Jeff Hammel <jhammel@mozilla.com>
parents: 71
diff changeset
71
42
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
72 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
73 """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
74
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
75 # 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
76 name = ''
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
77
56
728cae02a6ed * fix another variable-related bug
Jeff Hammel <jhammel@mozilla.com>
parents: 44
diff changeset
78 # description of the template
728cae02a6ed * fix another variable-related bug
Jeff Hammel <jhammel@mozilla.com>
parents: 44
diff changeset
79 description = ''
728cae02a6ed * fix another variable-related bug
Jeff Hammel <jhammel@mozilla.com>
parents: 44
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 # 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
82 # 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
83 templates = []
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
84
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
85 # variables
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
86 vars = []
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
87
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
88 # 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
89 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
90
90
26b9c3bba04e make the api for substitute() variables, output
Jeff Hammel <jhammel@mozilla.com>
parents: 88
diff changeset
91 def __init__(self, interactive=True, usedefaults=True, variables=None):
44
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
92 """
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
93 - 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
94 - 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
95 - 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
96 """
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
97
b91133e3b02d override get_variables for API template; could instead do this in the ctor, alternately
Jeff Hammel <jhammel@mozilla.com>
parents: 62
diff changeset
98 # boilerplate
65
0152741621c1 check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
99 variables = variables or {}
78
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents: 75
diff changeset
100 if not self.description and hasattr(self, '__doc__'):
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents: 75
diff changeset
101 self.description = self.__doc__
42
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
102 self.interactive = interactive
66
7821c82772f5 determine the location in a better way
Jeff Hammel <jhammel@mozilla.com>
parents: 65
diff changeset
103 _file = sys.modules[self.__class__.__module__].__file__
7821c82772f5 determine the location in a better way
Jeff Hammel <jhammel@mozilla.com>
parents: 65
diff changeset
104 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
105 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
106 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
107
58
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 56
diff changeset
108 # 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
109 self.vardict = {}
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
110 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
111 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
112
42
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
113 # ensure all of these templates exist
65
0152741621c1 check in a failing test wrt location
Jeff Hammel <jhammel@mozilla.com>
parents: 64
diff changeset
114 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
115 for template in self.templates:
159
Jeff Hammel <jhammel@mozilla.com>
parents: 122
diff changeset
116 if not isinstance(template, basestring):
Jeff Hammel <jhammel@mozilla.com>
parents: 122
diff changeset
117 template = os.path.join(*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
118 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
119 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
120 continue
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
121 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
122 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
123 else:
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
124 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
125 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
126 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
127
122
b2152efec89a get the description from the docstring if applicable
Jeff Hammel <jhammel@mozilla.com>
parents: 113
diff changeset
128 @classmethod
b2152efec89a get the description from the docstring if applicable
Jeff Hammel <jhammel@mozilla.com>
parents: 113
diff changeset
129 def get_description(cls):
b2152efec89a get the description from the docstring if applicable
Jeff Hammel <jhammel@mozilla.com>
parents: 113
diff changeset
130 if hasattr(cls, 'description'):
b2152efec89a get the description from the docstring if applicable
Jeff Hammel <jhammel@mozilla.com>
parents: 113
diff changeset
131 if cls.description:
b2152efec89a get the description from the docstring if applicable
Jeff Hammel <jhammel@mozilla.com>
parents: 113
diff changeset
132 return cls.description
b2152efec89a get the description from the docstring if applicable
Jeff Hammel <jhammel@mozilla.com>
parents: 113
diff changeset
133 if hasattr(cls, '__doc__'):
b2152efec89a get the description from the docstring if applicable
Jeff Hammel <jhammel@mozilla.com>
parents: 113
diff changeset
134 return cls.__doc__
b2152efec89a get the description from the docstring if applicable
Jeff Hammel <jhammel@mozilla.com>
parents: 113
diff changeset
135
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
136 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
137 # 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
138 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
139 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
140 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
141 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
142 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
143 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
144 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
145 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
146
44
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
147 def missing(self, **variables):
113
c3b8ce33d3ad make variable getting logic a little less horrible
Jeff Hammel <jhammel@mozilla.com>
parents: 112
diff changeset
148
c3b8ce33d3ad make variable getting logic a little less horrible
Jeff Hammel <jhammel@mozilla.com>
parents: 112
diff changeset
149 # boilerplate
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
150 vars = self.get_variables(**variables)
62
30100690ad3f display defaults with command line --help option
Jeff Hammel <jhammel@mozilla.com>
parents: 58
diff changeset
151 missing = set([])
64
c20277dbf8fa closer to substitution
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
152
c20277dbf8fa closer to substitution
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
153 # get known needed variables
c20277dbf8fa closer to substitution
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
154 for var in self.vars:
113
c3b8ce33d3ad make variable getting logic a little less horrible
Jeff Hammel <jhammel@mozilla.com>
parents: 112
diff changeset
155 if var.name not in vars:
99
d9c6e26a42ff fix up interactivity of API template a bit. still doesnt quite work
Jeff Hammel <jhammel@mozilla.com>
parents: 90
diff changeset
156 if var.default is Undefined:
d9c6e26a42ff fix up interactivity of API template a bit. still doesnt quite work
Jeff Hammel <jhammel@mozilla.com>
parents: 90
diff changeset
157 missing.add(var.name)
d9c6e26a42ff fix up interactivity of API template a bit. still doesnt quite work
Jeff Hammel <jhammel@mozilla.com>
parents: 90
diff changeset
158 continue
100
b54898f7d8a9 now API template variables seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 99
diff changeset
159 if (not self.usedefaults) and (not var.isset()):
99
d9c6e26a42ff fix up interactivity of API template a bit. still doesnt quite work
Jeff Hammel <jhammel@mozilla.com>
parents: 90
diff changeset
160 missing.add(var.name)
64
c20277dbf8fa closer to substitution
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
161
113
c3b8ce33d3ad make variable getting logic a little less horrible
Jeff Hammel <jhammel@mozilla.com>
parents: 112
diff changeset
162 # scan templates for other variables
44
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
163 if self.look:
99
d9c6e26a42ff fix up interactivity of API template a bit. still doesnt quite work
Jeff Hammel <jhammel@mozilla.com>
parents: 90
diff changeset
164 template = PolyTemplate(self._templates,
112
51c9cb49edec fix a few minor issues
Jeff Hammel <jhammel@mozilla.com>
parents: 101
diff changeset
165 interactive=False,
99
d9c6e26a42ff fix up interactivity of API template a bit. still doesnt quite work
Jeff Hammel <jhammel@mozilla.com>
parents: 90
diff changeset
166 variables=vars)
d9c6e26a42ff fix up interactivity of API template a bit. still doesnt quite work
Jeff Hammel <jhammel@mozilla.com>
parents: 90
diff changeset
167 missing.update(template.missing())
d9c6e26a42ff fix up interactivity of API template a bit. still doesnt quite work
Jeff Hammel <jhammel@mozilla.com>
parents: 90
diff changeset
168
64
c20277dbf8fa closer to substitution
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
169 return missing
44
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
170
101
80a57bf2b7f4 fixed a few problems
Jeff Hammel <jhammel@mozilla.com>
parents: 100
diff changeset
171 def pre(self, variables, output):
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
172 """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
173
100
b54898f7d8a9 now API template variables seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 99
diff changeset
174 def substitute(self, variables, output=None):
44
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
175 """do the substitution"""
113
c3b8ce33d3ad make variable getting logic a little less horrible
Jeff Hammel <jhammel@mozilla.com>
parents: 112
diff changeset
176
c3b8ce33d3ad make variable getting logic a little less horrible
Jeff Hammel <jhammel@mozilla.com>
parents: 112
diff changeset
177 # get the variables
44
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
178 vars = self.get_variables(**variables)
101
80a57bf2b7f4 fixed a few problems
Jeff Hammel <jhammel@mozilla.com>
parents: 100
diff changeset
179 self.pre(vars, output)
44
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
180 self.check_missing(vars)
64
c20277dbf8fa closer to substitution
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
181
c20277dbf8fa closer to substitution
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
182 # do the substitution
67
a0f7bfa98755 API templates now hobble along on their own two feet
Jeff Hammel <jhammel@mozilla.com>
parents: 66
diff changeset
183 template = PolyTemplate(self._templates,
a0f7bfa98755 API templates now hobble along on their own two feet
Jeff Hammel <jhammel@mozilla.com>
parents: 66
diff changeset
184 interactive=self.interactive,
a0f7bfa98755 API templates now hobble along on their own two feet
Jeff Hammel <jhammel@mozilla.com>
parents: 66
diff changeset
185 variables=vars)
88
712a6d358083 fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents: 78
diff changeset
186 template.check_output(output)
90
26b9c3bba04e make the api for substitute() variables, output
Jeff Hammel <jhammel@mozilla.com>
parents: 88
diff changeset
187 template.substitute({}, output)
113
c3b8ce33d3ad make variable getting logic a little less horrible
Jeff Hammel <jhammel@mozilla.com>
parents: 112
diff changeset
188
c3b8ce33d3ad make variable getting logic a little less horrible
Jeff Hammel <jhammel@mozilla.com>
parents: 112
diff changeset
189 # do whatever you need to do afterwards
101
80a57bf2b7f4 fixed a few problems
Jeff Hammel <jhammel@mozilla.com>
parents: 100
diff changeset
190 self.post(vars, output)
44
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
191
101
80a57bf2b7f4 fixed a few problems
Jeff Hammel <jhammel@mozilla.com>
parents: 100
diff changeset
192 def post(self, variables, output):
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
193 """do stuff after interpolation"""
159
Jeff Hammel <jhammel@mozilla.com>
parents: 122
diff changeset
194
44
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
195 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
196 """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
197 retval = {}
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
198 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
199 if i in self.vardict:
99
d9c6e26a42ff fix up interactivity of API template a bit. still doesnt quite work
Jeff Hammel <jhammel@mozilla.com>
parents: 90
diff changeset
200 value = self.vardict[i].read()
d9c6e26a42ff fix up interactivity of API template a bit. still doesnt quite work
Jeff Hammel <jhammel@mozilla.com>
parents: 90
diff changeset
201 retval[i] = value
44
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
202 else:
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
203 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
204 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
205
42
73dac34d2692 more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
206 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
207 """template for backwards compatability with PasteScript"""