Compare commits

...

76 Commits

Author SHA1 Message Date
d204eb19e1 Merge branch 'decoupled' into dev
All checks were successful
continuous-integration/drone/push Build is passing
2022-07-20 16:46:45 +02:00
b5f7cdb0cf Feat: add stat for templates 2022-07-20 16:45:21 +02:00
ae09d37523 Feat: update README 2022-07-20 16:44:03 +02:00
5bf9d8b7c1 Fix: Reading subject infos from csv file 2022-07-20 16:38:38 +02:00
e6595b7041 Fix: adapt examples with the new variable access 2022-07-20 16:20:21 +02:00
67656c2cf1 Feat: arrange variables for templates
Split in 3 categories
- from "options" key build with default_config, file_config and cli options
- from "subject" key build csv or quantity of subject
- other in direct access from "direct_access" dictionnary inside the
  file_config
2022-07-20 16:16:09 +02:00
37d779c0ab Feat: activate solution worker 2022-07-19 18:09:17 +02:00
177128afe2 Feat: add a make file to run docker 2022-07-19 17:28:32 +02:00
b1424f096b Feat: move Dockerfile to Dockerfile.simple 2022-07-19 17:28:19 +02:00
0a40e132c5 Feat: rename dockerfile from mapytex to usecase 2022-07-19 17:13:23 +02:00
3d54cce718 Refact: move mapytex to usecase and build example 2022-07-19 16:37:14 +02:00
473f554ebe Doc: update README with docker examples 2022-07-19 16:33:51 +02:00
1fd5ca9c96 Feat: activate texenv by default 2022-07-19 16:33:27 +02:00
8c20ff8e4a Feat: rename get_config 2022-07-19 15:15:06 +02:00
01a0f9db17 Fix: Fix test on randint returned value 2022-05-09 09:11:56 +02:00
fae2afa76c Feat: make dockerfile to test my workflow 2022-05-09 09:09:38 +02:00
b1353bb6c7 Feat: add test with a template using random 2022-05-08 09:45:45 +02:00
78f6ddc813 Feat: dockerfile to test bopytex in new environment 2022-05-08 09:15:13 +02:00
8f1d9cb4d4 Feat: four un peu tout 2022-05-07 16:55:33 +02:00
90ae3e936e Feat: Enable configfile loading 2022-05-05 14:35:01 +02:00
e4f234d241 Feat: add e2e test with failed compilation 2022-05-04 21:21:26 +02:00
0d614465f0 Refact: move bopytex.bopytex to service.main 2022-05-04 21:16:27 +02:00
467135abc6 Feat: Create dockerfile to test in new env 2022-05-04 18:03:58 +02:00
d9bd4ca5a1 Feat: Organize script to work in CLI 2022-05-04 18:00:54 +02:00
9d7f779f07 Fix: Fix planer when students_csv is empty 2022-05-04 17:04:16 +02:00
69b2e1c82e Refact: move config and workflow to bopytex 2022-05-04 11:39:28 +02:00
87ebb4c284 Feat: add test on add_filter 2022-05-04 11:11:39 +02:00
25b5bde823 Feat: Create tex jinja2 environment 2022-05-04 11:03:16 +02:00
04db450ceb Feat: remise sur pieds pour les tests 2022-04-16 07:30:47 +02:00
ca2a47f82a Fix: empty clean worker 2022-04-16 07:14:49 +02:00
1fdf223689 Feat: e2e test on default planner 2022-04-16 07:13:07 +02:00
1bcbf2a9a6 core: remove tests from .gitignore 2022-04-14 11:29:39 +02:00
5af4662d43 Fix: use tmp_path fixture 2022-04-13 20:41:34 +02:00
fe18dc4ef1 Feat: manage failing tasks in scheduler 2022-04-13 15:20:34 +02:00
b455ef23c4 Feat: Integrate message in service, scheduler and dispatcher 2022-04-13 15:09:08 +02:00
9c05ef1551 feat: integrate message to worker 2022-04-13 12:26:04 +02:00
ca0f498d4e fix: list to set in typehints 2022-04-13 11:29:30 +02:00
527ad160cf Feat: join_pdf work 2022-04-13 10:46:53 +02:00
ac8fe3dfdd Feat: latexmk worker to compile tex 2022-04-10 16:27:12 +02:00
76a033cf43 feat: create generate worker 2022-04-10 16:19:41 +02:00
08411bd42d Feat: move action to worker with a dispatcher 2022-04-10 14:37:19 +02:00
3f1464f3f6 Feat: service work but do nothing! 2022-04-10 06:45:10 +02:00
abd517b339 Feat: planner generate tasks all by themselves 2022-04-09 23:00:43 +02:00
c89959673b refact: move planners to own directory 2022-04-09 22:46:09 +02:00
dfcc48dd20 refact: planners accept only options in parameters 2022-04-09 22:29:58 +02:00
8c9d7bf9a2 refact: rename planner to default_planner 2022-04-09 22:06:42 +02:00
e52b6eb064 refact: rename bopytex to service 2022-04-09 21:53:00 +02:00
963348611a Feat: scheduler don't manage actions 2022-04-09 21:46:24 +02:00
1f0547c1ac doc: add docstring and typehints 2022-04-09 21:23:28 +02:00
03482d4b3d Feat: add doable_task to scheduler 2022-04-09 17:08:05 +02:00
dc12a919d0 feat: dirty bopytex, need to test it 2022-04-09 16:18:56 +02:00
374c5f7467 fix: remove comment 2022-04-09 16:16:54 +02:00
dca69f94aa Fix: actions typehint 2022-04-09 16:16:39 +02:00
4e7805f40d Feat: add list tex files with no tpl 2022-04-09 16:08:10 +02:00
7f40b7c38f feat: only_corr_planner 2022-04-09 15:42:00 +02:00
f9dd70a2f1 feat: planner for normal workflow 2022-04-09 15:33:28 +02:00
1865f9ec63 Feat: build subjects metadatas 2022-04-09 07:30:13 +02:00
ebf5bd0c7d refact: rename planner to tasks 2022-04-09 06:58:44 +02:00
32c74ae679 Feat: write is_finishable 2022-04-09 06:56:57 +02:00
4d76dc8992 Feat: reactivate output in task 2022-04-09 06:32:56 +02:00
211cee2f4f Fix: linting 2022-04-09 06:13:40 +02:00
2b0c325203 feat: run the scheduler 2022-04-08 22:04:47 +02:00
e0377a3e92 Feat: scheduler handle dependencies (basic) 2022-04-08 21:49:18 +02:00
bfe9e6f91e feat: schelule multiple task 2022-04-08 21:41:29 +02:00
4dbfe7a82c feat: dispatch transmit args to actions 2022-04-08 21:35:55 +02:00
480ced3259 refact: remove output to tasks 2022-04-08 21:29:35 +02:00
7b64dcf8d6 Feat: append, dispatch and __next__ (basic) scheduler 2022-04-08 21:24:43 +02:00
6e718b987a Feat: code planners and tests 2022-04-08 19:34:38 +02:00
ce98f46bca Feat: clean repository 2022-04-08 19:34:22 +02:00
083831acdf Feat: adapt to new mapytex 2022-01-21 16:34:17 +01:00
5f0fc8bc42 Feat: only_corr works
All checks were successful
continuous-integration/drone/push Build is passing
2020-11-03 10:23:18 +01:00
7782330ed2 Feat: activate all mapytex
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2020-08-21 19:29:32 +02:00
4db2b1ce73 Fix: only on tagging?
All checks were successful
continuous-integration/drone/push Build is passing
2020-08-20 16:27:17 +02:00
d8c758677d Fix: change trigger
Some checks failed
continuous-integration/drone/push Build is failing
2020-08-20 16:21:32 +02:00
49c2e505d0 Feat: add badge
Some checks failed
continuous-integration/drone/push Build is failing
2020-08-20 16:20:43 +02:00
84f0a3a521 Feat: add .drone for publishing in pypi
All checks were successful
continuous-integration/drone/push Build is passing
2020-08-20 16:18:47 +02:00
57 changed files with 1852 additions and 806 deletions

