-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_heic_unpack_literate.py
executable file
·92 lines (76 loc) · 2.11 KB
/
make_heic_unpack_literate.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
#!/usr/bin/python3
"""
Extract code blocks from README.md to generate heic_unpack.py
so I don't accidentally let them fall out of sync.
I guess the cool kids use "notebooks" now instead of LP?
"""
TEMPLATE = '''\
#!/usr/bin/python3
import sys
import os
import box_parts
def main():
in_path, out_path = sys.argv[1:]
boxes = box_parts.parse_path(in_path)
with open(out_path, "wb") as out_fh:
unpack_to(boxes, out_fh)
def unpack_to(boxes, out_fh):
{1}
{2}
{3}
{4}
{6}
{5}
def get_child_box(box, boxtype):
"""
Recursively find first matching box. Raises if unmatched.
"""
try:
return next(box_parts.box_iter(box.children, boxtype=boxtype))
except StopIteration:
raise box_parts.BoxFormatError("Missing box", boxtype)
def get_one_box(boxes, boxtype):
"""
Require exactly one matching box and return it.
"""
boxes = [
box
for box in boxes
if box.type == boxtype
]
if len(boxes) == 0:
raise box_parts.BoxFormatError("Missing boxes", boxtype)
if len(boxes) > 1:
raise box_parts.BoxFormatError("Too many boxes", boxtype)
return boxes[0]
if __name__ == "__main__":
main()
'''
def main():
code_sections = []
in_code = False
code_lines = []
for l in open("README.md"):
if in_code:
if l.startswith("```"):
if code_lines[0].startswith("def "):
indent = ""
else:
indent = " " * 4
code_section = indent.join(code_lines)
code_section = code_section.strip()
code_section = code_section.replace(
("\n" + indent + "\n"),
"\n\n")
code_sections.append(code_section)
code_lines.clear()
in_code = False
else:
code_lines.append(l)
elif l.startswith("```python"):
in_code = True
py = TEMPLATE.format(*code_sections)
with open("heic_unpack.py", "w") as fh:
fh.write(py)
if __name__ == "__main__":
main()