Skip to content

Commit c632e0b

Browse files
committed
Fix wrongly located paths key in mkdocs.yml
Signed-off-by: Leandro Lucarella <[email protected]>
1 parent f4db215 commit c632e0b

File tree

9 files changed

+72
-8
lines changed

9 files changed

+72
-8
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ But you might still need to adapt your code:
3434

3535
### Cookiecutter template
3636

37-
<!-- Here bug fixes for cookiecutter specifically -->
37+
- mkdocstrings: Move `paths` key to the rigth section in `mkdocs.yml`.

cookiecutter/migrate.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,74 @@ def main() -> None:
3939
"mkdocs.yml", " import:", " inventories:"
4040
)
4141
print("=" * 72)
42+
print("Fixing wrongly located `paths` keys in mkdocs.yml...")
43+
migrate_mkdocs_yaml(Path("mkdocs.yml"))
44+
print("=" * 72)
4245
print("Migration script finished. Remember to follow any manual instructions.")
4346
print("=" * 72)
4447

4548

49+
def migrate_mkdocs_yaml(file_path: Path) -> None:
50+
"""
51+
Inspect a mkdocs.yml file textually and, if it defines `paths` under
52+
`plugins: -> mkdocstrings: -> handlers: -> python: -> options:`,
53+
rewrite the file in place to move `paths` directly under `python:`
54+
and remove the old `options:` and its `paths:` line. This uses only
55+
line-based detection (no external dependencies).
56+
57+
Args:
58+
file_path: Path to the mkdocs.yml file to migrate.
59+
"""
60+
if not file_path.is_file():
61+
return
62+
63+
lines = file_path.read_text(encoding="utf-8").splitlines(keepends=True)
64+
needs_migration = False
65+
in_python = False
66+
in_options = False
67+
68+
# 1) Detect whether there's a " python:" followed by " options:"
69+
# and then " paths:" in that block.
70+
for line in lines:
71+
if line.startswith(" python:"):
72+
in_python = True
73+
in_options = False
74+
continue
75+
if in_python and line.startswith(" options:"):
76+
in_options = True
77+
continue
78+
if in_options and line.startswith(" paths:"):
79+
needs_migration = True
80+
break
81+
# If indentation drops back below python-level, stop looking in this block
82+
if in_python and not line.startswith(" ") and not line.isspace():
83+
in_python = False
84+
in_options = False
85+
86+
if not needs_migration:
87+
return
88+
89+
# 2) Perform the line-based rewrite:
90+
new_lines: list[str] = []
91+
inserted_paths = False
92+
93+
for line in lines:
94+
# When we hit the " python:" line, insert new " paths:" directly under it
95+
if line.startswith(" python:") and not inserted_paths:
96+
new_lines.append(line)
97+
new_lines.append(' paths: ["src"]\n')
98+
inserted_paths = True
99+
continue
100+
101+
# After inserting, drop the old " paths:" line
102+
if inserted_paths and line.startswith(" paths:"):
103+
continue
104+
105+
new_lines.append(line)
106+
107+
file_path.write_text("".join(new_lines), encoding="utf-8")
108+
109+
46110
def apply_patch(patch_content: str) -> None:
47111
"""Apply a patch using the patch utility."""
48112
subprocess.run(["patch", "-p1"], input=patch_content.encode(), check=True)

cookiecutter/{{cookiecutter.github_repo_name}}/mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ plugins:
103103
default_handler: python
104104
handlers:
105105
python:
106+
paths: ["{{cookiecutter | src_path}}"]
106107
options:
107-
paths: ["{{cookiecutter | src_path}}"]
108108
docstring_section_style: spacy
109109
inherited_members: true
110110
merge_init_into_class: false

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ plugins:
101101
default_handler: python
102102
handlers:
103103
python:
104+
paths: ["src"]
104105
options:
105-
paths: ["src"]
106106
docstring_section_style: spacy
107107
inherited_members: true
108108
merge_init_into_class: false

tests_golden/integration/test_cookiecutter_generation/actor/frequenz-actor-test/mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ plugins:
103103
default_handler: python
104104
handlers:
105105
python:
106+
paths: ["src"]
106107
options:
107-
paths: ["src"]
108108
docstring_section_style: spacy
109109
inherited_members: true
110110
merge_init_into_class: false

tests_golden/integration/test_cookiecutter_generation/api/frequenz-api-test/mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ plugins:
103103
default_handler: python
104104
handlers:
105105
python:
106+
paths: ["py"]
106107
options:
107-
paths: ["py"]
108108
docstring_section_style: spacy
109109
inherited_members: true
110110
merge_init_into_class: false

tests_golden/integration/test_cookiecutter_generation/app/frequenz-app-test/mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ plugins:
103103
default_handler: python
104104
handlers:
105105
python:
106+
paths: ["src"]
106107
options:
107-
paths: ["src"]
108108
docstring_section_style: spacy
109109
inherited_members: true
110110
merge_init_into_class: false

tests_golden/integration/test_cookiecutter_generation/lib/frequenz-test-python/mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ plugins:
103103
default_handler: python
104104
handlers:
105105
python:
106+
paths: ["src"]
106107
options:
107-
paths: ["src"]
108108
docstring_section_style: spacy
109109
inherited_members: true
110110
merge_init_into_class: false

tests_golden/integration/test_cookiecutter_generation/model/frequenz-model-test/mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ plugins:
103103
default_handler: python
104104
handlers:
105105
python:
106+
paths: ["src"]
106107
options:
107-
paths: ["src"]
108108
docstring_section_style: spacy
109109
inherited_members: true
110110
merge_init_into_class: false

0 commit comments

Comments
 (0)