-
Notifications
You must be signed in to change notification settings - Fork 2
/
init_template.py
157 lines (119 loc) · 4.79 KB
/
init_template.py
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from __future__ import annotations
import dataclasses
import os
import re
from collections.abc import Callable
from functools import wraps
from pathlib import Path
from typing import Any
PLACEHOLDER_FILES = [
Path("src/module_name/sample_data/sample.csv"),
Path("src/module_name/sample_data/sample.json"),
Path("src/module_name/sample.py"),
Path("tests/test_sample.py"),
]
PLACEHOLDER_DIR = [Path("src/module_name/sample_data")]
PYPROJECT_TARGET = Path("pyproject.toml")
README_TARGET = Path("README.md")
CONTRIBUTING_TARGET = Path("CONTRIBUTING.md")
NOX_TARGET = Path("noxfile.py")
ALT_FILE_DIR = Path("alt_files")
REQUIREMENTS_DIR = Path("requirements")
ORG = "Preocts"
REPO = r"python\-src\-template"
@dataclasses.dataclass
class ProjectData:
name: str = "module-name"
module: str = "module_name"
version: str = "0.1.0"
description: str = "Module Description"
author_email: str = "[email protected]"
author_name: str = "[YOUR NAME]"
org_name: str = "[ORG NAME]"
repo_name: str = "[REPO NAME]"
def bookends(label: str) -> Callable[..., Callable[..., None]]:
"""Add start/stop print statements to functoin calls."""
def dec_bookends(func: Callable[..., Any]) -> Callable[..., None]:
@wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> None:
print(f"{label}...")
func(*args, **kwargs)
print("Done.\n")
return wrapper
return dec_bookends
@bookends("Deleting placeholder files")
def delete_placeholder_files() -> None:
"""Delete placeholder files."""
for file in PLACEHOLDER_FILES:
if file.exists():
os.remove(file)
@bookends("Deleting placeholder directories")
def delete_placeholder_directories() -> None:
"""Remove placeholder directories."""
for directory in PLACEHOLDER_DIR:
if directory.exists():
os.rmdir(directory)
def get_input(prompt: str) -> str:
"""Extract input for ease of testing."""
return input(prompt)
def get_project_data() -> ProjectData:
"""Query user for details on the project. This is the quiz."""
data = ProjectData()
data.name = os.path.basename(os.getcwd())
data.module = data.name.replace("-", "_")
data.repo_name = os.path.basename(os.getcwd())
for key, value in dataclasses.asdict(data).items():
user_input = get_input(f"Enter {key} (default: {value}) : ")
if user_input:
setattr(data, key, user_input)
data.module = data.name.replace("-", "_")
return data
@bookends("Updating pyproject.toml values")
def replace_pyproject_values(data: ProjectData) -> None:
"""Update pyproject values."""
pyproject = PYPROJECT_TARGET.read_text()
for key, value in dataclasses.asdict(ProjectData()).items():
pattern = re.compile(re.escape(value))
pyproject = pattern.sub(getattr(data, key), pyproject)
PYPROJECT_TARGET.write_text(pyproject)
@bookends("Updating badges in README.md")
def replace_readme_values(data: ProjectData) -> None:
"""Update badge urls and placeholders in README.md"""
readme = README_TARGET.read_text()
default = ProjectData()
readme = re.sub(ORG, data.org_name, readme)
readme = re.sub(REPO, data.repo_name, readme)
readme = re.sub(re.escape(default.org_name), data.org_name, readme)
readme = re.sub(re.escape(default.repo_name), data.repo_name, readme)
README_TARGET.write_text(readme)
@bookends("Updating references in CONTRIBUTING.md")
def replace_contributing_values(data: ProjectData) -> None:
"""Update badge urls and placeholders in README.md"""
readme = CONTRIBUTING_TARGET.read_text()
default = ProjectData()
readme = re.sub(ORG, data.org_name, readme)
readme = re.sub(REPO, data.repo_name, readme)
readme = re.sub(re.escape(default.org_name), data.org_name, readme)
readme = re.sub(re.escape(default.repo_name), data.repo_name, readme)
CONTRIBUTING_TARGET.write_text(readme)
@bookends("Updating noxfile.py values")
def replace_nox_values(data: ProjectData) -> None:
"""Update nox value, replacing module_name with actual module name."""
noxfile = Path(NOX_TARGET).read_text()
noxfile = noxfile.replace("module_name", data.module)
Path(NOX_TARGET).write_text(noxfile)
@bookends("Renaming src/module_name folder")
def rename_module_folder(name: str) -> None:
"""Rename module folder."""
name = name.replace("-", "_")
os.rename("src/module_name", f"src/{name}")
if __name__ == "__main__":
print("Eggcellent template setup:\n")
project_data = get_project_data()
replace_pyproject_values(project_data)
replace_nox_values(project_data)
replace_readme_values(project_data)
replace_contributing_values(project_data)
delete_placeholder_files()
delete_placeholder_directories()
rename_module_folder(project_data.name)