13
.drone.yml Normal file
View File

@ -0,0 +1,13 @@
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,4 +1,3 @@
tests/
__pycache__/ __pycache__/
*.pyc *.pyc
documentation/build/ documentation/build/

16
Dockerfile.simple Normal file
View File

@ -0,0 +1,16 @@
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

17
Dockerfile.usecase Normal file
View File

@ -0,0 +1,17 @@
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"

12
Makefile Normal file
View File

@ -0,0 +1,12 @@
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"

139
README.md
View File

@ -1,6 +1,8 @@
# Bopytex # 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). [![Build Status](https://drone.opytex.org/api/badges/lafrite/Bopytex/status.svg)](https://drone.opytex.org/lafrite/Bopytex)
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
@ -10,84 +12,91 @@ Install and update using [pip](https://pip.pypa.io/en/stable/quickstart/)
## Simple example ## Simple example
Let's say I want an exercise on adding 2 fractions (files are in `examples`). ``` latex
% save this as tpl_simple.tex
\documentclass[12pt]{article}
The *latex* template called `tpl_add_fraction.tex` \title{Bopytex example -- {{ 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 n
\item \Var{a}
%- endfor
\end{itemize}
\end{document}
```
To create a version a this document type this
``` bash
$ bopytex tpl_simple.tex
```
## How I use it
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.
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`.
``` 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 ``` latex
% tpl_example.tpl
\documentclass[12pt]{article} \documentclass[12pt]{article}
\title{Bopytex with Mapytex example -- \Var{ subject.name }}
\begin{document} \begin{document}
\section{Ajouts de fractions} \maketitle
Adding two fractions %- set e = Expression.random("{a} + {b}")
%- set e = Expression.random("{a} / {b} + {c} / {k*b}", ["b > 1", "k>1"]) \Var{e}
\[
A = \Var{e}
\]
Solution
\[
\Var{e.simplify().explain() | join('=')}
\]
\end{document}
```
Generate latex files and compile those for 2 different subjects. \Var{e.simplify()}
```
bopytex -t tpl_add_fractions.tex -N 2
```
It produces 2 sources files
- `01_add_fractions.tex`
```latex
\documentclass[12pt]{article}
\begin{document}
\section{Ajouts de fractions}
Adding two fractions
\[
A = \frac{- 2}{4} + \frac{7}{8}
\]
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}
``` ```
- `02_add_fractions.tex` Information about my students are stored in a csv file (here `students.csv`)
```latex ``` csv
\documentclass[12pt]{article} "Name","Age","Email","fraction level","calculus level"
"Spike Tucker","22","s.tucker@randatmail.com","7","3"
\begin{document} "Martin Payne","21","m.payne@randatmail.com","7","3"
"Kimberly Baker","20","k.baker@randatmail.com","1","8"
\section{Ajouts de fractions} "Emma Bailey","29","e.bailey@randatmail.com","2","5"
"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}
``` ```
And a ready to print pdf. Then I can produce a subject for each of my student
- [ all_add_fraction.pdf ]( ./examples/all_add_fraction.pdf )
``` bash
$ bopytex tpl_simple.tex -s students.csv -c bopytex_config.py
```

View File

@ -1,10 +1,6 @@
#!/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:

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

@ -1,239 +1,2 @@
#!/usr/bin/env python import bopytex.default_config as DEFAULT
# encoding: utf-8 from bopytex.service import orcherstrator
"""
Producing then compiling templates
"""
import csv
import os
import logging
from pathlib import Path
import pytex
from mapytex import Expression, Integer, Decimal
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():
Expression.set_render("tex")
logger.debug(f"Render for Expression is {Expression.RENDER}")
mapytex_tools = {
"Expression": Expression,
"Integer": Integer,
"Decimal": Decimal,
# "Polynom": mapytex.Polynom,
# "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)
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

26
bopytex/default_config.py Normal file
View File

@ -0,0 +1,26 @@
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",
}

View File

@ -1,33 +0,0 @@
#!/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

@ -0,0 +1,31 @@
#!/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

View File

@ -1,38 +0,0 @@
#!/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

@ -1,33 +0,0 @@
\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}

36
bopytex/message.py Normal file
View File

@ -0,0 +1,36 @@
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

View File

@ -0,0 +1,58 @@
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

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

View File

@ -0,0 +1,9 @@
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

@ -0,0 +1,103 @@
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

15
bopytex/planner/naming.py Normal file
View File

@ -0,0 +1,15 @@
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:])

