Compare commits

..

3 Commits

57 changed files with 799 additions and 1843 deletions

View File

@ -1,13 +0,0 @@
kind: pipeline
name: default
steps:
- name: Publish
image: plugins/pypi
settings:
username:
from_secret: pypi_username
password:
from_secret: pypi_password
when:
event: tag

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
tests/
__pycache__/ __pycache__/
*.pyc *.pyc
documentation/build/ documentation/build/

View File

@ -1,16 +0,0 @@
FROM python:3.11-rc-slim
RUN apt-get update
RUN apt-get install -y texlive texlive-extra-utils
RUN apt-get install -y python3-dev
COPY requirements.txt /tmp/
RUN pip install -r /tmp/requirements.txt
RUN mkdir -p /src/bopytex
COPY bopytex/ /src/bopytex
COPY setup.py /src/
RUN pip install -e /src
COPY example/simple /example
WORKDIR /example

View File

@ -1,17 +0,0 @@
FROM python:3.11-rc-slim
RUN apt-get update
RUN apt-get install -y texlive texlive-extra-utils
RUN apt-get install -y python3-dev
COPY requirements.txt /tmp/
RUN pip install -r /tmp/requirements.txt
RUN pip install mapytex
RUN mkdir -p /src/bopytex
COPY bopytex/ /src/bopytex
COPY setup.py /src/
RUN pip install -e /src
COPY example/usecase /example
WORKDIR /example
ENV BOPYTEXCONFIG="bopytex_config.py"

View File

@ -1,12 +0,0 @@
docker-build-simple:
docker build -f Dockerfile.simple -t simple .
docker-simple: docker-build-simple
docker run simple sh -c "bopytex -q 2 tpl_example.tex && cat 1_example.tex"
docker-build-usecase:
docker build -f Dockerfile.usecase -t usecase .
docker-usecase: docker-build-usecase
docker run usecase sh -c "bopytex -s students.csv tpl_example.tex && cat 1_example.tex"

121
README.md
View File

