mapper + lint
This commit is contained in:
126
search_script.py
126
search_script.py
@@ -1,88 +1,88 @@
|
||||
# pylint: disable=too-many-arguments,too-many-locals,too-many-statements,too-many-branches,unused-argument,invalid-name,redefined-outer-name
|
||||
"""
|
||||
[MODULE] Dataset Search Utilities
|
||||
@contract: Предоставляет функционал для поиска текстовых паттернов в метаданных датасетов Superset.
|
||||
"""
|
||||
# <GRACE_MODULE id="search_script" name="search_script.py">
|
||||
# @SEMANTICS: search, superset, dataset, regex
|
||||
# @PURPOSE: Предоставляет утилиты для поиска по текстовым паттернам в метаданных датасетов Superset.
|
||||
# @DEPENDS_ON: superset_tool.client -> Для взаимодействия с API Superset.
|
||||
# @DEPENDS_ON: superset_tool.utils -> Для логирования и инициализации клиентов.
|
||||
|
||||
# [IMPORTS] Стандартная библиотека
|
||||
# <IMPORTS>
|
||||
import logging
|
||||
import re
|
||||
from typing import Dict, Optional
|
||||
|
||||
# [IMPORTS] Third-party
|
||||
from requests.exceptions import RequestException
|
||||
|
||||
# [IMPORTS] Локальные модули
|
||||
from superset_tool.client import SupersetClient
|
||||
from superset_tool.exceptions import SupersetAPIError
|
||||
from superset_tool.utils.logger import SupersetLogger
|
||||
from superset_tool.utils.init_clients import setup_clients
|
||||
# </IMPORTS>
|
||||
|
||||
# [ENTITY: Function('search_datasets')]
|
||||
# CONTRACT:
|
||||
# PURPOSE: Выполняет поиск по строковому паттерну в метаданных всех датасетов.
|
||||
# PRECONDITIONS:
|
||||
# - `client` должен быть инициализированным экземпляром `SupersetClient`.
|
||||
# - `search_pattern` должен быть валидной строкой регулярного выражения.
|
||||
# POSTCONDITIONS:
|
||||
# - Возвращает словарь с результатами поиска.
|
||||
# --- Начало кода модуля ---
|
||||
|
||||
# <ANCHOR id="search_datasets" type="Function">
|
||||
# @PURPOSE: Выполняет поиск по строковому паттерну в метаданных всех датасетов.
|
||||
# @PRE: `client` должен быть инициализированным экземпляром `SupersetClient`.
|
||||
# @PRE: `search_pattern` должен быть валидной строкой регулярного выражения.
|
||||
# @POST: Возвращает словарь с результатами поиска, где ключ - ID датасета, значение - список совпадений.
|
||||
# @PARAM: client: SupersetClient - Клиент для доступа к API Superset.
|
||||
# @PARAM: search_pattern: str - Регулярное выражение для поиска.
|
||||
# @PARAM: logger: Optional[SupersetLogger] - Инстанс логгера.
|
||||
# @RETURN: Optional[Dict] - Словарь с результатами или None, если ничего не найдено.
|
||||
# @THROW: re.error - Если паттерн регулярного выражения невалиден.
|
||||
# @THROW: SupersetAPIError, RequestException - При критических ошибках API.
|
||||
# @RELATION: CALLS -> client.get_datasets
|
||||
def search_datasets(
|
||||
client: SupersetClient,
|
||||
search_pattern: str,
|
||||
logger: Optional[SupersetLogger] = None
|
||||
) -> Optional[Dict]:
|
||||
logger = logger or SupersetLogger(name="dataset_search")
|
||||
logger.info(f"[STATE][search_datasets][ENTER] Searching for pattern: '{search_pattern}'")
|
||||
logger.info(f"[search_datasets][Enter] Searching for pattern: '{search_pattern}'")
|
||||
try:
|
||||
_, datasets = client.get_datasets(query={
|
||||
"columns": ["id", "table_name", "sql", "database", "columns"]
|
||||
})
|
||||
_, datasets = client.get_datasets(query={"columns": ["id", "table_name", "sql", "database", "columns"]})
|
||||
|
||||
if not datasets:
|
||||
logger.warning("[STATE][search_datasets][EMPTY] No datasets found.")
|
||||
logger.warning("[search_datasets][State] No datasets found.")
|
||||
return None
|
||||
|
||||
pattern = re.compile(search_pattern, re.IGNORECASE)
|
||||
results = {}
|
||||
available_fields = set(datasets[0].keys())
|
||||
|
||||
|
||||
for dataset in datasets:
|
||||
dataset_id = dataset.get('id')
|
||||
if not dataset_id:
|
||||
continue
|
||||
|
||||
matches = []
|
||||
for field in available_fields:
|
||||
value = str(dataset.get(field, ""))
|
||||
if pattern.search(value):
|
||||
match_obj = pattern.search(value)
|
||||
for field, value in dataset.items():
|
||||
value_str = str(value)
|
||||
if pattern.search(value_str):
|
||||
match_obj = pattern.search(value_str)
|
||||
matches.append({
|
||||
"field": field,
|
||||
"match": match_obj.group() if match_obj else "",
|
||||
"value": value
|
||||
"value": value_str
|
||||
})
|
||||
|
||||
if matches:
|
||||
results[dataset_id] = matches
|
||||
|
||||
logger.info(f"[STATE][search_datasets][SUCCESS] Found matches in {len(results)} datasets.")
|
||||
logger.info(f"[search_datasets][Success] Found matches in {len(results)} datasets.")
|
||||
return results
|
||||
|
||||
except re.error as e:
|
||||
logger.error(f"[STATE][search_datasets][FAILURE] Invalid regex pattern: {e}", exc_info=True)
|
||||
logger.error(f"[search_datasets][Failure] Invalid regex pattern: {e}", exc_info=True)
|
||||
raise
|
||||
except (SupersetAPIError, RequestException) as e:
|
||||
logger.critical(f"[STATE][search_datasets][FAILURE] Critical error during search: {e}", exc_info=True)
|
||||
logger.critical(f"[search_datasets][Failure] Critical error during search: {e}", exc_info=True)
|
||||
raise
|
||||
# END_FUNCTION_search_datasets
|
||||
# </ANCHOR id="search_datasets">
|
||||
|
||||
# [ENTITY: Function('print_search_results')]
|
||||
# CONTRACT:
|
||||
# PURPOSE: Форматирует результаты поиска для читаемого вывода в консоль.
|
||||
# PRECONDITIONS:
|
||||
# - `results` является словарем, возвращенным `search_datasets`, или `None`.
|
||||
# POSTCONDITIONS:
|
||||
# - Возвращает отформатированную строку с результатами.
|
||||
# <ANCHOR id="print_search_results" type="Function">
|
||||
# @PURPOSE: Форматирует результаты поиска для читаемого вывода в консоль.
|
||||
# @PRE: `results` является словарем, возвращенным `search_datasets`, или `None`.
|
||||
# @POST: Возвращает отформатированную строку с результатами.
|
||||
# @PARAM: results: Optional[Dict] - Словарь с результатами поиска.
|
||||
# @PARAM: context_lines: int - Количество строк контекста для вывода до и после совпадения.
|
||||
# @RETURN: str - Отформатированный отчет.
|
||||
def print_search_results(results: Optional[Dict], context_lines: int = 3) -> str:
|
||||
if not results:
|
||||
return "Ничего не найдено"
|
||||
@@ -91,46 +91,40 @@ def print_search_results(results: Optional[Dict], context_lines: int = 3) -> str
|
||||
for dataset_id, matches in results.items():
|
||||
output.append(f"\n--- Dataset ID: {dataset_id} ---")
|
||||
for match_info in matches:
|
||||
field = match_info['field']
|
||||
match_text = match_info['match']
|
||||
full_value = match_info['value']
|
||||
|
||||
field, match_text, full_value = match_info['field'], match_info['match'], match_info['value']
|
||||
output.append(f" - Поле: {field}")
|
||||
output.append(f" Совпадение: '{match_text}'")
|
||||
|
||||
lines = full_value.splitlines()
|
||||
if not lines:
|
||||
continue
|
||||
if not lines: continue
|
||||
|
||||
match_line_index = -1
|
||||
for i, line in enumerate(lines):
|
||||
if match_text in line:
|
||||
match_line_index = i
|
||||
break
|
||||
|
||||
|
||||
if match_line_index != -1:
|
||||
start_line = max(0, match_line_index - context_lines)
|
||||
end_line = min(len(lines), match_line_index + context_lines + 1)
|
||||
|
||||
start = max(0, match_line_index - context_lines)
|
||||
end = min(len(lines), match_line_index + context_lines + 1)
|
||||
output.append(" Контекст:")
|
||||
for i in range(start_line, end_line):
|
||||
line_number = i + 1
|
||||
for i in range(start, end):
|
||||
prefix = f"{i + 1:5d}: "
|
||||
line_content = lines[i]
|
||||
prefix = f"{line_number:5d}: "
|
||||
if i == match_line_index:
|
||||
highlighted_line = line_content.replace(match_text, f">>>{match_text}<<<")
|
||||
output.append(f" {prefix}{highlighted_line}")
|
||||
highlighted = line_content.replace(match_text, f">>>{match_text}<<<")
|
||||
output.append(f" {prefix}{highlighted}")
|
||||
else:
|
||||
output.append(f" {prefix}{line_content}")
|
||||
output.append("-" * 25)
|
||||
return "\n".join(output)
|
||||
# END_FUNCTION_print_search_results
|
||||
# </ANCHOR id="print_search_results">
|
||||
|
||||
# [ENTITY: Function('main')]
|
||||
# CONTRACT:
|
||||
# PURPOSE: Основная точка входа скрипта.
|
||||
# PRECONDITIONS: None
|
||||
# POSTCONDITIONS: None
|
||||
# <ANCHOR id="main" type="Function">
|
||||
# @PURPOSE: Основная точка входа для запуска скрипта поиска.
|
||||
# @RELATION: CALLS -> setup_clients
|
||||
# @RELATION: CALLS -> search_datasets
|
||||
# @RELATION: CALLS -> print_search_results
|
||||
def main():
|
||||
logger = SupersetLogger(level=logging.INFO, console=True)
|
||||
clients = setup_clients(logger)
|
||||
@@ -145,8 +139,12 @@ def main():
|
||||
)
|
||||
|
||||
report = print_search_results(results)
|
||||
logger.info(f"[STATE][main][SUCCESS] Search finished. Report:\n{report}")
|
||||
# END_FUNCTION_main
|
||||
logger.info(f"[main][Success] Search finished. Report:\n{report}")
|
||||
# </ANCHOR id="main">
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
# --- Конец кода модуля ---
|
||||
|
||||
# </GRACE_MODULE id="search_script">
|
||||
|
||||
Reference in New Issue
Block a user