|
| 1 | +from re import DOTALL, findall |
| 2 | +from json import dump, load |
| 3 | +from os import listdir, makedirs, path, walk |
| 4 | +from err import Errors |
| 5 | +from utils.iterable_handler import find_db_files |
| 6 | +from utils.response_handler import get_ok_response |
| 7 | +from dao import list_tables |
| 8 | +from constants import DB_PATHS, OUTPUT_DB_PATH, PATTERN_PATH, PATTERN_PATH_JSON |
| 9 | + |
| 10 | + |
| 11 | +def generate_json(multiple_files = False, multiple_db_files = False) -> str: |
| 12 | + db_paths = find_db_files(DB_PATHS) |
| 13 | + db_info = {} |
| 14 | + |
| 15 | + try: |
| 16 | + for db_path in db_paths: |
| 17 | + if path.exists(db_path): |
| 18 | + dir_name = path.basename(path.dirname(db_path)) |
| 19 | + file_name = path.basename(db_path).replace(".db", "") |
| 20 | + if dir_name not in db_info: |
| 21 | + db_info[dir_name] = {} |
| 22 | + db_info[dir_name][file_name] = list_tables(db_path) |
| 23 | + else: |
| 24 | + db_info[db_path] = "Database file not found" |
| 25 | + |
| 26 | + if not path.exists(OUTPUT_DB_PATH): |
| 27 | + makedirs(OUTPUT_DB_PATH) |
| 28 | + |
| 29 | + if multiple_files: |
| 30 | + for db_name in db_info: |
| 31 | + output_path = path.join(OUTPUT_DB_PATH, db_name) |
| 32 | + |
| 33 | + if not path.exists(output_path): |
| 34 | + makedirs(output_path) |
| 35 | + |
| 36 | + for table_name in db_info[db_name]: |
| 37 | + with open(path.join(OUTPUT_DB_PATH, db_name, f"{table_name}.json"), "w") as json_file: |
| 38 | + dump(db_info[db_name][table_name], json_file, indent=4) |
| 39 | + return "JSON files generated successfully" |
| 40 | + |
| 41 | + if multiple_db_files: |
| 42 | + for db_name in db_info: |
| 43 | + with open(path.join(OUTPUT_DB_PATH, f"{db_name}.json"), "w") as json_file: |
| 44 | + dump(db_info[db_name], json_file, indent=4) |
| 45 | + return "JSON files generated successfully" |
| 46 | + |
| 47 | + with open(path.join(OUTPUT_DB_PATH, "Data.json"), "w") as json_file: |
| 48 | + dump(db_info, json_file, indent=4) |
| 49 | + |
| 50 | + return "JSON file generated successfully" |
| 51 | + except Exception as e: |
| 52 | + raise Errors(code=7, message=f"Error generating JSON file: {str(e)}") |
| 53 | + |
| 54 | +def create_json_from_data() -> None: |
| 55 | + file_path = "" |
| 56 | + json_data = {} |
| 57 | + |
| 58 | + try: |
| 59 | + for file in listdir(PATTERN_PATH): |
| 60 | + if file.endswith('.txt'): |
| 61 | + file_path = path.join(PATTERN_PATH, file) |
| 62 | + break |
| 63 | + |
| 64 | + with open(file_path, 'r', encoding='utf-8') as file: |
| 65 | + data = file.read() |
| 66 | + |
| 67 | + sections = findall(r'(\w+):\[(.*?)\]', data, DOTALL) |
| 68 | + |
| 69 | + for section_name, section_content in sections: |
| 70 | + entries = [] |
| 71 | + matches = findall(r'\{nome: (\w+), tipo: (\w+)\}', section_content) |
| 72 | + for nome, tipo in matches: |
| 73 | + entries.append({ |
| 74 | + "nome": nome, |
| 75 | + "tipo": tipo |
| 76 | + }) |
| 77 | + |
| 78 | + json_data[section_name] = entries |
| 79 | + |
| 80 | + with open(PATTERN_PATH_JSON, 'w', encoding='utf-8') as json_file: |
| 81 | + dump(json_data, json_file, indent=4, ensure_ascii=False) |
| 82 | + except FileNotFoundError: |
| 83 | + raise Errors(code=3, message=f"Directory {file_path} not found") |
| 84 | + except Exception as e: |
| 85 | + raise Errors(code=9, message=f"Error processing data: {str(e)}") |
| 86 | + |
| 87 | +def read_json_files_from_directory() -> list: |
| 88 | + data_files = [] |
| 89 | + |
| 90 | + try: |
| 91 | + for dirpath, _, filenames in walk(OUTPUT_DB_PATH): |
| 92 | + for filename in filenames: |
| 93 | + if filename.endswith('.json'): |
| 94 | + file_path = path.join(dirpath, filename) |
| 95 | + with open(file_path, 'r', encoding='utf-8') as file: |
| 96 | + data = load(file) |
| 97 | + data['database_name'] = path.basename(dirpath) |
| 98 | + data['file_name'] = filename |
| 99 | + data_files.append(data) |
| 100 | + |
| 101 | + return data_files |
| 102 | + except FileNotFoundError: |
| 103 | + raise Errors(code=3, message=f"Directory {OUTPUT_DB_PATH} not found") |
| 104 | + except Exception as e: |
| 105 | + raise Errors(code=3, message=f"Error reading files from directory {OUTPUT_DB_PATH}: {str(e)}") |
| 106 | + |
| 107 | +def write_json_output(data: dict) -> None: |
| 108 | + try: |
| 109 | + with open("Output.json", 'w') as file: |
| 110 | + dump(data, file, indent=4, ensure_ascii=False) |
| 111 | + |
| 112 | + return get_ok_response() |
| 113 | + except Exception as e: |
| 114 | + raise Errors(code=5, message=f"Error writing output file: {str(e)}") |
0 commit comments