diff options
author | Karl Berry <karl@freefriends.org> | 2023-01-14 20:52:29 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2023-01-14 20:52:29 +0000 |
commit | b893980d60ad3f1a6dc513ca784a27941a9c55bd (patch) | |
tree | e1f3f48e5915997f972aeae2638c4c0c917d0f3c /Master/texmf-dist | |
parent | 38a85335a98b9ebd1d72dc8423ac1a4aa9061000 (diff) |
pyluatex (14jan23)
git-svn-id: svn://tug.org/texlive/trunk@65537 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist')
-rw-r--r-- | Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.pdf | bin | 82073 -> 83049 bytes | |||
-rw-r--r-- | Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.tex | 19 | ||||
-rw-r--r-- | Master/texmf-dist/tex/lualatex/pyluatex/pyluatex-interpreter.py | 37 | ||||
-rw-r--r-- | Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua | 17 | ||||
-rw-r--r-- | Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.sty | 2 |
5 files changed, 60 insertions, 15 deletions
diff --git a/Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.pdf b/Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.pdf Binary files differindex 69fe67558e0..bc41fa99e41 100644 --- a/Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.pdf +++ b/Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.pdf diff --git a/Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.tex b/Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.tex index 12c539d6609..4dc0c09e353 100644 --- a/Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.tex +++ b/Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.tex @@ -15,7 +15,7 @@ \usepackage{url} \title{The \emph{pyluatex} package} \author{Tobias Enderle\\\url{https://github.com/tndrle/PyLuaTeX}} -\date{v0.5.2 (2023/01/03)} +\date{v0.6.0 (2023/01/14)} \begin{document} \maketitle \raggedright @@ -196,6 +196,23 @@ in your environment definition, e.g. {\end{python}} \end{tcblisting} +\subsection{Logging from Python} +\begin{tcblisting}{breakable,listing only, + size=fbox,colframe=black!8,boxrule=3pt,colback=black!8} +tex.log(*objects, sep=' ', end='\n') +\end{tcblisting} +Writes \inlcode|objects| to the TeX log, separated by \inlcode|sep| and followed by \inlcode|end|. +All elements in \inlcode|objects| are converted to strings using \inlcode|str()|. +Both \inlcode|sep| and \inlcode|end| must be strings. + +\textit{Example:} +\begin{tcblisting}{breakable,listing only, + size=fbox,colframe=black!8,boxrule=3pt,colback=black!8} +\begin{python} +tex.log('This text goes to the TeX log.') +\end{python} +\end{tcblisting} + \section{Requirements} \begin{itemize} \item Lua\LaTeX{} diff --git a/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex-interpreter.py b/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex-interpreter.py index 28ec28e12e7..341c2ace04b 100644 --- a/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex-interpreter.py +++ b/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex-interpreter.py @@ -34,7 +34,30 @@ import re import os import sys +class PyLTTex: + def __init__(self): + self._log_buffer = [] + + def log(self, *objects, sep=' ', end='\n'): + try: + objects = [str(v) for v in objects] + except Exception as exc: + raise RuntimeError( + 'The object to log could not be transformed to a string.', + ) from exc + self._log_buffer.append(sep.join(objects)) + self._log_buffer.append(end) + + def _log_message(self): + msg = ''.join(self._log_buffer) + self._log_buffer = [] + return msg + class Interpreter(InteractiveInterpreter): + def __init__(self): + self.tex = PyLTTex() + super().__init__({'tex': self.tex}) + def execute_repl(self, code, ignore_errors): self.success = True output = '' @@ -60,7 +83,7 @@ class Interpreter(InteractiveInterpreter): output += out.getvalue() if not ignore_errors and not self.success: return False, output - return self.success, output + return self.success, output, self.tex._log_message() def execute(self, code): with StringIO() as out, redirect_stdout(out), redirect_stderr(out): @@ -75,7 +98,7 @@ class Interpreter(InteractiveInterpreter): except: traceback.print_exc() self.success = False - return self.success, out.getvalue() + return self.success, out.getvalue(), self.tex._log_message() def showtraceback(self): super().showtraceback() @@ -95,13 +118,17 @@ class Handler(socketserver.StreamRequestHandler): interpreter = interpreters[data['session']] code = textwrap.dedent(data['code']) if data['repl_mode']: - success, output = interpreter.execute_repl( + success, output, log_msg = interpreter.execute_repl( code, data['ignore_errors'] ) else: - success, output = interpreter.execute(code) - response = { 'success': success, 'output': output } + success, output, log_msg = interpreter.execute(code) + response = { + 'success': success, + 'output': output, + 'log_msg': log_msg + } self.wfile.write((json.dumps(response) + '\n').encode('utf-8')) if __name__ == '__main__': diff --git a/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua b/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua index 9f7d5f7936b..c12faa5cc80 100644 --- a/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua +++ b/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua @@ -118,8 +118,7 @@ end local function request(data) tcp:send(utilities.json.tostring(data) .. "\n") local output = tcp:receive("*l") - local response = utilities.json.tolua(output) - return response.success, response.output + return utilities.json.tolua(output) end local function log_input(code) @@ -152,7 +151,7 @@ function pyluatex.execute(code, auto_print, write, repl_mode, store) if pyluatex.verbose then log_input(full_code) end - local success, output = request( + local resp = request( { session = pyluatex.session, code = full_code, @@ -160,27 +159,29 @@ function pyluatex.execute(code, auto_print, write, repl_mode, store) ignore_errors = pyluatex.ignore_errors } ) - local output_lines = split_lines(output) + local output_lines = split_lines(resp.output) if store then last_code = split_lines(code) last_output = output_lines end - if success or pyluatex.ignore_errors then - if pyluatex.verbose or not success then log_output(output) end + if resp.success or pyluatex.ignore_errors then + if pyluatex.verbose or not resp.success then log_output(resp.output) end if write then tex.print(output_lines) end else if not pyluatex.verbose then log_input(full_code) end - log_output(output) + log_output(resp.output) if write then tex.sprint(err_cmd("Python error (see above)")) end end - return success + if resp.log_msg ~= "" then texio.write(resp.log_msg) end + + return resp.success end function pyluatex.print_env() diff --git a/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.sty b/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.sty index a5adf62973b..db5654e6b8c 100644 --- a/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.sty +++ b/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.sty @@ -9,7 +9,7 @@ %% version 2005/12/01 or later. \NeedsTeXFormat{LaTeX2e} -\ProvidesPackage{pyluatex}[2023/01/03 v0.5.2 Execute Python code on the fly] +\ProvidesPackage{pyluatex}[2023/01/14 v0.6.0 Execute Python code on the fly] \RequirePackage{expl3} \ExplSyntaxOn |