78
bopytex/scheduler.py Normal file
View File

@ -0,0 +1,78 @@
""" 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 import subject_metadatas, crazy_feed, pdfjoin, feed, clean, texcompile, setup, activate_printanswers from bopytex.service import main
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,9 +35,6 @@ 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",
@ -46,11 +43,18 @@ logger.addHandler(steam_handler)
help="Do not compile source code", help="Do not compile source code",
) )
@click.option( @click.option(
"-N", "-d",
"--number_subjects", "--dirty",
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 number of subjects to make", help="The quantity of subjects to make",
) )
@click.option( @click.option(
"-j", "-j",
@ -64,94 +68,31 @@ logger.addHandler(steam_handler)
"--only-corr", "--only-corr",
is_flag=True, is_flag=True,
default=False, default=False,
help="Create and compile only correction from existing subjects", help="Activate correction and compile only 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",
"--crazy", "--configfile",
is_flag=True, type=str,
default=False, default="bopyptex_config.py",
help="Crazy mode. Tries and tries again until template feeding success!", help="Config file path",
) )
def new(**options): def new(**options):
""" Bopytex for message in main(**options):
try:
Feed the template (tpl_...) and then compile it with latex. assert message.status == 0
except AssertionError:
""" logger.warning(message)
setup() break
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:
feed( logger.info(message.out)
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__":

99
bopytex/service.py Executable file
View File

@ -0,0 +1,99 @@
#!/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

64
bopytex/tasks.py Normal file
View File

@ -0,0 +1,64 @@
""" 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

