Export in .csv
Importing is not supported, since there's no way to represent a Null field in .csv
This commit is contained in:
parent
6f68d971ee
commit
dd724b6c28
@ -253,9 +253,9 @@ if __name__ == "__main__":
|
|||||||
| Operation.ExportCategoryGroups
|
| Operation.ExportCategoryGroups
|
||||||
| Operation.ImportCategoryGroups
|
| Operation.ImportCategoryGroups
|
||||||
):
|
):
|
||||||
keys = {"file"}
|
keys = {"file", "format"}
|
||||||
assert args.keys() >= keys, f"missing {args.keys() - keys}"
|
assert args.keys() >= keys, f"missing {args.keys() - keys}"
|
||||||
|
|
||||||
params = args["file"]
|
params = [args["file"][0], args["format"][0]]
|
||||||
|
|
||||||
Manager(db, verbosity).action(op, params)
|
Manager(db, verbosity).action(op, params)
|
||||||
|
|||||||
@ -63,7 +63,7 @@ def argparser() -> argparse.ArgumentParser:
|
|||||||
# Exports transactions to .csv file
|
# Exports transactions to .csv file
|
||||||
export = subparsers.add_parser("export")
|
export = subparsers.add_parser("export")
|
||||||
export.set_defaults(op=Operation.Export)
|
export.set_defaults(op=Operation.Export)
|
||||||
export_args(export)
|
file_options(export)
|
||||||
|
|
||||||
pimport = subparsers.add_parser("import")
|
pimport = subparsers.add_parser("import")
|
||||||
pimport.set_defaults(op=Operation.Import)
|
pimport.set_defaults(op=Operation.Import)
|
||||||
@ -215,11 +215,11 @@ def bank(parser: argparse.ArgumentParser):
|
|||||||
|
|
||||||
export = commands.add_parser("export")
|
export = commands.add_parser("export")
|
||||||
export.set_defaults(op=Operation.ExportBanks)
|
export.set_defaults(op=Operation.ExportBanks)
|
||||||
export_args(export)
|
file_options(export)
|
||||||
|
|
||||||
pimport = commands.add_parser("import")
|
pimport = commands.add_parser("import")
|
||||||
pimport.set_defaults(op=Operation.ImportBanks)
|
pimport.set_defaults(op=Operation.ImportBanks)
|
||||||
export_args(pimport)
|
file_options(pimport)
|
||||||
|
|
||||||
|
|
||||||
def nordigen(parser: argparse.ArgumentParser):
|
def nordigen(parser: argparse.ArgumentParser):
|
||||||
@ -276,11 +276,11 @@ def category(parser: argparse.ArgumentParser):
|
|||||||
|
|
||||||
export = commands.add_parser("export")
|
export = commands.add_parser("export")
|
||||||
export.set_defaults(op=Operation.ExportCategories)
|
export.set_defaults(op=Operation.ExportCategories)
|
||||||
export_args(export)
|
file_options(export)
|
||||||
|
|
||||||
pimport = commands.add_parser("import")
|
pimport = commands.add_parser("import")
|
||||||
pimport.set_defaults(op=Operation.ImportCategories)
|
pimport.set_defaults(op=Operation.ImportCategories)
|
||||||
export_args(pimport)
|
file_options(pimport)
|
||||||
|
|
||||||
|
|
||||||
def category_group(parser: argparse.ArgumentParser):
|
def category_group(parser: argparse.ArgumentParser):
|
||||||
@ -296,11 +296,11 @@ def category_group(parser: argparse.ArgumentParser):
|
|||||||
|
|
||||||
export = commands.add_parser("export")
|
export = commands.add_parser("export")
|
||||||
export.set_defaults(op=Operation.ExportCategoryGroups)
|
export.set_defaults(op=Operation.ExportCategoryGroups)
|
||||||
export_args(export)
|
file_options(export)
|
||||||
|
|
||||||
pimport = commands.add_parser("import")
|
pimport = commands.add_parser("import")
|
||||||
pimport.set_defaults(op=Operation.ImportCategoryGroups)
|
pimport.set_defaults(op=Operation.ImportCategoryGroups)
|
||||||
export_args(pimport)
|
file_options(pimport)
|
||||||
|
|
||||||
|
|
||||||
def category_rule(parser: argparse.ArgumentParser):
|
def category_rule(parser: argparse.ArgumentParser):
|
||||||
@ -324,11 +324,11 @@ def category_rule(parser: argparse.ArgumentParser):
|
|||||||
|
|
||||||
export = commands.add_parser("export")
|
export = commands.add_parser("export")
|
||||||
export.set_defaults(op=Operation.ExportCategoryRules)
|
export.set_defaults(op=Operation.ExportCategoryRules)
|
||||||
export_args(export)
|
file_options(export)
|
||||||
|
|
||||||
pimport = commands.add_parser("import")
|
pimport = commands.add_parser("import")
|
||||||
pimport.set_defaults(op=Operation.ImportCategoryRules)
|
pimport.set_defaults(op=Operation.ImportCategoryRules)
|
||||||
export_args(pimport)
|
file_options(pimport)
|
||||||
|
|
||||||
|
|
||||||
def tags(parser: argparse.ArgumentParser):
|
def tags(parser: argparse.ArgumentParser):
|
||||||
@ -366,11 +366,11 @@ def tag_rule(parser: argparse.ArgumentParser):
|
|||||||
|
|
||||||
export = commands.add_parser("export")
|
export = commands.add_parser("export")
|
||||||
export.set_defaults(op=Operation.ExportTagRules)
|
export.set_defaults(op=Operation.ExportTagRules)
|
||||||
export_args(export)
|
file_options(export)
|
||||||
|
|
||||||
pimport = commands.add_parser("import")
|
pimport = commands.add_parser("import")
|
||||||
pimport.set_defaults(op=Operation.ImportTagRules)
|
pimport.set_defaults(op=Operation.ImportTagRules)
|
||||||
export_args(pimport)
|
file_options(pimport)
|
||||||
|
|
||||||
|
|
||||||
def rules(parser: argparse.ArgumentParser):
|
def rules(parser: argparse.ArgumentParser):
|
||||||
@ -397,5 +397,6 @@ def link(parser: argparse.ArgumentParser):
|
|||||||
dismantle.add_argument("links", nargs="+", type=int)
|
dismantle.add_argument("links", nargs="+", type=int)
|
||||||
|
|
||||||
|
|
||||||
def export_args(parser: argparse.ArgumentParser):
|
def file_options(parser: argparse.ArgumentParser):
|
||||||
parser.add_argument("file", nargs=1, type=str)
|
parser.add_argument("file", nargs=1, type=str)
|
||||||
|
parser.add_argument("format", nargs=1, default="pickle")
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
import csv
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import pickle
|
import pickle
|
||||||
import webbrowser
|
import webbrowser
|
||||||
@ -241,11 +242,11 @@ class Manager:
|
|||||||
|
|
||||||
case Operation.Export:
|
case Operation.Export:
|
||||||
with self.db.session() as session:
|
with self.db.session() as session:
|
||||||
self.dump(params[0], sorted(session.get(Transaction)))
|
self.dump(params[0], params[1], sorted(session.get(Transaction)))
|
||||||
|
|
||||||
case Operation.Import:
|
case Operation.Import:
|
||||||
transactions = []
|
transactions = []
|
||||||
for row in self.load(params[0]):
|
for row in self.load(params[0], params[1]):
|
||||||
match row["type"]:
|
match row["type"]:
|
||||||
case "bank":
|
case "bank":
|
||||||
transaction = BankTransaction(
|
transaction = BankTransaction(
|
||||||
@ -278,11 +279,11 @@ class Manager:
|
|||||||
|
|
||||||
case Operation.ExportBanks:
|
case Operation.ExportBanks:
|
||||||
with self.db.session() as session:
|
with self.db.session() as session:
|
||||||
self.dump(params[0], session.get(Bank))
|
self.dump(params[0], params[1], session.get(Bank))
|
||||||
|
|
||||||
case Operation.ImportBanks:
|
case Operation.ImportBanks:
|
||||||
banks = []
|
banks = []
|
||||||
for row in self.load(params[0]):
|
for row in self.load(params[0], params[1]):
|
||||||
bank = Bank(row["name"], row["BIC"], row["type"])
|
bank = Bank(row["name"], row["BIC"], row["type"])
|
||||||
if row["nordigen"]:
|
if row["nordigen"]:
|
||||||
bank.nordigen = Nordigen(**row["nordigen"])
|
bank.nordigen = Nordigen(**row["nordigen"])
|
||||||
@ -294,10 +295,10 @@ class Manager:
|
|||||||
|
|
||||||
case Operation.ExportCategoryRules:
|
case Operation.ExportCategoryRules:
|
||||||
with self.db.session() as session:
|
with self.db.session() as session:
|
||||||
self.dump(params[0], session.get(CategoryRule))
|
self.dump(params[0], params[1], session.get(CategoryRule))
|
||||||
|
|
||||||
case Operation.ImportCategoryRules:
|
case Operation.ImportCategoryRules:
|
||||||
rules = [CategoryRule(**row) for row in self.load(params[0])]
|
rules = [CategoryRule(**row) for row in self.load(params[0], params[1])]
|
||||||
|
|
||||||
if self.certify(rules):
|
if self.certify(rules):
|
||||||
with self.db.session() as session:
|
with self.db.session() as session:
|
||||||
@ -305,10 +306,10 @@ class Manager:
|
|||||||
|
|
||||||
case Operation.ExportTagRules:
|
case Operation.ExportTagRules:
|
||||||
with self.db.session() as session:
|
with self.db.session() as session:
|
||||||
self.dump(params[0], session.get(TagRule))
|
self.dump(params[0], params[1], session.get(TagRule))
|
||||||
|
|
||||||
case Operation.ImportTagRules:
|
case Operation.ImportTagRules:
|
||||||
rules = [TagRule(**row) for row in self.load(params[0])]
|
rules = [TagRule(**row) for row in self.load(params[0], params[1])]
|
||||||
|
|
||||||
if self.certify(rules):
|
if self.certify(rules):
|
||||||
with self.db.session() as session:
|
with self.db.session() as session:
|
||||||
@ -316,12 +317,12 @@ class Manager:
|
|||||||
|
|
||||||
case Operation.ExportCategories:
|
case Operation.ExportCategories:
|
||||||
with self.db.session() as session:
|
with self.db.session() as session:
|
||||||
self.dump(params[0], session.get(Category))
|
self.dump(params[0], params[1], session.get(Category))
|
||||||
|
|
||||||
case Operation.ImportCategories:
|
case Operation.ImportCategories:
|
||||||
# rules = [Category(**row) for row in self.load(params[0])]
|
# rules = [Category(**row) for row in self.load(params[0])]
|
||||||
categories = []
|
categories = []
|
||||||
for row in self.load(params[0]):
|
for row in self.load(params[0], params[1]):
|
||||||
category = Category(row["name"], row["group"])
|
category = Category(row["name"], row["group"])
|
||||||
if len(row["rules"]) > 0:
|
if len(row["rules"]) > 0:
|
||||||
# Only category rules could have been created with a rule
|
# Only category rules could have been created with a rule
|
||||||
@ -340,10 +341,12 @@ class Manager:
|
|||||||
|
|
||||||
case Operation.ExportCategoryGroups:
|
case Operation.ExportCategoryGroups:
|
||||||
with self.db.session() as session:
|
with self.db.session() as session:
|
||||||
self.dump(params[0], session.get(CategoryGroup))
|
self.dump(params[0], params[1], session.get(CategoryGroup))
|
||||||
|
|
||||||
case Operation.ImportCategoryGroups:
|
case Operation.ImportCategoryGroups:
|
||||||
groups = [CategoryGroup(**row) for row in self.load(params[0])]
|
groups = [
|
||||||
|
CategoryGroup(**row) for row in self.load(params[0], params[1])
|
||||||
|
]
|
||||||
|
|
||||||
if self.certify(groups):
|
if self.certify(groups):
|
||||||
with self.db.session() as session:
|
with self.db.session() as session:
|
||||||
@ -364,14 +367,26 @@ class Manager:
|
|||||||
return TransactionCategory(category, selector)
|
return TransactionCategory(category, selector)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def dump(fn, sequence):
|
def dump(fn, format, sequence):
|
||||||
with open(fn, "wb") as f:
|
if format == "pickle":
|
||||||
pickle.dump([e.format for e in sequence], f)
|
with open(fn, "wb") as f:
|
||||||
|
pickle.dump([e.format for e in sequence], f)
|
||||||
|
elif format == "csv":
|
||||||
|
with open(fn, "w", newline="") as f:
|
||||||
|
csv.writer(f).writerows([e.format.values() for e in sequence])
|
||||||
|
else:
|
||||||
|
print("format not well specified")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def load(fn):
|
def load(fn, format):
|
||||||
with open(fn, "rb") as f:
|
if format == "pickle":
|
||||||
return pickle.load(f)
|
with open(fn, "rb") as f:
|
||||||
|
return pickle.load(f)
|
||||||
|
elif format == "csv":
|
||||||
|
raise Exception("CSV import not supported")
|
||||||
|
else:
|
||||||
|
print("format not well specified")
|
||||||
|
return []
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def certify(imports: list) -> bool:
|
def certify(imports: list) -> bool:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user