# HG changeset patch
# User Nick Alexander <ncalexander@gmail.com>
# Date 1200778236 28800
# Node ID 013ce46490d39dfc8d2192c725aded799998908f
# Parent 9d2d4aeaf414259f8a2b77f1231ddfffe5684b84
Address #652: at http://trac.sagemath.org/sage_trac/ticket/652. Now 'load'
preparses .sage files to .py files in temporary directories.
diff -r 9d2d4aeaf414 -r 013ce46490d3 sage/misc/interpreter.py
a
|
b
|
def do_prefilter_paste(line, continuatio |
185 | 185 | if F.endswith('.py'): |
186 | 186 | _ip.magic('run -i "%s"'%F) |
187 | 187 | elif F.endswith('.sage'): |
188 | | _ip.magic('run -i "%s"'%process_file(F)) |
| 188 | _ip.magic('run -i "%s"'%preparse_file_named(F)) |
189 | 189 | elif F.endswith('.spyx') or F.endswith('.pyx'): |
190 | 190 | X = load_cython(F) |
191 | 191 | __IPYTHON__.push(X) |
… |
… |
def do_prefilter_paste(line, continuatio |
276 | 276 | raise ImportError, "Error loading '%s'"%name |
277 | 277 | elif name.endswith('.sage'): |
278 | 278 | try: |
279 | | line = '%run -i "' + process_file(name) + '"' |
| 279 | line = '%run -i "' + preparse_file_named(name) + '"' |
280 | 280 | except IOError, s: |
281 | 281 | print s |
282 | 282 | raise ImportError, "Error loading '%s'"%name |
… |
… |
def do_prefilter_paste(line, continuatio |
330 | 330 | raise ImportError, "File '%s' not found."%name |
331 | 331 | elif name.endswith('.sage'): |
332 | 332 | try: |
333 | | line = '%run -i "' + process_file(name) + '"' |
| 333 | line = '%run -i "' + preparse_file_named(name) + '"' |
334 | 334 | attached[name] = os.path.getmtime(name) |
335 | 335 | except IOError, OSError: |
336 | 336 | raise ImportError, "File '%s' not found."%name |
… |
… |
def load_cython(name): |
359 | 359 | import sys |
360 | 360 | sys.path.append(dir) |
361 | 361 | return 'from %s import *'%mod |
362 | | |
363 | | def process_file(name): |
| 362 | |
| 363 | def preparse_file_named_to_stream(name, out): |
| 364 | r""" |
| 365 | Preparse file named \code{name} (presumably a .sage file), outputting to |
| 366 | stream \code{out}. |
| 367 | """ |
364 | 368 | cur = os.path.abspath(os.curdir) |
365 | 369 | dir, name = os.path.split(os.path.abspath(name)) |
366 | | name2 = "%s/%s.py"%(dir,name[:name.find('.')]) |
367 | 370 | os.chdir(dir) |
368 | 371 | contents = open(name).read() |
369 | 372 | parsed = preparse_file(contents, attached, do_time=True) |
370 | 373 | os.chdir(cur) |
371 | | W = open(name2,'w') |
372 | | W.write('#'*70+'\n') |
373 | | W.write('# This file was *autogenerated* from the file %s.\n'%name) |
374 | | W.write('#'*70+'\n') |
375 | | W.write(parsed) |
376 | | W.close() |
377 | | return name2 |
378 | | |
| 374 | out.write('#'*70+'\n') |
| 375 | out.write('# This file was *autogenerated* from the file %s.\n'%os.path.abspath(name)) |
| 376 | out.write('#'*70+'\n') |
| 377 | out.write(parsed) |
| 378 | |
| 379 | def preparse_file_named(name): |
| 380 | r""" |
| 381 | Preparse file named \code{name} (presumably a .sage file), outputting to a |
| 382 | temporary file. Returns name of temporary file. |
| 383 | """ |
| 384 | import sage.misc.misc |
| 385 | dir, name = os.path.split(os.path.abspath(name)) |
| 386 | tmpfilename = sage.misc.misc.tmp_filename(name) + ".py" |
| 387 | out = open(tmpfilename,'w') |
| 388 | preparse_file_named_to_stream(name, out) |
| 389 | out.close() |
| 390 | return tmpfilename |
379 | 391 | |
380 | 392 | def sage_prefilter(self, block, continuation): |
381 | 393 | """ |