|
1 | | -from re import DOTALL, findall |
2 | 1 | from json import dump, load |
3 | 2 | from os import listdir, makedirs, path, walk |
| 3 | +from typing import Callable |
4 | 4 | from err import Errors |
5 | 5 | from utils.iterable_handler import find_db_files |
6 | 6 | from utils.response_handler import get_ok_response |
@@ -51,36 +51,38 @@ def generate_json(multiple_files = False, multiple_db_files = False) -> str: |
51 | 51 | except Exception as e: |
52 | 52 | raise Errors(code=7, message=f"Error generating JSON file: {str(e)}") |
53 | 53 |
|
54 | | -def create_json_from_data() -> None: |
| 54 | +def create_json_from_data(interpret_txt_file: Callable[[str], dict]) -> None: |
55 | 55 | file_path = "" |
56 | 56 | json_data = {} |
| 57 | + sqlite_file = False |
57 | 58 |
|
58 | 59 | try: |
59 | 60 | for file in listdir(PATTERN_PATH): |
60 | | - if file.endswith('.txt'): |
| 61 | + if file.endswith('.db') or file.endswith('.sqlite'): |
61 | 62 | file_path = path.join(PATTERN_PATH, file) |
| 63 | + sqlite_file = True |
62 | 64 | break |
63 | 65 |
|
64 | | - with open(file_path, 'r', encoding='utf-8') as file: |
65 | | - data = file.read() |
| 66 | + if not sqlite_file: |
| 67 | + for file in listdir(PATTERN_PATH): |
| 68 | + if file.endswith('.txt'): |
| 69 | + file_path = path.join(PATTERN_PATH, file) |
| 70 | + break |
| 71 | + |
| 72 | + if file_path == "": |
| 73 | + raise Errors(code=10, message="No valid file found") |
66 | 74 |
|
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 |
| 75 | + if sqlite_file: |
| 76 | + json_data = list_tables(file_path) |
| 77 | + else: |
| 78 | + json_data = interpret_txt_file(file_path) |
79 | 79 |
|
80 | 80 | with open(PATTERN_PATH_JSON, 'w', encoding='utf-8') as json_file: |
81 | 81 | dump(json_data, json_file, indent=4, ensure_ascii=False) |
82 | 82 | except FileNotFoundError: |
83 | 83 | raise Errors(code=3, message=f"Directory {file_path} not found") |
| 84 | + except Errors as err: |
| 85 | + raise err |
84 | 86 | except Exception as e: |
85 | 87 | raise Errors(code=9, message=f"Error processing data: {str(e)}") |
86 | 88 |
|
|
0 commit comments