def testInitIndent(self): out = io.BytesIO() xml = loxun.XmlWriter(out, indent="\t") self.assertEqual(xml._indent, "\t") xml = loxun.XmlWriter(out, indent=" ") self.assertEqual(xml._indent, " ") try: loxun.XmlWriter(out, indent="xxx") except AssertionError as error: self.assertEqual( str(error), "`indent` must contain only blanks or tabs but also has: %r" % 'xxx')
def testInitNewline(self): out = io.BytesIO() # FIXME: Add support to specify newLine as Unicode-string and convert it to bytes internally. for newline in [b"\r", b"\n", b"\r\n"]: xml = loxun.XmlWriter(out, newline=newline) self.assertEqual(xml._newline, loxun.unicode_type(newline, "ascii")) try: loxun.XmlWriter(out, newline="xxx") except AssertionError as error: actual_message = str(error) expected_start_of_message = "`newline` is %r but must be one of: " % 'xxx' self.assertTrue( actual_message.startswith(expected_start_of_message), 'error message %r must start with %r' % (actual_message, expected_start_of_message))
def testPerformance(self): out = io.BytesIO() with loxun.XmlWriter(out) as xml: tagName = _randomName() attributes = {} for _ in range(_randy.randint(2, 8)): attributes[_randomName()] = "" xml.tag(tagName, attributes)
def testWithException(self): out = io.BytesIO() try: with loxun.XmlWriter(out) as xml: xml.startTag("x") raise ValueError("test") except ValueError as error: # Ignore expected error. self.assertEqual(str(error), "test")
def testWithMissingEndTag(self): out = io.BytesIO() try: with loxun.XmlWriter(out) as xml: xml.startTag("x") self.fail("XmlWriter.__exit__() must detect missing </x>") except loxun.XmlError: # Ignore expected error. pass
def __init__(self, f, prefix=None, allow_none=True): if prefix is None: self.prefix = XMLWriter.DEFAULT_PREFIX else: self.prefix = prefix self.raise_error_on_empty_value = not allow_none self.writer = loxun.XmlWriter(f) self.written = set()
def __init__(self, output): z = self.z = zipfile.ZipFile(output, "w") z.writestr("mimetype", "application/vnd.oasis.opendocument.spreadsheet") z.writestr( "META-INF/manifest.xml", """<?xml version="1.0" encoding="UTF-8"?> <manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"> <manifest:file-entry manifest:full-path="/" manifest:media-type="application/vnd.oasis.opendocument.spreadsheet"/> <manifest:file-entry manifest:full-path="styles.xml" manifest:media-type="text/xml"/> <manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/> <manifest:file-entry manifest:full-path="META-INF/manifest.xml" manifest:media-type="text/xml"/> <manifest:file-entry manifest:full-path="mimetype" manifest:media-type="text/plain"/> </manifest:manifest>""", ) z.writestr( "styles.xml", """<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <office:document-styles xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"> </office:document-styles>""", ) self.content = tempfile.NamedTemporaryFile() xml = self.xmlwriter = loxun.XmlWriter(self.content, pretty=False) xml.addNamespace("office", OFFICE_NS) xml.addNamespace("style", STYLE_NS) xml.addNamespace("table", TABLE_NS) xml.addNamespace("xlink", XLINK_NS) xml.addNamespace("text", TEXT_NS) # add bold style for headers xml.startTag("office:document-content") xml.startTag("office:automatic-styles") xml.startTag( "style:style", { "style:family": "paragraph", "style:name": "bold", "style:display-name": "bold", }, ) xml.tag( "style:text-properties", { "style:font-weight-complex": "bold", "style:font-weight": "bold", "style:font-weight-asian": "bold", }, ) xml.endTag() xml.endTag() xml.startTag("office:body") xml.startTag("office:spreadsheet") self.status = self.OPENED
def testDefautltIndentWithoutPretty(self): # Regression test for issue #1. out = io.BytesIO() xml = loxun.XmlWriter(out, pretty=False) xml.startTag("a") xml.startTag("b") xml.tag("c") xml.text("some text") xml.endTag("b") xml.endTag("a") self._assertXmlTextEqual(xml, [ b"<?xml version=\"1.0\" encoding=\"utf-8\"?><a><b><c/>some text</b></a>" ])
def testIndentWithPretty(self): out = io.BytesIO() xml = loxun.XmlWriter(out, indent="\t") xml.startTag("a") xml.startTag("b") xml.tag("c") xml.text("some text") xml.endTag("b") xml.endTag("a") self._assertXmlTextEqual(xml, [ b"<?xml version=\"1.0\" encoding=\"utf-8\"?>", b"<a>", b"\t<b>", b"\t\t<c />", b"\t\tsome text", b"\t</b>", b"</a>" ])
def __init__(self, output): z = self.z = zipfile.ZipFile(output, 'w') z.writestr('mimetype', 'application/vnd.oasis.opendocument.spreadsheet') z.writestr('META-INF/manifest.xml', '''<?xml version="1.0" encoding="UTF-8"?> <manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"> <manifest:file-entry manifest:full-path="/" manifest:media-type="application/vnd.oasis.opendocument.spreadsheet"/> <manifest:file-entry manifest:full-path="styles.xml" manifest:media-type="text/xml"/> <manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/> <manifest:file-entry manifest:full-path="META-INF/manifest.xml" manifest:media-type="text/xml"/> <manifest:file-entry manifest:full-path="mimetype" manifest:media-type="text/plain"/> </manifest:manifest>''') z.writestr('styles.xml', '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <office:document-styles xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"> </office:document-styles>''') self.content = tempfile.NamedTemporaryFile() xml = self.xmlwriter = loxun.XmlWriter(self.content, pretty=False) xml.addNamespace('office', OFFICE_NS) xml.addNamespace('style', STYLE_NS) xml.addNamespace('table', TABLE_NS) xml.addNamespace('xlink', XLINK_NS) xml.addNamespace('text', TEXT_NS) # add bold style for headers xml.startTag('office:document-content') xml.startTag('office:automatic-styles') xml.startTag('style:style', { 'style:family': 'paragraph', 'style:name': 'bold', 'style:display-name': 'bold', }) xml.tag('style:text-properties', { 'style:font-weight-complex': 'bold', 'style:font-weight': 'bold', 'style:font-weight-asian': 'bold' }) xml.endTag() xml.endTag() xml.startTag('office:body') xml.startTag('office:spreadsheet') self.status = self.OPENED
def testCanSetEncoding(self): xml = loxun.XmlWriter(io.BytesIO(), encoding='iso-8859-15') self.assertEqual(xml.encoding, 'iso-8859-15')
def _createXmlStringIoWriter(pretty=True, prolog=False): out = io.BytesIO() result = loxun.XmlWriter(out, pretty=pretty, prolog=prolog) return result
def testWithOk(self): out = io.BytesIO() with loxun.XmlWriter(out) as xml: xml.tag("x")
def testIsoEuro(self): out = io.BytesIO() xml = loxun.XmlWriter(out, sourceEncoding="iso-8859-15", prolog=False) xml.text(b"\xa4") self._assertXmlTextEqual(xml, [b"\xe2\x82\xac"])
for key in keys: temp = fdaccess[key] try: if temp['operation'] == 'write': print "%s[\033[1;36m%s\033[1;m]\t\t Path: %s" % ( space3, str(key), hexToStr(temp['path'])) print "%s\t\t\t\t Data: %s" % (space3, hexToStr( temp['data'])) + '\n' except ValueError: pass except KeyError: pass # Print file access log in XML xmlfile = open('fileaccesslog.xml', 'w') xml = loxun.XmlWriter(xmlfile) xml.startTag("FileAccessLog") for path in filelog.keys(): xml.startTag("File", {"path": hexToStr(path)}) for action in filelog[path]: if action[0] == 'w': xml.startTag("WriteOperation") else: xml.startTag("ReadOperation") xml.text(action[1]) xml.endTag() xml.endTag() xml.endTags()