Interactive categorization moved to __main__.py
It didn't make sense to have it inside the manager, it should only be used to process commands and its paramaters.
This commit is contained in:
parent
5235fcdfc3
commit
36e7f84bd9
@ -1,6 +1,49 @@
|
|||||||
import pfbudget
|
import pfbudget
|
||||||
|
|
||||||
|
|
||||||
|
def interactive(manager: pfbudget.Manager):
|
||||||
|
with manager.db.session() as session:
|
||||||
|
categories = session.get(pfbudget.t.Category)
|
||||||
|
print(f"Available categories: {categories}")
|
||||||
|
print(f"Available tags: {session.get(pfbudget.t.Tag)}")
|
||||||
|
transactions = session.get(
|
||||||
|
pfbudget.t.Transaction, ~pfbudget.t.Transaction.category.has()
|
||||||
|
)
|
||||||
|
print(f"{len(transactions)} transactions left to categorize")
|
||||||
|
|
||||||
|
for transaction in sorted(transactions):
|
||||||
|
print(f"{transaction}")
|
||||||
|
quit = False
|
||||||
|
next = True
|
||||||
|
while next:
|
||||||
|
match (input("(<category>/split/tag/note/quit): ")):
|
||||||
|
case "quit" | "exit":
|
||||||
|
next = False
|
||||||
|
quit = True
|
||||||
|
|
||||||
|
case "tag":
|
||||||
|
tag = input("tag: ")
|
||||||
|
transaction.tags.add(pfbudget.t.TransactionTag(tag))
|
||||||
|
|
||||||
|
case "note":
|
||||||
|
note = input("note: ")
|
||||||
|
transaction.note = pfbudget.t.Note(note)
|
||||||
|
|
||||||
|
case other:
|
||||||
|
if other not in [c.name for c in categories]:
|
||||||
|
print(f"{other} is not a valid category")
|
||||||
|
continue
|
||||||
|
|
||||||
|
transaction.category = pfbudget.t.TransactionCategory(
|
||||||
|
other,
|
||||||
|
pfbudget.t.CategorySelector(pfbudget.t.Selector_T.manual),
|
||||||
|
)
|
||||||
|
next = False
|
||||||
|
|
||||||
|
if quit:
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
argparser = pfbudget.argparser()
|
argparser = pfbudget.argparser()
|
||||||
args = vars(argparser.parse_args())
|
args = vars(argparser.parse_args())
|
||||||
@ -16,6 +59,10 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
params = []
|
params = []
|
||||||
match (op):
|
match (op):
|
||||||
|
case pfbudget.Operation.ManualCategorization:
|
||||||
|
interactive(pfbudget.Manager(db, verbosity))
|
||||||
|
exit()
|
||||||
|
|
||||||
case pfbudget.Operation.Parse:
|
case pfbudget.Operation.Parse:
|
||||||
keys = {"path", "bank", "creditcard"}
|
keys = {"path", "bank", "creditcard"}
|
||||||
assert args.keys() >= keys, f"missing {args.keys() - keys}"
|
assert args.keys() >= keys, f"missing {args.keys() - keys}"
|
||||||
|
|||||||
@ -33,21 +33,6 @@ class Categorizer:
|
|||||||
self._rule_based_categories(transactions, categories)
|
self._rule_based_categories(transactions, categories)
|
||||||
self._rule_based_tags(transactions, tags)
|
self._rule_based_tags(transactions, tags)
|
||||||
|
|
||||||
def manual(
|
|
||||||
self,
|
|
||||||
transactions: Sequence[t.Transaction],
|
|
||||||
categories: Sequence[t.Category],
|
|
||||||
tags: Sequence[t.Tag],
|
|
||||||
):
|
|
||||||
"""Manual categorization input
|
|
||||||
|
|
||||||
Args:
|
|
||||||
transactions (Sequence[Transaction]): uncategorized transactions
|
|
||||||
categories (Sequence[Category]): available categories
|
|
||||||
tags (Sequence[Tag]): currently available tags
|
|
||||||
"""
|
|
||||||
self._manual(transactions)
|
|
||||||
|
|
||||||
@Timer(name="nullify")
|
@Timer(name="nullify")
|
||||||
def _nullify(self, transactions: Sequence[t.BankTransaction]):
|
def _nullify(self, transactions: Sequence[t.BankTransaction]):
|
||||||
count = 0
|
count = 0
|
||||||
@ -151,21 +136,3 @@ class Categorizer:
|
|||||||
|
|
||||||
for k, v in d.items():
|
for k, v in d.items():
|
||||||
print(f"{v}: {k}")
|
print(f"{v}: {k}")
|
||||||
|
|
||||||
def _manual(self, transactions: Sequence[t.Transaction]):
|
|
||||||
uncategorized = [t for t in transactions if not t.category]
|
|
||||||
print(f"{len(uncategorized)} transactions left to categorize")
|
|
||||||
|
|
||||||
for transaction in uncategorized:
|
|
||||||
while True:
|
|
||||||
category = input(f"{transaction} category: ")
|
|
||||||
if category == "quit":
|
|
||||||
return
|
|
||||||
if not category:
|
|
||||||
print("{category} doesn't exist")
|
|
||||||
continue
|
|
||||||
transaction.category = t.TransactionCategory(
|
|
||||||
category, t.CategorySelector(t.Selector_T.manual)
|
|
||||||
)
|
|
||||||
|
|
||||||
break
|
|
||||||
|
|||||||
@ -88,15 +88,6 @@ class Manager:
|
|||||||
tags = session.get(Tag)
|
tags = session.get(Tag)
|
||||||
Categorizer().rules(uncategorized, categories, tags)
|
Categorizer().rules(uncategorized, categories, tags)
|
||||||
|
|
||||||
case Operation.ManualCategorization:
|
|
||||||
with self.db.session() as session:
|
|
||||||
uncategorized = session.get(
|
|
||||||
Transaction, ~Transaction.category.has()
|
|
||||||
)
|
|
||||||
categories = session.get(Category)
|
|
||||||
tags = session.get(Tag)
|
|
||||||
Categorizer().manual(uncategorized, categories, tags)
|
|
||||||
|
|
||||||
case Operation.BankMod:
|
case Operation.BankMod:
|
||||||
with self.db.session() as session:
|
with self.db.session() as session:
|
||||||
session.update(Bank, params)
|
session.update(Bank, params)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user