afterpython 0.4.7__py3-none-any.whl → 0.4.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,8 +7,6 @@ import click
7
7
  import afterpython as ap
8
8
  from afterpython._io.yaml import read_yaml
9
9
 
10
- build_path = ap.paths.build_path
11
-
12
10
 
13
11
  def build_faq_json():
14
12
  """Build faq.json from the user's afterpython/faq.yml.
@@ -40,5 +38,5 @@ def build_faq_json():
40
38
  for item in raw
41
39
  ]
42
40
 
43
- with open(build_path / "faq.json", "w") as f:
41
+ with open(ap.paths.build_path / "faq.json", "w") as f:
44
42
  json.dump(items, f, indent=2)
@@ -157,11 +157,14 @@ def build_marimo_notebook(
157
157
  _inject_molab_badge(output_html, molab_url)
158
158
 
159
159
 
160
- def build_marimo_readme(mode: MarimoExportMode = "wasm"):
160
+ def build_marimo_readme(mode: MarimoExportMode | None = None):
161
161
  """Build afterpython/README.py to _build/readme_py/readme_py.html.
162
162
 
163
163
  Skips silently if README.py is absent or isn't a marimo notebook so users
164
164
  who keep only README.md aren't penalized.
165
+
166
+ `mode` defaults to `[website].readme_py` in afterpython.toml ("wasm" when
167
+ unset or unknown, the same fallback the metadata.json resolver uses).
165
168
  """
166
169
  from afterpython._io.toml import _from_tomlkit
167
170
  from afterpython.tools._afterpython import read_afterpython
@@ -172,5 +175,8 @@ def build_marimo_readme(mode: MarimoExportMode = "wasm"):
172
175
  output_html = ap.paths.build_path / "readme_py" / "readme_py.html"
173
176
  content_path = Path("README.py")
174
177
  website = _from_tomlkit(read_afterpython().get("website", {}))
178
+ if mode is None:
179
+ configured = str(website.get("readme_py", "wasm"))
180
+ mode = "static" if configured == "static" else "wasm"
175
181
  execute = bool(website.get("execute_readme_py", False))
176
182
  build_marimo_notebook(readme_path, output_html, content_path, mode, execute=execute)
@@ -0,0 +1,215 @@
1
+ """`ap dev`: rebuild the website's data files when their sources change.
2
+
3
+ `prebuild()` and `postbuild()` turn project files into data files under
4
+ afterpython/_build/ (faq.json, metadata.json, blog.json, ...) and copy them to
5
+ afterpython/_website/static/, where SvelteKit reads them. Without this
6
+ watcher that only happens once, when `ap dev` starts, so editing e.g. faq.yml
7
+ needed a restart to show up.
8
+
9
+ Each `_Source` below pairs the files to watch with the builder that reads
10
+ them and the outputs that builder writes. The MyST pages themselves are
11
+ already live (`myst start` watches them); what goes stale is the data built
12
+ around them, such as the blog/tutorial listings.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ import shutil
18
+ import threading
19
+ from collections.abc import Callable, Iterable
20
+ from dataclasses import dataclass
21
+ from pathlib import Path
22
+
23
+ import click
24
+
25
+ import afterpython as ap
26
+ from afterpython.const import CONTENT_TYPES
27
+
28
+
29
+ @dataclass(frozen=True)
30
+ class _Source:
31
+ name: str
32
+ # returns the files to watch; re-evaluated on every poll so newly
33
+ # created files (e.g. a first faq.yml, a new blog post) are picked up
34
+ files: Callable[[], Iterable[Path]]
35
+ build: Callable[[], None]
36
+ # paths relative to afterpython/_build/ that `build` writes; they are
37
+ # removed before rebuilding so a deleted source doesn't leave stale data
38
+ outputs: tuple[str, ...] = ()
39
+
40
+
41
+ def _rglob_files(path: Path, pattern: str = "*") -> list[Path]:
42
+ if not path.exists():
43
+ return []
44
+ return [p for p in path.rglob(pattern) if p.is_file()]
45
+
46
+
47
+ def _build_faq():
48
+ from afterpython.builders.faq_json import build_faq_json
49
+
50
+ build_faq_json()
51
+
52
+
53
+ def _build_metadata():
54
+ from afterpython.builders.metadata import build_metadata
55
+
56
+ build_metadata()
57
+
58
+
59
+ def _build_comments():
60
+ from afterpython.builders.comments_json import build_comments_json
61
+
62
+ build_comments_json()
63
+
64
+
65
+ def _build_readme_py():
66
+ from afterpython.builders.marimo_notebook import build_marimo_readme
67
+
68
+ build_marimo_readme()
69
+
70
+
71
+ def _build_content_listings():
72
+ from afterpython.builders.content_json import build_content_json
73
+
74
+ build_content_json()
75
+ # thumbnails and other images referenced by the listings, mirroring the
76
+ # dev branch of postbuild()
77
+ for content_type in CONTENT_TYPES:
78
+ public = ap.paths.afterpython_path / content_type / "_build" / "site" / "public"
79
+ if public.exists():
80
+ shutil.copytree(
81
+ public,
82
+ ap.paths.website_path / "static" / content_type / "build",
83
+ dirs_exist_ok=True,
84
+ )
85
+
86
+
87
+ def _copy_static():
88
+ # afterpython/static/ is copied as-is in postbuild(); there is nothing to
89
+ # build. Deleted files are left in _website/static/ because the template
90
+ # ships its own files there too and we can't tell them apart.
91
+ if ap.paths.static_path.exists():
92
+ shutil.copytree(
93
+ ap.paths.static_path, ap.paths.website_path / "static", dirs_exist_ok=True
94
+ )
95
+
96
+
97
+ def _sources(content_types: set[str]) -> list[_Source]:
98
+ afterpython_path = ap.paths.afterpython_path
99
+ config_files = [ap.paths.pyproject_path, afterpython_path / "afterpython.toml"]
100
+ readme_py = afterpython_path / "README.py"
101
+ # content types with a listing page; doc has none
102
+ listed = sorted(ct for ct in content_types if ct != "doc")
103
+ return [
104
+ _Source(
105
+ name="faq.yml",
106
+ files=lambda: [afterpython_path / "faq.yml"],
107
+ build=_build_faq,
108
+ outputs=("faq.json",),
109
+ ),
110
+ _Source(
111
+ name="project metadata",
112
+ # README.md feeds the home page description
113
+ files=lambda: [
114
+ *config_files,
115
+ readme_py,
116
+ *ap.paths.user_path.glob("README*"),
117
+ ],
118
+ build=_build_metadata,
119
+ outputs=("metadata.json",),
120
+ ),
121
+ _Source(
122
+ # separate from metadata: it calls the giscus API, which README
123
+ # edits shouldn't trigger
124
+ name="comments config",
125
+ files=lambda: config_files,
126
+ build=_build_comments,
127
+ outputs=("comments.json",),
128
+ ),
129
+ _Source(
130
+ name="README.py",
131
+ files=lambda: [readme_py],
132
+ build=_build_readme_py,
133
+ outputs=("readme_py",),
134
+ ),
135
+ _Source(
136
+ name="afterpython/static/",
137
+ files=lambda: _rglob_files(ap.paths.static_path),
138
+ build=_copy_static,
139
+ ),
140
+ _Source(
141
+ name="content listings",
142
+ # `myst start` rewrites these JSON files whenever a page changes
143
+ files=lambda: [
144
+ path
145
+ for ct in listed
146
+ for path in _rglob_files(
147
+ afterpython_path / ct / "_build" / "site", "*.json"
148
+ )
149
+ ],
150
+ build=_build_content_listings,
151
+ outputs=tuple(f"{ct}.json" for ct in listed),
152
+ ),
153
+ ]
154
+
155
+
156
+ def _snapshot(source: _Source) -> dict[Path, float]:
157
+ mtimes = {}
158
+ for path in source.files():
159
+ try:
160
+ mtimes[path] = path.stat().st_mtime
161
+ except OSError: # missing, or deleted mid-scan
162
+ continue
163
+ return mtimes
164
+
165
+
166
+ def _remove(path: Path):
167
+ if path.is_dir():
168
+ shutil.rmtree(path)
169
+ else:
170
+ path.unlink(missing_ok=True)
171
+
172
+
173
+ def rebuild_source(source: _Source):
174
+ """Rebuild one source and publish its outputs to _website/static/."""
175
+ build_path = ap.paths.build_path
176
+ website_static = ap.paths.website_path / "static"
177
+ for output in source.outputs:
178
+ _remove(build_path / output)
179
+ _remove(website_static / output)
180
+ source.build()
181
+ for output in source.outputs:
182
+ built = build_path / output
183
+ if built.is_dir():
184
+ shutil.copytree(built, website_static / output, dirs_exist_ok=True)
185
+ elif built.exists():
186
+ shutil.copy2(built, website_static / output)
187
+
188
+
189
+ def watch_site_data(
190
+ content_types: set[str], stop_event: threading.Event, interval: float = 1.0
191
+ ):
192
+ """Rebuild a source's data files whenever one of its files changes.
193
+
194
+ Best effort: a half-saved or invalid file prints an error and the watcher
195
+ keeps going, so the next save can fix it.
196
+ """
197
+ sources = _sources(content_types)
198
+ seen = {source.name: _snapshot(source) for source in sources}
199
+ while not stop_event.wait(interval):
200
+ for source in sources:
201
+ current = _snapshot(source)
202
+ if current == seen[source.name]:
203
+ continue
204
+ seen[source.name] = current
205
+ click.echo(
206
+ click.style(f"↻ {source.name} changed, rebuilding...", fg="cyan")
207
+ )
208
+ try:
209
+ rebuild_source(source)
210
+ except click.ClickException as e:
211
+ click.echo(click.style(f"✗ {e.format_message()}", fg="red"))
212
+ except Exception as e: # keep watching: a crash here would go unnoticed
213
+ click.echo(
214
+ click.style(f"✗ rebuilding {source.name} failed: {e!r}", fg="red")
215
+ )
@@ -5,7 +5,6 @@ from typing import TYPE_CHECKING
5
5
 
6
6
  if TYPE_CHECKING:
7
7
  from pathlib import Path
8
- from typing import Literal
9
8
 
10
9
  from afterpython._typing import NodeEnv
11
10
 
@@ -85,7 +84,7 @@ def _read_api_reference() -> bool:
85
84
  return bool(website.get("api_reference", False))
86
85
 
87
86
 
88
- def prebuild(dev_build: bool = False):
87
+ def prebuild(dev_build: bool = False, build_readme_py: bool = True):
89
88
  def _check_initialized():
90
89
  # Check if 'ap init' has been run
91
90
  afterpython_toml = ap.paths.afterpython_path / "afterpython.toml"
@@ -117,21 +116,6 @@ def prebuild(dev_build: bool = False):
117
116
  shutil.rmtree(path)
118
117
  build_path.mkdir(parents=True, exist_ok=True)
119
118
 
120
- def _get_readme_py_mode() -> Literal["wasm", "static"]:
121
- """Read `[website].readme_py` from afterpython.toml. Defaults to "wasm".
122
-
123
- Falls back to "wasm" silently for unknown values rather than failing the
124
- build — the metadata.json resolver does the same, keeping the two views
125
- consistent.
126
- """
127
- from afterpython._io.toml import _from_tomlkit
128
- from afterpython.tools._afterpython import read_afterpython
129
-
130
- afterpython = read_afterpython()
131
- website = _from_tomlkit(afterpython.get("website", {}))
132
- mode = str(website.get("readme_py", "wasm"))
133
- return mode if mode in ("wasm", "static") else "wasm"
134
-
135
119
  _check_initialized()
136
120
  _clean_up_builds()
137
121
 
@@ -140,7 +124,12 @@ def prebuild(dev_build: bool = False):
140
124
  build_metadata()
141
125
  build_faq_json()
142
126
  build_comments_json()
143
- build_marimo_readme(mode=_get_readme_py_mode())
127
+ if build_readme_py:
128
+ build_marimo_readme()
129
+ else:
130
+ # _clean_up_builds() leaves _website/static/readme_py/ alone, so the
131
+ # home page keeps showing the previous export
132
+ click.echo("Skipping README.py (--no-readme-py flag)")
144
133
  build_markdown()
145
134
  # before build_jupyter_notebooks() so generated notebooks get the same
146
135
  # thumbnail/molab post-processing as hand-written ones. Production builds
@@ -138,6 +138,11 @@ def _wait_for_myst_server(
138
138
  is_flag=True,
139
139
  help="Skip running the website dev server (pnpm dev). Useful when you want to run pnpm dev manually with custom options.",
140
140
  )
141
+ @click.option(
142
+ "--no-readme-py",
143
+ is_flag=True,
144
+ help="Skip exporting the marimo afterpython/README.py at startup; the home page keeps the previous export. It is still re-exported when README.py changes.",
145
+ )
141
146
  def dev(
142
147
  ctx,
143
148
  all: bool,
@@ -148,6 +153,7 @@ def dev(
148
153
  guide: bool,
149
154
  execute: bool,
150
155
  no_website: bool,
156
+ no_readme_py: bool,
151
157
  ):
152
158
  """Run the development server for the project website.
153
159
 
@@ -167,6 +173,8 @@ def dev(
167
173
 
168
174
  Use --no-website to skip the automatic 'pnpm dev' command, allowing you to run it manually
169
175
  with custom Vite options in the afterpython/_website directory.
176
+
177
+ Use --no-readme-py to skip the (slow) marimo README.py export at startup.
170
178
  """
171
179
 
172
180
  from afterpython.utils import handle_passthrough_help, is_website_initialized
@@ -233,7 +241,7 @@ def dev(
233
241
  stop_watching = threading.Event()
234
242
 
235
243
  try:
236
- prebuild(dev_build=True)
244
+ prebuild(dev_build=True, build_readme_py=not no_readme_py)
237
245
 
238
246
  # re-export marimo notebooks on save for the content types being served
239
247
  if enabled_content_types:
@@ -304,6 +312,16 @@ def dev(
304
312
 
305
313
  postbuild(dev_build=True)
306
314
 
315
+ # rebuild faq.json, metadata.json, blog.json etc. when their sources
316
+ # change; started after postbuild() so its first snapshot is current
317
+ from afterpython.builders.site_data import watch_site_data
318
+
319
+ threading.Thread(
320
+ target=watch_site_data,
321
+ args=(enabled_content_types, stop_watching),
322
+ daemon=True,
323
+ ).start()
324
+
307
325
  if not no_website:
308
326
  node_env: NodeEnv = find_node_env()
309
327
  click.echo("Running the web dev server...")
@@ -316,11 +334,10 @@ def dev(
316
334
  click.echo(
317
335
  "Skipping website dev server (--no-website flag). Run 'pnpm dev' manually in afterpython/_website/ with your custom options."
318
336
  )
319
- if myst_processes:
320
- # Keep the process running to maintain MyST servers
321
- click.echo("Press Ctrl+C to stop MyST servers...")
322
- while True:
323
- time.sleep(1)
337
+ # Keep running to maintain the MyST servers and the site data watcher
338
+ click.echo("Press Ctrl+C to stop...")
339
+ while True:
340
+ time.sleep(1)
324
341
  except KeyboardInterrupt:
325
342
  # Handle Ctrl+C during subprocess.run
326
343
  pass
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: afterpython
3
- Version: 0.4.7
3
+ Version: 0.4.8
4
4
  Summary: CLI tool to streamline Python package maintenance and generate a dedicated project website
5
5
  Keywords: package-maintenance,project-website,static-site-generator,developer-tools,documentation,cli-tool,ci/cd,mystmd,semantic-release,python-packaging
6
6
  Author: Stephen Yau
@@ -7,24 +7,25 @@ afterpython/builders/__init__.py,sha256=A8LWY4ESeH3SuhkjRcwrCgIAAZXnwHk4asNGGTmV
7
7
  afterpython/builders/build_pdoc.py,sha256=OzCy4H_jfKT7VPh76PfzwNxjmyypkHMEYzQ-6xPKMgc,2868
8
8
  afterpython/builders/comments_json.py,sha256=7gY1wy78wqSscTIFlDhwDKNYN6ioLzpDpACLBmwB9rw,3603
9
9
  afterpython/builders/content_json.py,sha256=yem8qhVjy5-aTnmUIING5ttNZFsuTQ-r5M_dYKGRyTk,5362
10
- afterpython/builders/faq_json.py,sha256=mMfFepRBL_1QYO0tQ6orf5ESo2LVntk55nfFzencViM,1193
10
+ afterpython/builders/faq_json.py,sha256=v4s_v-BJ16O7U9_tlc4NT92d1gtqdTONF-DwTxlGod0,1168
11
11
  afterpython/builders/index_md.py,sha256=ZpBA1FIHgw9MXD8kRaq2OBaEAKnf94_sJmB2cG68iag,6021
12
12
  afterpython/builders/jupyter_notebook.py,sha256=6PBz-jYPRMhk_1MsM8pfoyVbbvCKqSr1FIp7n8xQO8g,10580
13
13
  afterpython/builders/llms_txt.py,sha256=9x-B1i1buTDJt4zGs8nugyto_pI5QIp7WUBcv-zpcso,9226
14
14
  afterpython/builders/marimo_content.py,sha256=2LgNUnNh0flhjisYWIH4DVSX73xb75kABuOSJ9w-cEo,20000
15
- afterpython/builders/marimo_notebook.py,sha256=bYX65k8aKDVw3hdDI_EZQ1AXsd34q-M5YYwJchRANms,6362
15
+ afterpython/builders/marimo_notebook.py,sha256=MwcXu5fz5Mvf3-SR_o6phw-UNwDz12IAH3txiM0yoeM,6662
16
16
  afterpython/builders/markdown.py,sha256=0ExfRqvOTWsgUkAVaGGNkiNDtNedRr7JR1yTGxpbsmA,8736
17
17
  afterpython/builders/metadata.py,sha256=EueKoYWM__Hb9MwX0KYejxJvouoaAxD1oz9AP-Na-ZI,3830
18
+ afterpython/builders/site_data.py,sha256=29p2Hg1RBraZLd9l4jofvCq_QT43ny209-2TP5MDQBU,7133
18
19
  afterpython/builders/url_md.py,sha256=MfXSy0f44J72OlHASir-Xscqq1onA10vtqUO_L4b1xM,3949
19
20
  afterpython/cli/__init__.py,sha256=G4AB-rllhmr7tURFc5cXJfkjZZ2bXtMBg3oy4zUGEFw,84
20
21
  afterpython/cli/commands/add.py,sha256=Ppp4msdvOwN9_-fdG5-b0-IV9p0Eef9CiIEU3srUjYk,1792
21
- afterpython/cli/commands/build.py,sha256=PhhykRYpF9Vekv8KkLnTVZ95lsL0xG61KG0OpFMwhMg,12931
22
+ afterpython/cli/commands/build.py,sha256=C5Mee6hykCrF8Z9xMXws3kSDDwmMJhpaTI1aeIugCZ0,12454
22
23
  afterpython/cli/commands/bump.py,sha256=ULrZmQuQ-oJSYXNyEyCg3VxrHDhzwOSMv37Uk1fQlgs,13354
23
24
  afterpython/cli/commands/changelog.py,sha256=R4GdvJlnfd8joVnRRon9y-eM8RfSIxE1GCOspMrxXEw,1597
24
25
  afterpython/cli/commands/check.py,sha256=QBt_KHlD72QJmBordcsoGKvyX4Iuj7ezjFStuNZVaOk,1694
25
26
  afterpython/cli/commands/clean.py,sha256=3KETi9-LdRdfyxwoMr59bXNaFw2NajfZIWIyu31U8JE,2841
26
27
  afterpython/cli/commands/commit_msg.py,sha256=2PYTqsxMeYaGwQSb2MER3a73gmUoSAietuApJW0XKw4,4513
27
- afterpython/cli/commands/dev.py,sha256=ZOxTM_ztRM3qvoWRFBmHDKPbNiFGhdUIP1dfBFQcy1o,10937
28
+ afterpython/cli/commands/dev.py,sha256=0B5mOvqcHPYitx52J8jTfUwiplNM7w5sADnq_69I3IU,11635
28
29
  afterpython/cli/commands/format.py,sha256=VFwCG9KDTF6j4Y_Ewp4_RpS8uNBQNOAj9HU_G97my5A,710
29
30
  afterpython/cli/commands/init.py,sha256=DJXvXmw3yJJpDKhUpoTqoJB5_yng70QDEhnWnoHTIjY,15284
30
31
  afterpython/cli/commands/init_branch_rules.py,sha256=fzAm8D5qbW3u4bEMpqPuRgO-0akuCGG7pu09JlKhYn4,243
@@ -67,7 +68,7 @@ afterpython/tools/prek.py,sha256=NEEGf8alTt9MIelAJ8yxWD-V4mnM_pJ7oHgVQG8pbzo,354
67
68
  afterpython/tools/pyproject.py,sha256=Slhxo-Z9M7Gn9ZGL2BKmbPONpDgAJ1g33TdiulxwEyQ,3700
68
69
  afterpython/utils.py,sha256=JyNFhanWllmuE3Ax5URi6_UxVgM9GsqgGwceg4PbFlo,12572
69
70
  afterpython/version.py,sha256=hihRV1S-gc1m46OaXD32ikkEkqzs-Gtq7AcAqOQV-wY,1603
70
- afterpython-0.4.7.dist-info/WHEEL,sha256=MWx7psoF7KNST9G72pGtiBYuaXRXQXBk9pw5R6FzYko,80
71
- afterpython-0.4.7.dist-info/entry_points.txt,sha256=roa0PDgBueM1JPbRG0G2OSxzaN394OUL_oS9iEwVLBc,80
72
- afterpython-0.4.7.dist-info/METADATA,sha256=aviqdUAjoYBlIns8QMhVtb9-mxeAziVGBsvou076f7g,6416
73
- afterpython-0.4.7.dist-info/RECORD,,
71
+ afterpython-0.4.8.dist-info/WHEEL,sha256=MWx7psoF7KNST9G72pGtiBYuaXRXQXBk9pw5R6FzYko,80
72
+ afterpython-0.4.8.dist-info/entry_points.txt,sha256=roa0PDgBueM1JPbRG0G2OSxzaN394OUL_oS9iEwVLBc,80
73
+ afterpython-0.4.8.dist-info/METADATA,sha256=vYQnku2ZfKm7017gX_ahUj26qEE3H-w895PqY-6SoTU,6416
74
+ afterpython-0.4.8.dist-info/RECORD,,