"""JSON outputter.""" import json import os from ..model import Forum, Board, Thread from ..util import sanitize_title def output(data, destination): """Output the given object to the specified folder.""" if isinstance(data, Forum): output_forum(data, destination) elif isinstance(data, Board): output_board(data, destination) elif isinstance(data, Thread): output_thread(data, destination) def output_forum(data, destination): """Output the given Forum object to the specified folder.""" os.makedirs(destination) with open(os.path.join(destination, "index.json"), "w") as out_file: out_file.write(json.dumps({"title": data.title}, indent=4)) for category in data.categories: category_dir = os.path.join(destination, sanitize_title(category.title)) os.makedirs(category_dir) for board in category.children: output_board( board, os.path.join(category_dir, sanitize_title(board.title)) ) def output_board(data, destination): """Output the given Board object to the specified folder.""" os.makedirs(destination) os.makedirs(os.path.join(destination, "threads")) with open(os.path.join(destination, "index.json"), "w") as out_file: out_file.write(json.dumps({ "title": data.title, "description": data.description }, indent=4)) for thread in data.children: output_thread(thread, os.path.join(destination, "threads", sanitize_title(thread.title))) def output_thread(data, destination): """Output the given Thread object to the specified file.""" with open(destination, "w") as out_file: out_file.write(json.dumps(data, default=vars, indent=4))