Refactor build_examples.py
- Use `pathlib.Path` instead of `os.path` - Fix order of files while building - Consolidate code for building demos, examples and tutorial - Change argument from `tutorials` to `tutorial` to remain consistent - Add some indentation in console output for better readability
This commit is contained in:
parent
1815a13cd6
commit
3fa015cabd
@ -1,113 +1,118 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import os
|
|
||||||
import sys
|
import sys
|
||||||
from fnmatch import fnmatch
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
# noinspection PyUnresolvedReferences
|
script_path = Path(__file__).absolute()
|
||||||
from wv_helper import open_file_write, open_file_read
|
|
||||||
|
|
||||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
|
|
||||||
|
|
||||||
|
sys.path.insert(0, str(script_path.parent.parent)) # to find wireviz module
|
||||||
from wireviz import wireviz
|
from wireviz import wireviz
|
||||||
|
from wv_helper import open_file_write, open_file_read, open_file_append
|
||||||
|
|
||||||
examples_path = os.path.join('..','..','examples')
|
|
||||||
tutorials_path = os.path.join('..','..','tutorial')
|
paths = {}
|
||||||
demos_path = examples_path
|
paths['examples'] = {'path': Path(script_path).parent.parent.parent / 'examples',
|
||||||
|
'prefix': 'ex',
|
||||||
|
'title': 'Example Gallery'}
|
||||||
|
paths['tutorial'] = {'path': Path(script_path).parent.parent.parent / 'tutorial',
|
||||||
|
'prefix': 'tutorial',
|
||||||
|
'title': 'WireViz Tutorial'}
|
||||||
|
paths['demos'] = {'path': Path(script_path).parent.parent.parent / 'examples',
|
||||||
|
'prefix': 'demo'}
|
||||||
|
|
||||||
readme = 'readme.md'
|
readme = 'readme.md'
|
||||||
|
|
||||||
|
|
||||||
def build_demos():
|
def build(dirname, build_readme, include_source, include_readme):
|
||||||
for fn in sorted(os.listdir(demos_path)):
|
filename_list = []
|
||||||
if fnmatch(fn, "demo*.yml"):
|
path = paths[dirname]['path']
|
||||||
abspath = os.path.join(demos_path, fn)
|
prefix = paths[dirname]['prefix']
|
||||||
|
print(f'Building {path}')
|
||||||
|
# collect input YAML files
|
||||||
|
file_iterator = path.iterdir()
|
||||||
|
for entry in file_iterator:
|
||||||
|
if entry.is_file() and entry.match(f'{prefix}*.yml'):
|
||||||
|
filename_list.append(entry)
|
||||||
|
filename_list = sorted(filename_list)
|
||||||
|
# build files
|
||||||
|
if build_readme:
|
||||||
|
with open_file_write(path / 'readme.md') as out:
|
||||||
|
out.write(f'# {paths[dirname]["title"]}\n\n')
|
||||||
|
for yaml_file in filename_list:
|
||||||
|
print(f' {yaml_file}')
|
||||||
|
wireviz.parse_file(yaml_file)
|
||||||
|
|
||||||
print(abspath)
|
if build_readme:
|
||||||
wireviz.parse_file(abspath)
|
i = ''.join(filter(str.isdigit, yaml_file.stem))
|
||||||
|
|
||||||
def build_examples():
|
if include_readme:
|
||||||
with open_file_write(os.path.join(examples_path, readme)) as file:
|
with open_file_append(path / readme) as out:
|
||||||
file.write('# Example gallery\n')
|
with open_file_read(path / f'{yaml_file.stem}.md') as info:
|
||||||
for fn in sorted(os.listdir(examples_path)):
|
for line in info:
|
||||||
if fnmatch(fn, "ex*.yml"):
|
out.write(line.replace('## ', '## {} - '.format(i)))
|
||||||
i = ''.join(filter(str.isdigit, fn))
|
out.write('\n\n')
|
||||||
|
else:
|
||||||
|
with open_file_append(path / readme) as out:
|
||||||
|
out.write(f'## Example {i}\n')
|
||||||
|
|
||||||
abspath = os.path.join(examples_path, fn)
|
with open_file_append(path / readme) as out:
|
||||||
outfile_name = abspath.split(".yml")[0]
|
if include_source:
|
||||||
|
with open_file_read(yaml_file) as src:
|
||||||
|
out.write('```yaml\n')
|
||||||
|
for line in src:
|
||||||
|
out.write(line)
|
||||||
|
out.write('```\n')
|
||||||
|
out.write('\n')
|
||||||
|
|
||||||
|
out.write(f'\n\n')
|
||||||
|
out.write(f'[Source]({yaml_file.name}) - [Bill of Materials]({yaml_file.stem}.bom.tsv)\n\n\n')
|
||||||
|
|
||||||
print(abspath)
|
|
||||||
wireviz.parse_file(abspath)
|
|
||||||
|
|
||||||
file.write(f'## Example {i}\n')
|
|
||||||
file.write(f'\n\n')
|
|
||||||
file.write(f'[Source]({fn}) - [Bill of Materials]({outfile_name}.bom.tsv)\n\n\n')
|
|
||||||
|
|
||||||
def build_tutorials():
|
|
||||||
with open_file_write(os.path.join(tutorials_path, readme)) as file:
|
|
||||||
file.write('# WireViz Tutorial\n')
|
|
||||||
for fn in sorted(os.listdir(tutorials_path)):
|
|
||||||
if fnmatch(fn, "tutorial*.yml"):
|
|
||||||
i = ''.join(filter(str.isdigit, fn))
|
|
||||||
abspath = os.path.join(tutorials_path, fn)
|
|
||||||
print(abspath)
|
|
||||||
|
|
||||||
wireviz.parse_file(abspath)
|
|
||||||
|
|
||||||
outfile_name = abspath.split(".yml")[0]
|
|
||||||
|
|
||||||
with open_file_read(outfile_name + '.md') as info:
|
|
||||||
for line in info:
|
|
||||||
file.write(line.replace('## ', '## {} - '.format(i)))
|
|
||||||
file.write(f'\n[Source]({fn}):\n\n')
|
|
||||||
|
|
||||||
with open_file_read(abspath) as src:
|
|
||||||
file.write('```yaml\n')
|
|
||||||
for line in src:
|
|
||||||
file.write(line)
|
|
||||||
file.write('```\n')
|
|
||||||
file.write('\n')
|
|
||||||
|
|
||||||
file.write('\nOutput:\n\n'.format(i))
|
|
||||||
|
|
||||||
file.write(f'\n\n')
|
|
||||||
|
|
||||||
file.write(f'[Bill of Materials](tutorial{outfile_name}.bom.tsv)\n\n\n')
|
|
||||||
|
|
||||||
def clean_examples():
|
def clean_examples():
|
||||||
generated_extensions = ['.gv', '.png', '.svg', '.html', '.bom.tsv']
|
generated_extensions = ['.gv', '.png', '.svg', '.html', '.bom.tsv']
|
||||||
|
for k, v in paths.items():
|
||||||
|
filepath = v['path']
|
||||||
|
print(f'Cleaning {filepath}')
|
||||||
|
# collect files to remove
|
||||||
|
filename_list = []
|
||||||
|
file_iterator = filepath.iterdir()
|
||||||
|
for entry in file_iterator:
|
||||||
|
for ext in generated_extensions:
|
||||||
|
if entry.is_file() and entry.match(f'*{ext}'):
|
||||||
|
filename_list.append(entry)
|
||||||
|
filename_list.append(filepath / readme)
|
||||||
|
|
||||||
for filepath in [examples_path, demos_path, tutorials_path]:
|
filename_list = sorted(filename_list)
|
||||||
print(filepath)
|
# remove files
|
||||||
for file in sorted(os.listdir(filepath)):
|
for filename in filename_list:
|
||||||
if os.path.exists(os.path.join(filepath, file)):
|
if filename.is_file():
|
||||||
if list(filter(file.endswith, generated_extensions)) or file == 'readme.md':
|
print(f' rm {filename}')
|
||||||
print('rm ' + os.path.join(filepath, file))
|
os.remove(filename)
|
||||||
os.remove(os.path.join(filepath, file))
|
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(description='Wireviz Example Manager',)
|
||||||
description='Wireviz Example Manager',
|
|
||||||
)
|
|
||||||
parser.add_argument('action', nargs='?', action='store', default='build')
|
parser.add_argument('action', nargs='?', action='store', default='build')
|
||||||
parser.add_argument('-generate', nargs='*', choices=['examples', 'demos', 'tutorials'], default=['examples', 'demos', 'tutorials'])
|
parser.add_argument('-generate', nargs='*', choices=['examples', 'demos', 'tutorial'], default=['examples', 'demos', 'tutorial'])
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
if args.action == 'build':
|
if args.action == 'build':
|
||||||
generate_types = {
|
|
||||||
'examples': build_examples,
|
|
||||||
'demos': build_demos,
|
|
||||||
'tutorials': build_tutorials
|
|
||||||
}
|
|
||||||
for gentype in args.generate:
|
for gentype in args.generate:
|
||||||
if gentype in generate_types:
|
if gentype == 'demos':
|
||||||
generate_types.get(gentype) ()
|
build('demos', build_readme = False, include_source = False, include_readme = False)
|
||||||
|
if gentype == 'examples':
|
||||||
|
build('examples', build_readme = True, include_source = False, include_readme = False)
|
||||||
|
if gentype == 'tutorial':
|
||||||
|
build('tutorial', build_readme = True, include_source = True, include_readme = True)
|
||||||
elif args.action == 'clean':
|
elif args.action == 'clean':
|
||||||
clean_examples()
|
clean_examples()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@ -119,6 +119,8 @@ def open_file_read(filename):
|
|||||||
def open_file_write(filename):
|
def open_file_write(filename):
|
||||||
return open(filename, 'w', encoding='UTF-8')
|
return open(filename, 'w', encoding='UTF-8')
|
||||||
|
|
||||||
|
def open_file_append(filename):
|
||||||
|
return open(filename, 'a', encoding='UTF-8')
|
||||||
|
|
||||||
def manufacturer_info_field(manufacturer, mpn):
|
def manufacturer_info_field(manufacturer, mpn):
|
||||||
if manufacturer or mpn:
|
if manufacturer or mpn:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user