Mercurial > hg > MakeItSo
annotate makeitso/template.py @ 42:73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 04 Jan 2011 07:38:49 -0800 |
parents | 9956e13558dd |
children | 6e08cca7d656 |
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 |
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
|
10 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
|
11 """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
|
12 |
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
|
13 def __init__(self, name, default=None, description=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
|
14 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
|
15 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
|
16 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
|
17 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
|
18 |
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 # 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
|
20 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
|
21 |
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._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
|
23 |
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 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
|
25 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
|
26 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
|
27 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
|
28 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
|
29 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
|
30 |
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 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
|
32 """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
|
33 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
|
34 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
|
35 |
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 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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 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
|
42 |
42
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
43 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
|
44 """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
|
45 |
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
46 # 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
|
47 name = '' |
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
48 |
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
49 # 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
|
50 # 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
|
51 templates = [] |
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 # variables |
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
54 vars = [] |
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
55 |
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
56 # 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
|
57 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
|
58 |
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
59 def __init__(self, output=None, interactive=True, **variables): |
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
60 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
|
61 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
|
62 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
|
63 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
|
64 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
|
65 |
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
66 # 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
|
67 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
|
68 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
|
69 continue |
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
70 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
|
71 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
|
72 else: |
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
73 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
|
74 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
|
75 |
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
|
76 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
|
77 """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
|
78 |
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
|
79 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
|
80 """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
|
81 |
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
|
82 |
42
73dac34d2692
more stubbing of API class; first get something off the ground; then rewrite
Jeff Hammel <jhammel@mozilla.com>
parents:
41
diff
changeset
|
83 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
|
84 """template for backwards compatability with PasteScript""" |