Skip to content

Commit 76d1f31

Browse files
committed
Simplify build script
There appears to be no need to write `all.yml` as nothing outside the build script uses it. The effect of using it is to re-order items and flatten the outer dictionary into list. But we can just do all that work when reading, and the sorting in the template.
1 parent 8a738d7 commit 76d1f31

File tree

2 files changed

+21
-55
lines changed

2 files changed

+21
-55
lines changed

python/build.py

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,34 @@
11
#!/usr/bin/env python
22

3+
from collections import defaultdict
34
from pathlib import Path
45
import pprint
56

67
from jinja2 import Template
78
from yaml import safe_load
8-
from markdown import markdown
99
import requests
1010

11-
# concatenate yml files...
1211

1312
here = Path(__file__).parent.resolve()
1413

15-
all_path = here.parent / 'packages/all.yml'
16-
all_path.unlink(missing_ok=True)
17-
packages = (here.parent / 'packages').glob('*')
18-
19-
2014
print("Opening section names file")
2115
with (here.parent / 'section_names.yml').open() as f:
2216
section_names = safe_load(f)
2317

2418
section_names = section_names['section_names']
2519

2620
print("section_names", section_names)
27-
packs = dict()
28-
# divide the yml files into sections based on teh section tag...
29-
for package in packages:
30-
with package.open('r') as fin:
31-
pack = safe_load(fin)
32-
if 'section' not in pack:
33-
pack['section'] = 'miscellaneous'
34-
if pack['section'] in packs:
35-
packs[pack['section']] += [pack]
36-
else:
37-
packs[pack['section']] = [pack]
38-
39-
pprint.pprint(packs)
21+
config = defaultdict(list)
22+
for path in (here.parent / 'packages').glob('*'):
23+
with path.open('r') as fin:
24+
package = safe_load(fin)
25+
26+
# Divide the yml files into sections based on the section tag...
27+
if 'section' not in package:
28+
package['section'] = 'miscellaneous'
29+
package['section'] = section_names[package['section'].lower()]
4030

41-
with all_path.open('w') as out:
42-
for secname in sorted(packs.keys()):
43-
packs_sec = sorted(packs[secname], key=lambda i: i['repo'].split('/')[1].lower())
44-
45-
out.write(f' - name: {section_names[secname]}\n')
46-
out.write(f' packages:\n\n')
47-
for pack in packs_sec:
48-
out.write(f' - repo: {pack["repo"]}\n')
49-
for k, v in pack.items():
50-
if k != 'repo':
51-
out.write(f' {k}: {v}\n')
52-
out.write('\n')
53-
54-
55-
56-
print("Opening config file")
57-
with all_path.open() as f:
58-
config = safe_load(f)
59-
pprint.pprint(config)
60-
print()
61-
62-
for section in config:
63-
print(section.get('name', ''))
64-
if section.get('intro'):
65-
section['intro'] = markdown(section['intro'])
66-
for package in section['packages']:
67-
print(f" {package['repo']}")
31+
print(f" {package['repo']} -> {package['section']}")
6832
try:
6933
package['user'], package['repo_name'] = package['repo'].split('/')
7034
except:
@@ -73,7 +37,6 @@
7337
package['conda_package'] = package.get('conda_package', package['repo_name'])
7438
package['pypi_name'] = package.get('pypi_name', package['repo_name'])
7539

76-
package['section'] = section_names[package['section'].lower()]
7740
if package.get('badges'):
7841
package['badges'] = [x.strip() for x in package['badges'].split(',')]
7942
else:
@@ -125,11 +88,14 @@
12588
else:
12689
package['site'] = package['site'].rstrip('/')
12790

128-
template = Template((here / 'template.rst').read_text(),
129-
lstrip_blocks=True, trim_blocks=True)
91+
config[package['section']].append(package)
13092

131-
config = sorted(config, key = lambda i: i['name'])
93+
# Turn defaultdict into plain dict.
94+
config = {**config}
95+
pprint.pprint(config)
13296

97+
template = Template((here / 'template.rst').read_text(),
98+
lstrip_blocks=True, trim_blocks=True)
13399

134100
(here.parent / 'docs/source/packages.rst').write_text(f"""\
135101
Third-party and user-contributed packages

python/template.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
<div>
44
<table id="packages">
5-
{% for section in config %}
5+
{% for section, packages in config | dictsort %}
66
<tr>
77
<th colspan=5>
8-
{% with section_id = section.name | lower | replace(" ", "-") %}
8+
{% with section_id = section | lower | replace(" ", "-") %}
99
<h3 id="{{ section_id }}">
10-
{{ section.name }}
10+
{{ section }}
1111
<a class="headerlink" href="#{{ section_id }}" title="Permalink to this headline">#</a>
1212
</h3>
1313
{% endwith %}
1414
</th>
1515
</tr>
1616

17-
{% for package in section.packages %}
17+
{% for package in packages | sort(attribute="repo_name") %}
1818
<tr>
1919
<td>
2020
<a href="https://github.com/{{ package.repo }}">

0 commit comments

Comments
 (0)