replace argparse.Namespace plumbing with typed command inputs

This commit is contained in:
Matteo Rosati
2026-04-22 16:03:51 +02:00
parent 8ebab832d5
commit 2962a2e088
15 changed files with 560 additions and 115 deletions
+13 -15
View File
@@ -1,40 +1,38 @@
from argparse import Namespace
from __future__ import annotations
from chromy.chroma_functions import delete_collection, delete_data
from chromy.command_inputs import DeleteCollectionInput, DeleteRecordsInput
def _parse_where_clause(where_clause: str) -> dict[str, str]:
condition, separator, value = where_clause.partition("=")
if separator == "":
raise ValueError("Invalid --where value. Expected <condition>=<value>.")
raise ValueError(
"Invalid --where value. Expected <condition>=<value>.")
condition = condition.strip()
value = value.strip()
if not condition or not value:
raise ValueError("Invalid --where value. Expected <condition>=<value>.")
raise ValueError(
"Invalid --where value. Expected <condition>=<value>.")
return {condition: value}
def handle_delete_collection(args: Namespace) -> int:
delete_collection(args.collection)
print(f"Deleted collection '{args.collection}'.")
def handle_delete_collection(command: DeleteCollectionInput) -> int:
delete_collection(command.collection)
print(f"Deleted collection '{command.collection}'.")
return 0
def handle_delete_records(args: Namespace) -> int:
try:
where = _parse_where_clause(args.where)
except ValueError as exc:
args.error_message = str(exc)
raise
deleted = delete_data(args.collection, where)
def handle_delete_records(command: DeleteRecordsInput) -> int:
where = _parse_where_clause(command.where)
deleted = delete_data(command.collection, where)
condition, value = next(iter(where.items()))
print(
f"Deleted {deleted} record(s) from collection '{args.collection}' "
f"Deleted {deleted} record(s) from collection '{command.collection}' "
f"where {condition}={value}."
)
return 0