forked from hgrecco/python-sty
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.sty
More file actions
257 lines (223 loc) · 7.34 KB
/
Copy pathpython.sty
File metadata and controls
257 lines (223 loc) · 7.34 KB
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
%% 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, write to the Free Software
%% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
%%
%% 0.41 Version by Changlai Du <duchanglai@gmail.com>
%% 0.4 Version by Hernán Grecco <hgrecco@gmail.com>
%% 0.3 Version by James Brotchie <brotchie@gmail.com>
%%
%% Author: Martin R. Ehmsen, ehmsen@imada.sdu.dk.
%% Department of Mathematics and Computer Science,
%% University of Southern Denmark, DK
%%
%% You can find an online copy of the GPL at
%% http://www.gnu.org/copyleft/gpl.html .
%%
%% Note: shell-escape needs to be activated for this to work.
%% This can either be done by passing -shell-escape as an option to
%% latex or by adding/changing "shell_escape = t" in your texmf.cnf .
% Python-sty Changelog
% ====================
%
% 0.41 (2019-01-23)
% ----------------
% - Options to support -output-directory.
% - Remove the need of runpy.py but generate it from this package.
%
%
% 0.4 (2013-11-28)
% ----------------
% - Python code is only executed if it has changed.
% - Option to save auxiliary files in a subfolder.
% - Option to choose the python binary.
% - Code refactoring.
% - UTF-8 support.
%
%
% 0.3 (2011-12-22)
% ----------------
%
% - Intermediate files are now stored with different filenames.
% - The return code of each Python shell execution is stored
% in an .rc file.
% - Any stderr output of a failed Python shell execution is
% inserted as a red verbatim in the resulting document.
% -- James Brotchie <brotchie@gmail.com>
%
% 0.21
% ----
%
% - Moved \newwrite\@module from \@writemodule and out, since
% no more than 15 \newwrites are allowed (and the previous
% version created a new every time \@writemodule was called.
%
%
\NeedsTeXFormat{LaTeX2e}[1994/12/01]
\ProvidesPackage{python}[2019/01/23 v0.41 Python in LaTeX]
\RequirePackage{etoolbox}
\RequirePackage{verbatim}
\RequirePackage{color}
\RequirePackage{ifplatform}
\RequirePackage{fancyvrb}
\RequirePackage[utf8]{inputenc}
\RequirePackage{kvoptions}
%% I want to find a way to do the following without using temporary files
%% 1) define the python environment
%% 2) pipe its content to python and then
%% 3) input the results.
%% The following three lines of code accomplish 1) and 3)(?), but fail to pipe
%% verbatim to python. Is it possible?
%\def\FVB@python{\FVB@SaveVerbatim{python}}
%\def\FVE@python{\FVE@SaveVerbatim} %\immediate\@@input|"echo \UseVerbatim{code}|python"}
%\DefineVerbatimEnvironment{python}{python}{}
% Put the resulting Python code in separate files.
\newcommand{\@PYT@outname}{\jobname\arabic{@PythonEnvironmentCounter}}
\newcounter{@PythonEnvironmentCounter}
\setcounter{@PythonEnvironmentCounter}{1}
% Package Options
\SetupKeyvalOptions{
family=PYT,
prefix=PYT@
}
\DeclareStringOption[.]{outputdir}[.]
\DeclareVoidOption{subfolder}{
\renewcommand{\@PYT@outname}{py/\jobname\arabic{@PythonEnvironmentCounter}}
}
\DeclareStringOption[python]{bin}[python]
\ProcessKeyvalOptions*
% Output run.py
\begin{VerbatimOut}{\jobname.run.py}
# -*- coding: utf-8 -*-
import sys
try:
reload(sys).setdefaultencoding("UTF-8")
except:
pass
filename = sys.argv[1]
with open(filename[:-3] + '.tex', "w") as fout:
sys.stdout = fout
with open(filename + '.err', "w") as ferr:
with open(filename + '.rc', 'w') as frc:
with open(filename) as f:
try:
code = compile(f.read(), filename, 'exec')
exec(code, {'print': fout.write})
frc.write('0')
except Exception as e:
frc.write('1')
import traceback
ferr.write(traceback.format_exc())
raise e
\end{VerbatimOut}
\typeout{PYTHON: Output \jobname.run.py}
\newcommand{\@pythonbin}{\PYT@bin \space\jobname.run.py\space}
\typeout{PYTHON: Binary \@pythonbin}
\newcommand{\@PYT@copyrunpy}{
\ifwindows
\count@ = \escapechar
\escapechar = -1
\immediate\write18{copy \PYT@outputdir\string\\\jobname.run.py \jobname.run.py /y}
\escapechar = \count@
\else
\immediate\write18{cp \PYT@outputdir/\jobname.run.py \jobname.run.py}
\fi
}
\newcommand{\@PYT@copytocache}{
\ifwindows
\count@ = \escapechar
\escapechar = -1
\immediate\write18{copy \PYT@outputdir\string\\\@PYT@outname.py \PYT@outputdir\string\\\@PYT@outname.py.cache /y}
\escapechar = \count@
\else
\immediate\write18{cp \PYT@outputdir/\@PYT@outname.py \PYT@outputdir/\@PYT@outname.py.cache}
\fi
}
\newcommand{\@PYT@deletescript}{
\ifwindows
\count@ = \escapechar
\escapechar = -1
\immediate\write18{del \PYT@outputdir\string\\\@PYT@outname.py}
\escapechar = \count@
\else
\immediate\write18{rm \PYT@outputdir/\@PYT@outname.py}
\fi
}
\newcommand{\@PYT@comparefiles}{
\ifwindows
\count@ = \escapechar
\escapechar = -1
\immediate\write18{fc \PYT@outputdir\string\\\@PYT@outname.py
\PYT@outputdir\string\\\@PYT@outname.py.cache & call echo ^\@percentchar
ERRORLEVEL^\@percentchar\space>\PYT@outputdir\string\\\@PYT@outname.cmp.rc}
\escapechar = \count@
\else
\immediate\write18{cmp \PYT@outputdir/\@PYT@outname.py \PYT@outputdir/\@PYT@outname.py.cache; echo $? > \PYT@outputdir/\@PYT@outname.cmp.rc}
\fi
}
\newcommand{\@PYT@executescript}{
\typeout{PYTHON: Running file \@PYT@outname.py}
\immediate\write18{\@pythonbin \space\PYT@outputdir/\@PYT@outname.py}
% Copy to the current script to keep a cache.
\@PYT@copytocache
\@PYT@deletescript
}
\newread\@PYT@retcode
\newcommand{\@PYT@returncode}{
\inputencoding{utf8}
\input{\@PYT@outname}
% Read the return code of the executed Python script.
\immediate\openin\@PYT@retcode=\@PYT@outname.py.rc
\immediate\read\@PYT@retcode to \rc
\immediate\closein\@PYT@retcode
% If the return code isn't zero then include
% the traceback.
\ifnumequal{\rc}{0}{
\typeout{PYTHON: Run was successful}
}{%
\typeout{PYTHON: Run failed}
\begingroup
\color{red}
\verbatiminput{\@PYT@outname.py.err}
\endgroup
}
}
\newcommand{\@PYT@deactivateeightbit}{%
\count@=127
\loop
\catcode\count@=12
\ifnum\count@<255
\advance\count@\@ne
\repeat
}
\newread\@PYT@diff
\newenvironment{python}
{
\@PYT@copyrunpy
\typeout{PYTHON: Writing file \@PYT@outname.py}
\@PYT@deactivateeightbit\VerbatimOut{\@PYT@outname.py}}
{
\endVerbatimOut
\@PYT@comparefiles
\immediate\openin\@PYT@diff=\@PYT@outname.cmp.rc
\immediate\read\@PYT@diff to \rc
\immediate\closein\@PYT@diff
\ifnumequal{\rc}{0}{
% Old file and new file are identical
\typeout{PYTHON: No need to run the script again}
}{
% Old file and new file are different or old is not present
\@PYT@executescript
}
\@PYT@returncode
\immediate\stepcounter{@PythonEnvironmentCounter}
}