from os import path, walk from recipe import Recipe from sys import argv def write_file_safely(filepath, content): if path.exists(filepath): response = input(f"File '{filepath}' exists. Overwrite? (y/N): ") if not response or not response.lower().startswith("y"): print(f"Skipping '{filepath}'") return with open(filepath, 'w') as file: file.write(content) if len(argv) < 2: print(f"Usage: {argv[0]} [index_header.html]") exit(1) recipe_dir = argv[1] if len(argv) > 2: with open(argv[2], 'r') as index_header_file: index_header_html = index_header_file.read() else: index_header_html = "" recipe_names = {} for dirpath, dirnames, filenames in walk(recipe_dir): for filename in filenames: if filename.endswith("json"): with open(path.join(dirpath, filename), 'r') as json_file: recipe = Recipe.from_json(json_file.read()) html = recipe.to_html() html_filepath = path.join(dirpath, f"{filename.rsplit('.', maxsplit=1)[0]}.html") write_file_safely(html_filepath, html) recipe_names[recipe.name] = path.relpath(html_filepath, start=recipe_dir) index_html = f""" Recipes
{index_header_html}

Recipes

""" write_file_safely(path.join(recipe_dir, "index.html"), index_html)