File: sberbank.py

package info (click to toggle)
ofxstatement-plugins 20161204
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,160 kB
  • ctags: 1,619
  • sloc: python: 4,374; xml: 292; makefile: 103
file content (195 lines) | stat: -rw-r--r-- 6,680 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#    SberBank (http://sbrf.ru) plugin for ofxstatement
#
#    Copyright 2013 Andrey Lebedev <andrey@lebedev.lt>
#    Copyright 2016 Alexander Gerasiov <gq@cs.msu.su>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License version 3 as
#    published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

from ofxstatement.parser import StatementParser
from ofxstatement.plugin import Plugin
from ofxstatement import statement
from datetime import datetime
import re

#file format options
sb_encoding='cp1251'

class ParserState:
    name = ""
    matchers = None

    def __init__(self, name, parser):
        self.name = name
        self.matchers = list()
        parser.append(self)

    def addMatcher(self, reString, nextState = None, function = None):
        self.matchers.append([re.compile(reString), nextState, function])

    def run(self, line):
        for (matcher, nextState, function) in self.matchers:
            match = matcher.match(line)
            if match:
                if function:
                    function(match)
                return nextState
        return None

    def __str__(self):
        string = "State '%s' matchers:\n"%self.name
        for matcher in self.matchers:
            string += "re '%s' => state '%s'\n"%(matcher[0], matcher[1])
        return string

class SberBankStatementParser(StatementParser):

    statement = None

    transaction = None
    account_id = ""
    account_fl_len = 0

    machine = {}
    currentState = None
    statement = None
    internal = None

    def append(self, state):
        self.machine[state.name] = state

    def extractCurrency(self, match):
        if not self.statement.currency:
            self.statement.currency = match.group(1)

    def extractBeginBalance(self, match):
        if not self.statement.start_balance:
            self.statement.start_balance = float(match.group(1))

    def extractEndBalance(self, match):
        if not self.statement.end_balance:
            self.statement.end_balance = float(match.group(1))
        if not self.statement.account_id:
            self.statement.account_id = " ".join(self.account_id.split())
        if self.transaction:
            self.transaction.memo = " ".join(self.transaction.memo.split())
            self.statement.lines.append(self.transaction)
            self.transaction = None

    def parseDate(self, string):
        rusMonths = {
            u'ЯНВ': 1,
            u'ФЕВ': 2,
            u'МАР': 3,
            u'АПР': 4,
            u'МАЙ': 5,
            u'ИЮН': 6,
            u'ИЮЛ': 7,
            u'АВГ': 8,
            u'СЕН': 9,
            u'ОКТ': 10,
            u'НОЯ': 11,
            u'ДЕК': 12,
        }
        return datetime(2000 + int(string[5:]), rusMonths[string[2:5]], int(string[:2]))

    def extractTransaction(self, match):
        if self.transaction:
            self.transaction.memo = " ".join(self.transaction.memo.split())
            self.statement.lines.append(self.transaction)

        self.transaction = statement.StatementLine()

        self.account_fl_len = len(match.group(1))

        self.transaction.date = self.parseDate(match.group(3))
        self.transaction.memo = match.group(4)
        self.transaction.amount = float(match.group(5))*(1 if match.group(6) else -1)
        self.transaction.trntype = 'DEBIT' if match.group(6) else 'CREDIT'
        if match.group(1).strip():
            self.account_id += match.group(1)

    def extractTransactionAppend(self, match):
        first = match.group(1)[:self.account_fl_len]
        second = match.group(1)[self.account_fl_len:]
        self.account_id += first
        self.transaction.memo += second.strip()

    def __init__(self, fin):
        self.statement = statement.Statement()
        self.internal = {}
        self.fin = fin

        self.currentState = 'init'

        state = ParserState('init', self)
        state.addMatcher(u"^.*ВАЛЮТА СЧЕТА.*$", 'currency')

        state = ParserState('currency', self)
        state.addMatcher("^\s*(\w{3})\s*$",
                'begin_balance',
                self.extractCurrency)

        state = ParserState('begin_balance', self)
        state.addMatcher(u"^ОСТАТОК НА НАЧАЛО ПЕРИОДА:\s*(\d+\.\d{2})(\+)?\s*$",
                'table_header',
                self.extractBeginBalance)

        state = ParserState('table_header', self)
        state.addMatcher("^[-+]{80,}$", 'table_header2')

        state = ParserState('table_header2', self)
        state.addMatcher("^[-+]{80,}$", 'transaction')

        state = ParserState('transaction', self)
        state.addMatcher("^[-+]{80,}$", 'end_balance')
        state.addMatcher(
                u"^(.*)\s*(\d{2}[А-Я]{3})\s+(\d{2}[А-Я]{3}\d{2})\s+\d{6}\s+(.*)\s\w{3}\s+\d*\.\d{2}\s+(\d*\.\d{2})(CR)?\s*$",
                None,
                self.extractTransaction)
        state.addMatcher(
                u"^(.*)\s*(\d{2}[А-Я]{3})\s+(\d{2}[А-Я]{3}\d{2})\s+\d{6}\s+(КОМИССИЯ)\s+(\d*\.\d{2})(CR)?\s*$",
                None,
                self.extractTransaction)
        state.addMatcher(u".*ИТОГО ПО.*")
        state.addMatcher(u"^(.+)\s*$",
                None,
                self.extractTransactionAppend)

        state = ParserState('end_balance', self)
        state.addMatcher(u"^ОСТАТОК НА КОНЕЦ ПЕРИОДА:\s*(\d+\.\d{2})\+\s*$",
                'table_header',
                self.extractEndBalance)

    def run(self, line):
        nextState = self.machine[self.currentState].run(line)
        if nextState:
            self.currentState = nextState


    def parse(self):
        for line in self.fin:
            self.run(line)

        return self.statement

class SberBankPlugin(Plugin):
    """ SberBank TXT (http://sbrf.ru)
    """

    def get_parser(self, fin):
        f = open(fin, 'r', encoding=sb_encoding)
        parser = SberBankStatementParser(f)
        parser.statement.currency = self.settings.get('currency', None)
        parser.statement.account_id = self.settings.get('account', None)
        parser.statement.bank_id = self.settings.get('bank', 'SberBank')
        return parser