@ -0,0 +1,18 @@
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

@ -0,0 +1,14 @@
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}"], [])

2
bopytex/worker/clean.py Normal file
View File

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

25
bopytex/worker/compile.py Normal file
View File

@ -0,0 +1,25 @@
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

@ -0,0 +1,32 @@
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

@ -0,0 +1,33 @@
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

@ -0,0 +1,30 @@
\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

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

Binary file not shown.

View File

@ -0,0 +1,6 @@
"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

@ -0,0 +1,22 @@
\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

@ -1,19 +0,0 @@
% 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

@ -1,33 +0,0 @@
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,21 +4,18 @@
from setuptools import setup from setuptools import setup
setup( setup(
name='Bopytex', name="Bopytex",
version='0.1.2', version="0.9",
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='programming@opytex.org', author_email="benjamin.bertrand@opytex.org",
packages=['bopytex'], packages=["bopytex"],
install_requires=[ install_requires=[
'mapytex', "click",
'mypytex', "jinja2",
'click', ],
], entry_points={"console_scripts": ["bopytex=bopytex.script:new"]},
entry_points={ )
"console_scripts": ['bopytex=bopytex.script:new']
},
)
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'

0
test/__init__.py Normal file
View File

0
test/fakes/__init__.py Normal file
View File

10
test/fakes/dispatcher.py Normal file
View File

@ -0,0 +1,10 @@
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,
}
)

9
test/fakes/planner.py Normal file
View File

@ -0,0 +1,9 @@
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"])
]

13
test/fakes/workers.py Normal file
View File

@ -0,0 +1,13 @@
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}"], [])

View File

@ -1,6 +0,0 @@
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

@ -1,22 +0,0 @@
\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}

View File

@ -1,212 +0,0 @@
#!/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

85
test/test_e2e.py Normal file
View File

@ -0,0 +1,85 @@
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()

382
test/test_planner.py Normal file
View File

@ -0,0 +1,382 @@
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",
),
]

161
test/test_scheduler.py Normal file
View File

@ -0,0 +1,161 @@
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 == []

60
test/test_service.py Normal file
View File

@ -0,0 +1,60 @@
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"]

41
test/test_tasks.py Normal file
View File

@ -0,0 +1,41 @@
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

40
test/test_texenv.py Normal file
View File

@ -0,0 +1,40 @@
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"

BIN
test/worker/source.pdf Normal file

Binary file not shown.

View File

@ -0,0 +1,35 @@
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

@ -0,0 +1,86 @@
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

@ -0,0 +1,50 @@
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()