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()
def parse(self, source): if not hasattr(source, 'read'): source = open(source) source = source.read() # Validate and strip the OFX header source = OFXHeader.strip(source) # Then parse tag soup into tree of Elements parser = TreeBuilder(element_factory=self.element_factory) parser.feed(source) self._root = parser.close()
def parse(self, source): if not hasattr(source, 'read'): source = open(source) with source as s: source = s.read() # Validate and strip the OFX header source = OFXHeader.strip(source) # Then parse tag soup into tree of Elements parser = TreeBuilder(element_factory=self.element_factory) parser.feed(source) self._root = parser.close()
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
def ofxheader(self): """ Prepend to OFX markup. """ return str(OFXHeader(version=self.version, newfileuid=uuid.uuid4()))
def _stripHeader(source): """ Validate and strip the OFX header """ return OFXHeader.strip(source)