|
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() |
| 1 | +from utils import generate_json, compare_all_with_default, write_json_output, load_data, read_json_files_from_directory |
| 2 | +from err import Errors |
| 3 | + |
| 4 | + |
| 5 | +def main(): |
| 6 | + RESPONSE = None |
| 7 | + |
| 8 | + try: |
| 9 | + # Gerar JSON de um único arquivo |
| 10 | + # generate_json() |
| 11 | + |
| 12 | + # Gerar JSON de múltiplos arquivos |
| 13 | + generate_json(multiple_files=True) |
| 14 | + |
| 15 | + # Gerar JSON de múltiplos arquivos de um mesmo banco |
| 16 | + # generate_json(multiple_db_files=True) |
| 17 | + |
| 18 | + default_data = load_data() |
| 19 | + data_files = read_json_files_from_directory() |
| 20 | + comparison_results = compare_all_with_default(default_data, *data_files) |
| 21 | + |
| 22 | + RESPONSE = write_json_output(data=comparison_results) |
| 23 | + except Errors as e: |
| 24 | + RESPONSE = e.get_response() |
| 25 | + except Exception as e: |
| 26 | + RESPONSE = Errors(code=1, message=f"{str(e)}").get_response() |
| 27 | + finally: |
| 28 | + print(RESPONSE) |
| 29 | + |
| 30 | +if __name__ == '__main__': |
| 31 | + # RUNTIME TEST |
| 32 | + from time import time |
| 33 | + |
| 34 | + start_time = time() |
| 35 | + # --------------- |
| 36 | + |
| 37 | + main() |
| 38 | + |
| 39 | + # RUNTIME TEST |
| 40 | + end_time = time() |
| 41 | + |
| 42 | + print(f"\nTempo de execução: {round(end_time - start_time, 2)} segundos") |
| 43 | + # --------------- |
0 commit comments