-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_template.py
More file actions
53 lines (40 loc) · 1.48 KB
/
init_template.py
File metadata and controls
53 lines (40 loc) · 1.48 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
"""Update the template with the project information."""
import tomllib
from dataclasses import dataclass
from pathlib import Path
from typing import Self
@dataclass
class Folder:
"""Folder."""
label: str
directory: str
def to_formatted_str(self: Self) -> str:
"""Return a formatted string."""
return f"{{label: '{self.label}',autogenerate: {{ directory: '{self.directory}' }}}}"
@dataclass
class Project:
"""Project."""
pretty_name: str
repo_name: str
folders: list[Folder]
@classmethod
def from_dict(cls: type[Self], data: dict) -> Self:
"""Create a project from a dictionary."""
return cls(
pretty_name=data["project"]["pretty_name"],
repo_name=data["project"]["repo_name"],
folders=[Folder(**folder) for folder in data["project"]["folders"]],
)
def run() -> None:
"""Update the template with the project information."""
project = Project.from_dict(tomllib.loads(Path("config.toml").read_text()))
astro_config_text = Path("astro.config.mjs").read_text()
astro_config_text = astro_config_text.replace("template", project.repo_name)
astro_config_text = astro_config_text.replace("TEMPLATE", project.pretty_name)
astro_config_text = astro_config_text.replace(
"// sidebar-items",
",".join(folder.to_formatted_str() for folder in project.folders),
)
Path("astro.config.mjs").write_text(astro_config_text)
if __name__ == "__main__":
run()