The Manager doesn't need to know that it was called from the CLI, so it now is initialized with the database and performs an action, based on the operation it receives and its parameters. The work isn't finished, some Manager actions are still based on the CLI arguments. The CLI logic and creation of parameters to pass to the manager have been moved to the __main__.py file, which brings it to line to the program being called as a package from the command line.
77 lines
2.6 KiB
Python
77 lines
2.6 KiB
Python
import pfbudget
|
|
|
|
|
|
if __name__ == "__main__":
|
|
argparser = pfbudget.argparser()
|
|
args = vars(argparser.parse_args())
|
|
|
|
assert "op" in args, "No pfbudget.Operation selected"
|
|
op: pfbudget.Operation = args["op"]
|
|
|
|
assert "database" in args, "No database selected"
|
|
db = args["database"]
|
|
|
|
params = None
|
|
match (op):
|
|
case pfbudget.Operation.CategoryAdd:
|
|
assert args.keys() >= {"category", "group"}, "argparser ill defined"
|
|
params = [
|
|
pfbudget.types.Category(cat, args["group"][0])
|
|
for cat in args["category"]
|
|
]
|
|
|
|
case pfbudget.Operation.CategoryUpdate:
|
|
assert args.keys() >= {"category", "group"}, "argparser ill defined"
|
|
params = [pfbudget.types.Category(cat) for cat in args["category"]]
|
|
params.append(args["group"][0])
|
|
|
|
case pfbudget.Operation.CategoryRemove:
|
|
assert "category" in args, "argparser ill defined"
|
|
params = [pfbudget.types.Category(cat) for cat in args["category"]]
|
|
|
|
case pfbudget.Operation.CategorySchedule:
|
|
assert args.keys() >= {
|
|
"category",
|
|
"period",
|
|
"frequency",
|
|
}, "argparser ill defined"
|
|
|
|
params = [
|
|
pfbudget.types.CategorySchedule(
|
|
cat, True, args["period"][0], args["frequency"][0]
|
|
)
|
|
for cat in args["category"]
|
|
]
|
|
|
|
case pfbudget.Operation.CategoryRule:
|
|
assert args.keys() >= {
|
|
"category",
|
|
"date",
|
|
"description",
|
|
"bank",
|
|
"min",
|
|
"max",
|
|
}, "argparser ill defined"
|
|
|
|
params = [
|
|
pfbudget.types.CategoryRule(
|
|
cat,
|
|
args["date"][0] if args["date"] else None,
|
|
args["description"][0] if args["description"] else None,
|
|
args["bank"][0] if args["bank"] else None,
|
|
args["min"][0] if args["min"] else None,
|
|
args["max"][0] if args["max"] else None,
|
|
)
|
|
for cat in args["category"]
|
|
]
|
|
|
|
case pfbudget.Operation.GroupAdd:
|
|
assert "group" in args, "argparser ill defined"
|
|
params = [pfbudget.types.CategoryGroup(group) for group in args["group"]]
|
|
|
|
case pfbudget.Operation.GroupRemove:
|
|
assert "group" in args, "argparser ill defined"
|
|
params = [pfbudget.types.CategoryGroup(group) for group in args["group"]]
|
|
|
|
pfbudget.Manager(db, args).action(op, params)
|