Fixes several FileNotFound at initialization
Added multiple Path.mkdir() when .pfstate, backup, raw and data directories weren't created. Adds try-except clause around functions called in init, so that errors in the early stages are catched to remove already initialized state files. Moves filename definitions to tools using global vars. Removed state.filename = p from main.py introduced when the state filename was changed. `self._save()` removed from `PFState` `__init__` as it's not needed. All setattr already call it.
This commit is contained in:
parent
647af8be5b
commit
51b16c7977
29
main.py
29
main.py
@ -8,9 +8,6 @@ import pfbudget.report as report
|
|||||||
import pfbudget.tools as tools
|
import pfbudget.tools as tools
|
||||||
|
|
||||||
|
|
||||||
p = ".pfbudget/state"
|
|
||||||
|
|
||||||
|
|
||||||
class PfBudgetInitialized(Exception):
|
class PfBudgetInitialized(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -34,7 +31,7 @@ def init(state, args):
|
|||||||
"""
|
"""
|
||||||
if not state:
|
if not state:
|
||||||
s = dict(
|
s = dict(
|
||||||
filename=p,
|
filename=tools.STATE_FILE,
|
||||||
raw_dir=args.raw,
|
raw_dir=args.raw,
|
||||||
data_dir=args.data,
|
data_dir=args.data,
|
||||||
raw_files=[],
|
raw_files=[],
|
||||||
@ -43,8 +40,15 @@ def init(state, args):
|
|||||||
last_backup="",
|
last_backup="",
|
||||||
last_datadir_backup="",
|
last_datadir_backup="",
|
||||||
)
|
)
|
||||||
state = tools.pfstate(p, s)
|
try:
|
||||||
parse(state, args)
|
state = tools.pfstate(tools.STATE_FILE, s)
|
||||||
|
parse(state, args)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
if Path(tools.STATE_FILE).is_file():
|
||||||
|
print(f"Deleting {tools.STATE_FILE}")
|
||||||
|
Path(tools.STATE_FILE).unlink()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise PfBudgetInitialized()
|
raise PfBudgetInitialized()
|
||||||
|
|
||||||
@ -244,17 +248,6 @@ if __name__ == "__main__":
|
|||||||
p_graph.set_defaults(func=graph)
|
p_graph.set_defaults(func=graph)
|
||||||
p_report.set_defaults(func=f_report)
|
p_report.set_defaults(func=f_report)
|
||||||
|
|
||||||
state = tools.pfstate(p)
|
state = tools.pfstate(tools.STATE_FILE)
|
||||||
state.filename = p
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
args.func(state, args)
|
args.func(state, args)
|
||||||
|
|
||||||
# income = [sum(t.value for cat, transactions in months.items() for t in transactions
|
|
||||||
# if cat in get_income_categories()) for months in monthly_transactions_by_cat]
|
|
||||||
|
|
||||||
# expenses = []
|
|
||||||
# for category in expense_categories:
|
|
||||||
# expense_value = [-sum(t.value for t in month[category]) for month in monthly_transactions_by_cat]
|
|
||||||
# expenses.extend(expense_value)
|
|
||||||
|
|
||||||
# print("Income: {}, Expenses: {}, Net = {}"".format(sum(income), sum(expenses), sum(income) - sum(expenses)))
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import csv
|
import csv
|
||||||
import datetime as dt
|
import datetime as dt
|
||||||
|
import os
|
||||||
import pickle
|
import pickle
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
@ -14,15 +15,24 @@ from .transactions import (
|
|||||||
from .parsers import parse_data
|
from .parsers import parse_data
|
||||||
|
|
||||||
|
|
||||||
|
DIR = ".pfbudget/"
|
||||||
|
STATE_FILE = DIR + "state"
|
||||||
|
BACKUP_DIR = DIR + "backup/"
|
||||||
|
|
||||||
|
|
||||||
def get_filename(t: Transaction):
|
def get_filename(t: Transaction):
|
||||||
return "{}_{}.csv".format(t.year, t.bank)
|
return "{}_{}.csv".format(t.year, t.bank)
|
||||||
|
|
||||||
|
|
||||||
class PFState:
|
class PFState:
|
||||||
def __init__(self, filename, *args, **kwargs):
|
def __init__(self, filename: str, *args, **kwargs):
|
||||||
if Path(filename).is_file():
|
if Path(filename).is_file():
|
||||||
raise FileExistsError("PFState already exists")
|
raise FileExistsError("PFState already exists")
|
||||||
|
|
||||||
|
if not Path(filename).parent.is_dir():
|
||||||
|
Path(filename).parent.mkdir(parents=True)
|
||||||
|
(Path(filename).parent / "backup/").mkdir(parents=True)
|
||||||
|
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
for d in args:
|
for d in args:
|
||||||
for k in d:
|
for k in d:
|
||||||
@ -30,7 +40,11 @@ class PFState:
|
|||||||
for k in kwargs:
|
for k in kwargs:
|
||||||
setattr(self, k, kwargs[k])
|
setattr(self, k, kwargs[k])
|
||||||
|
|
||||||
self._save()
|
if not Path(self.raw_dir).is_dir():
|
||||||
|
Path(self.raw_dir).mkdir(parents=True)
|
||||||
|
|
||||||
|
if not Path(self.data_dir).is_dir():
|
||||||
|
Path(self.data_dir).mkdir(parents=True)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def filename(self):
|
def filename(self):
|
||||||
@ -153,7 +167,7 @@ def pfstate(filename, *args, **kwargs):
|
|||||||
def backup(state: PFState):
|
def backup(state: PFState):
|
||||||
transactions = load_transactions(state.data_dir)
|
transactions = load_transactions(state.data_dir)
|
||||||
filename = (
|
filename = (
|
||||||
".pfbudget/backups/"
|
BACKUP_DIR
|
||||||
+ "transactions_"
|
+ "transactions_"
|
||||||
+ dt.datetime.now().strftime("%Y-%m-%d_%Hh%Mm%Ss")
|
+ dt.datetime.now().strftime("%Y-%m-%d_%Hh%Mm%Ss")
|
||||||
+ ".csv"
|
+ ".csv"
|
||||||
@ -164,7 +178,7 @@ def backup(state: PFState):
|
|||||||
|
|
||||||
|
|
||||||
def full_backup(state: PFState):
|
def full_backup(state: PFState):
|
||||||
filename = ".pfbudget/backups/" + dt.datetime.now().strftime("%Y-%m-%d_%Hh%Mm%Ss")
|
filename = BACKUP_DIR + dt.datetime.now().strftime("%Y-%m-%d_%Hh%Mm%Ss")
|
||||||
shutil.copytree(state.data_dir, Path(filename))
|
shutil.copytree(state.data_dir, Path(filename))
|
||||||
|
|
||||||
state.last_datadir_backup = filename
|
state.last_datadir_backup = filename
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user