2026-04-22 16:03:51 +02:00
|
|
|
from __future__ import annotations
|
2026-04-23 19:56:11 +02:00
|
|
|
import os
|
|
|
|
|
from pathlib import Path
|
2026-04-21 17:42:37 +02:00
|
|
|
|
2026-04-23 21:49:46 +02:00
|
|
|
from rich import print
|
2026-04-22 15:47:46 +02:00
|
|
|
from chromy.utilities import ingest_file
|
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-23 20:46:26 +02:00
|
|
|
return str(file_path.resolve(file_path))
|
2026-04-23 19:56:11 +02:00
|
|
|
|
2026-04-23 20:46:26 +02:00
|
|
|
|
|
|
|
|
def handle_import(collection: str, file: str) -> int:
|
|
|
|
|
records_added = ingest_file(collection, _get_absolute_path(file))
|
2026-04-23 21:49:46 +02:00
|
|
|
print(
|
|
|
|
|
f"[bold green]Added[/] {records_added} records to collection '{collection}'.")
|
2026-04-21 17:42:37 +02:00
|
|
|
return 0
|