Browse Source

Make parser state logic a bit more bearable

Alois Mahdal 5 years ago
parent
commit
53ab4ffc74
1 changed files with 18 additions and 19 deletions
  1. 18
    19
      gcconv.py

+ 18
- 19
gcconv.py View File

1
 # coding=utf-8
1
 # coding=utf-8
2
 """Convert Bank statement CSV to Gnucash-acceptable format"""
2
 """Convert Bank statement CSV to Gnucash-acceptable format"""
3
 
3
 
4
+from collections import namedtuple
5
+
6
+ParserState = namedtuple('ParserState', 'ths nxt')
7
+
4
 
8
 
5
 def valid_formats():
9
 def valid_formats():
6
     return _parsers.keys()
10
     return _parsers.keys()
53
 
57
 
54
     def __init__(self):
58
     def __init__(self):
55
         self.transactions = []
59
         self.transactions = []
56
-        self.state = False
57
-        self.next_state = False
60
+        self.state = ParserState(ths=False, nxt=False)
58
 
61
 
59
     def is_open(self):
62
     def is_open(self):
60
-        return self.state
63
+        return self.state.ths
61
 
64
 
62
     def is_closed(self):
65
     def is_closed(self):
63
-        return not self.state
66
+        return not self.state.ths
64
 
67
 
65
     def parse_file(self, fpath, encoding='utf-8'):
68
     def parse_file(self, fpath, encoding='utf-8'):
66
         lines_read = 0
69
         lines_read = 0
68
             for line in f.readlines():
71
             for line in f.readlines():
69
                 line = line.decode(encoding).strip()
72
                 line = line.decode(encoding).strip()
70
                 lines_read += 1
73
                 lines_read += 1
71
-                self.state = self.pick_state(line, lineno=lines_read)
74
+                self.state = self.advance(line, lineno=lines_read)
72
                 if self.is_open():
75
                 if self.is_open():
73
                     self.transactions.append(self.parse_line(line).to_gc())
76
                     self.transactions.append(self.parse_line(line).to_gc())
74
 
77
 
153
     def parse_line(self, line):
156
     def parse_line(self, line):
154
         return MbankTransaction(line)
157
         return MbankTransaction(line)
155
 
158
 
156
-    def pick_state(self, line, lineno=None):
159
+    def advance(self, line, lineno=None):
157
         """Choose parser state according to current line and line no."""
160
         """Choose parser state according to current line and line no."""
158
-        if self.next_state:
159
-            self.next_state = False
160
-            return True
161
+        if self.state.nxt:
162
+            return ParserState(ths=True, nxt=False)
161
         if self.is_closed() and line.startswith(u'#Datum uskute'):
163
         if self.is_closed() and line.startswith(u'#Datum uskute'):
162
-            self.next_state = True
163
-            return False
164
+            return ParserState(ths=False, nxt=True)
164
         if self.is_open() and not line:
165
         if self.is_open() and not line:
165
-            return False
166
+            return ParserState(ths=False, nxt=False)
166
         return self.state
167
         return self.state
167
 
168
 
168
 
169
 
252
     def parse_line(self, line):
253
     def parse_line(self, line):
253
         return FioTransaction(line)
254
         return FioTransaction(line)
254
 
255
 
255
-    def pick_state(self, line, lineno=None):
256
+    def advance(self, line, lineno=None):
256
         """Choose parser state according to current line and line no."""
257
         """Choose parser state according to current line and line no."""
257
-        if self.next_state:
258
-            self.next_state = False
259
-            return True
260
-        if self.is_closed() and line.startswith(u'"ID operace";"Datum";'):
261
-            self.next_state = True
262
-            return False
258
+        if self.state.nxt:
259
+            return ParserState(ths=True, nxt=False)
260
+        if self.is_open() and line.startswith(u'"ID operace";"Datum";'):
261
+            return ParserState(ths=False, nxt=True)
263
         return self.state
262
         return self.state
264
 
263
 
265
 
264