Skip to content

CsvStatementParser takes a file as input parameter that is never closed #71

Open
@omarkohl

Description

@omarkohl

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions