Skip to content

Commit 6420461

Browse files
committed
Split publishing into template / publish commands
1 parent 41b0900 commit 6420461

3 files changed

Lines changed: 71 additions & 12 deletions

File tree

devstats/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
source
2+
build

devstats/__main__.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,25 @@
33
import re
44
import sys
55
from glob import glob
6+
import collections
67

78
import click
89
import requests
910

1011
from .query import GithubGrabber
11-
from .publish import publish
12+
from .publish import template, publish
1213

1314

14-
@click.group()
15+
class OrderedGroup(click.Group):
16+
def __init__(self, name=None, commands=None, **attrs):
17+
super().__init__(name, commands, **attrs)
18+
self.commands = commands or collections.OrderedDict()
19+
20+
def list_commands(self, ctx):
21+
return self.commands
22+
23+
24+
@click.group(cls=OrderedGroup)
1525
def cli():
1626
pass
1727

@@ -20,7 +30,7 @@ def cli():
2030
@click.argument("repo_owner")
2131
@click.argument("repo_name")
2232
def query(repo_owner, repo_name):
23-
"""Download and save issue and pr data for `repo_owner`/`repo_name`."""
33+
"""Download and save issue and pr data for `repo_owner`/`repo_name`"""
2434

2535
try:
2636
token = os.environ["GRAPH_API_KEY"]
@@ -62,4 +72,5 @@ def query(repo_owner, repo_name):
6272
data.dump(f"{repo_name}_{ftype.get(qtype, qtype)}.json")
6373

6474

75+
cli.add_command(template)
6576
cli.add_command(publish)

devstats/publish.py

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,77 @@
11
import os
22
import sys
33
from glob import glob
4+
import shutil
5+
import re
6+
import functools
7+
48
import click
59

610

711
@click.command()
812
@click.argument("project")
913
@click.option(
10-
"-o", "--outdir", default="build", help="Output directory", show_default=True
14+
"-o", "--outdir", default="source", help="Output directory", show_default=True
1115
)
12-
def publish(project, outdir):
13-
"""Generate myst report for `repo_owner`/`repo_name`."""
16+
def template(project, outdir):
17+
"""Generate myst report template for `repo_owner`/`repo_name`"""
1418
os.makedirs(outdir, exist_ok=True)
1519
os.makedirs(os.path.join(outdir, project), exist_ok=True)
1620

21+
print(f"Populating [{outdir}] with templates for [{project}] report:", flush=True)
22+
1723
report_files = glob(os.path.join(os.path.dirname(__file__), "reports/*.md"))
24+
for f in report_files:
25+
dest = f"{outdir}/{project}/{os.path.basename(f)}"
26+
print(f" - {dest}")
27+
shutil.copyfile(f, dest)
28+
29+
30+
def _include_file(basedir, x):
31+
fn = x.group(1)
32+
with open(os.path.join(basedir, fn)) as f:
33+
return f.read()
34+
35+
36+
@click.command()
37+
@click.argument("project")
38+
@click.option(
39+
"-t",
40+
"--templatedir",
41+
default="source",
42+
help="Template directory",
43+
show_default=True,
44+
)
45+
@click.option(
46+
"-o", "--outdir", default="build", help="Output directory", show_default=True
47+
)
48+
def publish(project, templatedir, outdir):
49+
"""Compile templates into markdown files ready for sphinx / myst"""
50+
os.makedirs(outdir, exist_ok=True)
51+
os.makedirs(os.path.join(outdir, project), exist_ok=True)
1852

1953
variables = {"project": project}
2054

21-
print(f"Generating [{project}] report in [{outdir}]...", end="", flush=True)
55+
print(f"Templating [{project}] report from [{templatedir}] to [{outdir}]...")
56+
57+
template_files = glob(f"{templatedir}/{project}/*.md")
2258

23-
for report in report_files:
24-
with open(report) as fh:
59+
for f in template_files:
60+
with open(f) as fh:
2561
template = fh.read()
26-
with open(f"{outdir}/{project}/{os.path.basename(report)}", "w") as fh:
62+
dest_dir = f"{outdir}/{project}"
63+
dest = f"{dest_dir}/{os.path.basename(f)}"
64+
with open(dest, "w") as fh:
65+
print(f" - {dest}")
2766
for v in variables:
2867
template = template.replace("{{ " + v + " }}", variables[v])
29-
fh.write(template)
3068

31-
print("OK")
69+
# Handle myst includes
70+
template = re.sub(
71+
r"```{include}\s*(.*)\s*```",
72+
functools.partial(_include_file, dest_dir),
73+
template,
74+
flags=re.MULTILINE,
75+
)
76+
77+
fh.write(template)

0 commit comments

Comments
 (0)