annotate makeitso/makeitso.py @ 16:1818cc524cde

change API making substitute return the substituted quantity
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 15 Nov 2010 18:28:10 -0800
parents edabb0bfe107
children c879b93c3f15
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
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 import os
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 import re
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 import subprocess
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 import sys
15
edabb0bfe107 add method to work TTW
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
10 import urllib
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 from optparse import OptionParser
12
a6fd93354f23 add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
13
a6fd93354f23 add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
14 try:
a6fd93354f23 add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
15 import tempita
a6fd93354f23 add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
16 except ImportError:
a6fd93354f23 add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
17 raise NotImplementedError
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
19 # URL of this file
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
20 location = 'http://k0s.org/mozilla/hg/MakeItSo/raw-file/tip/makeitso/makeitso.py'
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
21
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
22 # 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
23 shebang_re = '#!.*makeitso.*'
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 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
25
6
ac78e26cd568 starting on web example; i have to understand more about shebangs
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
26 class MissingVariablesException(Exception):
ac78e26cd568 starting on web example; i have to understand more about shebangs
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
27 def __init__(self, message, missing):
ac78e26cd568 starting on web example; i have to understand more about shebangs
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
28 self.missing = missing
ac78e26cd568 starting on web example; i have to understand more about shebangs
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
29
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 def call(command, *args, **kw):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 code = subprocess.call(command, *args, **kw)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 if code:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 if isinstance(command, basestring):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 cmdstr = command
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 else:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 cmdstr = ' '.join(command)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 raise SystemExit("Command `%s` exited with code %d" % (cmdstr, code))
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 def get_missing(name_error):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 """
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 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
42 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
43 you a message:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 >>> try:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 ... eval('2*foo')
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 ... except Exception, e:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 ... pass
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 """
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 message = name_error.args[0]
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 varname = message.split("'")[1]
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 return varname
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53 def missing_variables(template, variables):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54 """return additional variables needed"""
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 vars = variables.copy()
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 missing = set([])
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57 while True:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 try:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 template.substitute(**vars)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 return missing
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 except NameError, e:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62 missed = get_missing(e)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
63 missing.add(missed)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
64 vars[missed] = ''
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65 return missing
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
66
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
67 def template_variables(template):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
68 """return the variables needed for a template"""
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
69 return missing_variables(template, {})
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
70
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
71 def read_variables(variables):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
72 retval = {}
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
73 for i in variables:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
74 print 'Enter %s: ' % i,
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
75 retval[i] = raw_input()
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
76 return retval
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
77
16
1818cc524cde change API making substitute return the substituted quantity
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
78 def substitute(content, variables=None):
1818cc524cde change API making substitute return the substituted quantity
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
79 """interactive (for now) substitution"""
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
80
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
81 # remove makeitso shebang if it has one
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
82 if shebang_re.match(content):
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
83 content = os.linesep.join(content.splitlines()[1:])
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
84
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
85 variables = variables or {}
13
99588f138993 use full dot path to template
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
86 template = tempita.Template(content)
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
87 missing = missing_variables(template, variables)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
88 if missing:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
89 # TODO: add a switch for interactive or not
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
90 variables.update(read_variables(missing))
16
1818cc524cde change API making substitute return the substituted quantity
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
91 return template.substitute(**variables)
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
92
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
93 def invocation(url, **variables):
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
94 """returns a string appropriate for TTW invocation"""
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
95 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
96 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
97
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
98 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
99
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
100 # create option parser
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
101 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
102 parser = OptionParser(usage, description=__doc__)
12
a6fd93354f23 add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
103 parser.add_option('-[', '--start-braces', dest='start_braces',
a6fd93354f23 add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
104 help='starting delimeter')
a6fd93354f23 add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
105 parser.add_option('-]', '--end-braces', dest='end_braces',
a6fd93354f23 add options for starting and ending braces (unused)
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
106 help='starting delimeter')
15
edabb0bfe107 add method to work TTW
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
107 parser.add_option('--in-place', dest='in_place',
edabb0bfe107 add method to work TTW
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
108 action='store_true', default=False,
edabb0bfe107 add method to work TTW
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
109 help='interpret files in place') # TODO: unused
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
110 parser.add_option('--commandline', dest='commandline',
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
111 action='store_true', default=False,
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
112 help="print the commandline to invoke this script TTW")
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
113 parser.add_option('--variables', dest='variables',
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
114 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
115 help='print the variables in a template')
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
116 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
117
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
118 # 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
119 if options.variables:
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
120
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
121 # makes no sense without a template
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
122 if not args:
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
123 parser.print_usage()
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
124 parser.exit()
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
125
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
126 # find all variables
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
127 variables = set()
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
128 for arg in args:
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
129 content = file(arg).read()
13
99588f138993 use full dot path to template
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
130 template = tempita.Template(content)
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
131 variables.update(template_variables(template))
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
132
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
133 # print them
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
134 for variable in variables:
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
135 print variable
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
136 return
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
137
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
138 # template variables
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
139 variables = {}
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
140 _vars = []
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
141 _args = []
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
142 for arg in args:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
143 if '=' in arg:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
144 key, value = arg.split('=')
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
145 variables[key] = value
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
146 else:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
147 _args.append(arg)
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
148 args = _args
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
149
9
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
150 # print TTW commandline for invocation
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
151 if options.commandline:
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
152 if args:
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
153 for arg in args:
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
154 print invocation(arg, **variables)
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
155 else:
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
156 print invocation('[URL]', **variables)
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
157 return
a77630b2b491 * add commandline to print commandlines
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
158
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
159 # get the content
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
160 if args:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
161 for arg in args:
15
edabb0bfe107 add method to work TTW
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
162 f, headers = urllib.urlretrieve(arg)
edabb0bfe107 add method to work TTW
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
163 content = file(f).read()
16
1818cc524cde change API making substitute return the substituted quantity
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
164 print substitute(content, variables=variables)
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
165 else:
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
166 content = sys.stdin.read()
16
1818cc524cde change API making substitute return the substituted quantity
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
167 print substitute(content, variables=variables)
5
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
168
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
169 if __name__ == '__main__':
f064be514e53 choose a better filename and remove some stuff from shebang
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
170 main()