Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
omarkohl opened this issue Mar 14, 2018 · 0 comments
Open

Comments

@omarkohl
Copy link
Contributor

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)
omarkohl added a commit to omarkohl/ofxstatement-1822direkt that referenced this issue Mar 14, 2018
Since currently this is not possible when using a file as parameter (see
kedder/ofxstatement#71) the easiest workaround
is to simply pass the file content to the parser. The csv.reader() takes
any iterable (such as a list of lines, as in this case) as input.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant