Skip to content

Commit 8fe6249

Browse files
authored
Merge pull request #66 from oscar-benderstone/addlicense
Simple Hook: Add License
2 parents 38d3833 + c9215c1 commit 8fe6249

8 files changed

Lines changed: 502 additions & 411 deletions

File tree

.editorconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
root = true
2+
[*.toml]
3+
indent_style = space
4+
indent_size = 8
5+

Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
.PHONY: test
22

33
test:
4-
poetry run coverage run --source=entangled -m pytest
5-
poetry run coverage xml
6-
poetry run coverage report
7-
poetry run mypy
4+
uv run coverage run --source=entangled -m pytest
5+
uv run coverage xml
6+
uv run coverage report
7+
uv run mypy
88

99
docs:
10-
poetry run mkdocs build
10+
uv run mkdocs build

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ Entangled has a system of *hooks*: these add actions to the tangling process:
195195
- `brei` trigger actions (or tasks) using [Brei](https://entangled.github.io/brei), which is automatically installed along with Entangled. This is now prefered over the `build` hook.
196196
- `quarto_attributes` add attributes to the code block in Quatro style with `#|` comments at the top of the code block.
197197
- `shebang` takes the first line if it starts with `#!` and puts it at the top of the file.
198+
- `spdx_license` takes the first line if it contains `SPDX-License-Identifier`
199+
and puts it at the top of the file.
198200

199201
### `build` hook
200202

entangled/hooks/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,28 @@
22
from importlib.metadata import entry_points
33

44
from .base import HookBase, PrerequisitesFailed
5-
from . import build, task, quarto_attributes, shebang
5+
from . import build, task, quarto_attributes, shebang, spdx_license
66
from ..config import config
77
from ..construct import construct
88
from typing import TypeVar
99

1010

11-
AbstractHook = TypeVar('AbstractHook', bound=HookBase)
11+
AbstractHook = TypeVar("AbstractHook", bound=HookBase)
1212

1313
discovered_hooks = entry_points(group="entangled.hooks")
1414

1515

1616
external_hooks = {
17-
name: discovered_hooks[name].load().Hook
18-
for name in discovered_hooks.names
17+
name: discovered_hooks[name].load().Hook for name in discovered_hooks.names
1918
}
2019

2120
hooks: dict[str, type[HookBase]] = {
2221
"build": build.Hook,
2322
"brei": task.Hook,
2423
"shebang": shebang.Hook,
25-
"quarto_attributes": quarto_attributes.Hook }
24+
"spdx_license": spdx_license.Hook,
25+
"quarto_attributes": quarto_attributes.Hook,
26+
}
2627

2728

2829
def get_hooks() -> list[HookBase]:

entangled/hooks/spdx_license.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from ..document import CodeBlock
2+
from .base import HookBase
3+
4+
5+
class Hook(HookBase):
6+
def on_read(self, code: CodeBlock):
7+
lines = code.source.splitlines()
8+
if lines and "SPDX-License-Identifier" in lines[0]:
9+
code.header = lines[0]
10+
code.source = "\n".join(lines[1:])

pyproject.toml

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@ requires-python = "~=3.11"
77
readme = "README.md"
88
license = "Apache-2.0"
99
dependencies = [
10-
"mawk>=0.1.4,<0.2",
11-
"watchdog>=3.0.0,<4",
12-
"filelock>=3.12.0,<4",
13-
"argh>=0.30,<0.31",
14-
"rich>=13.3.5,<14",
15-
"tomlkit>=0.12.1,<0.13",
16-
"copier>=9,<10",
17-
"brei>=0.2.3,<0.3",
18-
"rich-argparse>=1.4.0,<2",
19-
"pexpect>=4.9.0,<5",
20-
"pyyaml>=6.0.1,<7",
10+
"mawk>=0.1.4,<0.2",
11+
"watchdog>=3.0.0,<4",
12+
"filelock>=3.12.0,<4",
13+
"argh>=0.30,<0.31",
14+
"rich>=13.3.5,<14",
15+
"tomlkit>=0.12.1,<0.13",
16+
"copier>=9,<10",
17+
"brei>=0.2.3,<0.3",
18+
"rich-argparse>=1.4.0,<2",
19+
"pexpect>=4.9.0,<5",
20+
"pyyaml>=6.0.1,<7",
21+
"types-PyYAML>=6.0.1,<7",
2122
]
2223

