-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
70 lines (50 loc) · 2.11 KB
/
cli.py
File metadata and controls
70 lines (50 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""CLI for headless fafycat operations."""
import argparse
import json
import sys
from pathlib import Path
from src.fafycat.core.config import AppConfig
from src.fafycat.core.database import DatabaseManager
from src.fafycat.data.csv_processor import CSVProcessor
def cmd_import(args: argparse.Namespace) -> None:
"""Import a CSV file and optionally run ML categorization."""
csv_path = Path(args.file)
if not csv_path.exists():
print(json.dumps({"error": f"File not found: {csv_path}"}))
sys.exit(1)
config = AppConfig()
config.ensure_dirs()
db_manager = DatabaseManager(config)
db_manager.create_tables()
with db_manager.get_session() as session:
processor = CSVProcessor(session)
transactions, errors = processor.import_csv(csv_path)
if errors:
print(json.dumps({"error": f"CSV processing errors: {'; '.join(errors[:5])}"}))
sys.exit(1)
if not transactions:
print(json.dumps({"error": "No valid transactions found in CSV"}))
sys.exit(1)
new_count, duplicate_count = processor.save_transactions(transactions)
# Run ML predictions if available
from api.upload import predict_transaction_categories
cat_summary = predict_transaction_categories(session, transactions, new_count)
result = {
"filename": csv_path.name,
"rows_processed": len(transactions),
"transactions_imported": new_count,
"duplicates_skipped": duplicate_count,
**cat_summary,
}
print(json.dumps(result, indent=2))
def main() -> None:
"""CLI entry point."""
parser = argparse.ArgumentParser(prog="fafycat", description="FafyCat CLI for headless operations")
subparsers = parser.add_subparsers(dest="command", required=True)
import_parser = subparsers.add_parser("import", help="Import transactions from a CSV file")
import_parser.add_argument("file", help="Path to the CSV file")
args = parser.parse_args()
if args.command == "import":
cmd_import(args)
if __name__ == "__main__":
main()