-
Notifications
You must be signed in to change notification settings - Fork 0
/
weave.sage
executable file
·369 lines (307 loc) · 11.1 KB
/
weave.sage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/local/bin/sage
import argparse
import os
import re
import sys
import traceback
from cStringIO import StringIO
VERSION = "0.9"
AUTOGENERATE_MSG = [
"%" + ("-" * 78),
"% This file was *auto-generated* by sage-weave.",
"% Do NOT edit unless you know what you are doing.",
"%" + ("-" * 78),
"%"
]
QUIET = False
VERBOSE = False
def error(string):
sys.exit(string)
def info(string):
if not QUIET:
sys.stderr.write(string + '\n')
sys.stderr.flush()
def debug(string):
if not QUIET and VERBOSE:
sys.stderr.write(string + '\n')
sys.stderr.flush()
class SageWeaveException(Exception):
"""Exception thrown when compiling or running weaved Sage code."""
def __init__(self, exception, interpreter, trace = None):
super(Exception, self).__init__()
self.exception = exception
self.interpreter = interpreter
self.trace = trace
self.message = "%s: %s" % (type(self.exception).__name__, self.exception.message)
def __repr__(self):
return "SageWeaveException: " + self.message
def __str__(self):
return self.message
def traceback(self):
"""Returns stack trace"""
lines = []
for filename, lineno, func, _ in self.trace:
trace_snippet = self.interpreter.get_snippet_from_id(filename)
if trace_snippet:
real_lineno = lineno + trace_snippet.line_no
lines.append(' File "%s", line %d, in %s:' \
% (self.interpreter.filename, real_lineno, func))
lines.append(trace_snippet.format(mark = lineno))
else:
lines.append(' File "%s", line %d, in %s' % (filename, lineno, func))
return "\n".join(lines)
class SageWeaveSyntaxError(SageWeaveException):
"""Exception thrown when a syntax error occurs while compiling weaved Sage code."""
def __init__(self, exception, interpreter, funcname = "?"):
filename = exception.filename
lineno = exception.lineno
trace = [(filename, lineno, funcname, None)]
exception.message = exception.msg
super(SageWeaveSyntaxError, self).__init__(exception, interpreter, trace)
class LatexTokenReader(SageObject):
"""Latex token reader. Reads one LaTeX token."""
QUOTE="'"
DQUOTE='"'
def __init__(self):
self.stack = []
self.string = ""
self.in_string = False
def consume(self, char):
if len(self.stack) > 0 and char == self.stack[-1]:
del self.stack[-1]
self.in_string = False
elif not self.in_string and char == self.DQUOTE:
self.stack.append(self.DQUOTE)
self.in_string = True
elif not self.in_string and char == self.QUOTE:
self.stack.append(self.QUOTE)
self.in_string = True
elif not self.in_string and char == "{":
self.stack.append("}")
self.string = self.string + char
return len(self.stack) > 0
def readToken(self, string, start_index = 0):
index = start_index
sage_expr = ""
while True:
sage_expr += string[index]
if not self.consume(string[index]):
break
index += 1
if index >= len(string):
raise Exception("Unexpected end of line")
return sage_expr
class Snippet:
"""Code snippet embedded in weaved file."""
last_snippet_id = [0]
def __init__(self, code, line_no):
self.code = code
self.line_no = line_no
self.last_snippet_id[0] += 1
self.id = self.last_snippet_id[0]
def format(self, mark = None):
lines = []
for i, line in enumerate(self.code):
marker = ">>>" if i + 1 == mark else ""
real_lineno = i + self.line_no + 1
lines.append('%3s %3d: %s' % (marker, real_lineno, line))
return "\n".join(lines)
def compile(self, mode):
# Pre-parse the Sage code into Python code
preparsed_code = preparse("\n".join(self.code))
# Compile Python code
return compile(preparsed_code, '<sageweave#%d>' % self.id, mode)
def parse_sage_expressions(string, evalFn):
"""Takes a string and evaluates all \sageexpr{} expressions in it."""
output = ""
command = r'\sageexpr'
while True:
index = string.find(command)
if index == -1:
break
reader = LatexTokenReader()
sage_expr = reader.readToken(string, index + len(command))
output += string[:index]
output += str(evalFn(sage_expr[1:-1]))
string = string[index + len(command) + len(sage_expr):]
output += string
return output
def delete_common_indentation(lines):
"""Deletes any common indentation of a given list of strings, i.e., if all
non-empty lines start with k spaces, the first k characters of each non-empty
line are removed."""
spaces = [len(line) - len(line.lstrip(' ')) for line in lines if len(line.strip()) > 0]
if len(spaces) == 0: return lines
start = min(spaces)
if start == 0: return lines
new_lines = []
for line in lines:
if line.strip() != "":
line = line[start:]
new_lines.append(line)
return new_lines
class Interpreter:
"""Interpreter for Sage-weave files."""
def __init__(self, input_stream, output_stream, filename = '?'):
self.input_stream = input_stream
self.output_stream = output_stream
self.line_no = 0
self.snippets = {}
# All weaved code runs in a shared scope. Set up the scope by setting the
# __file__ attibute and importing Sage into the weave scope.
self.scope = {'__file__': filename}
exec "from sage.all_cmdline import *" in self.scope
self.filename = os.path.basename(filename)
def get_line(self):
"""Reads one line from the input stream."""
self.line_no += 1
line = self.input_stream.readline()
if not line: return None
return line.rstrip('\n')
def output(self, string):
"""Writes the given string to the output stream."""
self.output_stream.write(string)
def output_ln(self, string):
"""Writes the given string, followed by a newline, to the output stream."""
self.output_stream.write(string)
self.output_stream.write("\n")
def get_snippet_from_id(self, id):
"""Gets the snippet with the given file id (of the form <sageweave#lineno>)."""
match = re.search(r'<sageweave#(\d+)>', id)
if match and match.lastindex:
index = int(match.group(match.lastindex))
return self.snippets[index]
else:
return None
def new_snippet(self, code, line_start):
snippet = Snippet(code, line_start)
self.snippets[snippet.id] = snippet
return snippet
def weave_code(self):
# Gather Sage code to be executed
line_no_start = self.line_no
code = []
while True:
line = self.get_line()
if line == None:
raise SageWeaveException("Unexpected end of file", self)
if line.strip() == "@":
break
code.append(line)
# Remove any common indentation
code = delete_common_indentation(code)
# Store snippet
snippet = self.new_snippet(code, line_no_start)
# Capture any output
old_stdout = sys.stdout
capture_stdout = StringIO()
# Execute the Python code
debug("Executing Sage snippet at line %d" % line_no_start)
sys.stdout = capture_stdout
try:
exec(snippet.compile('exec'), self.scope)
except SyntaxError, e:
raise SageWeaveSyntaxError(e, self)
except Exception, e:
# Generate stack trace and re-throw
_, _, tb = sys.exc_info()
trace = traceback.extract_tb(tb)[1:]
raise SageWeaveException(e, self, trace)
finally:
# Restore stdout
sys.stdout = old_stdout
# Write any output generated by the code to the output stream
self.output(capture_stdout.getvalue())
def weave_line(self, line):
"""Weaves one line by parse \sageexpr{} commands."""
def evaluate(expr):
debug("Evaluating Sage expression at line %d" % (self.line_no - 1))
snippet = self.new_snippet([expr], self.line_no - 1)
try:
result = eval(snippet.compile('eval'), self.scope)
return latex(result)
except SyntaxError, e:
raise SageWeaveSyntaxError(e, self, r"\sageexpr{}")
except Exception, e:
_, _, tb = sys.exc_info()
trace = traceback.extract_tb(tb)[1:]
trace[0] = (self.filename, self.line_no, r"\sageexpr{}", None)
raise SageWeaveException(e, self, trace)
return parse_sage_expressions(line, evaluate)
def weave(self):
while True:
line = self.get_line()
if line == None:
return
if line.strip().startswith("<<") and line.strip().endswith(">>="):
self.weave_code()
else:
line = self.weave_line(line)
self.output_ln(line)
def check_output_file(filename):
if not os.path.isfile(filename):
return
with open(filename, "r") as in_file:
for expectedLine in AUTOGENERATE_MSG:
if in_file.readline().rstrip('\n') != expectedLine:
error(('%s does not look like a sage-weave output file. '
'Refusing to overwrite it.') % filename)
def main(args):
""" Sage-weave main entry point """
# Override default output: - if input is -, else replace extension by .tex.
if args.output == '':
if args.input == '-':
args.output = '-'
else:
(root, ext) = os.path.splitext(args.input)
args.output = root + ".tex"
check_output_file(args.output)
# Open input stream
input_stream = sys.stdin if args.input == "-" else open(args.input)
info("Reading from %s" % ("stdin" if args.input == "-" else args.input))
# Open output stream
output_stream = sys.stdout if args.output == "-" else open(args.output, "wt")
info("Writing to %s" % ("stdin" if args.output == "-" else args.output))
# Output a warning stating that this file is auto-generated
output_stream.write("\n".join(AUTOGENERATE_MSG))
output_stream.write("\n")
# Create an empty scope in which all embedded code will run
scope = dict(globals())
# Override the __file__ variable in the embedded scope
filename = os.path.realpath(args.input) if args.input != "-" else "stdin"
# Run the interpreter for the input stream
try:
interpreter = Interpreter(input_stream, output_stream, filename)
interpreter.weave()
finally:
# Close the output file, if necessary
if args.output != "-":
output_stream.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Sage-weave is a tool for embedding Sage code in LaTeX documents.')
parser.prog = "weave.sage"
parser.add_argument('input', metavar='<input>',
nargs='?', default='-',
help='input file (- for stdin)')
parser.add_argument('-q', dest='quiet',
action='store_true', default=False,
help='quite mode')
parser.add_argument('-v', dest='verbose',
action='store_true', default=False,
help='verbose mode')
parser.add_argument('-o', dest='output',
default='',
help='output file (- for stdout)')
args = parser.parse_args()
QUIET = args.quiet
VERBOSE = args.verbose
info("This is sage-weave, version %s" % VERSION)
try:
main(args)
except SageWeaveException, e:
sys.stderr.write('Traceback:\n')
sys.stderr.write(e.traceback() + '\n')
sys.stderr.write(e.message + '\n')
except Exception, e:
print(traceback.format_exc())