|
| 1 | +from json import dumps, load, loads |
| 2 | +from os import listdir, path |
| 3 | +from err import Errors |
| 4 | +from utils import get_ok_response |
| 5 | + |
| 6 | + |
| 7 | +def load_data(file_name: str) -> dict: |
| 8 | + try: |
| 9 | + with open(file_name, 'r', encoding='utf-8') as file: |
| 10 | + data = load(file) |
| 11 | + |
| 12 | + return data |
| 13 | + except FileNotFoundError: |
| 14 | + raise Errors(code=2, message=f"File {file_name} not found") |
| 15 | + except Exception as e: |
| 16 | + raise Errors(code=2, message=f"Error loading file {file_name}: {str(e)}") |
| 17 | + |
| 18 | +def compare_with_default(data_default: dict, data: dict) -> tuple: |
| 19 | + missing_tables = [] |
| 20 | + missing_data = {} |
| 21 | + |
| 22 | + try: |
| 23 | + for table, schema in data_default.items(): |
| 24 | + if table not in data: |
| 25 | + missing_tables.append(table) |
| 26 | + else: |
| 27 | + missing_columns = [ |
| 28 | + col for col in schema if col not in data[table] |
| 29 | + ] |
| 30 | + if missing_columns: |
| 31 | + missing_data[table] = missing_columns |
| 32 | + |
| 33 | + return missing_tables, missing_data |
| 34 | + except Exception as e: |
| 35 | + raise Errors(code=4, message=f"Error comparing data: {str(e)}") |
| 36 | + |
| 37 | +def compare_all_with_default(data_default: dict, *data_files: dict) -> dict: |
| 38 | + results = {} |
| 39 | + |
| 40 | + try: |
| 41 | + for data in data_files: |
| 42 | + file_name = data.get('file_name', 'unknown').replace('.json', '') |
| 43 | + missing_tables, missing_data = compare_with_default(data_default, data) |
| 44 | + results[file_name] = { |
| 45 | + "Missing_Tables": missing_tables, |
| 46 | + "Missing_Data": missing_data |
| 47 | + } |
| 48 | + |
| 49 | + return results |
| 50 | + except Exception as e: |
| 51 | + raise Errors(code=4, message=f"Error comparing all data: {str(e)}") |
| 52 | + |
| 53 | +def read_json_files_from_directory(directory: str) -> list: |
| 54 | + data_files = [] |
| 55 | + |
| 56 | + try: |
| 57 | + for filename in listdir(directory): |
| 58 | + if filename.endswith('.json'): |
| 59 | + file_path = path.join(directory, filename) |
| 60 | + with open(file_path, 'r', encoding='utf-8') as file: |
| 61 | + data = load(file) |
| 62 | + data['file_name'] = filename |
| 63 | + data_files.append(data) |
| 64 | + |
| 65 | + return data_files |
| 66 | + except FileNotFoundError: |
| 67 | + raise Errors(code=3, message=f"Directory {directory} not found") |
| 68 | + except Exception as e: |
| 69 | + raise Errors(code=3, message=f"Error reading files from directory {directory}: {str(e)}") |
| 70 | + |
| 71 | +def write_json_output(data: dict) -> None: |
| 72 | + try: |
| 73 | + with open("Output.json", 'w') as file: |
| 74 | + file.write(dumps(data, indent=4)) |
| 75 | + |
| 76 | + return get_ok_response() |
| 77 | + except Exception as e: |
| 78 | + raise Errors(code=5, message=f"Error writing output file: {str(e)}") |
| 79 | + |
| 80 | +def main(): |
| 81 | + RESPONSE = None |
| 82 | + |
| 83 | + try: |
| 84 | + default_data = load_data(file_name="MOCK/PATTERN/data_default.json") |
| 85 | + data_files = read_json_files_from_directory(directory="MOCK/DATA") |
| 86 | + |
| 87 | + comparison_results = compare_all_with_default(default_data, *data_files) |
| 88 | + |
| 89 | + RESPONSE = write_json_output(data=comparison_results) |
| 90 | + except Errors as e: |
| 91 | + RESPONSE = e.get_response() |
| 92 | + except Exception as e: |
| 93 | + RESPONSE = Errors(code=1, message=f"{str(e)}").get_response() |
| 94 | + finally: |
| 95 | + print(RESPONSE) |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + main() |
0 commit comments