Mercurial > hg > MakeItSo
annotate makeitso/makeitso.py @ 145:c07dd14d5842
we always want doctest.ELLIPSIS. why the hell wouldnt we?
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 05 Mar 2012 14:26:54 -0800 |
parents | 169e02e190ef |
children | cfd4f1e91090 |
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 | 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 | 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 |
103
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
17 from ConfigParser import RawConfigParser |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 from optparse import OptionParser |
12
a6fd93354f23
add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
19 |
33
190f310f2f5e
more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
20 # 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
|
21 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
|
22 |
33
190f310f2f5e
more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
23 ### import tempita |
190f310f2f5e
more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
24 |
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
|
25 # 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
|
26 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
|
27 |
23
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
28 def cleanup(): |
25
fbf19940fa97
remove the staging directory you attempted to download
Jeff Hammel <jhammel@mozilla.com>
parents:
24
diff
changeset
|
29 # remove temporary net module directory |
23
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
30 if 'tempdir' in globals(): |
25
fbf19940fa97
remove the staging directory you attempted to download
Jeff Hammel <jhammel@mozilla.com>
parents:
24
diff
changeset
|
31 shutil.remove(tempdir) |
12
a6fd93354f23
add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
32 try: |
a6fd93354f23
add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
33 import tempita |
a6fd93354f23
add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
34 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
|
35 |
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
|
36 # Get tempita from the net |
25
fbf19940fa97
remove the staging directory you attempted to download
Jeff Hammel <jhammel@mozilla.com>
parents:
24
diff
changeset
|
37 # 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
|
38 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
|
39 """ |
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 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
|
41 - url : base url |
23
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
42 - 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
|
43 - 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
|
44 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
|
45 """ |
23
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
46 globals()['tempdir'] = tempfile.mkdtemp() |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
47 os.mkdir(subdir) |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
48 url = url.rstrip('/') |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
49 for filename in files: |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
50 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
|
51 content = file(f).read() |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
52 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
|
53 o = file(outfile, 'w') |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
54 print >> o, content |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
55 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
|
56 |
23
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
57 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
|
58 |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
59 try: |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
60 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
|
61 sys.path.append(t) |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
62 import tempita |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
63 except: |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
64 cleanup() |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
65 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
|
66 |
9
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
67 # 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
|
68 shebang_re = '#!.*makeitso.*' |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
69 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
|
70 |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
71 ### URIs |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
72 |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
73 def parent_uri(uri): |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
74 """parent resource of a URI""" |
138
169e02e190ef
peg to tempita version and eliminate cruft
Jeff Hammel <jhammel@mozilla.com>
parents:
128
diff
changeset
|
75 |
26
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
76 if '://' in uri: |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
77 return uri.rsplit('/', 1)[0] + '/' |
27 | 78 else: |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
79 here = os.path.dirname(os.path.abspath(uri)) |
27 | 80 here = here.rstrip(os.path.sep) + os.path.sep |
81 return here | |
26
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
82 |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
83 def basename(uri): |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
84 """return the basename of a URI""" |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
85 if '://' in uri: |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
86 return uri.rsplit('/', 1)[1] |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
87 else: |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
88 return os.path.basename(uri) |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
89 |
26
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
90 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
|
91 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
|
92 return file(f).read() |
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
93 |
30
1549be7f0fcb
dont point at trunk tempital; as expected, hg repos dont work with setuptools :(
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
94 ### 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
|
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 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
|
97 """exception for (non-interactive) missing variables""" |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
98 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
|
99 self.missing = missing |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
100 message = 'Missing variables: %s' % ', '.join(missing) |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
101 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
|
102 |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
103 def get_missing(name_error): |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
104 """ |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
105 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
|
106 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
|
107 you a message: |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
108 >>> try: |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
109 ... eval('2*foo') |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
110 ... except Exception, e: |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
111 ... pass |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
112 """ |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
113 message = name_error.args[0] |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
114 varname = message.split("'")[1] |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
115 return varname |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
116 |
33
190f310f2f5e
more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
117 ### template classes |
190f310f2f5e
more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
118 |
190f310f2f5e
more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
119 class ContentTemplate(tempita.Template): |
190f310f2f5e
more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
120 """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
|
121 |
33
190f310f2f5e
more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
122 defaults = {'include': include} |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
123 |
55
b087a14a664b
fix several issues involving variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
54
diff
changeset
|
124 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
|
125 |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
126 # default variables |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
127 self.defaults = self.__class__.defaults.copy() |
55
b087a14a664b
fix several issues involving variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
54
diff
changeset
|
128 self.defaults.update(variables or {}) |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
129 |
33
190f310f2f5e
more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
130 # 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
|
131 self.interactive = interactive |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
132 |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
133 tempita.Template.__init__(self, content, name=name) |
33
190f310f2f5e
more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
134 |
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
|
135 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
|
136 """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
|
137 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
|
138 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
|
139 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
|
140 |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
141 def missing(self, **variables): |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
142 """return additional variables needed""" |
55
b087a14a664b
fix several issues involving variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
54
diff
changeset
|
143 vars = self.get_variables(**variables) |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
144 missing = set([]) |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
145 while True: |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
146 try: |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
147 tempita.Template.substitute(self, **vars) |
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 except NameError, e: |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
150 missed = get_missing(e) |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
151 missing.add(missed) |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
152 vars[missed] = '' |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
153 return missing |
44
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
154 |
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
155 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
|
156 """ |
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
157 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
|
158 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
|
159 """ |
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
160 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
|
161 if missing: |
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
162 if self.interactive: |
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
163 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
|
164 else: |
6e08cca7d656
do API variable reading and stubbing a bit for control flow
Jeff Hammel <jhammel@mozilla.com>
parents:
42
diff
changeset
|
165 raise MissingVariablesException(missing) |
101 | 166 |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
167 def variables(self): |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
168 """return the variables needed for a template""" |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
169 return self.missing() |
33
190f310f2f5e
more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
170 |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
171 def substitute(self, **variables): |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
172 """interactive (for now) substitution""" |
54
d3e1e5745f24
pass the variables, fixes tests
Jeff Hammel <jhammel@mozilla.com>
parents:
53
diff
changeset
|
173 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
|
174 self.check_missing(vars) |
90
26b9c3bba04e
make the api for substitute() variables, output
Jeff Hammel <jhammel@mozilla.com>
parents:
89
diff
changeset
|
175 return tempita.Template.substitute(self, **vars) |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
176 |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
177 def read_variables(self, variables): |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
178 """read variables from stdin""" |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
179 retval = {} |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
180 for i in variables: |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
181 print 'Enter %s: ' % i, |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
182 retval[i] = raw_input() |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
183 return retval |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
184 |
33
190f310f2f5e
more stubbing of template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
185 |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
186 class URITemplate(ContentTemplate): |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
187 """template for a file or URL""" |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
188 |
88
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
189 def __init__(self, uri, interactive=True, variables=None): |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
190 content = include(uri) |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
191 |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
192 # remove makeitso shebang if it has one |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
193 if shebang_re.match(content): |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
194 content = os.linesep.join(content.splitlines()[1:]) |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
195 |
56
728cae02a6ed
* fix another variable-related bug
Jeff Hammel <jhammel@mozilla.com>
parents:
55
diff
changeset
|
196 variables = variables or {} |
76
7909dfaef33b
note about adding more metadata
Jeff Hammel <jhammel@mozilla.com>
parents:
63
diff
changeset
|
197 if 'here' not in variables: |
7909dfaef33b
note about adding more metadata
Jeff Hammel <jhammel@mozilla.com>
parents:
63
diff
changeset
|
198 variables['here'] = parent_uri(uri) |
7909dfaef33b
note about adding more metadata
Jeff Hammel <jhammel@mozilla.com>
parents:
63
diff
changeset
|
199 # TODO: could add other metadata about the uri, |
7909dfaef33b
note about adding more metadata
Jeff Hammel <jhammel@mozilla.com>
parents:
63
diff
changeset
|
200 # such as last modification time' |
7909dfaef33b
note about adding more metadata
Jeff Hammel <jhammel@mozilla.com>
parents:
63
diff
changeset
|
201 |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
202 ContentTemplate.__init__(self, content, name=uri, |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
203 interactive=interactive, |
55
b087a14a664b
fix several issues involving variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
54
diff
changeset
|
204 variables=variables) |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
205 |
90
26b9c3bba04e
make the api for substitute() variables, output
Jeff Hammel <jhammel@mozilla.com>
parents:
89
diff
changeset
|
206 def substitute(self, variables, output=None): |
101 | 207 |
208 # interpolate | |
88
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
209 content = ContentTemplate.substitute(self, **variables) |
90
26b9c3bba04e
make the api for substitute() variables, output
Jeff Hammel <jhammel@mozilla.com>
parents:
89
diff
changeset
|
210 |
26b9c3bba04e
make the api for substitute() variables, output
Jeff Hammel <jhammel@mozilla.com>
parents:
89
diff
changeset
|
211 # write output |
88
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
212 output = output or sys.stdout |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
213 if isinstance(output, basestring): |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
214 path = output |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
215 if os.path.isdir(output): |
87
3571417ef92e
interpolate file permissions
Jeff Hammel <jhammel@mozilla.com>
parents:
86
diff
changeset
|
216 path = os.path.join(path, basename(self.name)) |
3571417ef92e
interpolate file permissions
Jeff Hammel <jhammel@mozilla.com>
parents:
86
diff
changeset
|
217 f = file(path, 'w') |
88
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
218 print >> f, content |
37
7cee2869bd7b
stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
219 f.close() |
87
3571417ef92e
interpolate file permissions
Jeff Hammel <jhammel@mozilla.com>
parents:
86
diff
changeset
|
220 try: |
88
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
221 os.chmod(path, os.stat(self.name).st_mode) |
87
3571417ef92e
interpolate file permissions
Jeff Hammel <jhammel@mozilla.com>
parents:
86
diff
changeset
|
222 except: |
3571417ef92e
interpolate file permissions
Jeff Hammel <jhammel@mozilla.com>
parents:
86
diff
changeset
|
223 pass |
37
7cee2869bd7b
stub out directory example
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
224 else: |
88
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
225 # file handler |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
226 print >> output, content |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
227 |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
228 |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
229 class DirectoryTemplate(ContentTemplate): |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
230 """template for a directory structure""" |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
231 |
88
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
232 def __init__(self, directory, interactive=True, variables=None): |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
233 """ |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
234 - 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
|
235 """ |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
236 assert os.path.isdir(directory) |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
237 self.name = directory |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
238 self.interactive = interactive |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
239 self.defaults = ContentTemplate.defaults.copy() |
55
b087a14a664b
fix several issues involving variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
54
diff
changeset
|
240 self.defaults.update(variables or {}) |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
241 |
88
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
242 def check_output(self, output): |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
243 """ |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
244 checks output for validity |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
245 """ |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
246 assert output # must provide output |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
247 if os.path.exists(output): |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
248 assert os.path.isdir(output), "%s: Must be a directory" % self.name |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
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: |
128 | 264 missed = ContentTemplate(f).missing(**vars) |
90
26b9c3bba04e
make the api for substitute() variables, output
Jeff Hammel <jhammel@mozilla.com>
parents:
89
diff
changeset
|
265 missing.update(missed) |
26b9c3bba04e
make the api for substitute() variables, output
Jeff Hammel <jhammel@mozilla.com>
parents:
89
diff
changeset
|
266 variables.update(dict([(i, '') for i in missed])) |
26b9c3bba04e
make the api for substitute() variables, output
Jeff Hammel <jhammel@mozilla.com>
parents:
89
diff
changeset
|
267 |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
268 path = os.path.join(dirpath, f) |
54
d3e1e5745f24
pass the variables, fixes tests
Jeff Hammel <jhammel@mozilla.com>
parents:
53
diff
changeset
|
269 template = URITemplate(path, interactive=self.interactive) |
55
b087a14a664b
fix several issues involving variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
54
diff
changeset
|
270 missed = template.missing(**vars) |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
271 missing.update(missed) |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
272 variables.update(dict([(i, '') for i in missed])) |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
273 |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
274 return missing |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
275 |
90
26b9c3bba04e
make the api for substitute() variables, output
Jeff Hammel <jhammel@mozilla.com>
parents:
89
diff
changeset
|
276 def substitute(self, variables, output): |
88
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
277 self.check_output(output) |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
278 vars = self.get_variables(**variables) |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
279 self.check_missing(vars) |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
280 |
39
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
281 # 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
|
282 # 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
|
283 |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
284 # make output directory if necessary |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
285 if output and not os.path.exists(output): |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
286 os.makedirs(output) |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
287 |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
288 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
|
289 |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
290 # interpolate directory names |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
291 for d in dirnames: |
39
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
292 path = os.path.join(dirname, d) |
101 | 293 interpolated = ContentTemplate(path).substitute(**vars) |
88
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
294 target = os.path.join(output, interpolated.split(self.name, 1)[-1].strip(os.path.sep)) |
39
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 if os.path.exists(target): |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
297 # ensure its a directory |
39
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
298 # 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
|
299 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
|
300 else: |
39
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
301 os.makedirs(target) |
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
302 |
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
303 # interpolate files |
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
304 for filename in filenames: |
90
26b9c3bba04e
make the api for substitute() variables, output
Jeff Hammel <jhammel@mozilla.com>
parents:
89
diff
changeset
|
305 |
26b9c3bba04e
make the api for substitute() variables, output
Jeff Hammel <jhammel@mozilla.com>
parents:
89
diff
changeset
|
306 # interpolate filenames |
39
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
307 path = os.path.join(dirname, filename) |
101 | 308 interpolated = ContentTemplate(path).substitute(**vars) |
88
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
309 target = os.path.join(output, interpolated.split(self.name, 1)[-1].strip(os.path.sep)) |
90
26b9c3bba04e
make the api for substitute() variables, output
Jeff Hammel <jhammel@mozilla.com>
parents:
89
diff
changeset
|
310 |
26b9c3bba04e
make the api for substitute() variables, output
Jeff Hammel <jhammel@mozilla.com>
parents:
89
diff
changeset
|
311 # interpolate their contents |
39
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
312 if os.path.exists(target): |
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
313 # ensure its a directory |
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
314 assert os.path.isfile(target), "Can't substitute a file on top of a directory" |
88
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
315 template = URITemplate(path, interactive=False) |
101 | 316 template.substitute(vars, target) |
39
a2cdce0108e1
get directory substitution sorta working; start stubbing mixed case
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
317 |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
318 |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
319 class PolyTemplate(ContentTemplate): |
113
c3b8ce33d3ad
make variable getting logic a little less horrible
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
320 """aggregate templates""" |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
321 |
88
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
322 def __init__(self, templates, interactive=True, variables=None): |
54
d3e1e5745f24
pass the variables, fixes tests
Jeff Hammel <jhammel@mozilla.com>
parents:
53
diff
changeset
|
323 self.interactive = interactive |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
324 self.templates = [] |
107 | 325 entry_points = get_entry_points() |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
326 for template in templates: |
94
b6a46332cced
allow Polytemplates to take template objects
Jeff Hammel <jhammel@mozilla.com>
parents:
93
diff
changeset
|
327 if isinstance(template, basestring): |
b6a46332cced
allow Polytemplates to take template objects
Jeff Hammel <jhammel@mozilla.com>
parents:
93
diff
changeset
|
328 # TODO: check if the template is a [e.g] PasteScript.template entry point |
b6a46332cced
allow Polytemplates to take template objects
Jeff Hammel <jhammel@mozilla.com>
parents:
93
diff
changeset
|
329 if os.path.isdir(template): |
b6a46332cced
allow Polytemplates to take template objects
Jeff Hammel <jhammel@mozilla.com>
parents:
93
diff
changeset
|
330 self.templates.append(DirectoryTemplate(template, interactive=self.interactive, variables=variables)) |
107 | 331 elif not os.path.exists(template) and template in entry_points: |
332 self.templates.append(entry_points[template](interactive=interactive, variables=variables)) | |
94
b6a46332cced
allow Polytemplates to take template objects
Jeff Hammel <jhammel@mozilla.com>
parents:
93
diff
changeset
|
333 else: |
b6a46332cced
allow Polytemplates to take template objects
Jeff Hammel <jhammel@mozilla.com>
parents:
93
diff
changeset
|
334 self.templates.append(URITemplate(template, interactive=self.interactive, variables=variables)) |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
335 else: |
94
b6a46332cced
allow Polytemplates to take template objects
Jeff Hammel <jhammel@mozilla.com>
parents:
93
diff
changeset
|
336 # assume the template is an object that conforms to the API |
b6a46332cced
allow Polytemplates to take template objects
Jeff Hammel <jhammel@mozilla.com>
parents:
93
diff
changeset
|
337 self.templates.append(template) |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
338 |
112 | 339 def get_variables(self, **variables): |
340 vars = variables.copy() | |
341 for template in self.templates: | |
342 vars.update(template.get_variables()) | |
343 return vars | |
344 | |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
345 def missing(self, **variables): |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
346 vars = variables.copy() |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
347 missing = set([]) |
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 missed = template.missing(**vars) |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
350 missing.update(missed) |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
351 vars.update(dict([(i, '') for i in missed])) |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
352 return missing |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
353 |
88
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
354 def check_output(self, output): |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
355 if output and isinstance(output, basestring) and os.path.exists(output) and len(self.templates) > 1: |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
356 assert os.path.isdir(output), "Must specify a directory for multiple templates" |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
357 for template in self.templates: |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
358 if hasattr(template, 'check_output'): |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
359 template.check_output(output) |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
360 |
90
26b9c3bba04e
make the api for substitute() variables, output
Jeff Hammel <jhammel@mozilla.com>
parents:
89
diff
changeset
|
361 def substitute(self, variables, output=None): |
28
dc18d6db4956
depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
362 |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
363 # determine where the hell to put these things |
88
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
364 self.check_output(output) |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
365 |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
366 # get the variables |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
367 vars = self.get_variables(**variables) |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
368 self.check_missing(vars) |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
369 |
114
b8d5d2041fe0
stub out project name -> package name
Jeff Hammel <jhammel@mozilla.com>
parents:
113
diff
changeset
|
370 # make the output directory if multiple templates |
88
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
371 if output and len(self.templates) > 1 and not os.path.exists(output): |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
372 os.makedirs(output) |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
373 |
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
374 # do the substitution |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
375 for template in self.templates: |
90
26b9c3bba04e
make the api for substitute() variables, output
Jeff Hammel <jhammel@mozilla.com>
parents:
89
diff
changeset
|
376 template.substitute(vars, output) |
138
169e02e190ef
peg to tempita version and eliminate cruft
Jeff Hammel <jhammel@mozilla.com>
parents:
128
diff
changeset
|
377 |
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
|
378 ### command line interface |
28
dc18d6db4956
depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
379 |
103
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
380 def read_config(config_files, templates=()): |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
381 """read variables from a set of .ini files""" |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
382 retval = {} |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
383 parser = RawConfigParser() |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
384 parser.read(config_files) |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
385 retval.update(dict(parser.items('DEFAULT'))) |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
386 for template in templates: |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
387 if template in parser.sections(): |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
388 retval.update(dict(parser.items(template))) |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
389 return retval |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
390 |
9
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
391 def invocation(url, **variables): |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
392 """returns a string appropriate for TTW invocation""" |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
393 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
|
394 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
|
395 |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
396 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
|
397 |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
398 # create option parser |
9
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
399 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
|
400 parser = OptionParser(usage, description=__doc__) |
28
dc18d6db4956
depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
401 |
103
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
402 # find dotfile |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
403 dotfile = None |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
404 if 'HOME' in os.environ: |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
405 dotfile = os.path.join(os.environ['HOME'], '.makeitso') |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
406 if not (os.path.exists(dotfile) and os.path.isfile(dotfile)): |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
407 dotfile = None |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
408 |
138
169e02e190ef
peg to tempita version and eliminate cruft
Jeff Hammel <jhammel@mozilla.com>
parents:
128
diff
changeset
|
409 parser.add_option('-[', '--start-braces', dest='start_braces', |
169e02e190ef
peg to tempita version and eliminate cruft
Jeff Hammel <jhammel@mozilla.com>
parents:
128
diff
changeset
|
410 help='starting delimeter') |
169e02e190ef
peg to tempita version and eliminate cruft
Jeff Hammel <jhammel@mozilla.com>
parents:
128
diff
changeset
|
411 parser.add_option('-]', '--end-braces', dest='end_braces', |
169e02e190ef
peg to tempita version and eliminate cruft
Jeff Hammel <jhammel@mozilla.com>
parents:
128
diff
changeset
|
412 help='starting delimeter') |
28
dc18d6db4956
depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
413 |
dc18d6db4956
depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
414 # options about where to put things |
dc18d6db4956
depend on trunk tempita; stubbing for next stage
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
415 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
|
416 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
|
417 |
93
d5da38fabdf7
note command line options needed for variable handling
Jeff Hammel <jhammel@mozilla.com>
parents:
90
diff
changeset
|
418 # options for (.ini) variables |
103
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
419 parser.add_option('-c', '--config', dest='config', |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
420 default=[], action='append', |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
421 help='.ini config files to read variables from') |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
422 if dotfile: |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
423 parser.add_option('--no-defaults', dest='use_defaults', |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
424 default=True, action='store_false', |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
425 help="don't read ~/.makeitso") |
104 | 426 |
427 # TODO | |
93
d5da38fabdf7
note command line options needed for variable handling
Jeff Hammel <jhammel@mozilla.com>
parents:
90
diff
changeset
|
428 # parser.add_option('-u', '--update', dest='update', |
d5da38fabdf7
note command line options needed for variable handling
Jeff Hammel <jhammel@mozilla.com>
parents:
90
diff
changeset
|
429 # help="update the specified .ini file for variables read from this run") |
d5da38fabdf7
note command line options needed for variable handling
Jeff Hammel <jhammel@mozilla.com>
parents:
90
diff
changeset
|
430 # parser.add_option('-U', '--Update', dest='Update', |
d5da38fabdf7
note command line options needed for variable handling
Jeff Hammel <jhammel@mozilla.com>
parents:
90
diff
changeset
|
431 # help="same as -u, but update the global [DEFAULTS] section") |
d5da38fabdf7
note command line options needed for variable handling
Jeff Hammel <jhammel@mozilla.com>
parents:
90
diff
changeset
|
432 # parser.add_option('--dry-run', dest='dry_run', |
d5da38fabdf7
note command line options needed for variable handling
Jeff Hammel <jhammel@mozilla.com>
parents:
90
diff
changeset
|
433 # default=False, action='store_true', |
d5da38fabdf7
note command line options needed for variable handling
Jeff Hammel <jhammel@mozilla.com>
parents:
90
diff
changeset
|
434 # help="don't actually do the substitution but do everything else") |
d5da38fabdf7
note command line options needed for variable handling
Jeff Hammel <jhammel@mozilla.com>
parents:
90
diff
changeset
|
435 |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
436 # options for getting information |
9
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
437 parser.add_option('--commandline', dest='commandline', |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
438 action='store_true', default=False, |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
439 help="print the commandline to invoke this script TTW") |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
440 parser.add_option('--variables', dest='variables', |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
441 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
|
442 help='print the variables in a template') |
110
613e9c19a98c
add a --list-templates command
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
443 entry_points = get_entry_points() |
613e9c19a98c
add a --list-templates command
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
444 if entry_points: |
613e9c19a98c
add a --list-templates command
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
445 parser.add_option('--list-templates', dest='list_templates', |
613e9c19a98c
add a --list-templates command
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
446 action='store_true', default=False, |
613e9c19a98c
add a --list-templates command
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
447 help="list installed MakeItSo! templates") |
613e9c19a98c
add a --list-templates command
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
448 |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
449 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
|
450 |
110
613e9c19a98c
add a --list-templates command
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
451 # list the templates |
613e9c19a98c
add a --list-templates command
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
452 if entry_points and options.list_templates: |
613e9c19a98c
add a --list-templates command
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
453 for key in sorted(entry_points.keys()): |
613e9c19a98c
add a --list-templates command
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
454 template_class = entry_points[key] |
613e9c19a98c
add a --list-templates command
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
455 description = getattr(template_class, 'description', '') |
613e9c19a98c
add a --list-templates command
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
456 description = description or getattr(template_class, '__doc__', '') |
613e9c19a98c
add a --list-templates command
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
457 description = description.strip() |
613e9c19a98c
add a --list-templates command
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
458 print key + ': ' + description |
613e9c19a98c
add a --list-templates command
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
459 return |
613e9c19a98c
add a --list-templates command
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
460 |
9
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
461 # 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
|
462 if options.variables: |
9
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
463 |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
464 # makes no sense without a template |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
465 if not args: |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
466 parser.print_usage() |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
467 parser.exit() |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
468 |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
469 # find all variables |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
470 template = PolyTemplate(templates=args) |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
471 variables = template.variables() |
9
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
472 |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
473 # print them |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
474 for variable in sorted(variables): |
9
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
475 print variable |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
476 return |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
477 |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
478 # template variables |
35
7e47ff4b0cd3
started writing tests; what a surprise, everything is broken ;)
Jeff Hammel <jhammel@mozilla.com>
parents:
34
diff
changeset
|
479 variables = {} |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
480 _args = [] |
103
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
481 |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
482 # read variables from configuration |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
483 config_files = options.config |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
484 if dotfile and options.use_defaults: |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
485 config_files.insert(0, dotfile) |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
486 if config_files: |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
487 variables.update(read_config(config_files)) |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
488 |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
489 # override with variables from the command line |
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
490 _variables = {} |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
491 for arg in args: |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
492 if '=' in arg: |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
493 key, value = arg.split('=') |
103
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
494 _variables[key] = value |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
495 else: |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
496 _args.append(arg) |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
497 args = _args |
103
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
498 variables.update(_variables) |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
499 |
9
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
500 # print TTW commandline for invocation |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
501 if options.commandline: |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
502 if args: |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
503 for arg in args: |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
504 print invocation(arg, **variables) |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
505 else: |
26
044ac9e0b29c
add a non-working example with an include clause
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
506 print invocation('[URI]', **variables) |
9
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
507 return |
a77630b2b491
* add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
508 |
103
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
509 |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
510 # get the content |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
511 if args: |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
512 template = PolyTemplate(templates=args, |
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
513 variables=variables) |
103
a6aff990985b
do rudimentary interpretation of config files
Jeff Hammel <jhammel@mozilla.com>
parents:
101
diff
changeset
|
514 template.substitute({}, output=options.output) |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
515 else: |
34
46c2d0a7335a
continued refactor to have template classes
Jeff Hammel <jhammel@mozilla.com>
parents:
33
diff
changeset
|
516 template = ContentTemplate(sys.stdin.read(), variables=variables) |
88
712a6d358083
fixed output broke other things
Jeff Hammel <jhammel@mozilla.com>
parents:
87
diff
changeset
|
517 print template.substitute() |
23
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
518 |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
519 # cleanup |
776805790c84
stub out getting tempita from the net; unttested
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
520 cleanup() |
138
169e02e190ef
peg to tempita version and eliminate cruft
Jeff Hammel <jhammel@mozilla.com>
parents:
128
diff
changeset
|
521 |
5
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
522 if __name__ == '__main__': |
f064be514e53
choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
523 main() |
106
1295df1700a4
move stubbing around...maybe last minute will work better? ::shrug::
Jeff Hammel <jhammel@mozilla.com>
parents:
105
diff
changeset
|
524 |
1295df1700a4
move stubbing around...maybe last minute will work better? ::shrug::
Jeff Hammel <jhammel@mozilla.com>
parents:
105
diff
changeset
|
525 |
1295df1700a4
move stubbing around...maybe last minute will work better? ::shrug::
Jeff Hammel <jhammel@mozilla.com>
parents:
105
diff
changeset
|
526 # get templates from pkg_resources |
1295df1700a4
move stubbing around...maybe last minute will work better? ::shrug::
Jeff Hammel <jhammel@mozilla.com>
parents:
105
diff
changeset
|
527 # (MakeItSo! and [TODO] pastescript templates) |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
107
diff
changeset
|
528 # this should go last to ensure the module is wholly loaded [?] |
107 | 529 def get_entry_points(): |
530 retval = {} | |
106
1295df1700a4
move stubbing around...maybe last minute will work better? ::shrug::
Jeff Hammel <jhammel@mozilla.com>
parents:
105
diff
changeset
|
531 try: |
1295df1700a4
move stubbing around...maybe last minute will work better? ::shrug::
Jeff Hammel <jhammel@mozilla.com>
parents:
105
diff
changeset
|
532 from pkg_resources import iter_entry_points |
1295df1700a4
move stubbing around...maybe last minute will work better? ::shrug::
Jeff Hammel <jhammel@mozilla.com>
parents:
105
diff
changeset
|
533 for i in iter_entry_points('makeitso.templates'): |
107 | 534 try: |
535 retval[i.name] = i.load() | |
536 except: | |
537 continue | |
106
1295df1700a4
move stubbing around...maybe last minute will work better? ::shrug::
Jeff Hammel <jhammel@mozilla.com>
parents:
105
diff
changeset
|
538 |
1295df1700a4
move stubbing around...maybe last minute will work better? ::shrug::
Jeff Hammel <jhammel@mozilla.com>
parents:
105
diff
changeset
|
539 except ImportError: |
107 | 540 return retval |
541 return retval |