Files
Chromy/chromy/handlers/import_data.py
T

132 lines
3.6 KiB
Python
Raw Normal View History

from __future__ import annotations
2026-04-24 18:20:22 +02:00
2026-04-23 19:56:11 +02:00
import os
import sys
2026-04-23 19:56:11 +02:00
from pathlib import Path
2026-04-29 15:39:42 +02:00
from typing import Final
2026-04-21 17:42:37 +02:00
2026-04-23 21:49:46 +02:00
from rich import print
from rich.progress import (
BarColumn,
MofNCompleteColumn,
Progress,
SpinnerColumn,
TextColumn,
)
2026-04-24 18:20:22 +02:00
2026-04-29 12:44:28 +02:00
from chromy.errors import UnsupportedTextFileError
2026-04-22 15:47:46 +02:00
from chromy.utilities import ingest_file
2026-04-21 17:42:37 +02:00
2026-04-24 18:40:51 +02:00
from ..utilities import is_probably_text_file
2026-04-29 15:39:42 +02:00
SUCCESS_EXIT_CODE: Final = 0
FAILURE_EXIT_CODE: Final = 1
2026-04-21 17:42:37 +02:00
2026-04-23 20:46:26 +02:00
def _get_absolute_path(file: str) -> str:
"""
A helper method that, given a valid relative path to a file, returns its
absolute path.
Args:
file (str): The relative path to the file.
Raises:
FileNotFoundError(): If the file does not exist.
"""
2026-04-23 19:56:11 +02:00
if not os.path.exists(file):
raise FileNotFoundError()
file_path = Path(file)
2026-04-24 18:20:22 +02:00
return str(file_path.resolve())
2026-04-23 19:56:11 +02:00
2026-04-23 20:46:26 +02:00
2026-04-29 15:39:42 +02:00
def _import_one(collection: str, file: str) -> int:
2026-04-24 18:40:51 +02:00
absolute_path = _get_absolute_path(file)
2026-04-29 15:39:42 +02:00
if not Path(absolute_path).is_file():
raise FileNotFoundError()
2026-04-24 18:40:51 +02:00
if not is_probably_text_file(absolute_path):
2026-04-29 12:44:28 +02:00
raise UnsupportedTextFileError()
2026-04-24 18:40:51 +02:00
return ingest_file(collection, absolute_path)
def _should_show_progress(file_count: int) -> bool:
return file_count > 1 and sys.stdout.isatty()
def _truncate_file_name(file_name: str, max_length: int = 20) -> str:
if len(file_name) <= max_length:
return file_name
return f"{file_name[: max_length - 3]}"
2026-04-29 15:39:42 +02:00
def handle_import(collection: str, files: list[str]) -> int:
successful_imports = 0
failed_imports = 0
seen_paths: set[str] = set()
unique_files: list[str] = []
2026-04-29 15:39:42 +02:00
for file in files:
try:
absolute_path = _get_absolute_path(file)
except FileNotFoundError:
unique_files.append(file)
continue
if absolute_path in seen_paths:
continue
seen_paths.add(absolute_path)
unique_files.append(file)
show_progress = _should_show_progress(len(unique_files))
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
MofNCompleteColumn(),
transient=True,
disable=not show_progress,
) as progress:
task_id = progress.add_task("Importing files...", total=len(unique_files))
for file in unique_files:
file_name = _truncate_file_name(Path(file).name)
description = f"Importing [bold]{file_name}[/]..."
progress.update(task_id, description=description)
try:
records_added = _import_one(collection, file)
successful_imports += 1
if not show_progress:
progress.console.print(
"[bold green]Added[/] "
f"{records_added} records from '{file}' to "
f"collection '{collection}'."
)
except FileNotFoundError:
failed_imports += 1
progress.console.print(
f"[bold red]Error[/]: The file '{file}' was not found."
)
except UnsupportedTextFileError:
failed_imports += 1
progress.console.print(
f"[bold red]Error[/]: The file '{file}' is not a text file."
)
finally:
progress.advance(task_id)
2026-04-29 15:39:42 +02:00
print(
f"Imported {successful_imports} file(s) successfully; {failed_imports} failed."
)
if failed_imports:
return FAILURE_EXIT_CODE
return SUCCESS_EXIT_CODE