CsvStatementParser.__init__() takes a fin Parameter that is later used by csv.reader . In theory fin could be any iterable but in practice it will most likely be a file object created by calling open() .
Generally it is responsability of the open() caller to close the file after being done with it. The problem with ofxstatement is that the general workflow is to create a Plugin class that returns a Parser. There is therefore no possibility to close the file.
class ProblemPlugin(Plugin):
def get_parser(self, filename):
f = open(filename, 'r', encoding=encoding)
return CsvStatementParser(f)
I see two general approaches to fixing this:
- Add some kind of optional
cleanup() function that can be defined for the plugin and that is called at the end of execution by ofxstatement
- Make CsvStatementParser take a filename (and encoding) as parameter and open/close the file internally
Some basic code for the second approach:
class CsvStatementParser(StatementParser):
"""Generic csv statement parser"""
def split_records(self):
with open(self.filename, 'r', encoding=self.encoding) as f:
yield from csv.reader(f)
CsvStatementParser.__init__()takes afinParameter that is later used bycsv.reader. In theoryfincould be any iterable but in practice it will most likely be a file object created by callingopen().Generally it is responsability of the
open()caller to close the file after being done with it. The problem with ofxstatement is that the general workflow is to create a Plugin class that returns a Parser. There is therefore no possibility to close the file.I see two general approaches to fixing this:
cleanup()function that can be defined for the plugin and that is called at the end of execution by ofxstatementSome basic code for the second approach: