%% Copyright 2021 Tobias Enderle
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{pyluatex}[2021/07/15 v0.1.3 Execute Python code on the fly]

\RequirePackage{expl3}
\ExplSyntaxOn
\sys_if_engine_luatex:TF{}{%
\PackageError{PyLuaTeX}{LuaTeX~is~required}{}%
}
\sys_if_shell_unrestricted:TF{}{%
\PackageError{PyLuaTeX}{Shell~escape~required~(add~-shell-escape~option)}{}%
}
\ExplSyntaxOff

\directlua{require("pyluatex")}

\RequirePackage{kvoptions}
\DeclareStringOption[python3]{executable}
\DeclareVoidOption{verbose}{\directlua{pyluatex.verbose = true}}
\ProcessKeyvalOptions*

\directlua{pyluatex.start([==[\pyluatex@executable]==])}

\newenvironment{python}{\directlua{pyluatex.record_env()}}%
{\directlua{pyluatex.print_env()}}

\newcommand*{\py}[1]{%
\directlua{pyluatex.execute([==[print(str(#1), end='')]==], true)}%
}

\newcommand*{\pyc}[1]{\directlua{pyluatex.execute([==[#1]==], true)}}

\newcommand*{\pysession}[1]{\directlua{pyluatex.session = [==[#1]==]}}

\newcommand*{\pyfile}[1]{\directlua{pyluatex.run_file([==[#1]==])}}

\endinput
