-
Notifications
You must be signed in to change notification settings - Fork 257
/
get_yaml_from_example.py
executable file
·69 lines (62 loc) · 2.48 KB
/
get_yaml_from_example.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
#! /usr/bin/env python3
import sys
import os
import re
import yaml
from PIL import Image
document_match = re.compile(r'^--- *$', flags=re.MULTILINE)
fix_tabs = re.compile(r'\t')
fix_initial = re.compile(r'^---\n')
def main():
if len(sys.argv) < 2:
sys.exit("Usage: get_yaml_from_example.py yaml_directory png_directory")
dirname = sys.argv[1]
if not os.path.isdir(dirname):
sys.exit("Directory " + str(dirname) + " not found")
pngdirname = sys.argv[2]
if not os.path.isdir(pngdirname):
sys.exit("Directory " + str(pngdirname) + " not found")
output = {}
for filename in os.listdir(dirname):
if not re.search(r'.ya*ml$', filename, flags=re.IGNORECASE):
continue
if re.search(r'\#', filename):
continue
example_name = os.path.splitext(filename)[0]
result = {}
result['yaml'] = read_file(os.path.join(dirname, filename))
if result['yaml'] is None:
sys.stderr.write("Missing YAML filename " + os.path.join(dirname, filename) + "\n")
continue
png_filename = os.path.join(pngdirname, re.sub(r'\.yml$', '.png', filename))
if os.path.isfile(png_filename):
image = Image.open(png_filename)
(result['width'], result['height']) = image.size
output[example_name] = result
print(yaml.safe_dump(output, default_flow_style=False, default_style='|'))
def read_file(filename):
start_block = 1
end_block = 2
if not os.path.isfile(filename):
sys.exit("File " + str(filename) + " not found")
with open(filename, 'r', encoding='utf-8') as fp:
content = fp.read()
content = fix_tabs.sub(' ', content)
content = fix_initial.sub('', content)
blocks = list(map(lambda x: x.strip(), document_match.split(content)))
if len(blocks) == 0:
sys.stderr.write("File " + str(filename) + " could not be read\n")
return None
metadata = {}
for the_block in blocks:
if re.search(r'metadata:', the_block):
block_info = yaml.load(the_block, Loader=yaml.FullLoader)
if 'metadata' in block_info:
metadata.update(block_info['metadata'])
start_block = int(metadata.get('example start', 1))
end_block = int(metadata.get('example end', start_block)) + 1
result = "\n---\n".join(blocks[start_block:end_block])
return result
if __name__ == "__main__":
main()
sys.exit(None)