Skip to content

Commit dac7bec

Browse files
committed
Refactoring a little bit
1 parent e607c2e commit dac7bec

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

ledgertools/read.py

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
class Ledger():
1414
"""Holds all transactions
1515
"""
16-
def __init__(self, raw_transactions=None):
16+
def __init__(self, ledger_filename=None, raw_transactions=None):
1717
self._transactions = []
1818
if raw_transactions is not None:
1919
self._transactions = self._import_raw_transactions(raw_transactions)
20+
if ledger_filename is not None:
21+
self._transactions = self.import_ledger_file(ledger_filename)
2022

2123
@property
2224
def json(self):
@@ -90,7 +92,7 @@ def _parse_posting(self, string):
9092
try:
9193
amount = float(parts[1].replace(',', '.'))
9294
except:
93-
print(parts)
95+
print(string, parts)
9496
raise
9597

9698
# check for tags
@@ -137,6 +139,26 @@ def _import_raw_transactions(self, raw_transactions):
137139

138140
return transactions
139141

142+
def import_ledger_file(self, filename):
143+
with open(filename, 'r') as in_file:
144+
lines = in_file.readlines()
145+
146+
# remove newline at EOL
147+
# remove global comments
148+
# strip whitespace
149+
lines = [l.replace('\n', '').strip() for l in lines if not l[0].strip() in [';', 'Y']]
150+
151+
transactions = []
152+
group = []
153+
for line in lines:
154+
if not line.strip(): # .strip handles lines with only spaces as chars
155+
transactions.append(group)
156+
group = []
157+
else:
158+
group.append(line)
159+
transactions = [g for g in transactions if g]
160+
return self._import_raw_transactions(transactions)
161+
140162

141163
class Transaction():
142164
"""Represents a transaction and contains at least two postings
@@ -151,9 +173,9 @@ def __init__(self):
151173
self._postings = []
152174

153175
def __str__(self):
154-
header = f'{self._header["date"]} {self._header["status"]} ({self._header["code"]}) {self._header["description"]}'
155-
h_tags = ', '.join([f'{k}:{v}' for k,v in self._header['tags'].items()])
156-
h_comment = self._header["comment"]
176+
header = f'{self.date} {self.status} ({self.code}) {self.description}'
177+
h_tags = ', '.join([f'{k}:{v}' for k,v in self.tags.items()])
178+
h_comment = self.comment
157179
postings = ''
158180
for posting in self._postings:
159181
postings += f'\t{posting.__repr__()}\n'
@@ -178,6 +200,11 @@ def header(self, header):
178200

179201
@property
180202
def date(self):
203+
try:
204+
self._header['primary_date'].isoformat()
205+
except:
206+
print(self.header)
207+
raise
181208
return self._header['primary_date'].isoformat()
182209

183210
@property
@@ -249,24 +276,4 @@ def date(self):
249276

250277

251278
def read_file(in_file):
252-
with open(in_file, 'r') as in_file:
253-
lines = in_file.readlines()
254-
255-
# remove newline at EOL
256-
# remove global comments
257-
# strip whitespace
258-
lines = [l.replace('\n', '').strip() for l in lines if not l[0].strip() in [';', 'Y']]
259-
260-
transactions = []
261-
group = []
262-
for line in lines:
263-
if not line.strip(): # .strip handles lines with only spaces as chars
264-
transactions.append(group)
265-
group = []
266-
else:
267-
group.append(line)
268-
transactions = [g for g in transactions if g]
269-
270-
ledger = Ledger(transactions)
271-
272-
return ledger.json
279+
return Ledger(ledger_filename=in_file).json

0 commit comments

Comments
 (0)