summaryrefslogtreecommitdiff
path: root/ofxparse/ofxutil.py
blob: 391c2212ae643309c8e774678f710d0ed206d877 (plain)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
from __future__ import absolute_import, with_statement

import os
import collections
import xml.etree.ElementTree as ET
import six

if 'OrderedDict' in dir(collections):
    odict = collections
else:
    import ordereddict as odict


class InvalidOFXStructureException(Exception):
    pass


class OfxData(object):
    def __init__(self, tag):
        self.nodes = odict.OrderedDict()
        self.tag = tag
        self.data = ""

    def add_tag(self, name):
        name = name.lower()
        if name not in self.nodes:
            self.nodes[name] = OfxData(name.upper())
            return self.nodes[name]
        elif not isinstance(self.nodes[name], list):
            self.nodes[name] = [self.nodes[name], OfxData(name.upper())]
        else:
            self.nodes[name].append(OfxData(name.upper()))
        return self.nodes[name][-1]

    def del_tag(self, name):
        name = name.lower()
        if name in self.nodes:
            del self.nodes[name]

    def __setattr__(self, name, value):
        if name in self.__dict__ or name in ['nodes', 'tag', 'data', '\
                headers', 'xml']:
            self.__dict__[name] = value
        else:
            self.del_tag(name)
            if isinstance(value, list):
                for val in value:
                    tag = self.add_tag(name)
                    tag.nodes = val.nodes
                    tag.data = val.data
            elif isinstance(value, OfxData):
                tag = self.add_tag(name)
                tag.nodes = value.nodes
                tag.data = value.data
            else:
                tag = self.add_tag(name)
                tag.data = str(value)

    def __getattr__(self, name):
        if name in self.__dict__:
            return self.__dict__[name]
        elif name in self.__dict__['nodes']:
            return self.__dict__['nodes'][name]
        else:
            tag = self.add_tag(name)
            return tag

    def __delattr__(self, name):
        if name in self.__dict__:
            del self.__dict__[name]
        elif name in self.__dict__['nodes']:
            del self.__dict__['nodes'][name]
        else:
            raise AttributeError

    def __getitem__(self, name):
        item_list = []
        self.find(name, item_list)
        return item_list

    def find(self, name, item_list):
        for n, child in six.iteritems(self.nodes):
            if isinstance(child, OfxData):
                if child.tag.lower() == name:
                    item_list.append(child)
                child.find(name, item_list)
            else:
                for grandchild in child:
                    if grandchild.tag.lower() == name:
                        item_list.append(grandchild)
                    grandchild.find(name, item_list)

    def __iter__(self):
        for k, v in six.iteritems(self.nodes):
            yield v

    def __contains__(self, item):
        return item in self.nodes

    def __len__(self):
        return len(self.nodes)

    def __str__(self):
        return os.linesep.join("\t" * line[1] + line[0] for line in
                               self.format())

    def format(self):
        if self.data or not self.nodes:
            if self.tag.upper() == "OFX":
                return [["<%s>%s</%s>" % (self.tag, self.data
                        if self.data else "", self.tag), 0]]
            return [["<%s>%s" % (self.tag, self.data), 0]]
        else:
            ret = [["<%s>" % self.tag, -1]]
            for _, child in six.iteritems(self.nodes):
                if isinstance(child, OfxData):
                    ret.extend(child.format())
                else:
                    for grandchild in child:
                        ret.extend(grandchild.format())
            ret.append(["</%s>" % self.tag, -1])

            for item in ret:
                item[1] += 1

            return ret


