13
13
class Ledger ():
14
14
"""Holds all transactions
15
15
"""
16
- def __init__ (self , raw_transactions = None ):
16
+ def __init__ (self , ledger_filename = None , raw_transactions = None ):
17
17
self ._transactions = []
18
18
if raw_transactions is not None :
19
19
self ._transactions = self ._import_raw_transactions (raw_transactions )
20
+ if ledger_filename is not None :
21
+ self ._transactions = self .import_ledger_file (ledger_filename )
20
22
21
23
@property
22
24
def json (self ):
@@ -90,7 +92,7 @@ def _parse_posting(self, string):
90
92
try :
91
93
amount = float (parts [1 ].replace (',' , '.' ))
92
94
except :
93
- print (parts )
95
+ print (string , parts )
94
96
raise
95
97
96
98
# check for tags
@@ -137,6 +139,26 @@ def _import_raw_transactions(self, raw_transactions):
137
139
138
140
return transactions
139
141
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
+
140
162
141
163
class Transaction ():
142
164
"""Represents a transaction and contains at least two postings
@@ -151,9 +173,9 @@ def __init__(self):
151
173
self ._postings = []
152
174
153
175
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
157
179
postings = ''
158
180
for posting in self ._postings :
159
181
postings += f'\t { posting .__repr__ ()} \n '
@@ -178,6 +200,11 @@ def header(self, header):
178
200
179
201
@property
180
202
def date (self ):
203
+ try :
204
+ self ._header ['primary_date' ].isoformat ()
205
+ except :
206
+ print (self .header )
207
+ raise
181
208
return self ._header ['primary_date' ].isoformat ()
182
209
183
210
@property
@@ -249,24 +276,4 @@ def date(self):
249
276
250
277
251
278
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