Fix build_examples.py

This commit is contained in:
Jason R. Jones 2020-07-24 22:03:34 -04:00
parent 50ff1a046a
commit 97e77f55b1

View File

@ -2,6 +2,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import argparse import argparse
import os import os
from pathlib import Path
import sys import sys
from fnmatch import fnmatch from fnmatch import fnmatch
@ -12,8 +13,8 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from wireviz import wireviz from wireviz import wireviz
examples_path = os.path.join('..','..','examples') examples_path = Path('../../examples').absolute()
tutorials_path = os.path.join('..','..','tutorial') tutorials_path = Path('../../tutorial').absolute()
demos_path = examples_path demos_path = examples_path
readme = 'readme.md' readme = 'readme.md'
@ -22,24 +23,25 @@ readme = 'readme.md'
def build_demos(): def build_demos():
for fn in sorted(os.listdir(demos_path)): for fn in sorted(os.listdir(demos_path)):
if fnmatch(fn, "demo*.yml"): if fnmatch(fn, "demo*.yml"):
abspath = os.path.join(demos_path, fn) path = Path(os.path.join(demos_path, fn))
print(abspath) print(path)
wireviz.main(abspath, prepend=None, out=['png', 'svg', 'html', 'csv']) wireviz.main(path.absolute(), prepend=None, out=['png', 'svg', 'html', 'csv'])
def build_examples(): def build_examples():
with open_file_write(os.path.join(examples_path, readme)) as file: with open_file_write(examples_path / readme) as file:
file.write('# Example gallery\n') file.write('# Example gallery\n')
for fn in sorted(os.listdir(examples_path)): for fn in sorted(os.listdir(examples_path)):
if fnmatch(fn, "ex*.yml"): if fnmatch(fn, "ex*.yml"):
i = ''.join(filter(str.isdigit, fn)) i = ''.join(filter(str.isdigit, fn))
abspath = os.path.join(examples_path, fn) path = examples_path / f'{fn}'
outfile_name = abspath.split(".yml")[0] os.chdir(path.parent.absolute())
outfile_name = path.name.replace('.yml', '')
print(abspath) print(path)
wireviz.main(abspath, prepend=None, out=['png', 'svg', 'html', 'csv']) wireviz.main(path, prepend=None, out=['png', 'svg', 'html', 'csv'])
file.write(f'## Example {i}\n') file.write(f'## Example {i}\n')
file.write(f'![]({outfile_name}.png)\n\n') file.write(f'![]({outfile_name}.png)\n\n')
@ -52,19 +54,21 @@ def build_tutorials():
for fn in sorted(os.listdir(tutorials_path)): for fn in sorted(os.listdir(tutorials_path)):
if fnmatch(fn, "tutorial*.yml"): if fnmatch(fn, "tutorial*.yml"):
i = ''.join(filter(str.isdigit, fn)) i = ''.join(filter(str.isdigit, fn))
abspath = os.path.join(tutorials_path, fn)
print(abspath)
wireviz.main(abspath, prepend=None, out=['png', 'svg', 'html', 'csv']) path = tutorials_path / f'{fn}'
os.chdir(path.parent.absolute())
outfile_name = path.name.replace('.yml', '')
outfile_name = abspath.split(".yml")[0] print(path)
wireviz.main(path, prepend=None, out=['png', 'svg', 'html', 'csv'])
with open_file_read(outfile_name + '.md') as info: with open_file_read(outfile_name + '.md') as info:
for line in info: for line in info:
file.write(line.replace('## ', '## {} - '.format(i))) file.write(line.replace('## ', '## {} - '.format(i)))
file.write(f'\n[Source]({fn}):\n\n') file.write(f'\n[Source]({fn}):\n\n')
with open_file_read(abspath) as src: with open_file_read(path) as src:
file.write('```yaml\n') file.write('```yaml\n')
for line in src: for line in src:
file.write(line) file.write(line)
@ -108,11 +112,14 @@ def main():
'demos': build_demos, 'demos': build_demos,
'tutorials': build_tutorials 'tutorials': build_tutorials
} }
for gentype in args.generate: for gentype in args.generate:
if gentype in generate_types: if gentype in generate_types:
generate_types.get(gentype)() generate_types.get(gentype)()
elif args.action == 'clean': elif args.action == 'clean':
clean_examples() clean_examples()
if __name__ == '__main__': if __name__ == '__main__':
main() main()