summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/latex/sagetex/sagetex.py
diff options
context:
space:
mode:
authorKarl Berry <karl@freefriends.org>2009-06-21 00:23:51 +0000
committerKarl Berry <karl@freefriends.org>2009-06-21 00:23:51 +0000
commitd5c69362ef92a985a1cecefe6bbbca5eb51e822a (patch)
treeb6af1179a24bd3d2401d9540741fc4c161d7664b /Master/texmf-dist/doc/latex/sagetex/sagetex.py
parentf7498423bd232008266cf75bb0c1faa1465fbb1e (diff)
sage 2.2.1 (20jun09), omitting python scripts in bin
git-svn-id: svn://tug.org/texlive/trunk@13843 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/doc/latex/sagetex/sagetex.py')
-rw-r--r--Master/texmf-dist/doc/latex/sagetex/sagetex.py222
1 files changed, 110 insertions, 112 deletions
diff --git a/Master/texmf-dist/doc/latex/sagetex/sagetex.py b/Master/texmf-dist/doc/latex/sagetex/sagetex.py
index 2686b4348f5..576847894f7 100644
--- a/Master/texmf-dist/doc/latex/sagetex/sagetex.py
+++ b/Master/texmf-dist/doc/latex/sagetex/sagetex.py
@@ -1,38 +1,39 @@
-"""
-%%
-%% This is file `sagetex.py',
-%% generated with the docstrip utility.
-%%
-%% The original source files were:
-%%
-%% sagetexpackage.dtx (with options: `python')
-%%
-%% This is a generated file.
-%%
-%% Copyright (C) 2008 by Dan Drake <ddrake@member.ams.org>
-%%
-%% This program is free software: you can redistribute it and/or modify it
-%% under the terms of the GNU General Public License as published by the
-%% Free Software Foundation, either version 2 of the License, or (at your
-%% option) any later version.
-%%
-%% This program is distributed in the hope that it will be useful, but
-%% WITHOUT ANY WARRANTY; without even the implied warranty of
-%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-%% General Public License for more details.
-%%
-%% You should have received a copy of the GNU General Public License along
-%% with this program. If not, see <http://www.gnu.org/licenses/>
-%%
+##
+## This is file `sagetex.py',
+## generated with the docstrip utility.
+##
+## The original source files were:
+##
+## sagetexpackage.dtx (with options: `python')
+## py-and-sty.dtx (with options: `python')
+##
+## This is a generated file. It is part of the SageTeX package.
+##
+## Copyright (C) 2009 by Dan Drake <ddrake@member.ams.org>
+##
+## This program is free software: you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by the
+## Free Software Foundation, either version 2 of the License, or (at your
+## option) any later version.
+##
+## This program is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+## Public License for more details.
+##
+## You should have received a copy of the GNU General Public License along
+## with this program. If not, see <http://www.gnu.org/licenses/>.
+##
+__version__ = """
+ [2009/06/17 v2.2.1 embedding Sage into LaTeX documents]
"""
import sys
if __name__ == "__main__":
print("""This file is part of the SageTeX package.
It is not meant to be called directly.
-This file will be used by Sage scripts generated from a LaTeX document
-using the sagetex package. Keep it somewhere where Sage and Python can
-find it and it will automatically be imported.""")
+This file will be automatically used by Sage scripts generated from a
+LaTeX document using the SageTeX package.""")
sys.exit()
from sage.misc.latex import latex
import os
@@ -41,86 +42,83 @@ import hashlib
import traceback
import subprocess
import shutil
-initplot_done = False
-dirname = None
-filename = ""
-def progress(t,linebreak=True):
- if linebreak:
- print(t)
- else:
- sys.stdout.write(t)
-def openout(f):
- global filename
- filename = f
- global _file_
- _file_ = open(f + '.sout.tmp', 'w')
- s = '% This file was *autogenerated* from the file ' + \
- os.path.splitext(filename)[0] + '.sage.\n'
- _file_.write(s)
- progress('Processing Sage code for %s.tex...' % filename)
-def initplot(f):
- global initplot_done
- if not initplot_done:
- progress('Initializing plots directory')
- global dirname
- dirname = 'sage-plots-for-' + f + '.tex'
- if os.path.isdir(dirname):
- shutil.rmtree(dirname)
- os.mkdir(dirname)
- initplot_done = True
-def inline(counter, s):
- progress('Inline formula %s' % counter)
- _file_.write('\\newlabel{@sagelabel' + str(counter) + '}{{' + \
- latex(s) + '}{}{}{}{}}\n')
-def blockbegin():
- progress('Code block begin...', False)
-def blockend():
- progress('end')
-def plot(counter, p, format='notprovided', epsmagick=False, **kwargs):
- global dirname
- progress('Plot %s' % counter)
- if format == 'notprovided':
- formats = ['eps', 'pdf']
- else:
- formats = [format]
- for fmt in formats:
- plotfilename = os.path.join(dirname, 'plot-%s.%s' % (counter, fmt))
- #print(' plotting %s with args %s' % (plotfilename, kwargs))
- p.save(filename=plotfilename, **kwargs)
- if format != 'notprovided' and epsmagick is True:
- print('Calling Imagemagick to convert plot-%s.%s to EPS' % \
- (counter, format))
- toeps(counter, format)
-def toeps(counter, ext):
- global dirname
- subprocess.check_call(['convert',\
- '%s/plot-%s.%s' % (dirname, counter, ext), \
- '%s/plot-%s.eps' % (dirname, counter)])
-def goboom(line):
- global filename
- print('\n**** Error in Sage code on line %s of %s.tex! Traceback\
- follows.' % (line, filename))
- traceback.print_exc()
- print('\n**** Running Sage on %s.sage failed! Fix %s.tex and try\
- again.' % (filename, filename))
- os.remove(filename + '.sout.tmp')
- sys.exit(1)
-def endofdoc():
- global filename
- sagef = open(filename + '.sage', 'r')
- m = hashlib.md5()
- for line in sagef:
- if line[0:15] != ' sagetex.goboom':
- m.update(line)
- s = '%' + m.hexdigest() + '% md5sum of .sage file (minus "goboom" \
-lines) that produced this\n'
- _file_.write(s)
- _file_.close()
- os.rename(filename + '.sout.tmp', filename + '.sout')
- progress('Sage processing complete. Run LaTeX on %s.tex again.' %\
- filename)
-"""
-\endinput
-%%
-%% End of file `sagetex.py'.
-"""
+class SageTeXProcessor():
+ def __init__(self, jobname):
+ self.progress('Processing Sage code for %s.tex...' % jobname)
+ self.didinitplot = False
+ self.useimagemagick = False
+ self.useepstopdf = False
+ self.plotdir = 'sage-plots-for-' + jobname + '.tex'
+ self.filename = jobname
+ self.souttmp = open(self.filename + '.sout.tmp', 'w')
+ s = '% This file was *autogenerated* from the file ' + \
+ os.path.splitext(jobname)[0] + '.sage.\n'
+ self.souttmp.write(s)
+ def progress(self, t,linebreak=True):
+ if linebreak:
+ print(t)
+ else:
+ sys.stdout.write(t)
+ sys.stdout.flush()
+ def initplot(self):
+ self.progress('Initializing plots directory')
+ if os.path.isdir(self.plotdir):
+ shutil.rmtree(self.plotdir)
+ os.mkdir(self.plotdir)
+ self.didinitplot = True
+ def inline(self, counter, s):
+ self.progress('Inline formula %s' % counter)
+ self.souttmp.write('\\newlabel{@sageinline' + str(counter) + '}{{' + \
+ latex(s).rstrip() + '}{}{}{}{}}\n')
+ def blockbegin(self):
+ self.progress('Code block begin...', False)
+ def blockend(self):
+ self.progress('end')
+ def plot(self, counter, _p_, format='notprovided', **kwargs):
+ if not self.didinitplot:
+ self.initplot()
+ self.progress('Plot %s' % counter)
+ if format == 'notprovided':
+ formats = ['eps', 'pdf']
+ else:
+ formats = [format]
+ for fmt in formats:
+ if fmt == 'pdf' and self.useepstopdf:
+ epsfile = os.path.join(self.plotdir, 'plot-%s.eps' % counter)
+ self.progress('Calling epstopdf to convert plot-%s.eps to PDF' % \
+ counter)
+ subprocess.check_call(['epstopdf', epsfile])
+ continue
+ plotfilename = os.path.join(self.plotdir, 'plot-%s.%s' % (counter, fmt))
+ _p_.save(filename=plotfilename, **kwargs)
+ if format != 'notprovided' and self.useimagemagick:
+ self.progress('Calling Imagemagick to convert plot-%s.%s to EPS' % \
+ (counter, format))
+ self.toeps(counter, format)
+ def toeps(self, counter, ext):
+ subprocess.check_call(['convert',\
+ '%s/plot-%s.%s' % (self.plotdir, counter, ext), \
+ '%s/plot-%s.eps' % (self.plotdir, counter)])
+ def goboom(self, line):
+ print('\n**** Error in Sage code on line %s of %s.tex! Traceback\
+ follows.' % (line, self.filename))
+ traceback.print_exc()
+ print('\n**** Running Sage on %s.sage failed! Fix %s.tex and try\
+ again.' % ((self.filename,) * 2))
+ self.souttmp.close()
+ os.remove(self.filename + '.sout.tmp')
+ sys.exit(int(1))
+ def endofdoc(self):
+ sagef = open(self.filename + '.sage', 'r')
+ m = hashlib.md5()
+ for line in sagef:
+ if line[0:12] != " _st_.goboom" and line[0:12] != "print 'SageT":
+ m.update(line)
+ s = '%' + m.hexdigest() + '% md5sum of corresponding .sage file\
+ (minus "goboom" and pause/unpause lines)\n'
+ self.souttmp.write(s)
+ self.souttmp.close()
+ os.rename(self.filename + '.sout.tmp', self.filename + '.sout')
+ self.progress('Sage processing complete. Run LaTeX on %s.tex again.' %\
+ self.filename)
+