annotate makeitso/makeitso.py @ 36:0cba953a03ca

fixed single-file test case
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 01 Jan 2011 22:13:33 -0800
parents 7e47ff4b0cd3
children 7cee2869bd7b
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
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 """
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 filesystem template interpreter
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 """
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5
30
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
6 import inspect
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 import os
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 import re
25
fbf19940fa97 remove the staging directory you attempted to download
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
9 import shutil
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 import subprocess
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 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
12 import tempfile
15
edabb0bfe107 add method to work TTW
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
13 import urllib
24
1b802678b341 add note about urllib and timeouts
Jeff Hammel <jhammel@mozilla.com>
parents: 23
diff changeset
14 # 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
15
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 from optparse import OptionParser
12
a6fd93354f23 add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
17
33
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
18 # 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
19 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
20
33
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
21 ### import tempita
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
22
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
23 # 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
24 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
25
23
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
26 def cleanup():
25
fbf19940fa97 remove the staging directory you attempted to download
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
27 # remove temporary net module directory
23
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
28 if 'tempdir' in globals():
25
fbf19940fa97 remove the staging directory you attempted to download
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
29 shutil.remove(tempdir)
12
a6fd93354f23 add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
30 try:
a6fd93354f23 add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
31 import tempita
a6fd93354f23 add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
32 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
33
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 # Get tempita from the net
25
fbf19940fa97 remove the staging directory you attempted to download
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
35 # 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
36 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
37 """
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 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
39 - url : base url
23
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
40 - 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
41 - 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
42 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
43 """
23
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
44 globals()['tempdir'] = tempfile.mkdtemp()
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
45 os.mkdir(subdir)
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
46 url = url.rstrip('/')
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
47 for filename in files:
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
48 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
49 content = file(f).read()
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
50 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
51 o = file(outfile, 'w')
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
52 print >> o, content
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
53 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
54
23
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
55 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
56
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
57 try:
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
58 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
59 sys.path.append(t)
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
60 import tempita
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
61 except:
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
62 cleanup()
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
63 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
64
30
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
65 # 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
66 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
67
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
68 # 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
69 shebang_re = '#!.*makeitso.*'
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
70 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
71
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
72 ### URIs
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
73
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
74 def parent_uri(uri):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
75 """parent resource of a URI"""
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
76
26
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
77 if '://' in uri:
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
78 return uri.rsplit('/', 1)[0] + '/'
27
ac44c36da885 make include example work
Jeff Hammel <jhammel@mozilla.com>
parents: 26
diff changeset
79 else:
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
80 here = os.path.dirname(os.path.abspath(uri))
27
ac44c36da885 make include example work
Jeff Hammel <jhammel@mozilla.com>
parents: 26
diff changeset
81 here = here.rstrip(os.path.sep) + os.path.sep
ac44c36da885 make include example work
Jeff Hammel <jhammel@mozilla.com>
parents: 26
diff changeset
82 return here
26
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
83
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
84 def basename(uri):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
85 """return the basename of a URI"""
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
86 if '://' in uri:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
87 return uri.rsplit('/', 1)[1]
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
88 else:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
89 return os.path.basename(uri)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
90
26
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
91 def include(uri):
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
92 f, headers = urllib.urlretrieve(uri)
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
93 return file(f).read()
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
94
30
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
95 ### 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
96
30
1549be7f0fcb dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
97 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
98 """exception for (non-interactive) missing variables"""
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
99 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
100 self.missing = missing
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
101 message = 'Missing variables: %s' % ', '.join(missing)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
102 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
103
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
104 def get_missing(name_error):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
105 """
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
106 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
107 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
108 you a message:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
109 >>> try:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
110 ... eval('2*foo')
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
111 ... except Exception, e:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
112 ... pass
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
113 """
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
114 message = name_error.args[0]
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
115 varname = message.split("'")[1]
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
116 return varname
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
117
33
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
118 ### template classes
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
119
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
120 class ContentTemplate(tempita.Template):
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
121 """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
122
33
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
123 defaults = {'include': include}
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
124
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
125 def __init__(self, content, name=None, interactive=True, **variables):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
126
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
127 # default variables
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
128 self.defaults = self.__class__.defaults.copy()
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
129 self.defaults.update(variables)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
130
33
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
131 # 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
132 self.interactive = interactive
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
133
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
134 tempita.Template.__init__(self, content, name=name)
33
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
135
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
136 def missing(self, **variables):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
137 """return additional variables needed"""
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
138 vars = variables.copy()
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
139 missing = set([])
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
140 while True:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
141 try:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
142 tempita.Template.substitute(self, **vars)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
143 return missing
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
144 except NameError, e:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
145 missed = get_missing(e)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
146 missing.add(missed)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
147 vars[missed] = ''
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
148 return missing
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
149
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
150 def variables(self):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
151 """return the variables needed for a template"""
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
152 return self.missing()
33
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
153
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
154 def substitute(self, **variables):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
155 """interactive (for now) substitution"""
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
156 vars = self.defaults.copy()
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
157 vars.update(variables)
35
7e47ff4b0cd3 started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
158 missing = self.missing(**vars)
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
159 if missing:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
160 if self.interactive:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
161 vars.update(self.read_variables(missing))
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
162 else:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
163 raise MissingVariablesException(missing)
36
0cba953a03ca fixed single-file test case
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
164 return self._substitute(**vars)
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
165
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
166 def _substitute(self, **variables):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
167 return tempita.Template.substitute(self, **variables)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
168
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
169 def read_variables(self, variables):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
170 """read variables from stdin"""
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
171 # TODO: variables should (optionally) be richer objects
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
172 retval = {}
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
173 for i in variables:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
174 print 'Enter %s: ' % i,
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
175 retval[i] = raw_input()
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
176 return retval
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
177
33
190f310f2f5e more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
178
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
179 class URITemplate(ContentTemplate):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
180 """template for a file or URL"""
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
181
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
182 def __init__(self, uri, output=None, interactive=True, **variables):
35
7e47ff4b0cd3 started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
183 self.output = output or sys.stdout
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
184 content = include(uri)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
185
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
186 # remove makeitso shebang if it has one
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
187 if shebang_re.match(content):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
188 content = os.linesep.join(content.splitlines()[1:])
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
189
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
190 variables['here'] = parent_uri(uri)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
191 ContentTemplate.__init__(self, content, name=uri,
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
192 interactive=interactive,
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
193 **variables)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
194
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
195 def substitute(self, **variables):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
196 output = ContentTemplate.substitute(self, **variables)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
197 f = self.output
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
198 if isinstance(f, basestring):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
199 if os.path.isdir(f):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
200 f = os.path.join(f, basename(self.name))
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
201 f = file(f, 'w')
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
202 print >> f, output
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
203
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
204
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
205 class DirectoryTemplate(ContentTemplate):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
206 """template for a directory structure"""
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
207
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
208 def __init__(self, directory, output=None, interactive=True, **variables):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
209 """
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
210 - 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
211 """
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
212 assert os.path.isdir(directory)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
213 self.name = directory
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
214 self.interactive = interactive
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
215 self.output = output
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
216 if output is not None:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
217 if os.path.exists(output):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
218 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
219 self.defaults = ContentTemplate.defaults.copy()
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
220 self.defaults.update(variables)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
221
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
222
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
223 def missing(self, **variables):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
224 variables = variables.copy()
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
225 missing = set([])
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
226 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
227
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
228 # find variables from directory names
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
229 for d in dirnames:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
230 missed = ContentTemplate(d).missing(**variables)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
231 missing.update(missed)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
232 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
233
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
234 # find variables from files
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
235 for f in filenames:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
236 path = os.path.join(dirpath, f)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
237 template = URITemplate(path)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
238 missed = template.missing(**variables)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
239 missing.update(missed)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
240 variables.update(dict([(i, '') for i in missed]))
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
241
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
242 return missing
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
243
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
244 def _substitute(self, **variables):
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
245
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
246 # make output directory if necessary
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
247 output = self.output
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
248 if output and not os.path.exists(output):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
249 os.makedirs(output)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
250
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
251 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
252
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
253 # interpolate directory names
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
254 for d in dirnames:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
255 path = os.path.join(dirname, interpolated)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
256 interpolated = ContentTemplate(path).substitute(**variables)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
257 if os.path.exists(interpolated):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
258 # ensure its a directory
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
259 pass
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
260 else:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
261 os.makedirs(interpolated)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
262
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
263
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
264 class PolyTemplate(ContentTemplate):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
265 """
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
266 template for several files/directories
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
267 """
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
268
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
269 def __init__(self, templates, output=None, interactive=True, **variables):
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
270
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
271 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
272
36
0cba953a03ca fixed single-file test case
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
273 self.interactive = True
35
7e47ff4b0cd3 started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
274 self._templates = templates[:]
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
275 self.templates = []
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
276 self.output = output
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
277 for template in templates:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
278 if os.path.isdir(template):
36
0cba953a03ca fixed single-file test case
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
279 self.templates.append(DirectoryTemplate(template, interactive=self.interactive, output=output, **variables))
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
280 else:
36
0cba953a03ca fixed single-file test case
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
281 self.templates.append(URITemplate(template, interactive=self.interactive, output=output, **variables))
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
282
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
283 def missing(self, **variables):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
284 vars = variables.copy()
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
285 missing = set([])
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
286 for template in self.templates:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
287 missed = template.missing(**vars)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
288 missing.update(missed)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
289 vars.update(dict([(i, '') for i in missed]))
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
290 return missing
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
291
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
292 def _substitute(self, **variables):
28
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
293
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
294 # determine where the hell to put these things
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
295 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
296 dirs = [i for i in self._templates if os.path.isdir(i)]
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
297 if not ((len(dirs) == 0) or len(dirs) == len(templates)):
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
298 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
299
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
300 # TODO: check for missing
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
301 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
302 os.makedirs(self.output)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
303 for template in self.templates:
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
304 template.substitute(**variables)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
305
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
306 ### command line stuff
28
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
307
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
308 def invocation(url, **variables):
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
309 """returns a string appropriate for TTW invocation"""
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
310 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
311 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
312
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
313 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
314
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
315 # create option parser
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
316 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
317 parser = OptionParser(usage, description=__doc__)
28
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
318
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
319 # delimeters
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
320 # 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
321 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
322 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
323 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
324 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
325 help='starting delimeter')
28
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
326
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
327 # options about where to put things
dc18d6db4956 depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
328 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
329 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
330
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
331 # options for getting information
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
332 parser.add_option('--commandline', dest='commandline',
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
333 action='store_true', default=False,
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
334 help="print the commandline to invoke this script TTW")
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
335 parser.add_option('--variables', dest='variables',
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
336 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
337 help='print the variables in a template')
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
338
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
339 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
340
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
341 # 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
342 if options.variables:
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
343
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
344 # makes no sense without a template
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
345 if not args:
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
346 parser.print_usage()
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
347 parser.exit()
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
348
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
349 # find all variables
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
350 template = PolyTemplate(templates=args)
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
351 variables = template.variables()
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
352
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
353 # print them
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
354 for variable in sorted(variables):
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
355 print variable
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
356 return
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 # template variables
35
7e47ff4b0cd3 started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
359 variables = {}
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
360 _args = []
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
361 for arg in args:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
362 if '=' in arg:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
363 key, value = arg.split('=')
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
364 variables[key] = value
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
365 else:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
366 _args.append(arg)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
367 args = _args
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
368
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
369 # print TTW commandline for invocation
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
370 if options.commandline:
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
371 if args:
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
372 for arg in args:
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
373 print invocation(arg, **variables)
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
374 else:
26
044ac9e0b29c add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
375 print invocation('[URI]', **variables)
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
376 return
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
377
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
378 # get the content
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
379 if args:
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
380 template = PolyTemplate(templates=args,
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
381 output=options.output,
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
382 variables=variables)
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
383 else:
34
46c2d0a7335a continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
384 template = ContentTemplate(sys.stdin.read(), variables=variables)
36
0cba953a03ca fixed single-file test case
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
385 template.substitute()
23
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
386
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
387 # cleanup
776805790c84 stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
388 cleanup()
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
389
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
390 if __name__ == '__main__':
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
391 main()