Repository navigation
Expand file tree
/
Copy path_afterpython.py
More file actions
142 lines (112 loc) · 4.86 KB
/
Copy path_afterpython.py
File metadata and controls
142 lines (112 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from pathlib import Path
from tomlkit.toml_document import TOMLDocument
from afterpython._typing import tContentType
import shutil
import tomlkit
import afterpython as ap
from afterpython._io.toml import _to_tomlkit, read_toml, write_toml
def get_default_thumbnail(
content_type: tContentType, file_path: Path | None = None
) -> str:
"""Get the default thumbnail value for a content type.
Reads default thumbnail configuration from afterpython.toml:
- Content type specific thumbnail: [website.{content_type}.thumbnail]
- Falls back to empty string if no default is configured
Args:
content_type: The content type (blog, tutorial, etc.)
file_path: Optional path to the file needing the thumbnail (for calculating relative path)
Returns:
The thumbnail value to use (e.g., "./static/image.png", "../static/image.png", or "")
"""
from afterpython._io.toml import _from_tomlkit
from afterpython.utils import get_relative_static_prefix, normalize_static_path
afterpython = read_afterpython()
try:
# DEPRECATED: [website].thumbnail is not a default for content types
# website_default_thumbnail = normalize_static_path(
# str(_from_tomlkit(afterpython.get("website", {})).get("thumbnail", ""))
# )
content_type_default_thumbnail = normalize_static_path(
str(
_from_tomlkit(afterpython.get("website", {}))
.get(content_type, {})
.get("thumbnail", "")
)
)
except ValueError as e:
print(f"Error in reading afterpython.toml for {content_type=}: {e}")
return "null"
# Determine the static prefix based on file location
if file_path is not None:
content_type_path = ap.paths.afterpython_path / content_type.lower()
static_prefix = get_relative_static_prefix(file_path, content_type_path)
else:
static_prefix = "./" # Default fallback
if content_type_default_thumbnail:
return f"{static_prefix}static{content_type_default_thumbnail}"
# DEPRECATED: use "thumbnail" set in afterpython.toml [website]
# elif website_default_thumbnail:
# Website-wide static is one level up from content_type folder
# return f"{static_prefix}../static{website_default_thumbnail}"
else:
return ""
def read_afterpython() -> TOMLDocument:
"""Read afterpython.toml"""
return read_toml(ap.paths.afterpython_path / "afterpython.toml")
def update_afterpython(data_update: dict):
"""Update afterpython.toml
Args:
data_update: dict of data to update
"""
from afterpython.utils import deep_merge
afterpython_toml_path = ap.paths.afterpython_path / "afterpython.toml"
# read existing data
if not afterpython_toml_path.exists():
afterpython_toml_path.touch()
existing_data = tomlkit.document()
else:
with open(afterpython_toml_path, "rb") as f:
existing_data = tomlkit.parse(f.read())
if existing_data is None:
existing_data = tomlkit.document()
# convert and update existing data
# Convert to tomlkit objects to use "array of inline tables" format
# e.g. authors = [{name = "..."}] instead of [[docs.authors]] (array of tables)
converted_data = _to_tomlkit(data_update)
existing_data = deep_merge(existing_data, converted_data)
# write updated data
write_toml(afterpython_toml_path, existing_data)
def _comment_out_commit_types(text: str) -> str:
"""Comment out the [commit.types] section, so the ap-commit hook doesn't check commit messages"""
lines = text.splitlines()
start = lines.index("[commit.types]")
end = next(
(i for i in range(start + 1, len(lines)) if lines[i].startswith("[")),
len(lines),
)
lines[start:end] = [
"# uncomment to check commit messages against these types",
*(f"# {line}" if line.strip() else line for line in lines[start:end]),
]
return "\n".join(lines) + "\n"
def init_afterpython(commit_types: bool = True):
"""Initialize afterpython.toml
Args:
commit_types: if False, [commit.types] is commented out (commit messages are not checked)
"""
afterpython_toml_path = ap.paths.afterpython_path / "afterpython.toml"
if afterpython_toml_path.exists():
print(f"afterpython.toml already exists at {afterpython_toml_path}")
return
afterpython_template_path = ap.paths.templates_path / "afterpython-template.toml"
if commit_types:
shutil.copy(afterpython_template_path, afterpython_toml_path)
else:
text = afterpython_template_path.read_text(encoding="utf-8")
afterpython_toml_path.write_text(
_comment_out_commit_types(text), encoding="utf-8"
)
print(f"Created {afterpython_toml_path}")