Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor build_examples.py #110

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,3 @@ jobs:
pip install .
- name: Create Examples
run: PYTHONPATH=$(pwd)/src:$PYTHONPATH cd src/wireviz/ && python build_examples.py
- name: Upload examples, demos, and tutorials
uses: actions/upload-artifact@v2
with:
name: examples-and-tutorials
path: |
examples/
tutorial/
59 changes: 30 additions & 29 deletions examples/readme.md
Original file line number Diff line number Diff line change
@@ -1,61 +1,62 @@
# Example gallery
## Example 09
![](../../examples/ex09.png)
# Example Gallery

[Source](ex09.yml) - [Bill of Materials](../../examples/ex09.bom.tsv)
## Example 01
![](ex01.png)

[Source](ex01.yml) - [Bill of Materials](ex01.bom.tsv)

## Example 08
![](../../examples/ex08.png)

[Source](ex08.yml) - [Bill of Materials](../../examples/ex08.bom.tsv)
## Example 02
![](ex02.png)

[Source](ex02.yml) - [Bill of Materials](ex02.bom.tsv)

## Example 01
![](../../examples/ex01.png)

[Source](ex01.yml) - [Bill of Materials](../../examples/ex01.bom.tsv)
## Example 03
![](ex03.png)

[Source](ex03.yml) - [Bill of Materials](ex03.bom.tsv)

## Example 03
![](../../examples/ex03.png)

[Source](ex03.yml) - [Bill of Materials](../../examples/ex03.bom.tsv)
## Example 04
![](ex04.png)

[Source](ex04.yml) - [Bill of Materials](ex04.bom.tsv)

## Example 02
![](../../examples/ex02.png)

[Source](ex02.yml) - [Bill of Materials](../../examples/ex02.bom.tsv)
## Example 05
![](ex05.png)

[Source](ex05.yml) - [Bill of Materials](ex05.bom.tsv)


## Example 06
![](../../examples/ex06.png)
![](ex06.png)

[Source](ex06.yml) - [Bill of Materials](../../examples/ex06.bom.tsv)
[Source](ex06.yml) - [Bill of Materials](ex06.bom.tsv)


## Example 07
![](../../examples/ex07.png)
![](ex07.png)

[Source](ex07.yml) - [Bill of Materials](../../examples/ex07.bom.tsv)
[Source](ex07.yml) - [Bill of Materials](ex07.bom.tsv)


## Example 05
![](../../examples/ex05.png)
## Example 08
![](ex08.png)

[Source](ex05.yml) - [Bill of Materials](../../examples/ex05.bom.tsv)
[Source](ex08.yml) - [Bill of Materials](ex08.bom.tsv)


## Example 10
![](../../examples/ex10.png)
## Example 09
![](ex09.png)

[Source](ex10.yml) - [Bill of Materials](../../examples/ex10.bom.tsv)
[Source](ex09.yml) - [Bill of Materials](ex09.bom.tsv)


## Example 04
![](../../examples/ex04.png)
## Example 10
![](ex10.png)

[Source](ex04.yml) - [Bill of Materials](../../examples/ex04.bom.tsv)
[Source](ex10.yml) - [Bill of Materials](ex10.bom.tsv)


171 changes: 88 additions & 83 deletions src/wireviz/build_examples.py
Original file line number Diff line number Diff line change
@@ -1,113 +1,118 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import argparse
import os
import sys
from fnmatch import fnmatch

# noinspection PyUnresolvedReferences
from wv_helper import open_file_write, open_file_read
import os
from pathlib import Path

sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
script_path = Path(__file__).absolute()

sys.path.insert(0, str(script_path.parent.parent)) # to find wireviz module
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')
demos_path = examples_path

readme = 'readme.md'


def build_demos():
for fn in sorted(os.listdir(demos_path)):
if fnmatch(fn, "demo*.yml"):
abspath = os.path.join(demos_path, fn)

print(abspath)
wireviz.parse_file(abspath)

def build_examples():
with open_file_write(os.path.join(examples_path, readme)) as file:
file.write('# Example gallery\n')
for fn in sorted(os.listdir(examples_path)):
if fnmatch(fn, "ex*.yml"):
i = ''.join(filter(str.isdigit, fn))

abspath = os.path.join(examples_path, fn)
outfile_name = abspath.split(".yml")[0]

paths = {}
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'}

print(abspath)
wireviz.parse_file(abspath)

file.write(f'## Example {i}\n')
file.write(f'![]({outfile_name}.png)\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')
readme = 'readme.md'

file.write('\nOutput:\n\n'.format(i))

file.write(f'![](tutorial{outfile_name}.png)\n\n')
def build(dirname, build_readme, include_source, include_readme):
filename_list = []
path = paths[dirname]['path']
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)

if build_readme:
i = ''.join(filter(str.isdigit, yaml_file.stem))

if include_readme:
with open_file_append(path / readme) as out:
with open_file_read(path / f'{yaml_file.stem}.md') as info:
for line in info:
out.write(line.replace('## ', '## {} - '.format(i)))
out.write('\n\n')
else:
with open_file_append(path / readme) as out:
out.write(f'## Example {i}\n')

with open_file_append(path / readme) as out:
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'![]({yaml_file.stem}.png)\n\n')
out.write(f'[Source]({yaml_file.name}) - [Bill of Materials]({yaml_file.stem}.bom.tsv)\n\n\n')

file.write(f'[Bill of Materials](tutorial{outfile_name}.bom.tsv)\n\n\n')

def clean_examples():
generated_extensions = ['.gv', '.png', '.svg', '.html', '.bom.tsv']

for filepath in [examples_path, demos_path, tutorials_path]:
print(filepath)
for file in sorted(os.listdir(filepath)):
if os.path.exists(os.path.join(filepath, file)):
if list(filter(file.endswith, generated_extensions)) or file == 'readme.md':
print('rm ' + os.path.join(filepath, file))
os.remove(os.path.join(filepath, file))
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)

filename_list = sorted(filename_list)
# remove files
for filename in filename_list:
if filename.is_file():
print(f' rm {filename}')
os.remove(filename)


def parse_args():
parser = argparse.ArgumentParser(
description='Wireviz Example Manager',
)
parser = argparse.ArgumentParser(description='Wireviz Example Manager',)
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()


def main():
args = parse_args()
if args.action == 'build':
generate_types = {
'examples': build_examples,
'demos': build_demos,
'tutorials': build_tutorials
}
for gentype in args.generate:
if gentype in generate_types:
generate_types.get(gentype) ()
if gentype == 'demos':
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':
clean_examples()


if __name__ == '__main__':
main()
5 changes: 4 additions & 1 deletion src/wireviz/wv_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,7 @@ def open_file_read(filename):
return open(filename, 'r', encoding='UTF-8')

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')
Loading