2324
[project.urls]
@@ -29,18 +30,18 @@ entangled = "entangled.main:cli"
2930

3031
[dependency-groups]
3132
dev = [
32-
"pytest>=7.3.1,<8",
33-
"mypy>=1.3.0,<2",
34-
"black>=23.3.0,<24",
35-
"pytest-cov>=4.0.0,<5",
36-
"mkdocs>=1.4.3,<2",
37-
"mkdocs-material>=9.1.13,<10",
38-
"mkdocstrings[python]>=0.21.2,<0.22",
39-
"pytest-asyncio>=0.21.1,<0.22",
40-
"ruff>=0.4.4,<0.5",
41-
"types-pyyaml>=6.0.12.20240311,<7",
42-
"types-pygments>=2.18.0.20240506,<3",
43-
"types-colorama>=0.4.15.20240311,<0.5",
33+
"pytest>=7.3.1,<8",
34+
"mypy>=1.3.0,<2",
35+
"black>=23.3.0,<24",
36+
"pytest-cov>=4.0.0,<5",
37+
"mkdocs>=1.4.3,<2",
38+
"mkdocs-material>=9.1.13,<10",
39+
"mkdocstrings[python]>=0.21.2,<0.22",
40+
"pytest-asyncio>=0.21.1,<0.22",
41+
"ruff>=0.4.4,<0.5",
42+
"types-pyyaml>=6.0.12.20240311,<7",
43+
"types-pygments>=2.18.0.20240506,<3",
44+
"types-colorama>=0.4.15.20240311,<0.5",
4445
]
4546

4647
[tool.hatch.build.targets.sdist]

test/test_spdx_license.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from entangled.markdown_reader import read_markdown_string
2+
from entangled.hooks.spdx_license import Hook as SPDXLicense
3+
from entangled.tangle import tangle_ref, AnnotationMethod
4+
from entangled.code_reader import CodeReader
5+
from entangled.commands.stitch import stitch_markdown
6+
7+
input_md = """
8+
A C file with a license!
9+
10+
``` {.c file=test.c}
11+
// SPDX-License-Identifier: MIT
12+
#include <stdio.h>
13+
14+
int main() {
15+
printf("Hello, World!\n");
16+
return 0;
17+
}
18+
```
19+
"""
20+
21+
output_test_c = """// SPDX-License-Identifier: MIT
22+
/* ~/~ begin <<-#test.c>>[init] */
23+
#include <stdio.h>
24+
25+
int main() {
26+
printf("Hello, World!\n");
27+
return 0;
28+
}
29+
/* ~/~ end */
30+
"""
31+
32+
output_test_c_modified = """// SPDX-License-Identifier: MIT
33+
# ~/~ begin <<-#test.c>>[init]
34+
35+
int main() {
36+
printf("Hello, World!\n");
37+
return 0;
38+
}
39+
# ~/~ end
40+
"""
41+
42+
input_md_modified = """
43+
A C file with a license!
44+
45+
``` {.c file=test.c}
46+
// SPDX-License-Identifier: MIT
47+
48+
int main() {
49+
printf("Hello, World!\n");
50+
return 0;
51+
}
52+
```
53+
"""
54+
55+
56+
def test_spdx_license():
57+
hooks = [SPDXLicense(SPDXLicense.Config())]
58+
refs, content = read_markdown_string(input_md, hooks=hooks)
59+
assert "test.c" in refs
60+
print(next(refs["test.c"]))
61+
assert next(refs["test.c"]).header == "// SPDX-License-Identifier: MIT"
62+
code_content, _ = tangle_ref(refs, "test.c", AnnotationMethod.STANDARD)
63+
print(code_content.strip())
64+
print(output_test_c.strip())
65+
assert code_content.strip() == output_test_c.strip()
66+
67+
cr = CodeReader("test.c", refs)
68+
cr.run(output_test_c_modified)
69+
md_content = stitch_markdown(refs, content)
70+
assert md_content.strip() == input_md_modified.strip()

0 commit comments

Comments
 (0)