texlive[65537] Master/texmf-dist: pyluatex (14jan23)

commits+karl at tug.org commits+karl at tug.org
Sat Jan 14 21:52:29 CET 2023


Revision: 65537
          http://tug.org/svn/texlive?view=revision&revision=65537
Author:   karl
Date:     2023-01-14 21:52:29 +0100 (Sat, 14 Jan 2023)
Log Message:
-----------
pyluatex (14jan23)

Modified Paths:
--------------
    trunk/Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.pdf
    trunk/Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.tex
    trunk/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex-interpreter.py
    trunk/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua
    trunk/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.sty

Modified: trunk/Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.pdf
===================================================================
(Binary files differ)

Modified: trunk/Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.tex
===================================================================
--- trunk/Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.tex	2023-01-14 20:48:59 UTC (rev 65536)
+++ trunk/Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.tex	2023-01-14 20:52:29 UTC (rev 65537)
@@ -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 @@
 {\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{}

Modified: trunk/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex-interpreter.py
===================================================================
--- trunk/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex-interpreter.py	2023-01-14 20:48:59 UTC (rev 65536)
+++ trunk/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex-interpreter.py	2023-01-14 20:52:29 UTC (rev 65537)
@@ -34,7 +34,30 @@
 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 @@
                 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 @@
             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 @@
             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__':

Modified: trunk/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua
===================================================================
--- trunk/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua	2023-01-14 20:48:59 UTC (rev 65536)
+++ trunk/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua	2023-01-14 20:52:29 UTC (rev 65537)
@@ -118,8 +118,7 @@
 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 @@
 
     if pyluatex.verbose then log_input(full_code) end
 
-    local success, output = request(
+    local resp = request(
         {
             session = pyluatex.session,
             code = full_code,
@@ -160,14 +159,14 @@
             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)
@@ -174,13 +173,15 @@
         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()

Modified: trunk/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.sty
===================================================================
--- trunk/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.sty	2023-01-14 20:48:59 UTC (rev 65536)
+++ trunk/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.sty	2023-01-14 20:52:29 UTC (rev 65537)
@@ -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



More information about the tex-live-commits mailing list.