|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +# asciidoc_templates_generator.py |
| 4 | +# Date: 2024 |
| 5 | +# Author: Generated for AsciiDoc template support |
| 6 | +import logging |
| 7 | +import pathlib |
| 8 | +from distutils.dir_util import copy_tree |
| 9 | +from pathlib import Path |
| 10 | +from shutil import copyfile |
| 11 | + |
| 12 | +import numpy as np |
| 13 | +import pandas as pd |
| 14 | +from dqgen.adapters.ap_reader import read_ap_from_csv |
| 15 | + |
| 16 | +from dqgen.services import INSTANCE_OPERATIONS, PROPERTIES_OPERATIONS, REIFIED_PROPERTIES_OPERATIONS, ASCII_DOC_TEMPLATES, \ |
| 17 | + PATH_TO_ASCIIDOC_STATIC_FOLDER, TEMPLATE_AND_ASCIIDOC_FILE_NAME_MAPPING |
| 18 | +from dqgen.services.asciidoc_generator import AsciiDocGenerator |
| 19 | +from dqgen.services.html_templates_data_source_builder import build_datasource_for_html_template, camel_case_to_words |
| 20 | +from dqgen.services.validate_application_profile import validate_application_profile |
| 21 | + |
| 22 | + |
| 23 | +def generate_class_level_asciidoc_templates(processed_csv_file: pd.DataFrame, asciidoc_output_folder_path): |
| 24 | + """ |
| 25 | + generate AsciiDoc templates for each class in the configuration CSV. |
| 26 | + """ |
| 27 | + |
| 28 | + for cls in processed_csv_file["class"].unique(): |
| 29 | + for operation in INSTANCE_OPERATIONS: |
| 30 | + class_name = cls.split(":")[1] |
| 31 | + class_folder_name = class_name.lower() |
| 32 | + output_folder_path = asciidoc_output_folder_path + "/" + class_folder_name |
| 33 | + pathlib.Path(output_folder_path).mkdir(parents=True, exist_ok=True) |
| 34 | + AsciiDocGenerator(cls=cls, operation=operation, |
| 35 | + class_name=camel_case_to_words(class_name).title(), |
| 36 | + output_folder_path=output_folder_path, |
| 37 | + template=ASCII_DOC_TEMPLATES.get_template("instance.jinja2")).to_file() |
| 38 | + logging.info("Generated instance AsciiDoc templates ...") |
| 39 | + |
| 40 | + |
| 41 | +def generate_property_level_asciidoc_templates(processed_csv_file: pd.DataFrame, asciidoc_output_folder_path): |
| 42 | + """ |
| 43 | + generate AsciiDoc template for data properties and their values for each instance in the configuration CSV |
| 44 | + """ |
| 45 | + for index, row in processed_csv_file.iterrows(): |
| 46 | + |
| 47 | + if not row["object property"]: |
| 48 | + for operation in PROPERTIES_OPERATIONS: |
| 49 | + class_folder_name = row["class"].split(":")[1].lower() |
| 50 | + if row["property group"] and row["property group"] is not np.NaN: |
| 51 | + property_group_folder = row["property group"].replace(" ", "_") |
| 52 | + output_folder_path = asciidoc_output_folder_path + "/" + class_folder_name + "/" + property_group_folder |
| 53 | + else: |
| 54 | + output_folder_path = asciidoc_output_folder_path + "/" + class_folder_name |
| 55 | + pathlib.Path(output_folder_path).mkdir(parents=True, exist_ok=True) |
| 56 | + AsciiDocGenerator(cls=row["class"], |
| 57 | + prop=row["property"], |
| 58 | + prop_name=camel_case_to_words(row["property"].split(":")[1]).lower(), |
| 59 | + operation=operation, |
| 60 | + output_folder_path=output_folder_path, |
| 61 | + template=ASCII_DOC_TEMPLATES.get_template("property.jinja2")).to_file() |
| 62 | + |
| 63 | + logging.info("Generated property AsciiDoc templates ...") |
| 64 | + |
| 65 | + |
| 66 | +def generate_reified_property_level_asciidoc_templates(processed_csv_file: pd.DataFrame, asciidoc_output_folder_path): |
| 67 | + """ |
| 68 | + generate AsciiDoc template of reified structures for each instance in the configuration CSV |
| 69 | + """ |
| 70 | + for index, row in processed_csv_file.iterrows(): |
| 71 | + if row["object property"]: |
| 72 | + for operation in REIFIED_PROPERTIES_OPERATIONS: |
| 73 | + class_folder_name = row["class"].split(":")[1].lower() |
| 74 | + if row["property group"] and row["property group"] is not np.NaN: |
| 75 | + property_group_folder = row["property group"].replace(" ", "_") |
| 76 | + output_folder_path = asciidoc_output_folder_path + "/" + class_folder_name + "/" + property_group_folder |
| 77 | + else: |
| 78 | + output_folder_path = asciidoc_output_folder_path + "/" + class_folder_name |
| 79 | + pathlib.Path(output_folder_path).mkdir(parents=True, exist_ok=True) |
| 80 | + AsciiDocGenerator(cls=row["class"], |
| 81 | + prop=row["property"], |
| 82 | + object_property=row["object property"], |
| 83 | + prop_name=camel_case_to_words(row["property"].split(":")[1]).lower(), |
| 84 | + operation=operation, |
| 85 | + output_folder_path=output_folder_path, |
| 86 | + template=ASCII_DOC_TEMPLATES.get_template("reified_property.jinja2")).to_file() |
| 87 | + |
| 88 | + logging.info("Generated reified property AsciiDoc templates ...") |
| 89 | + |
| 90 | + |
| 91 | +def generate_asciidoc_template(processed_csv_file: pd.DataFrame, asciidoc_output_folder_path, template, file_name): |
| 92 | + """ |
| 93 | + Builds an AsciiDoc page and puts into a specified folder |
| 94 | + :param file_name: |
| 95 | + :param template: |
| 96 | + :param processed_csv_file: |
| 97 | + :param asciidoc_output_folder_path: |
| 98 | + :return: |
| 99 | + """ |
| 100 | + |
| 101 | + data_source = build_datasource_for_html_template(processed_csv_file=processed_csv_file) |
| 102 | + build_template = template.stream(data_source=data_source) |
| 103 | + build_template.dump(asciidoc_output_folder_path + "/" + file_name) |
| 104 | + |
| 105 | + |
| 106 | +def copy_files_from_static_folder(file_list: list, destination_folder: str): |
| 107 | + """ |
| 108 | + Copy the files from the static folder to a specified destination |
| 109 | + :param file_list: |
| 110 | + :param destination_folder: |
| 111 | + """ |
| 112 | + for file in file_list: |
| 113 | + file_name = file.name |
| 114 | + copyfile(file, destination_folder + "/" + file_name) |
| 115 | + |
| 116 | + |
| 117 | +def generate_asciidoc_templates_from_csv(ap_file_path: pathlib.Path, output_base_dir: pathlib.Path): |
| 118 | + """ |
| 119 | + generates a set of AsciiDoc templates from the configuration CSV |
| 120 | + """ |
| 121 | + processed_csv_file = read_ap_from_csv(ap_file_path) |
| 122 | + validate_application_profile(application_profile_df=processed_csv_file) |
| 123 | + output = Path(output_base_dir) / ap_file_path.stem |
| 124 | + asciidoc_output = output / "asciidoc" |
| 125 | + asciidoc_output.mkdir(parents=True, exist_ok=True) |
| 126 | + |
| 127 | + generate_class_level_asciidoc_templates(processed_csv_file=processed_csv_file, asciidoc_output_folder_path=str(asciidoc_output)) |
| 128 | + generate_property_level_asciidoc_templates(processed_csv_file=processed_csv_file, |
| 129 | + asciidoc_output_folder_path=str(asciidoc_output)) |
| 130 | + generate_reified_property_level_asciidoc_templates(processed_csv_file=processed_csv_file, |
| 131 | + asciidoc_output_folder_path=str(asciidoc_output)) |
| 132 | + |
| 133 | + for file_name, template in TEMPLATE_AND_ASCIIDOC_FILE_NAME_MAPPING.items(): |
| 134 | + generate_asciidoc_template(processed_csv_file=processed_csv_file, |
| 135 | + asciidoc_output_folder_path=str(asciidoc_output), template=template, file_name=file_name) |
| 136 | + |
| 137 | + copy_tree(PATH_TO_ASCIIDOC_STATIC_FOLDER, str(asciidoc_output)) |
| 138 | + |
0 commit comments