aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMalte Brandy2019-10-12 20:34:25 +0200
committerMalte Brandy2019-10-28 23:52:21 +0100
commit84d2ce6abc6137a01ea64cf40bf328cd27fbb980 (patch)
treedd15639eda3af11e59acbaf9cbb215c8856a6e50
parentc9cf194490ea19d131ddb01523d9aa3dd77d7dbc (diff)
downloadjali-84d2ce6abc6137a01ea64cf40bf328cd27fbb980.tar.gz
jali-84d2ce6abc6137a01ea64cf40bf328cd27fbb980.zip
Reformat
-rw-r--r--jali/accounting.py67
1 files changed, 33 insertions, 34 deletions
diff --git a/jali/accounting.py b/jali/accounting.py
index 598bcb5..d0b39f5 100644
--- a/jali/accounting.py
+++ b/jali/accounting.py
@@ -101,8 +101,7 @@ class Account:
101 self.bank = bank 101 self.bank = bank
102 self.accounting = accounting 102 self.accounting = accounting
103 self.ctxfilename = os.path.join( 103 self.ctxfilename = os.path.join(
104 jali.config.TMP_DIR, 104 jali.config.TMP_DIR, "jali-{bank_name}-{account_name}.ctx".format(
105 "jali-{bank_name}-{account_name}.ctx".format(
106 bank_name=self.bank.config["name"], 105 bank_name=self.bank.config["name"],
107 account_name=self.config["name"])) 106 account_name=self.config["name"]))
108 self.csvfilename = os.path.join( 107 self.csvfilename = os.path.join(
@@ -116,15 +115,15 @@ class Account:
116 """Calls aqbanking to create csv file from self.ctxfilename and save it to self.csvfilename. 115 """Calls aqbanking to create csv file from self.ctxfilename and save it to self.csvfilename.
117 """ 116 """
118 try: 117 try:
119 subprocess.check_output( 118 subprocess.check_output([
120 [ 119 "aqbanking-cli", "listtrans", "-c", self.ctxfilename, "-o",
121 "aqbanking-cli", "listtrans", "-c", self.ctxfilename, "-o", 120 self.csvfilename, "--exporter=csv"
122 self.csvfilename, "--exporter=csv" 121 ],
123 ], 122 stderr=subprocess.STDOUT)
124 stderr=subprocess.STDOUT)
125 except subprocess.CalledProcessError as e: 123 except subprocess.CalledProcessError as e:
126 print( 124 print(
127 "Generating csv failed. This is the aqbanking output (if any):") 125 "Generating csv failed. This is the aqbanking output (if any):"
126 )
128 print(e.output.decode()) 127 print(e.output.decode())
129 128
130 def get_ctx(self): 129 def get_ctx(self):
@@ -177,8 +176,8 @@ class Account:
177 logging.debug("CSV Transaction:") 176 logging.debug("CSV Transaction:")
178 for item in row.items(): 177 for item in row.items():
179 logging.debug("{}: {}".format(*item)) 178 logging.debug("{}: {}".format(*item))
180 raw_transaction = jali.from_csv.from_csv[self.bank.config[ 179 raw_transaction = jali.from_csv.from_csv[
181 "locale"]](row) 180 self.bank.config["locale"]](row)
182 logging.debug("Resulting Transaction Dict:") 181 logging.debug("Resulting Transaction Dict:")
183 for item in raw_transaction.items(): 182 for item in raw_transaction.items():
184 logging.debug("{}: {}".format(*item)) 183 logging.debug("{}: {}".format(*item))
@@ -189,8 +188,7 @@ class Account:
189 # But the filedescriptor will already be gone by then. 188 # But the filedescriptor will already be gone by then.
190 transactions = [ 189 transactions = [
191 convert(row) 190 convert(row)
192 for row in csv.DictReader( 191 for row in csv.DictReader(csvfile, dialect='aqbanking')
193 csvfile, dialect='aqbanking')
194 ] 192 ]
195 return transactions 193 return transactions
196 194
@@ -217,9 +215,8 @@ def get_hash_from_raw_transaction(transaction):
217 215
218 Return type: string (16 chars hexadecimal) 216 Return type: string (16 chars hexadecimal)
219 """ 217 """
220 return hashlib.sha256(''.join(':'.join(transaction[key] 218 return hashlib.sha256(''.join(':'.join(transaction[key] for key in sorted(
221 for key in sorted(transaction.keys())) 219 transaction.keys())).split()).encode('utf8')).hexdigest()[:16]
222 .split()).encode('utf8')).hexdigest()[:16]
223 220
224 221
225class Accounting: 222class Accounting:
@@ -249,14 +246,13 @@ class Accounting:
249 """ 246 """
250 if "files" in self.config: 247 if "files" in self.config:
251 files = [ 248 files = [
252 filename 249 filename for pattern in self.config["files"]
253 for pattern in self.config["files"]
254 for filename in glob.glob(pattern) 250 for filename in glob.glob(pattern)
255 ] 251 ]
256 if len(files) is 0: 252 if len(files) is 0:
257 logging.warning( 253 logging.warning(
258 "None of your configured ledger files {files} could be found.". 254 "None of your configured ledger files {files} could be found."
259 format(files=self.config["files"])) 255 .format(files=self.config["files"]))
260 else: 256 else:
261 files = [] 257 files = []
262 logging.warning( 258 logging.warning(
@@ -290,7 +286,8 @@ class Accounting:
290 # Create rule dict. 286 # Create rule dict.
291 rule = { 287 rule = {
292 'match': {}, 288 'match': {},
293 'rule': [posting.to_dict() for posting in transaction.postings[1:]] 289 'rule':
290 [posting.to_dict() for posting in transaction.postings[1:]]
294 } 291 }
295 for tag in self.config["auto_rule_tags"]: 292 for tag in self.config["auto_rule_tags"]:
296 if tag in transaction.postings[0].tags: 293 if tag in transaction.postings[0].tags:
@@ -303,8 +300,8 @@ class Accounting:
303 rules.append(rule) 300 rules.append(rule)
304 rule_dir = os.path.dirname(self.config["rules"]) 301 rule_dir = os.path.dirname(self.config["rules"])
305 if not os.path.exists(rule_dir): 302 if not os.path.exists(rule_dir):
306 logging.warning("Creating directory {} to save rules.".format( 303 logging.warning(
307 rule_dir)) 304 "Creating directory {} to save rules.".format(rule_dir))
308 os.makedirs(rule_dir) 305 os.makedirs(rule_dir)
309 with open(self.config["rules"], 'w') as rulefile: 306 with open(self.config["rules"], 'w') as rulefile:
310 json.dump( 307 json.dump(
@@ -331,8 +328,8 @@ class Accounting:
331 328
332 def get_filtered_accounts(self, bank_name="", bank_account_name=""): 329 def get_filtered_accounts(self, bank_name="", bank_account_name=""):
333 return (bank_account for bank_account in self.bank_accounts 330 return (bank_account for bank_account in self.bank_accounts
334 if bank_name in bank_account.bank.config["name"] and 331 if bank_name in bank_account.bank.config["name"]
335 bank_account_name in bank_account.config["name"]) 332 and bank_account_name in bank_account.config["name"])
336 333
337 def clean(self, bank_name="", bank_account_name=""): 334 def clean(self, bank_name="", bank_account_name=""):
338 for bank_account in self.get_filtered_accounts(bank_name, 335 for bank_account in self.get_filtered_accounts(bank_name,
@@ -346,8 +343,8 @@ class Accounting:
346 print("Downloading transactions for account {} at {}".format( 343 print("Downloading transactions for account {} at {}".format(
347 bank_account.config["name"], bank_account.bank.config["name"])) 344 bank_account.config["name"], bank_account.bank.config["name"]))
348 bank_account.sync() 345 bank_account.sync()
349 print('All requested accounts for {} synchronized.'.format(self.config[ 346 print('All requested accounts for {} synchronized.'.format(
350 "name"])) 347 self.config["name"]))
351 348
352 def pretty_print(self): 349 def pretty_print(self):
353 print("Pretty printing …") 350 print("Pretty printing …")
@@ -363,11 +360,11 @@ class Accounting:
363 origin = False 360 origin = False
364 if use_git: 361 if use_git:
365 try: 362 try:
366 repo = git.Repo(self.config["ledger_dir"], search_parent_directories=True) 363 repo = git.Repo(
364 self.config["ledger_dir"], search_parent_directories=True)
367 if repo.is_dirty(index=True, working_tree=False): 365 if repo.is_dirty(index=True, working_tree=False):
368 print( 366 print(
369 "You have uncommited but staged changes in your repo." 367 "You have uncommited but staged changes in your repo.")
370 )
371 sys.exit(1) 368 sys.exit(1)
372 try: 369 try:
373 origin = repo.remote("origin") 370 origin = repo.remote("origin")
@@ -386,10 +383,12 @@ class Accounting:
386 hash = get_hash_from_raw_transaction(raw_transaction) 383 hash = get_hash_from_raw_transaction(raw_transaction)
387 if hash not in known_hashes: 384 if hash not in known_hashes:
388 raw_transaction["HASH"] = hash 385 raw_transaction["HASH"] = hash
389 raw_transactions.append((raw_transaction, bank_account)) 386 raw_transactions.append((raw_transaction,
387 bank_account))
390 except FileNotFoundError: 388 except FileNotFoundError:
391 print( 389 print(
392 "The csv file could not be found. Did you run 'jali sync'?") 390 "The csv file could not be found. Did you run 'jali sync'?"
391 )
393 logging.warning("Missing file: {}".format( 392 logging.warning("Missing file: {}".format(
394 bank_account.csvfilename)) 393 bank_account.csvfilename))
395 if auto: 394 if auto:
@@ -398,8 +397,8 @@ class Accounting:
398 matches = 0 397 matches = 0
399 fails = 0 398 fails = 0
400 should_push = False 399 should_push = False
401 for (index, 400 for (index, (raw_transaction,
402 (raw_transaction, bank_account)) in enumerate(raw_transactions): 401 bank_account)) in enumerate(raw_transactions):
403 transaction = None 402 transaction = None
404 dialog = jali.dialog.TransactionCreationDialog( 403 dialog = jali.dialog.TransactionCreationDialog(
405 raw_transaction, bank_account.config["account_name"], self) 404 raw_transaction, bank_account.config["account_name"], self)