@ -1,8 +1,6 @@
# Bopytex # Bopytex
[![Build Status](https://drone.opytex.org/api/badges/lafrite/Bopytex/status.svg)](https://drone.opytex.org/lafrite/Bopytex) Bopytex is a command line tool for producing random math exercises with their correction. It embeds [mapytex](https://git.opytex.org/lafrite/Mapytex) and [python](python.org) into [latex](latex-project.org) through [jinja](jinja.pocoo.org).
Bopytex is a command line tool which embed python into latex. It uses jinja2 to do so with a modified environnement to match with latex syntax.
## Installing ## Installing
@ -12,91 +10,84 @@ Install and update using [pip](https://pip.pypa.io/en/stable/quickstart/)
## Simple example ## Simple example
``` latex Let's say I want an exercise on adding 2 fractions (files are in `examples`).
% save this as tpl_simple.tex
\documentclass[12pt]{article}
\title{Bopytex example -- {{ number }}} The *latex* template called `tpl_add_fraction.tex`
``` latex
\documentclass[12pt]{article}
\begin{document} \begin{document}
\maketitle \section{Ajouts de fractions}
%- set a = 10
%- set n = 2
We have two variables
\begin{itemize}
\item a: \Var{a}
\item n: \Var{n}
\end{itemize}
%# We can use blocks
\begin{itemize}
%- for i in n
\item \Var{a}
%- endfor
\end{itemize}
Adding two fractions
%- set e = Expression.random("{a} / {b} + {c} / {k*b}", ["b > 1", "k>1"])
\[
A = \Var{e}
\]
Solution
\[
\Var{e.simplify().explain() | join('=')}
\]
\end{document} \end{document}
``` ```
To create a version a this document type this Generate latex files and compile those for 2 different subjects.
``` bash ```
$ bopytex tpl_simple.tex bopytex -t tpl_add_fractions.tex -N 2
``` ```
## How I use it It produces 2 sources files
I build this program to produce individual exams subjects for each of my student with the correction associated. I write a template, and bopytex build subject and correction. - `01_add_fractions.tex`
To produce formulas and values, I use an another tool I an developing: `mapytex <https://git.opytex.org/lafrite/Mapytex`. I am importing it through `bopytex_config.py`. ```latex
``` python
# bopytex_config.py
from mapytex import Expression
from random import random
direct_access = {
"Expression": Expression,
"random": random
}
```
Every variables, objects or function inside this file will be available inside the template.
``` latex
% tpl_example.tpl
\documentclass[12pt]{article} \documentclass[12pt]{article}
\title{Bopytex with Mapytex example -- \Var{ subject.name }}
\begin{document} \begin{document}
\maketitle \section{Ajouts de fractions}
%- set e = Expression.random("{a} + {b}") Adding two fractions
\Var{e} \[
A = \frac{- 2}{4} + \frac{7}{8}
\Var{e.simplify()} \]
Solution
\[
\frac{- 2}{4} + \frac{7}{8}=\frac{- 2 \times 2}{4 \times 2} + \frac{7}{8}=\frac{- 4}{8} + \frac{7}{8}=\frac{- 4 + 7}{8}=\frac{3}{8}
\]
\end{document} \end{document}
``` ```
Information about my students are stored in a csv file (here `students.csv`) - `02_add_fractions.tex`
``` csv ```latex
"Name","Age","Email","fraction level","calculus level" \documentclass[12pt]{article}
"Spike Tucker","22","s.tucker@randatmail.com","7","3"
"Martin Payne","21","m.payne@randatmail.com","7","3" \begin{document}
"Kimberly Baker","20","k.baker@randatmail.com","1","8"
"Emma Bailey","29","e.bailey@randatmail.com","2","5" \section{Ajouts de fractions}
"Nicholas Taylor","28","n.taylor@randatmail.com","3","3"
Adding two fractions
\[
A = \frac{8}{9} + \frac{3}{63}
\]
Solution
\[
\frac{8}{9} + \frac{3}{63}=\frac{8 \times 7}{9 \times 7} + \frac{3}{63}=\frac{56}{63} + \frac{3}{63}=\frac{56 + 3}{63}=\frac{59}{63}
\]
\end{document}
``` ```
Then I can produce a subject for each of my student And a ready to print pdf.
- [ all_add_fraction.pdf ]( ./examples/all_add_fraction.pdf )
``` bash
$ bopytex tpl_simple.tex -s students.csv -c bopytex_config.py
```

View File

@ -1,6 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
# encoding: utf-8 # encoding: utf-8
#from .bopytex import subject_metadatas, crazy_feed, pdfjoin
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: # vim:set autoindent expandtab tabstop=4 shiftwidth=4:

243
bopytex/bopytex.py Normal file → Executable file
View File

@ -1,2 +1,241 @@
import bopytex.default_config as DEFAULT #!/usr/bin/env python
from bopytex.service import orcherstrator # encoding: utf-8
"""
Producing then compiling templates
"""
import csv
import os
import logging
from pathlib import Path
import pytex
from mapytex import Expression, Integer, Decimal, Polynomial, render
import sympy
import bopytex.filters as filters
formatter = logging.Formatter("%(name)s :: %(levelname)s :: %(message)s")
steam_handler = logging.StreamHandler()
steam_handler.setLevel(logging.DEBUG)
steam_handler.setFormatter(formatter)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(steam_handler)
def setup():
render.set_render("tex")
logger.debug(f"Render for Expression is {render.render}")
mapytex_tools = {
"Expression": Expression,
"Integer": Integer,
"Decimal": Decimal,
"Polynom": Polynomial,
"sympy": sympy,
# "Fraction": mapytex.Fraction,
# "Equation": mapytex.Equation,
# "random_str": mapytex.random_str,
# "random_pythagore": mapytex.random_pythagore,
# "Dataset": mapytex.Dataset,
# "WeightedDataset": mapytex.WeightedDataset,
}
pytex.update_export_dict(mapytex_tools)
pytex.add_filter("calculus", filters.do_calculus)
def get_working_dir(options):
""" Get the working directory """
if options["working_dir"]:
working_dir = Path(options["working_dir"])
else:
try:
template = Path(options["template"])
except TypeError:
raise ValueError(
"Need to set the working directory \
or to give a template"
)
else:
working_dir = template.parent
logger.debug(f"The output directory will be {working_dir}")
return working_dir
def activate_printanswers(
texfile, noans=r"solution/print = false", ans=r"solution/print = true"
):
""" Activate printanswers mod in texfile """
output_fname = "corr_" + texfile
with open(texfile, "r") as input_f:
with open(output_fname, "w") as output_f:
for line in input_f.readlines():
output_f.write(line.replace(noans, ans))
return output_fname
def deactivate_printanswers(corr_fname):
""" Activate printanswers mod in texfile """
Path(corr_fname).remove()
def pdfjoin(pdf_files, destname, working_dir=".", rm_pdfs=1):
"""TODO: Docstring for pdfjoin.
:param pdf_files: list of pdf files to join
:param destname: name for joined pdf
:param working_dir: the working directory
:param rm_pdfs: Remove pdf_files after joining them
:returns: TODO
"""
joined_pdfs = Path(working_dir) / Path(destname)
pdf_files_str = " ".join(pdf_files)
pdfjam = f"pdfjam {pdf_files_str} -o {joined_pdfs}"
logger.debug(f"Run {pdfjam}")
logger.info("Joining pdf files")
os.system(pdfjam)
if rm_pdfs:
logger.info(f"Remove {pdf_files_str}")
os.system(f"rm {pdf_files_str}")
def extract_student_csv(csv_filename):
""" Extract student list from csv_filename """
with open(csv_filename, "r") as csvfile:
reader = csv.DictReader(csvfile)
return [r for r in reader]
def subject_metadatas(options):
""" Return metadata on subject to produce
if csv is given it will based on is
otherwise it will be based on quantity
:example:
>>> subject_metadata(10)
"""
if options["students_csv"]:
metadatas = []
for (i, s) in enumerate(extract_student_csv(options["students_csv"])):
d = {"num": f"{i+1:02d}"}
d.update(s)
metadatas.append(d)
elif options["number_subjects"] > 0:
metadatas = [{"num": f"{i+1:02d}"} for i in range(options["number_subjects"])]
else:
raise ValueError("Need metacsv or quantity to build subject metadata")
for meta in metadatas:
meta.update(
{
"template": str(Path(options["template"]).name),
"texfile": str(Path(options["template"]).name).replace(
"tpl", meta["num"]
),
"directory": str(Path(options["template"]).parent),
}
)
return metadatas
def feed(*args, **kwrds):
""" Nice and smooth pytex feed """
pytex.feed(*args, **kwrds)
def crazy_feed(*args, **kwrds):
""" Crazy mod for pytex feed """
while True:
try:
pytex.feed(*args, **kwrds)
except:
logger.debug(f"Crazy feed is working hard...! {args} {kwrds}")
else:
break
def clean(directory):
pytex.clean(directory, ["*.aux", "*.log", "*.out", "*.xsim", "*.tkzfonct.*"])
def texcompile(filename):
logger.debug(f"Start compiling {filename}")
pytex.pdflatex(Path(filename))
logger.debug(f"End compiling")
def produce_and_compile(options):
""" Produce and compile subjects
"""
logger.debug(f"CI parser gets {options}")
template = Path(options["template"]).name
directory = Path(options["template"]).parent
metadatas = subject_metadatas(options)
logger.debug(f"Metadata {metadatas}")
for meta in metadatas:
logger.debug(f"Feeding template toward {meta['texfile']}")
if options["crazy"]:
crazy_feed(
template=Path(meta["directory"]) / meta["template"],
data=meta,
output=meta["texfile"],
force=1,
)
else:
feed(
template=Path(meta["directory"]) / meta["template"],
data=meta,
output=meta["texfile"],
force=1,
)
assert(Path(meta["texfile"]).exists())
logger.debug(f"{meta['texfile']} fed")
if options["corr"]:
logger.debug(f"Building correction for {meta['texfile']}")
meta.update({
"corr_texfile": activate_printanswers(meta["texfile"]),
})
if not options["no_compile"]:
for prefix in ["", "corr_"]:
key = prefix + "texfile"
try:
meta[key]
except KeyError:
pass
else:
texcompile(meta[key])
meta.update({
prefix+'pdffile': meta[key].replace('tex', 'pdf')
})
if not options["no_join"]:
for prefix in ["", "corr_"]:
key = prefix + "pdffile"
try:
pdfs = [m[key] for m in metadatas]
except KeyError:
pass
else:
pdfjoin(
pdfs,
template.replace("tpl", prefix+"all").replace(".tex", ".pdf"),
directory,
rm_pdfs=1,
)
if not options["dirty"]:
clean(directory)
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del

View File

@ -1,26 +0,0 @@
from bopytex.planner.generate_compile_join_planner import planner
from bopytex.worker import Dispatcher
from bopytex.worker.activate_corr import activate_corr
from bopytex.worker.clean import clean
from bopytex.worker.compile import pdflatex
from bopytex.worker.generate import generate
from bopytex.worker.join_pdf import pdfjam
from bopytex.jinja2_env.texenv import texenv
jinja2 = {
"environment": texenv
}
dispatcher = Dispatcher({
"GENERATE": generate,
"COMPILE": pdflatex,
"JOIN": pdfjam,
"CLEAN": clean,
"ACTIVATE_CORR": activate_corr,
})
latex = {
"solution": r"solution/print = true",
"no_solution": r"solution/print = false",
}

33
bopytex/filters.py Normal file
View File

@ -0,0 +1,33 @@
#!/usr/bin/env python
# encoding: utf-8
"""
Custom filter for Bopytex
"""
__all__ = ["do_calculus"]
def do_calculus(steps, name="A", sep="=", end="", joining=" \\\\ \n"):
"""Display properly the calculus
Generate this form string:
"name & sep & a_step end joining"
:param steps: list of steps
:returns: latex string ready to be endbeded
"""
ans = joining.join([
name + " & "
+ sep + " & "
+ str(s) + end for s in steps
])
return ans
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del

View File

@ -1,31 +0,0 @@
#!/usr/bin/env python
# encoding: utf-8
import jinja2
__all__ = [
"texenv",
]
# Definition of jinja syntax for latex
texenv = jinja2.Environment(
block_start_string=r"\Block{",
block_end_string="}",
variable_start_string=r"\Var{",
variable_end_string="}",
comment_start_string=r"\#{",
comment_end_string="}",
line_statement_prefix="%-",
line_comment_prefix="%#",
loader=jinja2.ChoiceLoader(
[
jinja2.FileSystemLoader(["./"]),
]
),
extensions=["jinja2.ext.do"],
)
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del

38
bopytex/lib/pythagore.py Normal file
View File

@ -0,0 +1,38 @@
#!/usr/bin/env python
# encoding: utf-8
from random import randint
def pythagore_triplet(v_min = 1, v_max = 10):
"""Random pythagore triplet generator
:param v_min: minimum in randint
:param v_max: max in randint
:returns: (a,b,c) such that a^2 + b^2 = c^2
"""
u = randint(v_min,v_max)
v = randint(v_min,v_max)
while v == u:
v = randint(v_min,v_max)
u, v = max(u,v), min(u,v)
return (u**2-v**2 , 2*u*v, u**2 + v**2)
if __name__ == '__main__':
print(pythagore_triplet())
for j in range(1,10):
for i in range(j,10):
print((i**2-j**2 , 2*i*j, i**2 + j**2))
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del

View File

@ -0,0 +1,33 @@
\Block{macro solveEquation(P)}
On commence par calculer le discriminant de $P(x) = \Var{P}$.
\begin{eqnarray*}
\Delta & = & b^2-4ac \\
\Var{P.delta.explain()|calculus(name="\\Delta")}
\end{eqnarray*}
\Block{if P.delta > 0}
comme $\Delta = \Var{P.delta} > 0$ donc $P$ a deux racines
\begin{eqnarray*}
x_1 & = & \frac{-b - \sqrt{\Delta}}{2a} = \frac{\Var{-P.b} - \sqrt{\Var{P.delta}}}{2 \times \Var{P.a}} = \Var{P.roots()[0] } \\
x_2 & = & \frac{-b + \sqrt{\Delta}}{2a} = \frac{\Var{-P.b} + \sqrt{\Var{P.delta}}}{2 \times \Var{P.a}} = \Var{P.roots()[1] }
\end{eqnarray*}
Les solutions de l'équation $\Var{P} = 0$ sont donc $\mathcal{S} = \left\{ \Var{P.roots()[0]}; \Var{P.roots()[1]} \right\}$
\Block{elif P.delta == 0}
Comme $\Delta = 0$ donc $P$ a une racine
\begin{eqnarray*}
x_1 = \frac{-b}{2a} = \frac{-\Var{P.b}}{2\times \Var{P.a}} = \Var{P.roots()[0]} \\
\end{eqnarray*}
La solution de $\Var{P} = 0$ est donc $\mathcal{S} = \left\{ \Var{P.roots()[0]}\right\}$
\Block{else}
Alors $\Delta = \Var{P.delta} < 0$ donc $P$ n'a pas de racine donc l'équation $\Var{P} = 0$ n'a pas de solution.
\Block{endif}
\Block{endmacro}

View File

@ -1,36 +0,0 @@
class Message():
def __init__(self, status, out, err):
self._status = status
self._out = out
self._err = err
@property
def status(self):
return self._status
@property
def out(self):
return self._out
@property
def err(self):
return self._err
def __repr__(self):
return f"Message(status={self.status}, out={self.out}, err={self.err})"
class SubprocessMessage(Message):
def __init__(self, process):
self._process = process
@property
def status(self):
return self._process.wait()
@property
def out(self):
return self._process.stdout
@property
def err(self):
return self._process.stderr

View File

@ -1,58 +0,0 @@
from bopytex.tasks import Task, activate_corr_on, compile_pdf, join_pdfs
import bopytex.planner.naming as naming
import os
def list_files(dir=".", accept=lambda _: True, reject=lambda _: False):
files = []
for file in os.listdir(dir):
if accept(file) and not reject(file):
files.append(file)
return files
def planner(options: dict) -> list[Task]:
sources = list_files(
accept=lambda x: x.endswith(".tex"),
reject=lambda x: x.startswith("tpl_"),
)
options["sources"] = sources
return tasks_builder(options)
def tasks_builder(
options: dict,
) -> list[Task]:
opt = {
"no_join": False,
"no_pdf": False,
}
opt.update(options)
try:
sources = opt["sources"]
no_join = opt["no_join"]
no_pdf = opt["no_pdf"]
except KeyError:
raise PlannerMissingOption("An option is missing")
tasks = []
corr_pdfs = []
for source in sources:
corr_source = naming.corr(source)
tasks.append(activate_corr_on(source, opt, corr_source))
if not no_pdf:
corr_pdf = naming.source2pdf(corr_source)
tasks.append(compile_pdf(corr_source, corr_pdf))
corr_pdfs.append(corr_pdf)
if not no_join:
joined = "joined.pdf"
if corr_pdfs:
corr_joined = naming.corr(joined)
tasks.append(join_pdfs(corr_pdfs, corr_joined))
return tasks

View File

@ -1,2 +0,0 @@
class PlannerMissingOption(Exception):
pass

View File

@ -1,9 +0,0 @@
from bopytex.tasks import Task
def simple(options: dict) -> list[Task]:
"""Simple planner with options['quantity'] tasks and no dependencies"""
return [
Task("DO", args={"number": i}, deps=[], output=f"{i}")
for i in range(options["quantity"])
]

View File

@ -1,103 +0,0 @@
from bopytex.tasks import Task, activate_corr_on, compile_pdf, generate, join_pdfs
import bopytex.planner.naming as naming
from bopytex.planner.exceptions import PlannerMissingOption
import csv
def build_subject_list_from_infos(infos: list[dict]) -> list[dict]:
subjects = []
digit = len(str(len(infos)))
for i, infos in enumerate(infos):
subjects.append({"number": str(i + 1).zfill(digit), **infos})
return subjects
def build_subject_list_from_qty(qty: int) -> list[dict]:
subjects = []
digit = len(str(qty))
for i in range(qty):
subjects.append({"number": str(i + 1).zfill(digit)})
return subjects
def planner(options: dict) -> list[Task]:
try:
students_csv = options["students_csv"]
assert options["students_csv"] != ""
except (KeyError, AssertionError):
try:
quantity_subjects = options["quantity_subjects"]
assert options["quantity_subjects"] != 0
except (KeyError, AssertionError):
raise PlannerMissingOption("students_csv or quantity_subjects is required")
else:
options["subjects"] = build_subject_list_from_qty(qty=quantity_subjects)
else:
with open(students_csv, "r") as csv_file:
reader = csv.DictReader(csv_file)
infos = [r for r in reader]
options["subjects"] = build_subject_list_from_infos(infos)
return tasks_builder(options)
def tasks_builder(
options: dict,
) -> list[Task]:
opt = {
"corr": False,
"no_join": False,
"no_pdf": False,
}
opt.update(options)
try:
template = opt["template"]
subjects = opt["subjects"]
corr = opt["corr"]
no_join = opt["no_join"]
no_pdf = opt["no_pdf"]
except KeyError:
raise PlannerMissingOption("An option is missing")
tasks = []
pdfs = []
corr_pdfs = []
for subject in subjects:
source = naming.template2source(template, subject)
args = {
"subject": subject,
"options": options
}
tasks.append(generate(template, args, source))
if not no_pdf:
pdf = naming.source2pdf(source)
tasks.append(compile_pdf(source, pdf))
pdfs.append(pdf)
if corr:
corr_source = naming.corr(source)
tasks.append(activate_corr_on(source, opt, corr_source))
if not no_pdf:
corr_pdf = naming.source2pdf(corr_source)
tasks.append(compile_pdf(corr_source, corr_pdf))
corr_pdfs.append(corr_pdf)
if not no_join:
joined = naming.join(template)
if pdfs:
tasks.append(join_pdfs(pdfs, joined))
if corr_pdfs:
corr_joined = naming.corr(joined)
tasks.append(join_pdfs(corr_pdfs, corr_joined))
return tasks

View File

@ -1,15 +0,0 @@
def template2source(template: str, metadatas: dict):
return metadatas["number"] + template[3:]
def corr(source):
return "corr_" + source
def source2pdf(source):
return source[:-4] + ".pdf"
def join(template):
return source2pdf("joined" + template[3:])

View File

@ -1,78 +0,0 @@
""" Scheduler for action to make """
from bopytex.tasks import Task
from bopytex.worker import Dispatcher
class Scheduler:
"""Scheduler is responsible of getting tasks (the tasks) and yield those that can be done"""
def __init__(self, dispatcher:Dispatcher, output_done: list[str] = None):
self._dispatcher = dispatcher
if output_done is None:
self._output_done = []
else:
self._output_done = output_done
self._tasks = []
self._failed_tasks = []
@property
def tasks(self) -> list[Task]:
"""List all the tasks todo"""
return self._tasks
@property
def doable_tasks(self) -> list[Task]:
"""List all doable tasks"""
return [
task
for task in self.tasks
if not task.deps or all([d in self.output_done for d in task.deps])
]
@property
def all_deps(self) -> set[str]:
"""List dependencies of all tasks"""
return {d for task in self.tasks for d in task.deps}
@property
def all_output(self) -> set[str]:
"""List ouput of all tasks"""
return {task.output for task in self.tasks}
@property
def output_done(self) -> list[str]:
return self._output_done
@property
def failed_tasks(self) -> list[Task]:
return self._failed_tasks
def append(self, tasks: list[Task]):
self._tasks += tasks
def is_finishable(self):
return self.all_deps.issubset(self.all_output)
def next_task(self):
try:
task = self.doable_tasks[0]
except IndexError:
raise StopIteration
self._tasks.remove(task)
message = self._dispatcher(task)
if message.status == 0:
self._output_done.append(task.output)
else:
self._failed_tasks.append(task)
return message
def backlog(self):
""" Yield tasks sorted according to dependencies """
while self.doable_tasks:
yield self.next_task()

View File

@ -4,8 +4,8 @@
import click import click
import logging import logging
from pathlib import Path
from bopytex.service import main from .bopytex import subject_metadatas, crazy_feed, pdfjoin, feed, clean, texcompile, setup, activate_printanswers
formatter = logging.Formatter("%(name)s :: %(levelname)s :: %(message)s") formatter = logging.Formatter("%(name)s :: %(levelname)s :: %(message)s")
steam_handler = logging.StreamHandler() steam_handler = logging.StreamHandler()
@ -15,18 +15,18 @@ logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
logger.addHandler(steam_handler) logger.addHandler(steam_handler)
@click.command() @click.command()
@click.argument( @click.argument(
"template", "template",
type=click.Path(exists=True), type=click.Path(exists=True),
nargs=1, nargs=1,
# help="File with the template. The name should have the following form tpl_... .",
) )
@click.option( @click.option(
"-w", "-w",
"--working-dir", "--working-dir",
default=".",
type=click.Path(exists=True), type=click.Path(exists=True),
help="Where fed templates and compiled files will be placed",
) )
@click.option( @click.option(
"-s", "-s",
@ -35,6 +35,9 @@ logger.addHandler(steam_handler)
default="", default="",
help="CSV containing list of students names", help="CSV containing list of students names",
) )
@click.option(
"-d", "--dirty", is_flag=True, default=False, help="Do not clean after compilation",
)
@click.option( @click.option(
"-n", "-n",
"--no-compile", "--no-compile",
@ -43,18 +46,11 @@ logger.addHandler(steam_handler)
help="Do not compile source code", help="Do not compile source code",
) )
@click.option( @click.option(
"-d", "-N",
"--dirty", "--number_subjects",
is_flag=True,
default=False,
help="Do not clean after compilation",
)
@click.option(
"-q",
"--quantity_subjects",
type=int, type=int,
default=1, default=1,
help="The quantity of subjects to make", help="The number of subjects to make",
) )
@click.option( @click.option(
"-j", "-j",
@ -68,31 +64,94 @@ logger.addHandler(steam_handler)
"--only-corr", "--only-corr",
is_flag=True, is_flag=True,
default=False, default=False,
help="Activate correction and compile only from existing subjects", help="Create and compile only correction from existing subjects",
) )
@click.option( @click.option(
"-C", "-c",
"--corr", "--corr",
is_flag=True, is_flag=True,
default=False, default=False,
help="Create and compile correction while making subjects", help="Create and compile correction while making subjects",
) )
@click.option( @click.option(
"-c", "-C",
"--configfile", "--crazy",
type=str, is_flag=True,
default="bopyptex_config.py", default=False,
help="Config file path", help="Crazy mode. Tries and tries again until template feeding success!",
) )
def new(**options): def new(**options):
for message in main(**options): """ Bopytex
try:
assert message.status == 0 Feed the template (tpl_...) and then compile it with latex.
except AssertionError:
logger.warning(message) """
break setup()
logger.debug(f"CI parser gets {options}")
template = Path(options["template"]).name
directory = Path(options["template"]).parent
metadatas = subject_metadatas(options)
logger.debug(f"Metadata {metadatas}")
for meta in metadatas:
logger.debug(f"Feeding template toward {meta['texfile']}")
if options["crazy"]:
crazy_feed(
template=Path(meta["directory"]) / meta["template"],
data=meta,
output=meta["texfile"],
force=1,
)
else: else:
logger.info(message.out) feed(
template=Path(meta["directory"]) / meta["template"],
data=meta,
output=meta["texfile"],
force=1,
)
assert(Path(meta["texfile"]).exists())
logger.debug(f"{meta['texfile']} fed")
if options["corr"]:
logger.debug(f"Building correction for {meta['texfile']}")
meta.update({
"corr_texfile": activate_printanswers(meta["texfile"]),
})
if not options["no_compile"]:
for prefix in ["", "corr_"]:
key = prefix + "texfile"
try:
meta[key]
except KeyError:
pass
else:
texcompile(meta[key])
meta.update({
prefix+'pdffile': meta[key].replace('tex', 'pdf')
})
if not options["no_join"]:
for prefix in ["", "corr_"]:
key = prefix + "pdffile"
try:
pdfs = [m[key] for m in metadatas]
except KeyError:
pass
else:
pdfjoin(
pdfs,
template.replace("tpl", prefix+"all").replace(".tex", ".pdf"),
directory,
rm_pdfs=1,
)
if not options["dirty"]:
clean(directory)
# produce_and_compile(options)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -1,99 +0,0 @@
#!/usr/bin/env python
# encoding: utf-8
"""
Producing then compiling templates
"""
import importlib.util
import os
from pathlib import Path
from bopytex.scheduler import Scheduler
from bopytex import default_config
def orcherstrator(
options: dict,
planner,
dispatcher,
):
tasks = planner(options)
scheduler = Scheduler(dispatcher, [options["template"]])
scheduler.append(tasks)
for message in scheduler.backlog():
yield message
def load_module(modulefile: str):
spec = importlib.util.spec_from_file_location("module.name", modulefile)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def clean_vars_keys(
vars: dict,
keys: list[str] = [
"__name__",
"__doc__",
"__package__",
"__loader__",
"__spec__",
"__file__",
"__cached__",
"__builtins__",
],
) -> dict:
new_dict = vars.copy()
for k in keys:
del new_dict[k]
return new_dict
def config_from_file(filename: str) -> dict:
if Path(filename).exists():
local_config = vars(load_module(filename))
return clean_vars_keys(local_config)
else:
return {}
def build_config(options: dict) -> dict:
"""Look for options["configfile"] to load it with default_config and options"""
config = clean_vars_keys(vars(default_config))
configfile = ""
try:
configfile = options["configfile"]
except KeyError:
pass
try:
configfile = os.environ["BOPYTEXCONFIG"]
except KeyError:
pass
if configfile:
local_config = config_from_file(configfile)
config.update(local_config)
config.update(options)
return config
def main(**options):
config = build_config(options)
orcherstre = orcherstrator(
config, planner=default_config.planner, dispatcher=default_config.dispatcher
)
for message in orcherstre:
yield message
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del

View File

@ -1,64 +0,0 @@
""" Produce tasks to do
It essentially place things at the right place.
"""
from dataclasses import dataclass
@dataclass
class Task:
action: str
args: dict
deps: list
output: str
def generate(template: str, meta: dict, output: str):
"""Create a task to generate a subject"""
return Task(
action="GENERATE",
args=meta,
deps=[template],
output=output,
)
def activate_corr_on(src: str, meta:dict, output: str):
"""Create a task to activate correction for src"""
return Task(
action="ACTIVATE_CORR",
args=meta,
deps=[src],
output=output,
)
def compile_pdf(src: str, output: str):
"""Create a task to compile src"""
return Task(
action="COMPILE",
args={},
deps=[src],
output=output,
)
def join_pdfs(pdfs: list, output: str):
"""Create task to join pdf together"""
return Task(
action="JOIN",
args={},
deps=pdfs,
output=output,
)
def clean(files: list):
"""Create task to clean files"""
return Task(
action="CLEAN",
args={},
deps=files,
output=None,
)

View File

@ -1,18 +0,0 @@
class ActionNotFound(Exception):
pass
class Dispatcher:
def __init__(self, actions: list):
self._actions = actions
def __call__(self, task):
try:
choosen_action = self._actions[task.action]
except KeyError:
raise ActionNotFound(
f"The action {task.action} is not in {self._actions.keys()}"
)
return choosen_action(args=task.args, deps=task.deps, output=task.output)

View File

@ -1,14 +0,0 @@
from bopytex.message import Message
def activate_corr(args, deps, output):
no_solution = args["latex"]["no_solution"]
solution = args["latex"]["solution"]
with open(deps[0], "r") as input_f:
with open(output, "w") as output_f:
for line in input_f.readlines():
output_f.write(line.replace(no_solution, solution))
return Message(0, [f"ACTIVATE CORR - {deps[0]} to {output}"], [])

View File

@ -1,2 +0,0 @@
def clean(args: dict, deps, output):
pass

View File

@ -1,25 +0,0 @@
import subprocess
from bopytex.message import Message
from ..message import SubprocessMessage
def curstomtex(command: str, options: str):
def func(args: dict, deps, output) -> Message:
compile_process = subprocess.Popen(
[command, options, deps[0]],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
)
return Message(
compile_process.wait(),
list(compile_process.stdout),
list(compile_process.stderr),
)
return func
latexmk = curstomtex("latexmk", "-f")
pdflatex = curstomtex("pdflatex", "--interaction=nonstopmode")

View File

@ -1,32 +0,0 @@
from jinja2.environment import Template
from bopytex.message import Message
def generate(args, deps, output):
env = args["options"]["jinja2"]["environment"]
template = env.get_template(deps[0])
variables = {
"options":args["options"],
"subject":args["subject"],
}
try:
args["options"]["direct_access"]
except KeyError:
pass
else:
for (k,v) in args["options"]["direct_access"].items():
if k not in ["options", "subject"]:
variables[k] = v
try:
with open(output, "w") as out:
fed = template.render(variables)
out.write(fed)
return Message(0, [f"GENERATE - {deps[0]} to {output}"], [])
except Exception as e:
return Message(1, [], [e])

View File

@ -1,33 +0,0 @@
import subprocess
from bopytex.message import Message, SubprocessMessage
def pdfjam(args: dict, deps, output):
joining_process = subprocess.Popen(
["pdfjam"] + deps + ["-o", output],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
)
return Message(
joining_process.wait(),
list(joining_process.stdout),
list(joining_process.stderr),
)
def gs(args: dict, deps, output):
""" Not working. The command works in terminal but not here """
joining_process = subprocess.Popen(
["gs", f"-dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile={output}"]
+ deps,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
)
return Message(
joining_process.wait(),
list(joining_process.stdout),
list(joining_process.stderr),
)

Binary file not shown.

View File

@ -1,30 +0,0 @@
\documentclass[12pt]{article}
\title{Bopytex example -- \Var{ subject.number }}
\begin{document}
\maketitle
%- set a = 10
%- set n = 2
We have two variables
\begin{itemize}
\item a: \Var{a}
\item n: \Var{n}
\end{itemize}
%# We can use blocks
\begin{itemize}
%- for i in range(n)
\item \Var{a}
%- endfor
\end{itemize}
\section{Variables}
\subsection{subject}
\Var{subject}
\subsection{options}
\Var{options}
\end{document}

View File

@ -1,4 +0,0 @@
from mapytex import Expression
direct_access = {
"Expression": Expression
}

Binary file not shown.

View File

@ -1,6 +0,0 @@
"Name","Age","Email","fraction level","calculus level"
"Spike Tucker","22","s.tucker@randatmail.com","7","3"
"Martin Payne","21","m.payne@randatmail.com","7","3"
"Kimberly Baker","20","k.baker@randatmail.com","1","8"
"Emma Bailey","29","e.bailey@randatmail.com","2","5"
"Nicholas Taylor","28","n.taylor@randatmail.com","3","3"
1 Name Age Email fraction level calculus level
2 Spike Tucker 22 s.tucker@randatmail.com 7 3
3 Martin Payne 21 m.payne@randatmail.com 7 3
4 Kimberly Baker 20 k.baker@randatmail.com 1 8
5 Emma Bailey 29 e.bailey@randatmail.com 2 5
6 Nicholas Taylor 28 n.taylor@randatmail.com 3 3

View File

@ -1,22 +0,0 @@
\documentclass[12pt]{article}
\title{Bopytex with Mapytex example -- \Var{ subject.number }}
% solution/print = false
\begin{document}
\maketitle
%- set e = Expression.random("{a} + {b}")
\Var{e}
\Var{e.simplify()}
\section{Variables}
\subsection{subject}
\Var{subject}
\subsection{options}
\Var{options}
\end{document}

View File

@ -0,0 +1,19 @@
% vim:ft=tex:
%
\documentclass[12pt]{article}
\begin{document}
\section{Ajouts de fractions}
Adding two fractions
%- set e = Expression.random("{a} / {b} + {c} / {k*b}", ["b > 1", "k>1"])
\[
A = \Var{e}
\]
Solution
\[
\Var{e.simplify().explain() | join('=')}
\]
\end{document}

View File

@ -0,0 +1,33 @@
alabaster==0.7.10
appdirs==1.4.3
Babel==2.4.0
decorator==4.0.11
docutils==0.13.1
imagesize==0.7.1
ipython==5.3.0
ipython-genutils==0.2.0
Jinja2==2.9.6
location==0.0.7
MarkupSafe==1.0
mpmath==0.19
packaging==16.8
pexpect==4.2.1
pickleshare==0.7.4
prompt-toolkit==1.0.14
ptyprocess==0.5.1
Pygments==2.2.0
pyparsing==2.2.0
## !! Could not determine repository location
pytz==2017.2
requests==2.13.0
simplegeneric==0.8.1
six==1.10.0
snowballstemmer==1.2.1
Sphinx==1.5.5
sphinx-rtd-theme==0.2.4
sympy==1.0
traitlets==4.3.2
wcwidth==0.1.7
mapytex==2.0.8
mypytex==0.2
path.py==12.1

View File

@ -4,18 +4,21 @@
from setuptools import setup from setuptools import setup
setup( setup(
name="Bopytex", name='Bopytex',
version="0.9", version='0.1.2',
description="Command line tool for compiling latex with python command embedded", description='Command line tool for compiling latex with python command embedded',
author="Benjamin Bertrand", author='Benjamin Bertrand',
author_email="benjamin.bertrand@opytex.org", author_email='programming@opytex.org',
packages=["bopytex"], packages=['bopytex'],
install_requires=[ install_requires=[
"click", 'mapytex',
"jinja2", 'mypytex',
'click',
], ],
entry_points={"console_scripts": ["bopytex=bopytex.script:new"]}, entry_points={
) "console_scripts": ['bopytex=bopytex.script:new']
},
)
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'

View File

View File

View File

@ -1,10 +0,0 @@
from bopytex.worker import Dispatcher
from .workers import fake_worker, success_worker, fail_worker
fake_dispatcher = Dispatcher(
{
"FAKE": fake_worker,
"SUCCESS": success_worker,
"FAILURE": fail_worker,
}
)

View File

@ -1,9 +0,0 @@
from bopytex.tasks import Task
def simple(options: dict) -> list[Task]:
"""Simple planner with options['quantity'] tasks and no dependencies"""
return [
Task("FAKE", args={"number": i}, deps=[], output=f"{i}")
for i in range(options["quantity"])
]

View File

@ -1,13 +0,0 @@
from bopytex.message import Message
def fake_worker(args, deps, output):
return Message(0, [f"FAKE - {args} - {deps} - {output}"], [])
def success_worker(args, deps, output):
return Message(0, [f"SUCCESS - {args} - {deps} - {output}"], [])
def fail_worker(args, deps, output):
return Message(1, [f"FAILURE - {args} - {deps} - {output}"], [])

6
test/students.csv Normal file
View File

@ -0,0 +1,6 @@
nom,classe,elo
Bob,1ST,1000
Pipo,1ST,1300
Popi,1ST,100
Boule,1ST,4000
Bill,1ST,1300
1 nom classe elo
2 Bob 1ST 1000
3 Pipo 1ST 1300
4 Popi 1ST 100
5 Boule 1ST 4000
6 Bill 1ST 1300

View File

@ -0,0 +1,22 @@
\documentclass[12pt]{article}
\usepackage[utf8x]{inputenc}
\usepackage[francais]{babel}
\usepackage[T1]{fontenc}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsfonts}
\title{
Tests
}
\author{
Benjamin Bertrand
}
\begin{document}
\maketitle
\end{document}

212
test/test_bopytex.py Normal file
View File

@ -0,0 +1,212 @@
#!/usr/bin/env python
# encoding: utf-8
import pytest
import os
from pathlib import Path
from shutil import copyfile
from bopytex.bopytex import produce_and_compile, subject_metadatas
SNIPPETS_PATH = Path("snippets/")
TEST_PATH = Path("test")
TEST_TEMPLATE_PATH = TEST_PATH / "templates/"
@pytest.fixture
def prepare_test_template(tmp_path):
""" Create a tmp directory, copy snippets inside
return tmp directory name
"""
tmp = tmp_path
snippets = TEST_TEMPLATE_PATH.glob("tpl_*.tex")
for s in snippets:
copyfile(s, tmp / s.name)
csvs = TEST_PATH.glob("*.csv")
for s in csvs:
copyfile(s, tmp / s.name)
prev_dir = Path.cwd()
os.chdir(tmp)
yield tmp
os.chdir(prev_dir)
@pytest.fixture
def prepare_snippets(tmp_path):
""" Create a tmp directory, copy snippets inside
return tmp directory name
"""
tmp = tmp_path
snippets = SNIPPETS_PATH.glob("tpl_*.tex")
for s in snippets:
copyfile(s, tmp / s.name)
prev_dir = Path.cwd()
os.chdir(tmp)
yield tmp
os.chdir(prev_dir)
def test_produce_and_compile_base(prepare_test_template):
test_tpl = list(Path(".").glob("tpl_*.tex"))
assert [tpl.name for tpl in test_tpl] == ["tpl_test.tex"]
for tpl in test_tpl:
produce_and_compile(
{
"template": tpl,
"working_dir": None,
"only_corr": False,
"students_csv": None,
"number_subjects": 1,
"dirty": False,
"no_compile": False,
"no_join": False,
"corr": False,
"crazy": False,
}
)
def test_produce_and_compile_csv(prepare_test_template):
test_tpl = Path(".").glob("tpl_*.tex")
for tpl in test_tpl:
options = {
"template": tpl,
"working_dir": None,
"only_corr": False,
"students_csv": "students.csv",
"number_subjects": 1,
"dirty": False,
"no_compile": False,
"no_join": False,
"corr": False,
"crazy": False,
}
# produce_and_compile(options)
def test_metadatas(prepare_test_template):
test_tpl = Path(".").glob("tpl_*.tex")
for tpl in test_tpl:
options = {
"template": tpl,
"working_dir": None,
"only_corr": False,
"students_csv": "students.csv",
"number_subjects": 1,
"dirty": False,
"no_compile": False,
"no_join": False,
"corr": False,
"crazy": False,
}
metadatas = subject_metadatas(options)
meta = [
{
"num": "01",
"nom": "Bob",
"classe": "1ST",
"elo": "1000",
"texfile": "01_test.tex",
"template": "tpl_test.tex",
"directory": ".",
},
{
"num": "02",
"nom": "Pipo",
"classe": "1ST",
"elo": "1300",
"texfile": "02_test.tex",
"template": "tpl_test.tex",
"directory": ".",
},
{
"num": "03",
"nom": "Popi",
"classe": "1ST",
"elo": "100",
"texfile": "03_test.tex",
"template": "tpl_test.tex",
"directory": ".",
},
{
"num": "04",
"nom": "Boule",
"classe": "1ST",
"elo": "4000",
"texfile": "04_test.tex",
"template": "tpl_test.tex",
"directory": ".",
},
{
"num": "05",
"nom": "Bill",
"classe": "1ST",
"elo": "1300",
"texfile": "05_test.tex",
"template": "tpl_test.tex",
"directory": ".",
},
]
assert metadatas == meta
def test_pdfjoin_current_directory(prepare_test_template):
wdir = prepare_test_template
pass
def test_pdfjoin_deep_directory():
pass
def test_pdfjoin_dont_remove():
pass
def test_subject_names():
pass
def test_feed_texfiles():
pass
def test_tex2pdf_current_directory():
pass
def test_tex2pdf_deep_directory():
pass
def test_activate_solution():
pass
# def test_snippets(prepare_snippets):
# snippets = list(Path(".").glob("tpl_*.tex"))
# for tpl in snippets:
# produce_and_compile(
# {
# "template": tpl,
# "working_dir": None,
# "only_corr": False,
# "students_csv": None,
# "number_subjects": 1,
# "dirty": False,
# "no_compile": False,
# "no_join": False,
# "corr": False,
# "crazy": False,
# }
# )
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del

View File

@ -1,85 +0,0 @@
import os
import jinja2
from pathlib import Path
from bopytex.service import main
import pytest
@pytest.fixture
def template_path(tmp_path):
template = tmp_path / "tpl_source.tex"
with open(template, "w") as tpl:
tpl.write(
"""
\\documentclass{article}
\\begin{document}
First document.
Subject {{ number }}
\\end{document}
"""
)
return template
@pytest.fixture
def bad_template_path(tmp_path):
template = tmp_path / "tpl_source.tex"
with open(template, "w") as tpl:
tpl.write(
"""
\\documentclass{article}
\\begin{document}
First document.
Subject {{ number }}
"""
)
return template
@pytest.fixture
def jinja2_env(tmp_path):
templateEnv = jinja2.Environment(loader=jinja2.FileSystemLoader(tmp_path))
return templateEnv
def test_with_default_planner(template_path, jinja2_env, tmp_path):
os.chdir(tmp_path)
options = {
"template": str(template_path.name),
"quantity_subjects": 3,
"corr": False,
"no_join": False,
"no_pdf": False,
"jinja2": {
"environment": jinja2_env,
},
}
for message in main(**options):
assert message.status == 0
assert Path("joined_source.pdf").exists()
def test_with_default_planner_bad_template(bad_template_path, jinja2_env, tmp_path):
os.chdir(tmp_path)
options = {
"template": str(bad_template_path.name),
"quantity_subjects": 3,
"corr": False,
"no_join": False,
"no_pdf": False,
"jinja2": {
"environment": jinja2_env,
},
}
for message in main(**options):
pass
assert not Path("joined_source.pdf").exists()

View File

@ -1,382 +0,0 @@
from bopytex.planner.generate_compile_join_planner import tasks_builder as gcj_task_builder
from bopytex.planner.activate_corr_compile_join_planner import tasks_builder as accj_task_builder
from bopytex.tasks import Task
def test_tasks_builder_generate():
tasks = gcj_task_builder(
options={
"template": "tpl_source.tex",
"subjects": [{"number": "01"}, {"number": "02"}],
"no_pdf": True,
}
)
assert tasks == [
Task(
action="GENERATE",
args={
"options": {
"no_pdf": True,
"subjects": [{"number": "01"}, {"number": "02"}],
'template': 'tpl_source.tex',
},
"subject": {"number": "01"}
},
deps=["tpl_source.tex"],
output="01_source.tex",
),
Task(
action="GENERATE",
args={
"options": {
"no_pdf": True,
"subjects": [{"number": "01"}, {"number": "02"}],
'template': 'tpl_source.tex',
},
"subject": {"number": "02"}
},
deps=["tpl_source.tex"],
output="02_source.tex",
),
]
def test_tasks_builder_generate_compile():
tasks = gcj_task_builder(
options={
"template": "tpl_source.tex",
"subjects": [{"number": "01"}, {"number": "02"}],
"no_join": True,
}
)
assert tasks == [
Task(
action="GENERATE",
args={
"options": {
"no_join": True,
"subjects": [{"number": "01"}, {"number": "02"}],
'template': 'tpl_source.tex',
},
"subject": {"number": "01"}
},
deps=["tpl_source.tex"],
output="01_source.tex",
),
Task(
action="COMPILE",
args={},
deps=["01_source.tex"],
output="01_source.pdf",
),
Task(
action="GENERATE",
args={
"options": {
"no_join": True,
"subjects": [{"number": "01"}, {"number": "02"}],
'template': 'tpl_source.tex',
},
"subject": {"number": "02"}
},
deps=["tpl_source.tex"],
output="02_source.tex",
),
Task(
action="COMPILE",
args={},
deps=["02_source.tex"],
output="02_source.pdf",
),
]
def test_tasks_builder_generate_compile_join():
tasks = gcj_task_builder(
options={
"template": "tpl_source.tex",
"subjects": [{"number": "01"}, {"number": "02"}],
}
)
assert tasks == [
Task(
action="GENERATE",
args={
"options": {
"subjects": [{"number": "01"}, {"number": "02"}],
'template': 'tpl_source.tex',
},
"subject": {"number": "01"}
},
deps=["tpl_source.tex"],
output="01_source.tex",
),
Task(
action="COMPILE",
args={},
deps=["01_source.tex"],
output="01_source.pdf",
),
Task(
action="GENERATE",
args={
"options": {
"subjects": [{"number": "01"}, {"number": "02"}],
'template': 'tpl_source.tex',
},
"subject": {"number": "02"}
},
deps=["tpl_source.tex"],
output="02_source.tex",
),
Task(
action="COMPILE",
args={},
deps=["02_source.tex"],
output="02_source.pdf",
),
Task(
action="JOIN",
args={},
deps=["01_source.pdf", "02_source.pdf"],
output="joined_source.pdf",
),
]
def test_tasks_builder_generate_compile_corr():
tasks = gcj_task_builder(
options={
"template": "tpl_source.tex",
"subjects": [{"number": "01"}, {"number": "02"}],
"corr": True,
"no_join": True,
}
)
assert tasks == [
Task(
action="GENERATE",
args={
"options": {
"subjects": [{"number": "01"}, {"number": "02"}],
"corr": True,
"no_join": True,
'template': 'tpl_source.tex',
},
"subject": {"number": "01"}
},
deps=["tpl_source.tex"],
output="01_source.tex",
),
Task(
action="COMPILE",
args={},
deps=["01_source.tex"],
output="01_source.pdf",
),
Task(
action="ACTIVATE_CORR",
args={
'corr': True,
'no_join': True,
'no_pdf': False,
'template': 'tpl_source.tex',
"subjects": [{'number': '01'}, {'number': '02'}]
},
deps=["01_source.tex"],
output="corr_01_source.tex",
),
Task(
action="COMPILE",
args={},
deps=["corr_01_source.tex"],
output="corr_01_source.pdf",
),
Task(
action="GENERATE",
args={
"options": {
"subjects": [{"number": "01"}, {"number": "02"}],
"corr": True,
"no_join": True,
'template': 'tpl_source.tex',
},
"subject": {"number": "02"}
},
deps=["tpl_source.tex"],
output="02_source.tex",
),
Task(
action="COMPILE",
args={},
deps=["02_source.tex"],
output="02_source.pdf",
),
Task(
action="ACTIVATE_CORR",
args={
'corr': True,
'no_join': True,
'no_pdf': False,
'template': 'tpl_source.tex',
"subjects": [{'number': '01'}, {'number': '02'}]
},
deps=["02_source.tex"],
output="corr_02_source.tex",
),
Task(
action="COMPILE",
args={},
deps=["corr_02_source.tex"],
output="corr_02_source.pdf",
),
]
def test_tasks_builder_generate_compile_corr_joined():
tasks = gcj_task_builder(
options={
"template": "tpl_source.tex",
"subjects": [{"number": "01"}, {"number": "02"}],
"corr": True,
"no_join": False,
}
)
assert tasks == [
Task(
action="GENERATE",
args={
"options": {
"subjects": [{"number": "01"}, {"number": "02"}],
"corr": True,
"no_join": False,
'template': 'tpl_source.tex',
},
"subject": {"number": "01"}
},
deps=["tpl_source.tex"],
output="01_source.tex",
),
Task(
action="COMPILE",
args={},
deps=["01_source.tex"],
output="01_source.pdf",
),
Task(
action="ACTIVATE_CORR",
args={
'corr': True,
'no_join': False,
'no_pdf': False,
'template': 'tpl_source.tex',
"subjects": [{'number': '01'}, {'number': '02'}]
},
deps=["01_source.tex"],
output="corr_01_source.tex",
),
Task(
action="COMPILE",
args={},
deps=["corr_01_source.tex"],
output="corr_01_source.pdf",
),
Task(
action="GENERATE",
args={
"options": {
"subjects": [{"number": "01"}, {"number": "02"}],
"corr": True,
"no_join": False,
'template': 'tpl_source.tex',
},
"subject": {"number": "02"}
},
deps=["tpl_source.tex"],
output="02_source.tex",
),
Task(
action="COMPILE",
args={},
deps=["02_source.tex"],
output="02_source.pdf",
),
Task(
action="ACTIVATE_CORR",
args={
'corr': True,
'no_join': False,
'no_pdf': False,
'template': 'tpl_source.tex',
"subjects": [{'number': '01'}, {'number': '02'}]
},
deps=["02_source.tex"],
output="corr_02_source.tex",
),
Task(
action="COMPILE",
args={},
deps=["corr_02_source.tex"],
output="corr_02_source.pdf",
),
Task(
action="JOIN",
args={},
deps=["01_source.pdf", "02_source.pdf"],
output="joined_source.pdf",
),
Task(
action="JOIN",
args={},
deps=["corr_01_source.pdf", "corr_02_source.pdf"],
output="corr_joined_source.pdf",
),
]
def test_only_corr_tasks_builder():
tasks = accj_task_builder(
options={
"sources": ["01_source.tex", "02_source.tex"],
}
)
assert tasks == [
Task(
action="ACTIVATE_CORR",
args={
'no_join': False,
'no_pdf': False,
'sources': ['01_source.tex', '02_source.tex']
},
deps=["01_source.tex"],
output="corr_01_source.tex",
),
Task(
action="COMPILE",
args={},
deps=["corr_01_source.tex"],
output="corr_01_source.pdf",
),
Task(
action="ACTIVATE_CORR",
args={
'no_join': False,
'no_pdf': False,
'sources': ['01_source.tex', '02_source.tex']
},
deps=["02_source.tex"],
output="corr_02_source.tex",
),
Task(
action="COMPILE",
args={},
deps=["corr_02_source.tex"],
output="corr_02_source.pdf",
),
Task(
action="JOIN",
args={},
deps=["corr_01_source.pdf", "corr_02_source.pdf"],
output="corr_joined.pdf",
),
]

View File

@ -1,161 +0,0 @@
from bopytex.message import Message
from bopytex.tasks import Task
from bopytex.scheduler import Scheduler
from .fakes.dispatcher import fake_dispatcher
import pytest
def test_schedule_append():
scheduler = Scheduler(dispatcher=fake_dispatcher)
tasks = [
Task(action="FAKE", args={}, deps=["dep1", "dep2"], output="end1"),
Task(action="FAKE", args={}, deps=["dep1", "dep3"], output="end2"),
]
scheduler.append(tasks)
assert scheduler.tasks == tasks
assert scheduler.all_deps == {"dep1", "dep2", "dep3"}
assert scheduler.all_output == {"end1", "end2"}
def test_schedule_one_task():
scheduler = Scheduler(dispatcher=fake_dispatcher)
tasks = [Task(action="FAKE", args={}, deps=[], output="end")]
scheduler.append(tasks)
assert scheduler.doable_tasks == tasks
result = scheduler.next_task()
assert result.status == 0
assert result.out == ["FAKE - {} - [] - end"]
assert result.err == []
assert scheduler.tasks == []
assert scheduler.output_done == ["end"]
def test_schedule_one_task_with_args():
scheduler = Scheduler(dispatcher=fake_dispatcher)
tasks = [Task(action="FAKE", args={"task": "one"}, deps=[], output="one")]
scheduler.append(tasks)
result = scheduler.next_task()
assert result.status == 0
assert result.out == ["FAKE - {'task': 'one'} - [] - one"]
assert result.err == []
assert scheduler.tasks == []
assert scheduler.output_done == ["one"]
def test_schedule_multiple_tasks():
scheduler = Scheduler(dispatcher=fake_dispatcher)
t1 = Task(action="FAKE", args={"task": "one"}, deps=[], output="one")
t2 = Task(action="FAKE", args={"task": "two"}, deps=[], output="two")
t3 = Task(action="FAKE", args={"task": "three"}, deps=[], output="three")
scheduler.append([t1, t2, t3])
assert scheduler.doable_tasks == [t1, t2, t3]
assert scheduler.is_finishable()
result = scheduler.next_task()
assert result.status == 0
assert scheduler.tasks == [t2, t3]
assert scheduler.output_done == ["one"]
result = scheduler.next_task()
assert result.status == 0
assert scheduler.tasks == [t3]
assert scheduler.output_done == ["one", "two"]
result = scheduler.next_task()
assert result.status == 0
assert scheduler.tasks == []
assert scheduler.output_done == ["one", "two", "three"]
def test_schedule_multiple_tasks_with_dependencies():
scheduler = Scheduler(dispatcher=fake_dispatcher)
t1 = Task(action="FAKE", args={"task": "one"}, deps=["three"], output="one")
t2 = Task(action="FAKE", args={"task": "two"}, deps=["one"], output="two")
t3 = Task(action="FAKE", args={"task": "three"}, deps=[], output="three")
scheduler.append([t1, t2, t3])
assert scheduler.doable_tasks == [t3]
assert scheduler.is_finishable()
result = scheduler.next_task()
assert result.status == 0
assert scheduler.tasks == [t1, t2]
assert scheduler.output_done == ["three"]
assert scheduler.doable_tasks == [t1]
result = scheduler.next_task()
assert result.status == 0
assert scheduler.tasks == [t2]
assert scheduler.output_done == ["three", "one"]
assert scheduler.doable_tasks == [t2]
result = scheduler.next_task()
assert result.status == 0
assert scheduler.tasks == []
assert scheduler.output_done == ["three", "one", "two"]
def test_schedule_multiple_tasks_with_dependencies_loop():
scheduler = Scheduler(dispatcher=fake_dispatcher)
t1 = Task(action="FAKE", args={"task": "one"}, deps=["three"], output="one")
t2 = Task(action="FAKE", args={"task": "two"}, deps=["one"], output="two")
t3 = Task(action="FAKE", args={"task": "three"}, deps=[], output="three")
scheduler.append([t1, t2, t3])
for task in scheduler.backlog():
pass
assert scheduler.output_done == ["three", "one", "two"]
def test_schedule_empty_task():
scheduler = Scheduler(dispatcher=fake_dispatcher)
scheduler.append([])
with pytest.raises(StopIteration):
scheduler.next_task()
def test_schedule_multiple_tasks_with_undoable_dependencies():
scheduler = Scheduler(dispatcher=fake_dispatcher)
t1 = Task(action="FAKE", args={"task": "one"}, deps=["three"], output="one")
t2 = Task(action="FAKE", args={"task": "two"}, deps=[], output="two")
scheduler.append([t1, t2])
assert scheduler.doable_tasks == [t2]
assert not scheduler.is_finishable()
for _ in scheduler.backlog():
pass
assert scheduler.tasks == [t1]
assert scheduler.output_done == ["two"]
assert scheduler.doable_tasks == []
def test_schedule_multiple_tasks_with_failling_tasks():
scheduler = Scheduler(dispatcher=fake_dispatcher)
t1 = Task(action="FAILURE", args={"task": "one"}, deps=["three"], output="one")
t2 = Task(action="FAKE", args={"task": "two"}, deps=["one"], output="two")
t3 = Task(action="FAKE", args={"task": "three"}, deps=[], output="three")
t4 = Task(action="FAILURE", args={"task": "four"}, deps=[], output="four")
scheduler.append([t1, t2, t3, t4])
assert scheduler.doable_tasks == [t3, t4]
assert scheduler.is_finishable()
status = []
for message in scheduler.backlog():
status.append(message.status)
assert status == [0, 1, 1]
assert scheduler.tasks == [t2]
assert scheduler.failed_tasks == [t1, t4]
assert scheduler.output_done == ["three"]
assert scheduler.doable_tasks == []

View File

@ -1,60 +0,0 @@
import os
import pytest
from bopytex.planner import fake_planner
from bopytex.service import build_config, orcherstrator
from bopytex.tasks import Task
from bopytex.worker import Dispatcher
from .fakes.workers import fake_worker
def test_service():
options = {"quantity": 3, "template": "tpl_src.tex"}
dispatcher = Dispatcher(actions={"DO": fake_worker})
service = orcherstrator(options, fake_planner.simple, dispatcher)
for i, message in enumerate(service):
assert message.status == 0
assert message.out == [f"FAKE - {{'number': {i}}} - [] - {i}"]
def test_get_config_no_configfile():
pass
@pytest.fixture
def config_file(tmp_path):
config_file = tmp_path / "bopytex_config.py"
with open(config_file, "w") as f:
f.write(
"""
from bopytex.jinja2_env.texenv import texenv
jinja2 = {
"environment": texenv,
}
"""
)
return config_file
def test_get_config_with_configfile(config_file, tmp_path):
os.chdir(tmp_path)
config = build_config({"from_option": "config", "configfile": str(config_file)})
assert type(config) == dict
assert set(config.keys()) == {
"generate",
"configfile",
"Dispatcher",
"planner",
"pdflatex",
"clean",
"latex",
"activate_corr",
"dispatcher",
"pdfjam",
"jinja2",
"texenv",
"from_option",
}
assert list(config["jinja2"].keys()) == ["environment"]

View File

@ -1,41 +0,0 @@
from bopytex.tasks import activate_corr_on, clean, compile_pdf, generate, join_pdfs
def test_build_task_generate():
template = "tpl_source.tex"
task = generate(template, {"subject": "01"}, output="source.tex")
assert task.action == "GENERATE"
assert task.args == {"subject": "01"}
assert task.deps == [template]
def test_build_task_activate_corr_on():
src = "source.tex"
task = activate_corr_on(src, meta={}, output="corr_source.tex")
assert task.action == "ACTIVATE_CORR"
assert task.args == {}
assert task.deps == [src]
def test_build_task_compile():
src = "source.tex"
task = compile_pdf(src, output="source.pdf")
assert task.action == "COMPILE"
assert task.args == {}
assert task.deps == [src]
def test_build_task_join():
pdfs = [f"{i}_source.pdf" for i in range(3)]
task = join_pdfs(pdfs, output="joined.pdf")
assert task.action == "JOIN"
assert task.args == {}
assert task.deps == pdfs
def test_build_task_clean():
files = ["source.aux", "source.log"]
task = clean(files)
assert task.action == "CLEAN"
assert task.args == {}
assert task.deps == files

View File

@ -1,40 +0,0 @@
from bopytex.jinja2_env.texenv import texenv
def test_variable_block():
base_template = r"\Var{a}"
jinja2_template = texenv.from_string(base_template)
output = jinja2_template.render(a=2)
assert output == "2"
def test_block_string():
base_template = r"\Block{set a = 2}\Var{a}"
jinja2_template = texenv.from_string(base_template)
output = jinja2_template.render()
assert output == "2"
def test_block_line_statement():
base_template = r"""%-set a = 2
\Var{a}"""
jinja2_template = texenv.from_string(base_template)
output = jinja2_template.render()
assert output == "2"
def test_block_line_statement_with_comment():
base_template = r"""%-set a = 2
%# comment
\Var{a}"""
jinja2_template = texenv.from_string(base_template)
output = jinja2_template.render()
assert output == "\n2"
def test_add_filter():
texenv.filters["count_caracters"] = lambda x: len(x)
base_template = r"""\Var{a} has \Var{a | count_caracters}"""
jinja2_template = texenv.from_string(base_template)
output = jinja2_template.render(a="coucou")
assert output == "coucou has 6"

Binary file not shown.

View File

@ -1,35 +0,0 @@
import os
from pathlib import Path
from bopytex.worker.compile import latexmk
import pytest
@pytest.fixture
def tex_path(tmp_path):
source = tmp_path / "source.tex"
with open(source, "w") as src:
src.write(
"""
\\documentclass{article}
\\begin{document}
First document. This is a simple example, with no
extra parameters or packages included.
\\end{document}
"""
)
return source
def test_latexmk(tex_path, tmp_path):
#tmp_path = tex_path.parent
os.chdir(tmp_path)
texfile = str(tex_path.name)
output = "source.pdf"
message = latexmk({}, [texfile], "source.pdf")
assert message.status == 0
assert Path(output).exists

View File

@ -1,86 +0,0 @@
import os
import jinja2
from bopytex.worker.generate import generate
import pytest
@pytest.fixture
def jinja2_env(tmp_path):
templateEnv = jinja2.Environment(loader=jinja2.FileSystemLoader(tmp_path))
return templateEnv
@pytest.fixture
def template_path(tmp_path):
template = tmp_path / "template.j2"
with open(template, "w") as tpl:
tpl.write("Plop {{ a }}")
return template
def test_generate(template_path, jinja2_env):
tmp_path = template_path.parent
os.chdir(tmp_path)
assert template_path.exists
template = str(template_path.name)
output = "output"
message = generate(
args={
"options": {
"direct_access": {"a": 2},
"jinja2": {"environment": jinja2_env},
},
"subject": {},
},
deps=[template],
output=output,
)
print(message.err)
assert message.status == 0
assert message.out == ["GENERATE - template.j2 to output"]
with open(output, "r") as out:
lines = out.readlines()
assert lines == ["Plop 2"]
@pytest.fixture
def template_path_with_random(tmp_path):
template = tmp_path / "template.j2"
with open(template, "w") as tpl:
tpl.write("Plop {{ random.randint(0, 10) }}")
return template
def test_generate_with_random(template_path_with_random, jinja2_env):
tmp_path = template_path_with_random.parent
os.chdir(tmp_path)
assert template_path_with_random.exists
template = str(template_path_with_random.name)
output = "output"
import random
message = generate(
args={
"options": {
"jinja2": {"environment": jinja2_env},
"direct_access": {
"random": random,
}
},
"subject":{},
},
deps=[template],
output=output,
)
print(message.err)
assert message.status == 0
with open(output, "r") as out:
lines = out.readlines()
assert int(lines[0][-1]) >= 0
assert int(lines[0][-1]) < 10

View File

@ -1,50 +0,0 @@
import os
import shutil
from pathlib import Path
from bopytex.worker.join_pdf import pdfjam
import pytest
@pytest.fixture
def multiple_pdf(tmp_path, request):
this_file = Path(request.module.__file__)
source = this_file.parent / "source.pdf"
assert source.exists()
qty = 3
dests = []
for i in range(qty):
dest = tmp_path / f"source_{i}.pdf"
shutil.copyfile(source, dest)
assert dest.exists()
dests.append(dest)
return dests
def test_join_pdf(multiple_pdf):
tmp_path = multiple_pdf[0].parent
os.chdir(tmp_path)
deps = [str(d.name) for d in multiple_pdf]
output = "joined.pdf"
message = pdfjam({"pwd": Path.cwd()}, deps, output)
assert message.status == 0
assert Path(output).exists()
def test_join_pdf_failed(multiple_pdf, tmp_path):
os.chdir(tmp_path)
deps = [str(d.name) for d in multiple_pdf] + ["doesnotexists.pdf"]
output = "joined.pdf"
message = pdfjam({"pwd": Path.cwd()}, deps, output)
assert message.status == 66
assert not Path(output).exists()