Skip to content

Commit b7edd19

Browse files
authored
Merge pull request #8 from i-VRESSE/make-record-simpler
Make record simpler
2 parents d8aca96 + 85ed39d commit b7edd19

File tree

11 files changed

+2295
-2299
lines changed

11 files changed

+2295
-2299
lines changed

AGENTS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ uv run --group docs sphinx-build docs docs/_build # Build documentation
1515
- ruff
1616
- use type hints everywhere
1717
- Google style docstrings, in docstring to not repeat type as they are in function signature.
18+
19+
## Testing
20+
1821
- test naming: `test_<function under test>_<given>`
1922
- group tests on same function under `class Test_<function under test>:` with method `def test_<given>(self):`
23+
- test body structure should 3 parts: setup, execution, validation. Separate these parts with a blank line.
2024

2125
## Important
2226

README.md

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,37 @@ pip install rocrate-action-recorder
1919

2020
## Usage
2121

22+
Shown is an example of recording a CLI command (`example-cli input.txt output.txt`) implemented with `argparse`.
23+
2224
```python
2325
import argparse
2426
from datetime import datetime
2527
from pathlib import Path
26-
from rocrate_action_recorder import record_with_argparse, IOs
28+
from rocrate_action_recorder import record_with_argparse, IOArgumentNames
2729

30+
# Create an argparse parser
2831
parser = argparse.ArgumentParser(prog="example-cli", description="Example CLI")
2932
parser.add_argument("--version", action="version", version="1.2.3")
3033
parser.add_argument("input", type=Path, help="Input file")
3134
parser.add_argument("output", type=Path, help="Output file")
3235

36+
# Prepare input
3337
Path("input.txt").write_text("hello")
38+
39+
# Parse arguments
3440
args = ['input.txt', 'output.txt']
3541
ns = parser.parse_args(args)
36-
# Tell recorder about input and output files
37-
ios = IOs(input_files=["input"], output_files=["output"])
38-
start_time = datetime.now()
3942

40-
# Do handling of the CLI command here...
43+
# Do handling of the CLI command here
44+
start_time = datetime.now()
4145
# For demonstration, just upper case input to output
4246
Path(ns.output).write_text(ns.input.read_text().upper())
4347

4448
record_with_argparse(
45-
parser, ns, ios,
49+
parser,
50+
ns,
51+
# Tell recorder which arguments are for input and output files
52+
IOArgumentNames(input_files=["input"], output_files=["output"]),
4653
start_time,
4754
dataset_license="CC-BY-4.0",
4855
# argv argument is optional, in real usage you can omit it
@@ -156,6 +163,64 @@ Will generate a `ro-crate-metadata.json` file in the current working directory d
156163

157164
</details>
158165

166+
167+
168+
<details>
169+
<summary>
170+
You can also call the argument parser agnostic version of the recorder directly. (Click me to see code)
171+
</summary>
172+
173+
```python
174+
from datetime import datetime, UTC
175+
from pathlib import Path
176+
177+
from rocrate_action_recorder import (
178+
IOArgumentPath,
179+
IOArgumentPaths,
180+
Program,
181+
record,
182+
)
183+
184+
crate_dir = Path()
185+
input_path = crate_dir / "input.txt"
186+
output_path = crate_dir / "output.txt"
187+
input_path.write_text("Hello World\n")
188+
argv = [
189+
"myscript",
190+
"--input",
191+
str(input_path),
192+
"--output",
193+
str(output_path),
194+
]
195+
start_time = datetime(2026, 1, 16, 12, 0, 0, tzinfo=UTC)
196+
# Simulate the script's main operation
197+
output_path.write_text(input_path.read_text().upper())
198+
end_time = datetime(2026, 1, 16, 12, 0, 5, tzinfo=UTC)
199+
200+
crate_meta = record(
201+
program=Program(
202+
name="myscript", description="My test script", version="1.2.3"
203+
),
204+
ioargs=IOArgumentPaths(
205+
input_files=[
206+
IOArgumentPath(name="input", path=input_path, help="Input file")
207+
],
208+
output_files=[
209+
IOArgumentPath(name="output", path=output_path, help="Output file")
210+
],
211+
),
212+
argv=argv,
213+
current_user="tester",
214+
start_time=start_time,
215+
end_time=end_time,
216+
crate_dir=crate_dir,
217+
dataset_license="CC-BY-4.0",
218+
)
219+
# crate_meta == Path("ro-crate-metadata.json")
220+
```
221+
222+
</details>
223+
159224
<!-- SPHINX-END -->
160225

161226
## Example

docs/conf.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,6 @@
7070
"python": ("https://docs.python.org/3", None),
7171
}
7272

73-
# TODO what is this?
74-
nitpick_ignore = [
75-
("py:class", "_io.StringIO"),
76-
("py:class", "_io.BytesIO"),
77-
]
78-
7973
always_document_param_types = True
8074

8175
autoapi_dirs = ["../src/rocrate_action_recorder"]

docs/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
:end-before: <!-- SPHINX-END -->
1212
```
1313

14+
The main functions are:
15+
- [record_from_argparse](#rocrate_action_recorder.adapters.argparse.record_with_argparse) function.
16+
- [record](#rocrate_action_recorder.core.record) function.
17+
1418
## Example
1519

1620
See the [example](https://github.com/i-VRESSE/rocrate-action-recorder/tree/main/example) folder for a minimal example.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,13 @@ docs = [
5757
[tool.pytest]
5858
minversion = "9.0"
5959
strict = true
60+
addopts = ["--doctest-modules"]
6061
filterwarnings = [
6162
"error",
6263
]
6364
log_level = "INFO"
6465
testpaths = [
65-
"tests",
66+
"tests", "src",
6667
]
6768

6869
[tool.coverage]
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
"""RO-Crate action recorder for CLI invocations."""
22

3-
from rocrate_action_recorder.adapters.argparse import record_with_argparse
3+
from rocrate_action_recorder.adapters.argparse import record_with_argparse, IOArgumentNames
44
from rocrate_action_recorder.core import (
5-
IOArgument,
6-
IOs,
7-
Info,
5+
IOArgumentPath,
6+
IOArgumentPaths,
87
Program,
98
record,
109
playback,
@@ -15,7 +14,7 @@
1514
"record",
1615
"playback",
1716
"Program",
18-
"IOArgument",
19-
"Info",
20-
"IOs",
17+
"IOArgumentPath",
18+
"IOArgumentPaths",
19+
"IOArgumentNames",
2120
]

0 commit comments

Comments
 (0)