Skip to content

Commit cea1453

Browse files
committed
Cleaning up docs
Mostly just fussing around, but taking advantage of pathlib to eliminate several with blocks.
1 parent 897691b commit cea1453

1 file changed

Lines changed: 30 additions & 32 deletions

File tree

docs/make_docs.py

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,27 @@ def format_date(value, fmt):
1515

1616
def retrieve_cards(directory):
1717
cards = []
18-
for root, dirs, files in Path.walk(directory):
19-
for file in files:
20-
if file == "read.me":
21-
rme = (root/file).resolve()
22-
with rme.open('r') as yml:
23-
readme = yaml.safe_load(yml)
24-
program_file = readme.get('program',None)
25-
program_full = (root/program_file).resolve()
26-
if program_full.is_file():
27-
with program_full.open('r') as bas:
28-
readme['code'] = bas.read()
29-
30-
relpath = program_full.relative_to(root_dir,walk_up=True).as_posix()
31-
readme['url'] = root_url+relpath
32-
33-
screenshot = (root/readme.get('screenshot',None)).resolve()
34-
if screenshot.is_file():
35-
destination = output_dir/'images'/screenshot.name
36-
screenshot.copy(destination)
37-
readme['screenshot'] = 'images/' + screenshot.name
38-
else:
39-
readme['screenshot'] = None
40-
41-
cards.append(readme)
18+
for root, _, files in Path.walk(directory):
19+
for file in filter(lambda x: x == "read.me", files):
20+
yml = (root/file).resolve().read_text()
21+
readme = yaml.safe_load(yml)
22+
program_file = readme.get('program',None)
23+
program_full = (root/program_file).resolve()
24+
if program_full.is_file():
25+
readme['code'] = program_full.read_text()
26+
27+
relpath = program_full.relative_to(root_dir,walk_up=True).as_posix()
28+
readme['url'] = root_url+relpath
29+
30+
screenshot = (root/readme.get('screenshot',None)).resolve()
31+
if screenshot.is_file():
32+
destination = output_dir/'images'/screenshot.name
33+
screenshot.copy(destination)
34+
readme['screenshot'] = 'images/' + screenshot.name
35+
else:
36+
readme['screenshot'] = None
37+
38+
cards.append(readme)
4239
return cards
4340

4441
# find important directories
@@ -57,19 +54,20 @@ def retrieve_cards(directory):
5754
extensions=['jinja2.ext.loopcontrols'])
5855
environment.filters['format_date'] = format_date
5956

60-
filename = 'index.html'
61-
index = environment.get_template("index.html")
62-
content = index.render({ 'cards': cards })
63-
with open((output_dir/filename), mode="w", encoding="utf-8") as fout:
64-
fout.write(content)
57+
index_tmpl = environment.get_template("index.html")
58+
content = index_tmpl.render({ 'cards': cards })
59+
index_out = (output_dir/'index.html')
60+
index_out.touch()
61+
index_out.write_text(content, encoding="utf-8")
6562

6663
progfile = environment.get_template("program.html")
6764
for category in categories:
68-
for card in cards[category]:
65+
for card in filter(lambda x: x.get('program',None) is not None, cards[category]):
6966
filename = card['program'] + '.html'
7067
content = progfile.render({'program': card})
71-
with open((output_dir/filename), mode="w", encoding="utf-8") as fout:
72-
fout.write(content)
68+
file = (output_dir/filename)
69+
file.touch()
70+
file.write_text(content, encoding="utf-8")
7371

7472

7573

0 commit comments

Comments
 (0)