From 6dcf16cbdf418e7d793ac0a928658e3e25c4bfad Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Mon, 20 Jul 2020 12:50:07 +0200 Subject: [PATCH 1/7] 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 --- src/wireviz/build_examples.py | 171 +++++++++++++++++----------------- src/wireviz/wv_helper.py | 2 + 2 files changed, 90 insertions(+), 83 deletions(-) diff --git a/src/wireviz/build_examples.py b/src/wireviz/build_examples.py index ee59ded8..5633659d 100755 --- a/src/wireviz/build_examples.py +++ b/src/wireviz/build_examples.py @@ -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() diff --git a/src/wireviz/wv_helper.py b/src/wireviz/wv_helper.py index 13b660c9..77309e50 100644 --- a/src/wireviz/wv_helper.py +++ b/src/wireviz/wv_helper.py @@ -119,6 +119,8 @@ def open_file_read(filename): def open_file_write(filename): 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): if manufacturer or mpn: From aeadd75be91c14835a875cfa093774da329451d4 Mon Sep 17 00:00:00 2001 From: KV Date: Mon, 20 Jul 2020 19:06:52 +0200 Subject: [PATCH 2/7] Add actions to compare against and restore from the latest commit Add new actions: - 'compare' action to compare generated files (except those generated by Graphviz) against the latest commit, and - 'restore' action to restore generated files from the latest commit. This is a squash rebase of these commits: - p 9ad3e13 Reduce code duplication by moving common code into a generic function - s d4feae6 Add action to restore generated files from git repository - s 64f6507 Add action to compare generated files against git repository - s 099c202 Simplify code --- src/wireviz/build_examples.py | 73 ++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 27 deletions(-) diff --git a/src/wireviz/build_examples.py b/src/wireviz/build_examples.py index 5633659d..5790e52a 100755 --- a/src/wireviz/build_examples.py +++ b/src/wireviz/build_examples.py @@ -23,25 +23,29 @@ paths['demos'] = {'path': Path(script_path).parent.parent.parent / 'examples', 'prefix': 'demo'} +input_extensions = ['.yml'] +generated_extensions = ['.gv', '.png', '.svg', '.html', '.bom.tsv'] +extensions_not_from_graphviz = [ext for ext in generated_extensions if ext[-1] == 'v'] readme = 'readme.md' +def collect_filenames(description, pathkey, ext_list, extrafile = None): + path = paths[pathkey]['path'] + patterns = [f"{paths[pathkey]['prefix']}*{ext}" for ext in ext_list] + if extrafile is not None: + patterns.append(extrafile) + print(f"{description} {path}") + return sorted([filename for pattern in patterns for filename in path.glob(pattern)]) + + 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 + path = paths[dirname]['path'] 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: + # collect and iterate input YAML files + for yaml_file in collect_filenames('Building', dirname, input_extensions): print(f' {yaml_file}') wireviz.parse_file(yaml_file) @@ -72,27 +76,38 @@ def build(dirname, build_readme, include_source, include_readme): def clean_examples(): - 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) - - filename_list = sorted(filename_list) - # remove files - for filename in filename_list: + for key in paths.keys(): + # collect and remove files + for filename in collect_filenames('Cleaning', key, generated_extensions, readme): if filename.is_file(): print(f' rm {filename}') os.remove(filename) +def compare_generated(include_from_graphviz = False): + compare_extensions = generated_extensions if include_from_graphviz else extensions_not_from_graphviz + for key in paths.keys(): + # collect and compare files + for filename in collect_filenames('Comparing', key, compare_extensions, readme): + cmd = f'git --no-pager diff {filename}' + print(f' {cmd}') + os.system(cmd) + + +def restore_generated(): + for key, value in paths.items(): + # collect input YAML files + filename_list = collect_filenames('Restoring', key, input_extensions) + # collect files to restore + filename_list = [fn.with_suffix(ext) for fn in filename_list for ext in generated_extensions] + filename_list.append(value['path'] / readme) + # restore files + for filename in filename_list: + cmd = f'git checkout -- {filename}' + print(f' {cmd}') + os.system(cmd) + + def parse_args(): parser = argparse.ArgumentParser(description='Wireviz Example Manager',) parser.add_argument('action', nargs='?', action='store', default='build') @@ -112,6 +127,10 @@ def main(): build('tutorial', build_readme = True, include_source = True, include_readme = True) elif args.action == 'clean': clean_examples() + elif args.action == 'compare': + compare_generated() + elif args.action == 'restore': + restore_generated() if __name__ == '__main__': From 1d7ed6fc552e70e7dd72f1910b9f234cd6309f22 Mon Sep 17 00:00:00 2001 From: KV Date: Mon, 20 Jul 2020 20:48:37 +0200 Subject: [PATCH 3/7] Make all actions honor the optional argument -g or --group This make it possible to append '-g' or '--groups' followed by space separated group names to any CLI action command, and the set of generated files affected by the command will be limited to the selected groups ('examples', 'tutorial', and 'demos'). Default is all groups. A simple help text is added for each of the arguments (action and groups) to improve the autogenerated CLI help output. This is a squash rebase of these commits: - p ec29076 Make all actions honor the optional argument -generate - s e3ad11a Move open_file_append() outside the if to avoid re-open - s ba4b900 Avoid including readme in all file groups - s 1ca8bd1 Simplify code - s a9e7337 Rename some variables to better reflect their contents and relations - s 58a54b2 Move test to include readme inside collect_filenames() function - s f2a0db0 Improve status output by adding group name - s d3b299b Rename -generate option to -g/--groups and add argument help --- src/wireviz/build_examples.py | 92 ++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 44 deletions(-) diff --git a/src/wireviz/build_examples.py b/src/wireviz/build_examples.py index 5790e52a..0560756b 100755 --- a/src/wireviz/build_examples.py +++ b/src/wireviz/build_examples.py @@ -13,56 +13,59 @@ from wv_helper import open_file_write, open_file_read, open_file_append -paths = {} -paths['examples'] = {'path': Path(script_path).parent.parent.parent / 'examples', +readme = 'readme.md' +groups = {} +groups['examples'] = {'path': Path(script_path).parent.parent.parent / 'examples', 'prefix': 'ex', + readme: [], # Include no files 'title': 'Example Gallery'} -paths['tutorial'] = {'path': Path(script_path).parent.parent.parent / 'tutorial', +groups['tutorial'] = {'path': Path(script_path).parent.parent.parent / 'tutorial', 'prefix': 'tutorial', + readme: ['md', 'yml'], # Include .md and .yml files 'title': 'WireViz Tutorial'} -paths['demos'] = {'path': Path(script_path).parent.parent.parent / 'examples', +groups['demos'] = {'path': Path(script_path).parent.parent.parent / 'examples', 'prefix': 'demo'} input_extensions = ['.yml'] generated_extensions = ['.gv', '.png', '.svg', '.html', '.bom.tsv'] extensions_not_from_graphviz = [ext for ext in generated_extensions if ext[-1] == 'v'] -readme = 'readme.md' -def collect_filenames(description, pathkey, ext_list, extrafile = None): - path = paths[pathkey]['path'] - patterns = [f"{paths[pathkey]['prefix']}*{ext}" for ext in ext_list] - if extrafile is not None: - patterns.append(extrafile) - print(f"{description} {path}") +def collect_filenames(description, groupkey, ext_list): + path = groups[groupkey]['path'] + patterns = [f"{groups[groupkey]['prefix']}*{ext}" for ext in ext_list] + if ext_list != input_extensions and readme in groups[groupkey]: + patterns.append(readme) + print(f"{description} {groupkey} in {path}") return sorted([filename for pattern in patterns for filename in path.glob(pattern)]) -def build(dirname, build_readme, include_source, include_readme): +def build_generated(groupkey): # build files - path = paths[dirname]['path'] + path = groups[groupkey]['path'] + build_readme = readme in groups[groupkey] if build_readme: - with open_file_write(path / 'readme.md') as out: - out.write(f'# {paths[dirname]["title"]}\n\n') + include_readme = 'md' in groups[groupkey][readme] + include_source = 'yml' in groups[groupkey][readme] + with open_file_write(path / readme) as out: + out.write(f'# {groups[groupkey]["title"]}\n\n') # collect and iterate input YAML files - for yaml_file in collect_filenames('Building', dirname, input_extensions): + for yaml_file in collect_filenames('Building', groupkey, input_extensions): 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: + with open_file_append(path / readme) as out: + if include_readme: + with open_file_read(yaml_file.with_suffix('.md')) as info: for line in info: - out.write(line.replace('## ', '## {} - '.format(i))) + out.write(line.replace('## ', f'## {i} - ')) out.write('\n\n') - else: - with open_file_append(path / readme) as out: + else: 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') @@ -75,32 +78,33 @@ def build(dirname, build_readme, include_source, include_readme): out.write(f'[Source]({yaml_file.name}) - [Bill of Materials]({yaml_file.stem}.bom.tsv)\n\n\n') -def clean_examples(): - for key in paths.keys(): +def clean_generated(groupkeys): + for key in groupkeys: # collect and remove files - for filename in collect_filenames('Cleaning', key, generated_extensions, readme): + for filename in collect_filenames('Cleaning', key, generated_extensions): if filename.is_file(): print(f' rm {filename}') os.remove(filename) -def compare_generated(include_from_graphviz = False): +def compare_generated(groupkeys, include_from_graphviz = False): compare_extensions = generated_extensions if include_from_graphviz else extensions_not_from_graphviz - for key in paths.keys(): + for key in groupkeys: # collect and compare files - for filename in collect_filenames('Comparing', key, compare_extensions, readme): + for filename in collect_filenames('Comparing', key, compare_extensions): cmd = f'git --no-pager diff {filename}' print(f' {cmd}') os.system(cmd) -def restore_generated(): - for key, value in paths.items(): +def restore_generated(groupkeys): + for key in groupkeys: # collect input YAML files filename_list = collect_filenames('Restoring', key, input_extensions) # collect files to restore filename_list = [fn.with_suffix(ext) for fn in filename_list for ext in generated_extensions] - filename_list.append(value['path'] / readme) + if readme in groups[key]: + filename_list.append(groups[key]['path'] / readme) # restore files for filename in filename_list: cmd = f'git checkout -- {filename}' @@ -110,27 +114,27 @@ def restore_generated(): def parse_args(): parser = argparse.ArgumentParser(description='Wireviz Example Manager',) - parser.add_argument('action', nargs='?', action='store', default='build') - parser.add_argument('-generate', nargs='*', choices=['examples', 'demos', 'tutorial'], default=['examples', 'demos', 'tutorial']) + parser.add_argument('action', nargs='?', action='store', + choices=['build','clean','compare','restore'], default='build', + help='what to do with the generated files (default: build)') + parser.add_argument('-g', '--groups', nargs='+', + choices=groups.keys(), default=groups.keys(), + help='the groups of generated files (default: all)') return parser.parse_args() def main(): args = parse_args() if args.action == 'build': - for gentype in args.generate: - 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) + # TODO: Move this loop into the function for consistency? + for groupkey in args.groups: + build_generated(groupkey) elif args.action == 'clean': - clean_examples() + clean_generated(args.groups) elif args.action == 'compare': - compare_generated() + compare_generated(args.groups) elif args.action == 'restore': - restore_generated() + restore_generated(args.groups) if __name__ == '__main__': From 21fcd4c6f2fc4438b270b3d38df808ce6f3dce80 Mon Sep 17 00:00:00 2001 From: KV Date: Thu, 23 Jul 2020 15:16:22 +0200 Subject: [PATCH 4/7] Restructure the group dict initialization By putting all value entries on separate lines with a trailing comma, it becomes easier to read the diff when later inserting or deleting the first or last value entry in any dict. --- src/wireviz/build_examples.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/wireviz/build_examples.py b/src/wireviz/build_examples.py index 0560756b..83c20d69 100755 --- a/src/wireviz/build_examples.py +++ b/src/wireviz/build_examples.py @@ -14,17 +14,24 @@ readme = 'readme.md' -groups = {} -groups['examples'] = {'path': Path(script_path).parent.parent.parent / 'examples', - 'prefix': 'ex', - readme: [], # Include no files - 'title': 'Example Gallery'} -groups['tutorial'] = {'path': Path(script_path).parent.parent.parent / 'tutorial', - 'prefix': 'tutorial', - readme: ['md', 'yml'], # Include .md and .yml files - 'title': 'WireViz Tutorial'} -groups['demos'] = {'path': Path(script_path).parent.parent.parent / 'examples', - 'prefix': 'demo'} +groups = { + 'examples': { + 'path': Path(script_path).parent.parent.parent / 'examples', + 'prefix': 'ex', + readme: [], # Include no files + 'title': 'Example Gallery', + }, + 'tutorial' : { + 'path': Path(script_path).parent.parent.parent / 'tutorial', + 'prefix': 'tutorial', + readme: ['md', 'yml'], # Include .md and .yml files + 'title': 'WireViz Tutorial', + }, + 'demos' : { + 'path': Path(script_path).parent.parent.parent / 'examples', + 'prefix': 'demo', + }, +} input_extensions = ['.yml'] generated_extensions = ['.gv', '.png', '.svg', '.html', '.bom.tsv'] From add227de196f4af469ea58b1d24dbf6934309ac3 Mon Sep 17 00:00:00 2001 From: KV Date: Fri, 24 Jul 2020 18:08:50 +0200 Subject: [PATCH 5/7] Move group loop into build_generated() for consistency Now, all action functions are called with a group list as argument. --- src/wireviz/build_examples.py | 75 +++++++++++++++++------------------ 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/src/wireviz/build_examples.py b/src/wireviz/build_examples.py index 83c20d69..7c3496e5 100755 --- a/src/wireviz/build_examples.py +++ b/src/wireviz/build_examples.py @@ -47,42 +47,43 @@ def collect_filenames(description, groupkey, ext_list): return sorted([filename for pattern in patterns for filename in path.glob(pattern)]) -def build_generated(groupkey): - # build files - path = groups[groupkey]['path'] - build_readme = readme in groups[groupkey] - if build_readme: - include_readme = 'md' in groups[groupkey][readme] - include_source = 'yml' in groups[groupkey][readme] - with open_file_write(path / readme) as out: - out.write(f'# {groups[groupkey]["title"]}\n\n') - # collect and iterate input YAML files - for yaml_file in collect_filenames('Building', groupkey, input_extensions): - print(f' {yaml_file}') - wireviz.parse_file(yaml_file) - +def build_generated(groupkeys): + for key in groupkeys: + # preparation + path = groups[key]['path'] + build_readme = readme in groups[key] if build_readme: - i = ''.join(filter(str.isdigit, yaml_file.stem)) - - with open_file_append(path / readme) as out: - if include_readme: - with open_file_read(yaml_file.with_suffix('.md')) as info: - for line in info: - out.write(line.replace('## ', f'## {i} - ')) - out.write('\n\n') - else: - out.write(f'## Example {i}\n') - - 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') + include_readme = 'md' in groups[key][readme] + include_source = 'yml' in groups[key][readme] + with open_file_write(path / readme) as out: + out.write(f'# {groups[key]["title"]}\n\n') + # collect and iterate input YAML files + for yaml_file in collect_filenames('Building', key, input_extensions): + print(f' {yaml_file}') + wireviz.parse_file(yaml_file) + + if build_readme: + i = ''.join(filter(str.isdigit, yaml_file.stem)) + + with open_file_append(path / readme) as out: + if include_readme: + with open_file_read(yaml_file.with_suffix('.md')) as info: + for line in info: + out.write(line.replace('## ', f'## {i} - ')) + out.write('\n\n') + else: + out.write(f'## Example {i}\n') + + 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') def clean_generated(groupkeys): @@ -133,9 +134,7 @@ def parse_args(): def main(): args = parse_args() if args.action == 'build': - # TODO: Move this loop into the function for consistency? - for groupkey in args.groups: - build_generated(groupkey) + build_generated(args.groups) elif args.action == 'clean': clean_generated(args.groups) elif args.action == 'compare': From 45122b17df8f7467ee28d6b20089def6aab45116 Mon Sep 17 00:00:00 2001 From: KV Date: Sun, 26 Jul 2020 00:22:19 +0200 Subject: [PATCH 6/7] Add CLI option -c that allows comparing Graphviz output also --- src/wireviz/build_examples.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/wireviz/build_examples.py b/src/wireviz/build_examples.py index 7c3496e5..01983c07 100755 --- a/src/wireviz/build_examples.py +++ b/src/wireviz/build_examples.py @@ -125,6 +125,8 @@ def parse_args(): parser.add_argument('action', nargs='?', action='store', choices=['build','clean','compare','restore'], default='build', help='what to do with the generated files (default: build)') + parser.add_argument('-c', '--compare-graphviz-output', action='store_true', + help='the Graphviz output is also compared (default: False)') parser.add_argument('-g', '--groups', nargs='+', choices=groups.keys(), default=groups.keys(), help='the groups of generated files (default: all)') @@ -138,7 +140,7 @@ def main(): elif args.action == 'clean': clean_generated(args.groups) elif args.action == 'compare': - compare_generated(args.groups) + compare_generated(args.groups, args.compare_graphviz_output) elif args.action == 'restore': restore_generated(args.groups) From 39487ca9e4d9eced6ddc887cb205e10243c0dcc4 Mon Sep 17 00:00:00 2001 From: KV Date: Sun, 26 Jul 2020 20:12:45 +0200 Subject: [PATCH 7/7] Fix change requests from owner as descibed in PR #118 - Add double quotes around path string in `os.system()` call and status output to handle any spaces in the path. - Split the `generated_extensions` list into the two lists `extensions_not_containing_graphviz_output` and `extensions_containing_graphviz_output` for readability. --- src/wireviz/build_examples.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/wireviz/build_examples.py b/src/wireviz/build_examples.py index 01983c07..10a4b1bf 100755 --- a/src/wireviz/build_examples.py +++ b/src/wireviz/build_examples.py @@ -34,8 +34,9 @@ } input_extensions = ['.yml'] -generated_extensions = ['.gv', '.png', '.svg', '.html', '.bom.tsv'] -extensions_not_from_graphviz = [ext for ext in generated_extensions if ext[-1] == 'v'] +extensions_not_containing_graphviz_output = ['.gv', '.bom.tsv'] +extensions_containing_graphviz_output = ['.png', '.svg', '.html'] +generated_extensions = extensions_not_containing_graphviz_output + extensions_containing_graphviz_output def collect_filenames(description, groupkey, ext_list): @@ -43,7 +44,7 @@ def collect_filenames(description, groupkey, ext_list): patterns = [f"{groups[groupkey]['prefix']}*{ext}" for ext in ext_list] if ext_list != input_extensions and readme in groups[groupkey]: patterns.append(readme) - print(f"{description} {groupkey} in {path}") + print(f'{description} {groupkey} in "{path}"') return sorted([filename for pattern in patterns for filename in path.glob(pattern)]) @@ -59,7 +60,7 @@ def build_generated(groupkeys): out.write(f'# {groups[key]["title"]}\n\n') # collect and iterate input YAML files for yaml_file in collect_filenames('Building', key, input_extensions): - print(f' {yaml_file}') + print(f' "{yaml_file}"') wireviz.parse_file(yaml_file) if build_readme: @@ -91,16 +92,16 @@ def clean_generated(groupkeys): # collect and remove files for filename in collect_filenames('Cleaning', key, generated_extensions): if filename.is_file(): - print(f' rm {filename}') + print(f' rm "{filename}"') os.remove(filename) -def compare_generated(groupkeys, include_from_graphviz = False): - compare_extensions = generated_extensions if include_from_graphviz else extensions_not_from_graphviz +def compare_generated(groupkeys, include_graphviz_output = False): + compare_extensions = generated_extensions if include_graphviz_output else extensions_not_containing_graphviz_output for key in groupkeys: # collect and compare files for filename in collect_filenames('Comparing', key, compare_extensions): - cmd = f'git --no-pager diff {filename}' + cmd = f'git --no-pager diff "{filename}"' print(f' {cmd}') os.system(cmd) @@ -115,7 +116,7 @@ def restore_generated(groupkeys): filename_list.append(groups[key]['path'] / readme) # restore files for filename in filename_list: - cmd = f'git checkout -- {filename}' + cmd = f'git checkout -- "{filename}"' print(f' {cmd}') os.system(cmd)