def serialize(self, ofx, version=None, prettyprint=False, close_elements=True): if version is None: version = self.version header = make_header(version=version, newfileuid=self.uuid) header = bytes(str(header), "utf_8") tree = ofx.to_etree() if prettyprint: utils.indent(tree) # Some servers choke on OFXv1 requests including ending tags for # elements (which are optional per the spec). if close_elements is False: if version >= 200: msg = "OFX version {} requires ending tags for elements" raise ValueError(msg.format(version)) body = utils.tostring_unclosed_elements(tree) else: # ``method="html"`` skips the initial XML declaration body = ET.tostring(tree, encoding="utf_8", method="html") return header + body
def serialize(self, ofx: OFX, version: Optional[int] = None, prettyprint: Optional[bool] = None, close_elements: Optional[bool] = None, ) -> bytes: if version is None: version = self.version if prettyprint is None: prettyprint = self.prettyprint if close_elements is None: close_elements = self.close_elements header = bytes(str(make_header(version=version, newfileuid=self.uuid)), "utf_8") tree = ofx.to_etree() if prettyprint: utils.indent(tree) # Some servers choke on OFXv1 requests including ending tags for # elements (which are optional per the spec). if close_elements is False: if version >= 200: msg = "OFX version {} requires ending tags for elements" raise ValueError(msg.format(version)) body = utils.tostring_unclosed_elements(tree) else: # ``method="html"`` skips the initial XML declaration body = ET.tostring(tree, encoding="utf_8", method="html") return header + body
def serialize( self, ofx: OFX, version: Optional[int] = None, oldfileuid: Optional[str] = None, newfileuid: Optional[str] = None, prettyprint: Optional[bool] = None, close_elements: Optional[bool] = None, ) -> bytes: """ Transform a ``models.OFX`` instance into bytestring representation with OFX header prepended. N.B. ``version`` / ``prettyprint`` / ``close_elements`` kwargs are basically hacks for ``scripts.ofxget.scan_profile()``; ordinarily you should initialize the ``OFXClient`` with the proper version# and formatting parameters, rather than overriding the client config here. Optional kwargs: ``version`` - OFX version to report in header ``oldfileuid`` - OLDFILEUID to report in header ``newfileuid`` - NEWFILEUID to report in header ``prettyprint`` - add newlines between tags and indentation ``close_elements`` - add markup closing tags to leaf elements """ if version is None: version = self.version if prettyprint is None: prettyprint = self.prettyprint if close_elements is None: close_elements = self.close_elements header = bytes( str( make_header( version=version, oldfileuid=oldfileuid, newfileuid=newfileuid ) ), "utf_8", ) tree = ofx.to_etree() if prettyprint: utils.indent(tree) # Some servers choke on OFXv1 requests including ending tags for # elements (which are optional per the spec). if close_elements is False: if version >= 200: raise ValueError( f"OFX version {version} requires ending tags for elements" ) body = utils.tostring_unclosed_elements(tree) else: # ``method="html"`` skips the initial XML declaration body = ET.tostring(tree, encoding="utf_8", method="html") return header + body
def testTostringUnclosedElements(self): result = tostring_unclosed_elements(self.root).decode() self.assertEqual( result, ("<ROOT>" "<LEVEL1>" "<LEVEL2>level2" "</LEVEL1>" "<LEVEL1>" "</ROOT>"), )
def testTostringUnclosedElements(self): result = tostring_unclosed_elements(self.root).decode() self.assertEqual( result, ("<ROOT>" "<LEVEL1>" "<LEVEL2>level2" "</LEVEL1>" "<LEVEL1>" "</ROOT>"), )