コード例 #1
0
ファイル: Parser.py プロジェクト: chbndrhnns/ofxtools
    def parse(self, source, parser=None):
        """
        Overrides ElementTree.ElementTree.parse() to validate and strip the
        the OFX header before feeding the body tags to custom
        TreeBuilder subclass (below) for parsing into Element instances.
        """
        # If our source doesn't follow the file API...
        if not hasattr(source, 'read'):
            # ...try to interpret it as a file
            try:
                source = open(source, 'rb')
            except OSError:
                msg = "Can't read source '{}'".format(source)
                raise ParseError(msg)

        header = OFXHeader.parse(source)
        source = source.read()
        source = source.decode(header.codec)

        # Cut a parser instance
        parser = parser or TreeBuilder
        parser = parser()

        # Then parse tag soup into tree of Elements
        parser.feed(source)

        # ElementTree.TreeBuilder.close() returns the root.
        # Follow ElementTree API and stash as self._root (so all normal
        # ElementTree methods e.g. find() work normally on our subclass).
        self._root = parser.close()
コード例 #2
0
    def _read(self, source):
        """
        """
        # If our source doesn't follow the file API, try to interpret it
        # as a file path
        if not hasattr(source, 'read'):
            try:
                source = open(source, 'rb')
            except OSError:
                msg = "Can't read source '{}'".format(source)
                raise ParseError(msg)

        header = OFXHeader.parse(source)
        source = source.read()
        # Decode source stream according to the CHARSET declared by OFX header
        source = source.decode(header.codec)

        return header, source