-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathbase.py
More file actions
278 lines (225 loc) · 9.88 KB
/
base.py
File metadata and controls
278 lines (225 loc) · 9.88 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""Shared base class and helpers for README generators."""
from __future__ import annotations
import contextlib
import csv
import os
import shutil
from abc import ABC, abstractmethod
from datetime import datetime
from pathlib import Path
import yaml # type: ignore[import-untyped]
from scripts.readme.helpers.readme_config import get_root_style
from scripts.readme.helpers.readme_paths import (
ensure_generated_header,
resolve_asset_tokens,
)
from scripts.readme.helpers.readme_utils import build_general_anchor_map
from scripts.readme.markup.shared import generate_style_selector, load_announcements
from scripts.utils.repo_root import find_repo_root
REPO_ROOT = find_repo_root(Path(__file__))
def load_template(template_path: str) -> str:
"""Load a template file."""
with open(template_path, encoding="utf-8") as f:
return f.read()
def load_overrides(template_dir: str) -> dict:
"""Load resource overrides."""
override_path = os.path.join(template_dir, "resource-overrides.yaml")
if not os.path.exists(override_path):
return {}
with open(override_path, encoding="utf-8") as f:
data = yaml.safe_load(f)
return data.get("overrides", {})
def apply_overrides(row: dict, overrides: dict) -> dict:
"""Apply overrides to a resource row."""
resource_id = row.get("ID", "")
if not resource_id or resource_id not in overrides:
return row
override_config = overrides[resource_id]
for field, value in override_config.items():
if field in ["skip_validation", "notes"]:
continue
if field.endswith("_locked"):
continue
if field == "license":
row["License"] = value
elif field == "active":
row["Active"] = value
elif field == "description":
row["Description"] = value
return row
def create_backup(file_path: str, keep_latest: int = 1) -> str | None:
"""Create a backup of the file if it exists, pruning older backups."""
if not os.path.exists(file_path):
return None
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
backup_dir = os.path.join(REPO_ROOT, ".myob", "backups")
os.makedirs(backup_dir, exist_ok=True)
backup_filename = f"{os.path.basename(file_path)}.{timestamp}.bak"
backup_path = os.path.join(backup_dir, backup_filename)
shutil.copy2(file_path, backup_path)
if keep_latest > 0:
basename = os.path.basename(file_path)
backups = []
for name in os.listdir(backup_dir):
if name.startswith(f"{basename}.") and name.endswith(".bak"):
backups.append(os.path.join(backup_dir, name))
backups.sort(key=os.path.getmtime, reverse=True)
for stale_path in backups[keep_latest:]:
with contextlib.suppress(OSError):
os.remove(stale_path)
return backup_path
class ReadmeGenerator(ABC):
"""Base class for README generation with shared logic."""
def __init__(self, csv_path: str, template_dir: str, assets_dir: str, repo_root: str) -> None:
self.csv_path = csv_path
self.template_dir = template_dir
self.assets_dir = assets_dir
self.repo_root = repo_root
self.csv_data: list[dict] = []
self.categories: list[dict] = []
self.overrides: dict = {}
self.announcements: str = ""
self.footer: str = ""
self.general_anchor_map: dict = {}
@property
@abstractmethod
def template_filename(self) -> str:
"""Return the template filename to use."""
...
@property
@abstractmethod
def output_filename(self) -> str:
"""Return the preferred output filename for this style."""
...
@property
@abstractmethod
def style_id(self) -> str:
"""Return the style ID for this generator (extra, classic, awesome, flat)."""
...
@property
def is_root_style(self) -> bool:
"""Check if this generator produces the root README style."""
return self.style_id == get_root_style()
@property
def resolved_output_path(self) -> str:
"""Get the resolved output path for this generator."""
if self.output_filename == "README.md":
return f"README_ALTERNATIVES/README_{self.style_id.upper()}.md"
return self.output_filename
def get_style_selector(self, output_path: Path) -> str:
"""Generate the style selector HTML for this README."""
return generate_style_selector(self.style_id, output_path)
@abstractmethod
def format_resource_entry(self, row: dict, include_separator: bool = True) -> str:
"""Format a single resource entry."""
...
@abstractmethod
def generate_toc(self) -> str:
"""Generate the table of contents."""
...
@abstractmethod
def generate_weekly_section(self) -> str:
"""Generate the weekly additions section."""
...
@abstractmethod
def generate_section_content(self, category: dict, section_index: int) -> str:
"""Generate content for a category section."""
...
def generate_repo_ticker(self) -> str:
"""Generate the repo ticker section."""
return ""
def generate_banner_image(self, output_path: Path) -> str:
"""Generate banner image HTML. Override in subclasses to add a banner."""
_ = output_path
return ""
def load_csv_data(self) -> list[dict]:
"""Load and filter active resources from CSV."""
csv_data = []
with open(self.csv_path, newline="", encoding="utf-8") as f:
reader = csv.DictReader(f)
for row in reader:
row = apply_overrides(row, self.overrides)
if row["Active"].upper() == "TRUE":
csv_data.append(row)
return csv_data
def load_categories(self) -> list[dict]:
"""Load categories from the category manager."""
from scripts.categories.category_utils import category_manager
return category_manager.get_categories_for_readme()
def load_overrides(self) -> dict:
"""Load resource overrides from YAML."""
return load_overrides(self.template_dir)
def load_announcements(self) -> str:
"""Load announcements from YAML."""
return load_announcements(self.template_dir)
def load_footer(self) -> str:
"""Load footer template from file."""
footer_path = os.path.join(self.template_dir, "footer.template.md")
try:
with open(footer_path, encoding="utf-8") as f:
return f.read()
except FileNotFoundError:
print(f"⚠️ Warning: Footer template not found at {footer_path}")
return ""
def build_general_anchor_map(self) -> dict:
"""Build anchor map for General subcategories."""
return build_general_anchor_map(self.categories, self.csv_data)
def create_backup(self, output_path: str) -> str | None:
"""Create backup of existing file."""
return create_backup(output_path)
def generate(self, output_path: str | None = None) -> tuple[int, str | None]:
"""Generate the README to the default or provided output path."""
resolved_path = output_path or self.resolved_output_path
output_path = os.path.join(self.repo_root, resolved_path)
self.overrides = self.load_overrides()
self.csv_data = self.load_csv_data()
self.categories = self.load_categories()
self.announcements = self.load_announcements()
self.footer = self.load_footer()
self.general_anchor_map = self.build_general_anchor_map()
template_path = os.path.join(self.template_dir, self.template_filename)
template = load_template(template_path)
toc_content = self.generate_toc()
weekly_section = self.generate_weekly_section()
body_sections = []
for section_index, category in enumerate(self.categories):
section_content = self.generate_section_content(category, section_index)
body_sections.append(section_content)
readme_content = template
readme_content = readme_content.replace("{{ANNOUNCEMENTS}}", self.announcements)
readme_content = readme_content.replace("{{WEEKLY_SECTION}}", weekly_section)
readme_content = readme_content.replace("{{TABLE_OF_CONTENTS}}", toc_content)
readme_content = readme_content.replace(
"{{BODY_SECTIONS}}", "\n<br>\n\n".join(body_sections)
)
readme_content = readme_content.replace("{{FOOTER}}", self.footer)
readme_content = readme_content.replace(
"{{STYLE_SELECTOR}}", self.get_style_selector(Path(output_path))
)
readme_content = readme_content.replace("{{REPO_TICKER}}", self.generate_repo_ticker())
readme_content = readme_content.replace(
"{{BANNER_IMAGE}}", self.generate_banner_image(Path(output_path))
)
readme_content = ensure_generated_header(readme_content)
readme_content = resolve_asset_tokens(
readme_content, Path(output_path), Path(self.repo_root)
)
output_dir = os.path.dirname(output_path)
if output_dir and not os.path.exists(output_dir):
os.makedirs(output_dir, exist_ok=True)
backup_path = self.create_backup(output_path)
try:
with open(output_path, "w", encoding="utf-8") as f:
f.write(readme_content)
except Exception as e:
if backup_path:
print(f"❌ Error writing {resolved_path}: {e}")
print(f" Backup preserved at: {backup_path}")
raise
return len(self.csv_data), backup_path
@property
def alternative_output_path(self) -> str:
"""Return the output path for this style under README_ALTERNATIVES/."""
if self.output_filename == "README.md":
return f"README_ALTERNATIVES/README_{self.style_id.upper()}.md"
return self.output_filename