annotate makeitso/makeitso.py @ 86:2c1310e94645

try to interpolate templates
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 10 Jan 2011 12:39:26 -0800
parents 7909dfaef33b
children 3571417ef92e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
52
2765a700982e whitespace
Jeff Hammel <jhammel@mozilla.com>
parents: 44
diff changeset
2
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 filesystem template interpreter
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 """
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
30
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
7 import inspect
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 import os
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 import re
25
fbf19940fa97 remove the staging directory you attempted to download
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
10 import shutil
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 import subprocess
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 import sys
21
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
13 import tempfile
15
edabb0bfe107 add method to work TTW
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
14 import urllib
24
1b802678b341 add note about urllib and timeouts
Jeff Hammel <jhammel@mozilla.com>
parents: 23
diff changeset
15 # TODO: may have to use urllib2.urlopen to get reasonable timeouts
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 from optparse import OptionParser
12
a6fd93354f23 add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
18
33
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
19 # URL of -this file-
21
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
20 location = 'http://k0s.org/mozilla/hg/MakeItSo/raw-file/tip/makeitso/makeitso.py'
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
21
33
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
22 ### import tempita
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
23
21
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
24 # URL of tempita
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
25 tempita_location = 'http://bitbucket.org/ianb/tempita/raw-file/tip/tempita/'
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
26
23
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
27 def cleanup():
25
fbf19940fa97 remove the staging directory you attempted to download
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
28 # remove temporary net module directory
23
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
29 if 'tempdir' in globals():
25
fbf19940fa97 remove the staging directory you attempted to download
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
30 shutil.remove(tempdir)
12
a6fd93354f23 add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
31 try:
a6fd93354f23 add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
32 import tempita
a6fd93354f23 add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
33 except ImportError:
21
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
34
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
35 # Get tempita from the net
25
fbf19940fa97 remove the staging directory you attempted to download
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
36 # TODO: abstract this to get arbitrary modules from the net
23
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
37 def getFiles(url, subdir, files):
21
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
38 """
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
39 fetch files from the internet
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
40 - url : base url
23
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
41 - subdirectory: to put things in
21
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
42 - files : list of files to retrieve
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
43 returns the location of a temporary directory
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
44 """
23
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
45 globals()['tempdir'] = tempfile.mkdtemp()
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
46 os.mkdir(subdir)
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
47 url = url.rstrip('/')
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
48 for filename in files:
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
49 f, headers = urllib.urlretrive('%s/%s' % (url, filename))
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
50 content = file(f).read()
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
51 outfile = os.path.join(globals()['tempdir'], subdir, filename)
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
52 o = file(outfile, 'w')
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
53 print >> o, content
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
54 return globals()['tempdir']
21
c879b93c3f15 stub out fetching tempita from the internet; bitbucket is down, so no further work here atm
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
55
23
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
56 tempita_files = ['__init__.py', '_looper.py', 'compat3.py']
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
57
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
58 try:
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
59 t = getFiles(tempita_location, 'tempita', tempita_files)
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
60 sys.path.append(t)
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
61 import tempita
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
62 except:
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
63 cleanup()
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
64 raise NotImplementedError('This should say something like youre not connected to the net')
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
65
30
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
66 # does tempita support delimeters?
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
67 has_delimeters = 'delimeters' in inspect.getargspec(tempita.Template.__init__).args
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
68
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
69 # regular expressions for finding the shebang
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
70 shebang_re = '#!.*makeitso.*'
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
71 shebang_re = re.compile(shebang_re)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
72
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
73 ### URIs
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
74
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
75 def parent_uri(uri):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
76 """parent resource of a URI"""
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
77
26
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
78 if '://' in uri:
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
79 return uri.rsplit('/', 1)[0] + '/'
27
ac44c36da885 make include example work
Jeff Hammel <jhammel@mozilla.com>
parents: 26
diff changeset
80 else:
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
81 here = os.path.dirname(os.path.abspath(uri))
27
ac44c36da885 make include example work
Jeff Hammel <jhammel@mozilla.com>
parents: 26
diff changeset
82 here = here.rstrip(os.path.sep) + os.path.sep
ac44c36da885 make include example work
Jeff Hammel <jhammel@mozilla.com>
parents: 26
diff changeset
83 return here
26
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
84
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
85 def basename(uri):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
86 """return the basename of a URI"""
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
87 if '://' in uri:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
88 return uri.rsplit('/', 1)[1]
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
89 else:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
90 return os.path.basename(uri)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
91
26
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
92 def include(uri):
44
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
93 f, headers = urllib.urlretrieve(uri) # XXX -> urllib2 for timeout
26
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
94 return file(f).read()
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
95
30
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
96 ### things that deal with variables
26
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
97
30
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
98 class MissingVariablesException(Exception):
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
99 """exception for (non-interactive) missing variables"""
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
100 def __init__(self, missing):
30
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
101 self.missing = missing
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
102 message = 'Missing variables: %s' % ', '.join(missing)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
103 Exception.__init__(self, message)
30
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
104
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
105 def get_missing(name_error):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
106 """
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
107 This is a horrible hack because python doesn't do the proper thing
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
108 via eval and return the name of the variable; instead, it just gives
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
109 you a message:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
110 >>> try:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
111 ... eval('2*foo')
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
112 ... except Exception, e:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
113 ... pass
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
114 """
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
115 message = name_error.args[0]
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
116 varname = message.split("'")[1]
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
117 return varname
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
118
33
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
119 ### template classes
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
120
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
121 class ContentTemplate(tempita.Template):
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
122 """MakeItSo's extension of tempita's Template class"""
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
123
33
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
124 defaults = {'include': include}
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
125
55
b087a14a664b fix several issues involving variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 54
diff changeset
126 def __init__(self, content, name=None, interactive=True, variables=None):
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
127
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
128 # default variables
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
129 self.defaults = self.__class__.defaults.copy()
55
b087a14a664b fix several issues involving variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 54
diff changeset
130 self.defaults.update(variables or {})
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
131
33
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
132 # TODO: automagically tell if the program is interactive or not
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
133 self.interactive = interactive
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
134
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
135 tempita.Template.__init__(self, content, name=name)
33
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
136
63
b91133e3b02d override get_variables for API template; could instead do this in the ctor, alternately
Jeff Hammel <jhammel@mozilla.com>
parents: 56
diff changeset
137 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: 56
diff changeset
138 """the template's augmented variable set"""
b91133e3b02d override get_variables for API template; could instead do this in the ctor, alternately
Jeff Hammel <jhammel@mozilla.com>
parents: 56
diff changeset
139 vars = self.defaults.copy()
b91133e3b02d override get_variables for API template; could instead do this in the ctor, alternately
Jeff Hammel <jhammel@mozilla.com>
parents: 56
diff changeset
140 vars.update(variables)
b91133e3b02d override get_variables for API template; could instead do this in the ctor, alternately
Jeff Hammel <jhammel@mozilla.com>
parents: 56
diff changeset
141 return vars
b91133e3b02d override get_variables for API template; could instead do this in the ctor, alternately
Jeff Hammel <jhammel@mozilla.com>
parents: 56
diff changeset
142
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
143 def missing(self, **variables):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
144 """return additional variables needed"""
55
b087a14a664b fix several issues involving variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 54
diff changeset
145 vars = self.get_variables(**variables)
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
146 missing = set([])
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
147 while True:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
148 try:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
149 tempita.Template.substitute(self, **vars)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
150 return missing
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
151 except NameError, e:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
152 missed = get_missing(e)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
153 missing.add(missed)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
154 vars[missed] = ''
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
155 return missing
44
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
156
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
157 def check_missing(self, vars):
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
158 """
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
159 check for missing variables and, if applicable,
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
160 update them from the command line
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
161 """
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
162 missing = self.missing(**vars)
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
163 if missing:
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
164 if self.interactive:
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
165 vars.update(self.read_variables(missing))
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
166 else:
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
167 raise MissingVariablesException(missing)
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
168
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
169
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
170 def variables(self):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
171 """return the variables needed for a template"""
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
172 return self.missing()
33
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
173
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
174 def substitute(self, **variables):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
175 """interactive (for now) substitution"""
54
d3e1e5745f24 pass the variables, fixes tests
Jeff Hammel <jhammel@mozilla.com>
parents: 53
diff changeset
176 vars = self.get_variables(**variables)
44
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
177 self.check_missing(vars)
36
0cba953a03ca fixed single-file test case
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
178 return self._substitute(**vars)
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
179
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
180 def _substitute(self, **variables):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
181 return tempita.Template.substitute(self, **variables)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
182
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
183 def read_variables(self, variables):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
184 """read variables from stdin"""
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
185 # TODO: variables should (optionally) be richer objects
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
186 retval = {}
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
187 for i in variables:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
188 print 'Enter %s: ' % i,
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
189 retval[i] = raw_input()
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
190 return retval
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
191
33
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
192
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
193 class URITemplate(ContentTemplate):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
194 """template for a file or URL"""
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
195
55
b087a14a664b fix several issues involving variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 54
diff changeset
196 def __init__(self, uri, output=None, interactive=True, variables=None):
35
7e47ff4b0cd3 started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
197 self.output = output or sys.stdout
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
198 content = include(uri)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
199
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
200 # remove makeitso shebang if it has one
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
201 if shebang_re.match(content):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
202 content = os.linesep.join(content.splitlines()[1:])
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
203
56
728cae02a6ed * fix another variable-related bug
Jeff Hammel <jhammel@mozilla.com>
parents: 55
diff changeset
204 variables = variables or {}
76
7909dfaef33b note about adding more metadata
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
205 if 'here' not in variables:
7909dfaef33b note about adding more metadata
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
206 variables['here'] = parent_uri(uri)
7909dfaef33b note about adding more metadata
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
207 # TODO: could add other metadata about the uri,
7909dfaef33b note about adding more metadata
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
208 # such as last modification time'
7909dfaef33b note about adding more metadata
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
209
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
210 ContentTemplate.__init__(self, content, name=uri,
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
211 interactive=interactive,
55
b087a14a664b fix several issues involving variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 54
diff changeset
212 variables=variables)
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
213
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
214 def substitute(self, **variables):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
215 output = ContentTemplate.substitute(self, **variables)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
216 f = self.output
37
7cee2869bd7b stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents: 36
diff changeset
217
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
218 if isinstance(f, basestring):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
219 if os.path.isdir(f):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
220 f = os.path.join(f, basename(self.name))
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
221 f = file(f, 'w')
37
7cee2869bd7b stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents: 36
diff changeset
222 print >> f, output
7cee2869bd7b stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents: 36
diff changeset
223 f.close()
7cee2869bd7b stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents: 36
diff changeset
224 else:
7cee2869bd7b stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents: 36
diff changeset
225 print >> f, output
86
2c1310e94645 try to interpolate templates
Jeff Hammel <jhammel@mozilla.com>
parents: 76
diff changeset
226 try:
2c1310e94645 try to interpolate templates
Jeff Hammel <jhammel@mozilla.com>
parents: 76
diff changeset
227 os.chmod(self.output, os.stat(self.name).st_mode)
2c1310e94645 try to interpolate templates
Jeff Hammel <jhammel@mozilla.com>
parents: 76
diff changeset
228 except:
2c1310e94645 try to interpolate templates
Jeff Hammel <jhammel@mozilla.com>
parents: 76
diff changeset
229 pass
2c1310e94645 try to interpolate templates
Jeff Hammel <jhammel@mozilla.com>
parents: 76
diff changeset
230
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
231
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
232 class DirectoryTemplate(ContentTemplate):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
233 """template for a directory structure"""
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
234
55
b087a14a664b fix several issues involving variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 54
diff changeset
235 def __init__(self, directory, output=None, interactive=True, variables=None):
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
236 """
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
237 - output : output directory; if None will render in place
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
238 """
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
239 assert os.path.isdir(directory)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
240 self.name = directory
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
241 self.interactive = interactive
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
242 self.output = output
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
243 if output is not None:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
244 if os.path.exists(output):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
245 assert os.path.isdir(output), "%s: Must be a directory" % self.name
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
246 self.defaults = ContentTemplate.defaults.copy()
55
b087a14a664b fix several issues involving variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 54
diff changeset
247 self.defaults.update(variables or {})
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
248
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
249
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
250 def missing(self, **variables):
55
b087a14a664b fix several issues involving variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 54
diff changeset
251 vars = self.defaults.copy()
b087a14a664b fix several issues involving variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 54
diff changeset
252 vars.update(variables)
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
253 missing = set([])
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
254 for dirpath, dirnames, filenames in os.walk(self.name):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
255
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
256 # find variables from directory names
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
257 for d in dirnames:
55
b087a14a664b fix several issues involving variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 54
diff changeset
258 missed = ContentTemplate(d).missing(**vars)
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
259 missing.update(missed)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
260 variables.update(dict([(i, '') for i in missed]))
28
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
261
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
262 # find variables from files
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
263 for f in filenames:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
264 path = os.path.join(dirpath, f)
54
d3e1e5745f24 pass the variables, fixes tests
Jeff Hammel <jhammel@mozilla.com>
parents: 53
diff changeset
265 template = URITemplate(path, interactive=self.interactive)
55
b087a14a664b fix several issues involving variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 54
diff changeset
266 missed = template.missing(**vars)
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
267 missing.update(missed)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
268 variables.update(dict([(i, '') for i in missed]))
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
269
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
270 return missing
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
271
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
272 def _substitute(self, **variables):
39
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
273 # TODO: do this with recursion instead of os.walk so that
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
274 # per-directory control may be asserted
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
275
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
276 # make output directory if necessary
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
277 output = self.output
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
278 if output and not os.path.exists(output):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
279 os.makedirs(output)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
280
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
281 for dirname, dirnames, filenames in os.walk(self.name):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
282
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
283 # interpolate directory names
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
284 for d in dirnames:
39
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
285 path = os.path.join(dirname, d)
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
286 interpolated = ContentTemplate(path).substitute(**variables)
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
287 target = os.path.join(self.output, interpolated.split(self.name, 1)[-1].strip(os.path.sep))
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
288
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
289 if os.path.exists(target):
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
290 # ensure its a directory
39
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
291 # TODO: check this first before interpolation is in progress
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
292 assert os.path.isdir(target), "Can't substitute a directory on top of the file"
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
293 else:
39
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
294 os.makedirs(target)
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
295
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
296 # interpolate files
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
297 for filename in filenames:
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
298 path = os.path.join(dirname, filename)
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
299 interpolated = ContentTemplate(path).substitute(**variables)
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
300 target = os.path.join(self.output, interpolated.split(self.name, 1)[-1].strip(os.path.sep))
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
301
39
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
302 if os.path.exists(target):
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
303 # ensure its a directory
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
304 # TODO: check this first before interpolation is in progress
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
305 assert os.path.isfile(target), "Can't substitute a file on top of a directory"
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
306 template = URITemplate(path, output=target, interactive=False)
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
307 template.substitute(**variables)
a2cdce0108e1 get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
308
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
309
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
310 class PolyTemplate(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
311 """template for several files/directories"""
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
312
55
b087a14a664b fix several issues involving variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 54
diff changeset
313 def __init__(self, templates, output=None, interactive=True, variables=None):
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
314
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
315 assert templates, "No templates given!"
35
7e47ff4b0cd3 started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
316
54
d3e1e5745f24 pass the variables, fixes tests
Jeff Hammel <jhammel@mozilla.com>
parents: 53
diff changeset
317 self.interactive = interactive
35
7e47ff4b0cd3 started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
318 self._templates = templates[:]
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
319 self.templates = []
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
320 self.output = output
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
321 for template in templates:
44
6e08cca7d656 do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
322 # TODO: check if the template is a [e.g] PasteScript.template entry point
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
323 if os.path.isdir(template):
55
b087a14a664b fix several issues involving variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 54
diff changeset
324 self.templates.append(DirectoryTemplate(template, interactive=self.interactive, output=output, variables=variables))
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
325 else:
55
b087a14a664b fix several issues involving variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 54
diff changeset
326 self.templates.append(URITemplate(template, interactive=self.interactive, output=output, variables=variables))
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
327
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
328 def missing(self, **variables):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
329 vars = variables.copy()
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
330 missing = set([])
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
331 for template in self.templates:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
332 missed = template.missing(**vars)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
333 missing.update(missed)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
334 vars.update(dict([(i, '') for i in missed]))
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
335 return missing
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
336
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
337 def _substitute(self, **variables):
28
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
338
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
339 # determine where the hell to put these things
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
340 if self.output is None:
35
7e47ff4b0cd3 started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
341 dirs = [i for i in self._templates if os.path.isdir(i)]
40
6b4c8f23192f test and fix mixed output case
Jeff Hammel <jhammel@mozilla.com>
parents: 39
diff changeset
342 if not ((len(dirs) == 0) or len(dirs) == len(self.templates)):
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
343 raise AssertionError("Must specify output when mixing directories and URIs")
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
344
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
345 # TODO: check for missing
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
346 if len(self.templates) > 1 and not os.path.exists(self.output):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
347 os.makedirs(self.output)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
348 for template in self.templates:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
349 template.substitute(**variables)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
350
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: 40
diff changeset
351 ### command line interface
28
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
352
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
353 def invocation(url, **variables):
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
354 """returns a string appropriate for TTW invocation"""
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
355 variables_string = ' '.join(['%s=%s' % (i,j) for i,j in variables.items()])
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
356 return 'python <(curl %s) %s %s' % (location, url, variables_string)
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
357
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
358 def main(args=sys.argv[1:]):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
359
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
360 # create option parser
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
361 usage = '%prog [options] template <template> <...>'
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
362 parser = OptionParser(usage, description=__doc__)
28
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
363
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
364 # delimeters
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
365 # XXX needs tempita trunk
30
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
366 if has_delimeters:
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
367 parser.add_option('-[', '--start-braces', dest='start_braces',
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
368 help='starting delimeter')
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
369 parser.add_option('-]', '--end-braces', dest='end_braces',
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
370 help='starting delimeter')
28
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
371
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
372 # options about where to put things
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
373 parser.add_option('-o', '--output', dest='output',
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
374 help='where to put the output (filename or directory)')
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
375
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
376 # options for getting information
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
377 parser.add_option('--commandline', dest='commandline',
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
378 action='store_true', default=False,
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
379 help="print the commandline to invoke this script TTW")
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
380 parser.add_option('--variables', dest='variables',
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
381 action='store_true', default=False,
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
382 help='print the variables in a template')
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
383
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
384 options, args = parser.parse_args(args)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
385
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
386 # print the variables for the templates
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
387 if options.variables:
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
388
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
389 # makes no sense without a template
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
390 if not args:
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
391 parser.print_usage()
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
392 parser.exit()
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
393
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
394 # find all variables
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
395 template = PolyTemplate(templates=args)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
396 variables = template.variables()
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
397
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
398 # print them
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
399 for variable in sorted(variables):
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
400 print variable
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
401 return
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
402
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
403 # template variables
35
7e47ff4b0cd3 started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
404 variables = {}
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
405 _args = []
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
406 for arg in args:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
407 if '=' in arg:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
408 key, value = arg.split('=')
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
409 variables[key] = value
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
410 else:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
411 _args.append(arg)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
412 args = _args
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
413
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
414 # print TTW commandline for invocation
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
415 if options.commandline:
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
416 if args:
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
417 for arg in args:
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
418 print invocation(arg, **variables)
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
419 else:
26
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
420 print invocation('[URI]', **variables)
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
421 return
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
422
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
423 # get the content
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
424 if args:
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
425 template = PolyTemplate(templates=args,
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
426 output=options.output,
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
427 variables=variables)
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
428 else:
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
429 template = ContentTemplate(sys.stdin.read(), variables=variables)
36
0cba953a03ca fixed single-file test case
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
430 template.substitute()
23
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
431
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
432 # cleanup
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
433 cleanup()
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
434
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
435 if __name__ == '__main__':
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
436 main()