Skip to content

Commit 61c1345

Browse files
authored
Release v0.0.8 and require pytask v0.0.9. (#11)
1 parent 634d70d commit 61c1345

15 files changed

+438
-117
lines changed

.conda/meta.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ requirements:
2020

2121
run:
2222
- python >=3.6
23-
- pytask >=0.0.7
23+
- pytask >=0.0.9
2424

2525
test:
2626
requires:
2727
- pytest
28+
- pytask-parallel >=0.0.4
2829
source_files:
2930
- tox.ini
3031
- tests
@@ -33,6 +34,7 @@ test:
3334
- pytask --help
3435
- pytask clean
3536
- pytask markers
37+
- pytask collect
3638

3739
- pytest tests
3840

.github/workflows/continuous-integration-workflow.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jobs:
2323
steps:
2424
- uses: actions/checkout@v2
2525
- uses: r-lib/actions/setup-tinytex@v1
26+
if: runner.os != 'Windows'
2627
- uses: goanpeca/setup-miniconda@v1
2728
with:
2829
auto-update-conda: true

CHANGES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ all releases are available on `Anaconda.org
77
<https://anaconda.org/pytask/pytask-latex>`_.
88

99

10+
0.0.8 - 2020-10-29
11+
------------------
12+
13+
- :gh:`11` makes pytask-latex work with pytask v0.0.9.
14+
15+
1016
0.0.7 - 2020-10-14
1117
------------------
1218

README.rst

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,16 @@ Here is an example where you want to compile ``document.tex`` to a PDF.
6060
def task_compile_latex_document():
6161
pass
6262
63+
When the task is executed, you find a ``document.pdf`` in the same folder as your
64+
``document.text``, but you could also compile the report into a another folder by
65+
changing the path in ``produces``.
6366

64-
Note that, LaTeX document which will be compiled must be the first dependency. Add other
65-
dependencies like images after the source file.
67+
68+
Multiple dependencies and products
69+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70+
71+
What happens if a task has more dependencies? Using a list, the LaTeX document which
72+
should be compiled must be found in the first position of the list.
6673

6774
.. code-block:: python
6875
@@ -72,6 +79,38 @@ dependencies like images after the source file.
7279
def task_compile_latex_document():
7380
pass
7481
82+
If you use a dictionary to pass dependencies to the task, pytask-latex will, first, look
83+
for a ``"source"`` key in the dictionary and, secondly, under the key ``0``.
84+
85+
.. code-block:: python
86+
87+
@pytask.mark.depends_on({"source": "document.tex", "image": "image.png"})
88+
def task_compile_document():
89+
pass
90+
91+
92+
# or
93+
94+
95+
@pytask.mark.depends_on({0: "document.tex", "image": "image.png"})
96+
def task_compile_document():
97+
pass
98+
99+
100+
# or two decorators for the function, if you do not assign a name to the image.
101+
102+
103+
@pytask.mark.depends_on({"source": "document.tex"})
104+
@pytask.mark.depends_on("image.png")
105+
def task_compile_document():
106+
pass
107+
108+
The same applies to the compiled document which is either in the first position, under
109+
the key ``"document"`` or ``0``.
110+
111+
112+
Command Line Arguments
113+
~~~~~~~~~~~~~~~~~~~~~~
75114

76115
To customize the compilation, you can pass some command line arguments to ``latexmk``
77116
via the ``@pytask.mark.latex`` marker. The default is the following.
@@ -134,14 +173,34 @@ to include the latex decorator in the parametrization just like with
134173
@pytask.mark.parametrize(
135174
"produces, latex",
136175
[
137-
("document.pdf", (["--pdf", "interaction=nonstopmode"])),
138-
("document.dvi", (["--dvi", "interaction=nonstopmode"])),
176+
(
177+
"document.pdf",
178+
(["--pdf", "--interaction=nonstopmode", "--synctex=1", "--cd"],),
179+
),
180+
(
181+
"document.dvi",
182+
(["--dvi", "--interaction=nonstopmode", "--synctex=1", "--cd"],),
183+
),
139184
],
140185
)
141186
def task_compile_latex_document():
142187
pass
143188
144189
190+
Configuration
191+
-------------
192+
193+
If you want to change the names of the keys which identify the source file and the
194+
compiled document, change the following default configuration in your pytask
195+
configuration file.
196+
197+
.. code-block:: ini
198+
199+
latex_source_key = source
200+
latex_document_key = document
201+
202+
203+
145204
Changes
146205
-------
147206

environment.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ dependencies:
1212
- conda-verify
1313

1414
# Package dependencies
15-
- pytask >= 0.0.7
15+
- pytask >= 0.0.9
16+
- pytask-parallel >= 0.0.4
1617

1718
# Misc
1819
- bumpversion

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.0.7
2+
current_version = 0.0.8
33
parse = (?P<major>\d+)\.(?P<minor>\d+)(\.(?P<patch>\d+))(\-?((dev)?(?P<dev>\d+))?)
44
serialize =
55
{major}.{minor}.{patch}dev{dev}

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name="pytask-latex",
6-
version="0.0.7",
6+
version="0.0.8",
77
packages=find_packages(where="src"),
88
package_dir={"": "src"},
99
entry_points={"pytask": ["pytask_latex = pytask_latex.plugin"]},

src/pytask_latex/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.7"
1+
__version__ = "0.0.8"

src/pytask_latex/collect.py

Lines changed: 82 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from _pytask.nodes import FilePathNode
1515
from _pytask.nodes import PythonFunctionTask
1616
from _pytask.parametrize import _copy_func
17-
from _pytask.shared import to_list
1817

1918

2019
DEFAULT_OPTIONS = ["--pdf", "--interaction=nonstopmode", "--synctex=1", "--cd"]
@@ -37,45 +36,9 @@ def latex(options: Optional[Union[str, Iterable[str]]] = None):
3736
return options
3837

3938

40-
def compile_latex_document(depends_on, produces, latex):
41-
"""Compile a LaTeX document.
42-
43-
This function replaces the dummy function of an LaTeX task. It is a nice wrapper
44-
around subprocess.
45-
46-
The output folder needs to be declared as a relative path to the directory where the
47-
latex source lies.
48-
49-
1. It must be relative because bibtex / biber, which is necessary for
50-
bibliographies, does not accept full paths as a safety measure.
51-
2. Due to the ``--cd`` flag, latexmk will change the directory to the one where the
52-
source files are. Thus, relative to the latex sources.
53-
54-
See this `discussion on Github
55-
<https://github.com/James-Yu/LaTeX-Workshop/issues/1932#issuecomment-582416434>`_
56-
for additional information.
57-
58-
"""
59-
latex_document = to_list(depends_on)[0]
60-
compiled_document = to_list(produces)[0]
61-
62-
if latex_document.stem != compiled_document.stem:
63-
latex.append(f"--jobname={compiled_document.stem}")
64-
65-
# See comment in doc string.
66-
out_relative_to_latex_source = Path(
67-
os.path.relpath(compiled_document.parent, latex_document.parent)
68-
).as_posix()
69-
70-
subprocess.run(
71-
[
72-
"latexmk",
73-
*latex,
74-
f"--output-directory={out_relative_to_latex_source}",
75-
f"{latex_document.as_posix()}",
76-
],
77-
check=True,
78-
)
39+
def compile_latex_document(latex):
40+
"""Replaces the dummy function provided by the user."""
41+
subprocess.run(latex, check=True)
7942

8043

8144
@hookimpl
@@ -88,52 +51,56 @@ def pytask_collect_task(session, path, name, obj):
8851
8952
"""
9053
if name.startswith("task_") and callable(obj) and has_marker(obj, "latex"):
91-
# Collect the task.
9254
task = PythonFunctionTask.from_path_name_function_session(
9355
path, name, obj, session
9456
)
95-
latex_function = _copy_func(compile_latex_document)
96-
latex_function.pytaskmark = copy.deepcopy(task.function.pytaskmark)
97-
98-
merged_mark = _merge_all_markers(task)
99-
args = latex(*merged_mark.args, **merged_mark.kwargs)
100-
latex_function = functools.partial(latex_function, latex=args)
101-
102-
task.function = latex_function
10357

10458
return task
10559

10660

10761
@hookimpl
108-
def pytask_collect_task_teardown(task):
109-
"""Perform some checks.
110-
111-
Remove check for task is none with pytask 0.0.9.
112-
113-
"""
114-
if task is not None and get_specific_markers_from_task(task, "latex"):
115-
if (len(task.depends_on) == 0) or (
116-
not (
117-
isinstance(task.depends_on[0], FilePathNode)
118-
and task.depends_on[0].value.suffix == ".tex"
119-
)
120-
):
62+
def pytask_collect_task_teardown(session, task):
63+
"""Perform some checks."""
64+
if get_specific_markers_from_task(task, "latex"):
65+
source = _get_node_from_dictionary(
66+
task.depends_on, session.config["latex_source_key"]
67+
)
68+
if not (isinstance(source, FilePathNode) and source.value.suffix == ".tex"):
12169
raise ValueError(
12270
"The first or sole dependency of a LaTeX task must be the document "
12371
"which will be compiled and has a .tex extension."
12472
)
12573

126-
if (len(task.produces) == 0) or (
127-
not (
128-
isinstance(task.produces[0], FilePathNode)
129-
and task.produces[0].value.suffix in [".pdf", ".ps", ".dvi"]
130-
)
74+
document = _get_node_from_dictionary(
75+
task.produces, session.config["latex_document_key"]
76+
)
77+
if not (
78+
isinstance(document, FilePathNode)
79+
and document.value.suffix in [".pdf", ".ps", ".dvi"]
13180
):
13281
raise ValueError(
13382
"The first or sole product of a LaTeX task must point to a .pdf, .ps "
13483
"or .dvi file which is the compiled document."
13584
)
13685

86+
latex_function = _copy_func(compile_latex_document)
87+
latex_function.pytaskmark = copy.deepcopy(task.function.pytaskmark)
88+
89+
merged_mark = _merge_all_markers(task)
90+
args = latex(*merged_mark.args, **merged_mark.kwargs)
91+
options = _prepare_cmd_options(session, task, args)
92+
latex_function = functools.partial(latex_function, latex=options)
93+
94+
task.function = latex_function
95+
96+
97+
def _get_node_from_dictionary(obj, key, fallback=0):
98+
if isinstance(obj, Path):
99+
pass
100+
elif isinstance(obj, dict):
101+
obj = obj.get(key) or obj.get(fallback)
102+
return obj
103+
137104

138105
def _merge_all_markers(task):
139106
"""Combine all information from markers for the compile latex function."""
@@ -142,3 +109,51 @@ def _merge_all_markers(task):
142109
for mark_ in latex_marks[1:]:
143110
mark = mark.combined_with(mark_)
144111
return mark
112+
113+
114+
def _prepare_cmd_options(session, task, args):
115+
"""Prepare the command line arguments to compile the LaTeX document.
116+
117+
The output folder needs to be declared as a relative path to the directory where the
118+
latex source lies.
119+
120+
1. It must be relative because bibtex / biber, which is necessary for
121+
bibliographies, does not accept full paths as a safety measure.
122+
2. Due to the ``--cd`` flag, latexmk will change the directory to the one where the
123+
source files are. Thus, relative to the latex sources.
124+
125+
See this `discussion on Github
126+
<https://github.com/James-Yu/LaTeX-Workshop/issues/1932#issuecomment-582416434>`_
127+
for additional information.
128+
129+
"""
130+
latex_document = _get_node_from_dictionary(
131+
task.depends_on, session.config["latex_source_key"]
132+
).value
133+
compiled_document = _get_node_from_dictionary(
134+
task.produces, session.config["latex_document_key"]
135+
).value
136+
137+
# Jobname controls the name of the compiled document. No suffix!
138+
if latex_document.stem != compiled_document.stem:
139+
jobname = [f"--jobname={compiled_document.stem}"]
140+
else:
141+
jobname = []
142+
143+
# The path to the output directory must be relative from the location of the source
144+
# file. See docstring for more information.
145+
out_relative_to_latex_source = Path(
146+
os.path.relpath(compiled_document.parent, latex_document.parent)
147+
).as_posix()
148+
149+
return (
150+
[
151+
"latexmk",
152+
*args,
153+
]
154+
+ jobname
155+
+ [
156+
f"--output-directory={out_relative_to_latex_source}",
157+
latex_document.as_posix(),
158+
]
159+
)

src/pytask_latex/config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44

55
@hookimpl
6-
def pytask_parse_config(config):
6+
def pytask_parse_config(config, config_from_file):
77
"""Register the latex marker in the configuration."""
88
config["markers"]["latex"] = "Tasks which compile LaTeX documents."
9+
config["latex_source_key"] = config_from_file.get("latex_source_key", "source")
10+
config["latex_document_key"] = config_from_file.get(
11+
"latex_document_key", "document"
12+
)

0 commit comments

Comments
 (0)