class OfxUtil(OfxData):
    def __init__(self, ofx_data=None):
        super(OfxUtil, self).__init__('OFX')
        self.headers = odict.OrderedDict()
        self.xml = ""
        if ofx_data:
            if isinstance(ofx_data, six.string_types) and not \
                    ofx_data.lower().endswith('.ofx'):
                self.parse(ofx_data)
            else:
                self.parse(open(ofx_data).read() if isinstance(
                    ofx_data, six.string_types) else ofx_data.read())

    def parse(self, ofx):
        try:
            for line in ofx.splitlines():
                if line.strip() == "":
                    break
                header, value = line.split(":")
                self.headers[header] = value
        except ValueError:
            pass
        finally:
            if "OFXHEADER" not in self.headers:
                self.headers["OFXHEADER"] = "100"
            if "VERSION" not in self.headers:
                self.headers["VERSION"] = "102"
            if "SECURITY" not in self.headers:
                self.headers["SECURITY"] = "NONE"
            if "OLDFILEUID" not in self.headers:
                self.headers["OLDFILEUID"] = "NONE"
            if "NEWFILEUID" not in self.headers:
                self.headers["NEWFILEUID"] = "NONE"

        try:
            tags = ofx.split("<")
            if len(tags) > 1:
                tags = ["<" + t.strip() for t in tags[1:]]

            heirarchy = []
            can_open = True

            for i, tag in enumerate(tags):
                gt = tag.index(">")
                if tag[1] != "/":
                    # Is an opening tag
                    if not can_open:
                        tags[i - 1] = tags[i - 1] + "</" + \
                            heirarchy.pop() + ">"
                        can_open = True
                    tag_name = tag[1:gt].split()[0]
                    heirarchy.append(tag_name)
                    if len(tag) > gt + 1:
                        can_open = False
                else:
                    # Is a closing tag
                    tag_name = tag[2:gt].split()[0]
                    if tag_name not in heirarchy:
                        # Close tag with no matching open, so delete it
                        tags[i] = tag[gt + 1:]
                    else:
                        # Close tag with matching open, but other open
                        # tags that need to be closed first
                        while(tag_name != heirarchy[-1]):
                            tags[i - 1] = tags[i - 1] + "</" + \
                                heirarchy.pop() + ">"
                        can_open = True
                        heirarchy.pop()

            self.xml = ET.fromstringlist(tags)
            self.load_from_xml(self, self.xml)
        except Exception:
            raise InvalidOFXStructureException

    def load_from_xml(self, ofx, xml):
        ofx.data = xml.text
        for child in xml:
            tag = ofx.add_tag(child.tag)
            self.load_from_xml(tag, child)

    def reload_xml(self):
        super(OfxUtil, self).__init__('OFX')
        self.load_from_xml(self, self.xml)

    def write(self, output_file):
        with open(output_file, 'wb') as f:
            f.write(str(self))

    def __str__(self):
        ret = os.linesep.join(":".join(line) for line in
                              six.iteritems(self.headers)) + os.linesep * 2
        ret += super(OfxUtil, self).__str__()
        return ret


if __name__ == "__main__":
    here = os.path.dirname(__file__)
    fixtures = os.path.join(here, '../tests/fixtures/')
    ofx = OfxUtil(fixtures + 'checking.ofx')
#    ofx = OfxUtil(fixtures + 'fidelity.ofx')

    # Manipulate OFX file via XML library
#    for transaction in ofx.xml.iter('STMTTRN'):
#        transaction.find('NAME').text = transaction.find('MEMO').text
#        transaction.remove(transaction.find('MEMO'))
#    ofx.reload_xml()

    # Manipulate OFX file via object tree built from XML
#    for transaction in ofx.bankmsgsrsv1.stmttrnrs.stmtrs.banktranlist.stmttrn:
#        transaction.name = transaction.memo
#        del transaction.memo
#        transaction.notes = "Acknowledged"
    # Modified sytnax for object tree data manipulation
    # I'm using the __getitem__ method like the xml.iter method from
    # ElementTree, as a recursive search
    for transaction in ofx['stmttrn']:
        transaction.name = transaction.memo
        del transaction.memo
        transaction.notes = "Acknowledged"

#    for bal in ofx['bal']:
#        print(bal)

#    ofx.test = "First assignment operation"
#    ofx.test = "Second assignment operation"
#
    print(ofx)

    # Write OFX data to output file
#    ofx.write('out.ofx')

#    for file_name in os.listdir(fixtures):
#        if os.path.isfile(fixtures + file_name):
#            print "Attempting to parse", file_name
#            ofx = OfxParser('fixtures/' + file_name)
#
#            file_parts = file_name.split(".")
#            file_parts.insert(1, 'v2')
#            with open('fixtures/' + ".".join(file_parts), 'wb') as f:
#                f.write(str(ofx))