Skip to content

Commit

Permalink
Reduce code duplication by moving common code into a generic function
Browse files Browse the repository at this point in the history
  • Loading branch information
kvid committed Jul 20, 2020
1 parent 94b3ffa commit 9ad3e13
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions src/wireviz/build_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,25 @@
paths['demos'] = {'path': Path(script_path).parent.parent.parent / 'examples',
'prefix': 'demo'}

input_extensions = ['.yml']
generated_extensions = ['.gv', '.png', '.svg', '.html', '.bom.tsv']
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)
filename_list = collect_filenames('Building', dirname, input_extensions)
# 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')
Expand Down Expand Up @@ -72,20 +76,9 @@ 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)
filename_list = collect_filenames('Cleaning', k, generated_extensions, readme)
# remove files
for filename in filename_list:
if filename.is_file():
Expand Down

0 comments on commit 9ad3e13

Please sign in to comment.