Skip to content

Commit bd244d7

Browse files
authored
Release v0.0.2 and make it work with pytask v0.0.9. (#1)
1 parent a9923a8 commit bd244d7

15 files changed

+273
-43
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.8
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 markers
3536
- pytask clean
37+
- pytask collect
3638

3739
- pytest tests
3840

CHANGES.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ chronological order. Releases follow `semantic versioning <https://semver.org/>`
66
all releases are available on `Anaconda.org <https://anaconda.org/pytask/pytask-stata>`_.
77

88

9-
0.0.1 - 2020-xx-xx
9+
0.0.2 - 2020-10-30
10+
------------------
11+
12+
- :gh:`1` makes pytask-stata work with pytask v0.0.9.
13+
14+
15+
0.0.1 - 2020-10-04
1016
------------------
1117

1218
- Release v0.0.1.

README.rst

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,47 @@ Here is an example where you want to run ``script.do``.
5757
def task_run_do_file():
5858
pass
5959
60-
Note that, you need to apply the ``@pytask.mark.stata`` marker so that pytask-stata
61-
handles the task. The do-file must be the first dependency. Other dependencies can be
62-
added after that.
60+
61+
Multiple dependencies and products
62+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
63+
64+
What happens if a task has more dependencies? Using a list, the do-file which should be
65+
executed must be found in the first position of the list.
6366

6467
.. code-block:: python
6568
6669
@pytask.mark.stata
6770
@pytask.mark.depends_on(["script.do", "input.dta"])
68-
@pytask.mark.produces("out.dta")
71+
@pytask.mark.produces("output.dta")
72+
def task_run_do_file():
73+
pass
74+
75+
If you use a dictionary to pass dependencies to the task, pytask-stata will, first, look
76+
for a ``"source"`` key in the dictionary and, secondly, under the key ``0``.
77+
78+
.. code-block:: python
79+
80+
@pytask.mark.depends_on({"source": "script.do", "input": "input.dta"})
81+
def task_run_do_file():
82+
pass
83+
84+
85+
# or
86+
87+
88+
@pytask.mark.depends_on({0: "script.do", "input": "input.dta"})
89+
def task_run_do_file():
90+
pass
91+
92+
93+
# or two decorators for the function, if you do not assign a name to the input.
94+
95+
96+
@pytask.mark.depends_on({"source": "script.do"})
97+
@pytask.mark.depends_on("input.dta")
6998
def task_run_do_file():
7099
pass
71100
72-
If you are wondering why the function body is empty, know that pytask-stata replaces the
73-
body with a predefined internal function which will execute the do-file.
74101
75102
76103
Command Line Arguments
@@ -142,7 +169,7 @@ include the ``@pytask.mark.stata`` decorator in the parametrization just like wi
142169
@pytask.mark.depends_on("script.do")
143170
@pytask.mark.parametrize(
144171
"produces, stata",
145-
[("output_1.dta", 1), ("output_2.dta", 2)],
172+
[("output_1.dta", ("1",)), ("output_2.dta", ("2",))],
146173
)
147174
def task_execute_do_file():
148175
pass
@@ -180,6 +207,14 @@ stata_check_log_lines
180207
181208
$ pytask build --stata-check-log-lines 10
182209
210+
stata_source_key
211+
If you want to change the name of the key which identifies the do file, change the
212+
following default configuration in your pytask configuration file.
213+
214+
.. code-block:: ini
215+
216+
stata_source_key = source
217+
183218
184219
Changes
185220
-------

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.8
15+
- pytask >=0.0.9
16+
- pytask-parallel >=0.0.4
1617

1718
# Misc
1819
- black

setup.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[bumpversion]
2-
current_version = 0.0.1
2+
current_version = 0.0.2
33
parse = (?P<major>\d+)\.(?P<minor>\d+)(\.(?P<patch>\d+))(\-?((dev)?(?P<dev>\d+))?)
4-
serialize =
4+
serialize =
55
{major}.{minor}.{patch}dev{dev}
66
{major}.{minor}.{patch}
77

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-stata",
6-
version="0.0.1",
6+
version="0.0.2",
77
packages=find_packages(where="src"),
88
package_dir={"": "src"},
99
entry_points={"pytask": ["pytask_stata = pytask_stata.plugin"]},

src/pytask_stata/__init__.py

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

src/pytask_stata/collect.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import copy
33
import functools
44
import subprocess
5+
from pathlib import Path
56
from typing import Iterable
67
from typing import Optional
78
from typing import Union
@@ -56,13 +57,11 @@ def pytask_collect_task(session, path, name, obj):
5657
@hookimpl
5758
def pytask_collect_task_teardown(session, task):
5859
"""Perform some checks and prepare the task function."""
59-
if task is not None and get_specific_markers_from_task(task, "stata"):
60-
if (len(task.depends_on) == 0) or (
61-
not (
62-
isinstance(task.depends_on[0], FilePathNode)
63-
and task.depends_on[0].value.suffix == ".do"
64-
)
65-
):
60+
if get_specific_markers_from_task(task, "stata"):
61+
source = _get_node_from_dictionary(
62+
task.depends_on, session.config["stata_source_key"]
63+
)
64+
if not (isinstance(source, FilePathNode) and source.value.suffix == ".do"):
6665
raise ValueError(
6766
"The first dependency of a Stata task must be the do-file."
6867
)
@@ -78,6 +77,14 @@ def pytask_collect_task_teardown(session, task):
7877
task.function = stata_function
7978

8079

80+
def _get_node_from_dictionary(obj, key, fallback=0):
81+
if isinstance(obj, Path):
82+
pass
83+
elif isinstance(obj, dict):
84+
obj = obj.get(key) or obj.get(fallback)
85+
return obj
86+
87+
8188
def _merge_all_markers(task):
8289
"""Combine all information from markers for the Stata function."""
8390
stata_marks = get_specific_markers_from_task(task, "stata")
@@ -94,12 +101,15 @@ def _prepare_cmd_options(session, task, args):
94101
is unique and does not cause any errors when parallelizing the execution.
95102
96103
"""
104+
source = _get_node_from_dictionary(
105+
task.depends_on, session.config["stata_source_key"]
106+
)
97107
log_name = convert_task_id_to_name_of_log_file(task.name)
98108
return [
99109
session.config["stata"],
100110
"-e",
101111
"do",
102-
task.depends_on[0].value.as_posix(),
112+
source.value.as_posix(),
103113
*args,
104114
f"-{log_name}",
105115
]

src/pytask_stata/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def pytask_parse_config(config, config_from_cli, config_from_file):
3636
default=10,
3737
)
3838

39+
config["stata_source_key"] = config_from_file.get("stata_source_key", "source")
40+
3941

4042
def _nonnegative_nonzero_integer(x):
4143
"""Check for nonnegative and nonzero integer."""

src/pytask_stata/execute.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,22 @@ def pytask_execute_task_teardown(session, task):
3333
or ``r(601)``.
3434
3535
"""
36-
log_name = convert_task_id_to_name_of_log_file(task.name)
37-
path_to_log = task.path.with_name(log_name).with_suffix(".log")
36+
if get_specific_markers_from_task(task, "stata"):
37+
log_name = convert_task_id_to_name_of_log_file(task.name)
38+
path_to_log = task.path.with_name(log_name).with_suffix(".log")
3839

39-
n_lines = session.config["stata_check_log_lines"]
40+
n_lines = session.config["stata_check_log_lines"]
4041

41-
log = path_to_log.read_text()
42-
log_tail = log.split("\n")[-n_lines:]
43-
for line in log_tail:
44-
error_found = re.match(r"r\(([0-9]+)\)", line)
42+
log = path_to_log.read_text()
43+
log_tail = log.split("\n")[-n_lines:]
44+
for line in log_tail:
45+
error_found = re.match(r"r\(([0-9]+)\)", line)
4546

46-
if error_found:
47-
if not session.config["stata_keep_log"]:
48-
path_to_log.unlink()
47+
if error_found:
48+
if not session.config["stata_keep_log"]:
49+
path_to_log.unlink()
4950

50-
raise RuntimeError(
51-
f"An error occurred. Here are the last {n_lines} lines of the log:\n\n"
52-
+ "\n".join(log_tail)
53-
)
51+
raise RuntimeError(
52+
f"An error occurred. Here are the last {n_lines} lines of the log:"
53+
"\n\n" + "\n".join(log_tail)
54+
)

0 commit comments

Comments
 (0)