forked from yt-project/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
122 lines (102 loc) · 3.19 KB
/
generate.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
from __future__ import print_function
import git
import yaml
import os
import sys
import tempfile
from jinja2 import Environment, FileSystemLoader
if sys.version_info >= (3, 9):
import importlib.resources as importlib_resources
else:
import importlib_resources
PATH = os.path.dirname(os.path.abspath(__file__))
TEMPLATE_ENVIRONMENT = Environment(
autoescape=False,
loader=FileSystemLoader(os.path.join(PATH, 'templates')),
trim_blocks=False)
try:
yt_path = str(importlib_resources.files("yt"))
except ModuleNotFoundError:
yt_path = None
else:
if not (yt_path / '.git').is_dir():
yt_path = None
def render_template(template_filename, context):
return TEMPLATE_ENVIRONMENT.get_template(template_filename).render(context)
pages = {}
def page(name, output_name = None):
if output_name is None:
output_name = name + '.html'
def _page(func):
pages[name] = (func, output_name)
return func
return _page
@page('index')
def index():
examples = yaml.load(open("examples.yaml", "r"), Loader=yaml.SafeLoader)
return {'title': 'Home', 'examples': examples}
@page('gallery')
def gallery():
entries = yaml.load(open("gallery.yaml", "r"), Loader=yaml.SafeLoader)
return {'entries': entries}
name_ignores = ["convert-repo"]
def lastname_sort(a):
return a.rsplit(None, 1)[-1]
@page('about')
def about():
# Uncomment for rapid dev
if "--fast" in sys.argv:
return {'developers': []}
if yt_path:
repo = git.Repo(yt_path)
else:
with tempfile.TemporaryDirectory() as repo_path:
with git.Repo.clone_from(
"https://github.com/yt-project/yt", repo_path
) as repo:
shortlog = repo.git.shortlog(["-ns", "HEAD"]).split("\n")
devs = set([sl.split("\t")[1] for sl in shortlog])
for name in name_ignores:
devs.discard(name)
return {"developers": sorted(devs, key=lastname_sort)}
@page('community')
def community():
return {}
@page('members')
def members():
members = yaml.load(open("members.yaml", "r"), Loader=yaml.SafeLoader)
members.sort(key = lambda a: lastname_sort(a['name']))
return {'members': members}
@page('development')
def development():
return {}
@page('data/index')
def data():
return {'url_prefix':'../'}
@page('workshops/spring2019/index')
def data():
return {'url_prefix':'../../'}
@page('extensions')
def extensions():
extensions = yaml.load(open("extensions.yaml", "r"), Loader=yaml.SafeLoader)
return {'extensions': extensions}
@page('slack')
def slack():
return {}
def main():
for name in sorted(pages):
setup_func, template_name = pages[name]
out_name = os.path.join(".", template_name)
dir_name = os.path.dirname(out_name)
if not os.path.isdir(dir_name):
os.makedirs(dir_name)
context = {'theme': 'flatly', 'title': name,
'url_prefix': ''}
context.update(setup_func())
with open(out_name, "w") as f:
html = render_template(template_name, context)
if sys.version_info.major < 3:
html = html.encode('utf8')
f.write(html)
if __name__ == "__main__":
main()