def write(self, fileobj, pretty=False): """ Write XML string to **fileobj**. :param fileobj: a *file-like* object :param pretty: True for easy readable output Python 3.x - set encoding at the open command:: open('filename', 'w', encoding='utf-8') """ # write xml header fileobj.write('<?xml version="1.0" encoding="utf-8" ?>\n') # don't use DOCTYPE. It's useless. see also: # http://tech.groups.yahoo.com/group/svg-developers/message/48562 # write stylesheets stylesheet_template = '<?xml-stylesheet href="%s" type="text/css" ' \ 'title="%s" alternate="%s" media="%s"?>\n' # removed map(), does not work with Python 3 for stylesheet in self._stylesheets: fileobj.write(stylesheet_template % stylesheet) xml_string = self.tostring() if pretty: # write easy readable XML file xml_string = pretty_xml(xml_string) fileobj.write(xml_string)
def test_unicode_compatibility(self): if PYTHON3: return dwg = svgwrite.Drawing() dwg.add(dwg.text("•••", insert=(0, 0))) result = pretty_xml(dwg.tostring()) self.assertTrue(type(result) is unicode)
def write_str(self, pretty=False): """ Write XML string :param pretty: True for easy readable output Python 3.x - set encoding at the open command:: """ arr = [] # write xml header arr.append('<?xml version="1.0" encoding="utf-8" ?>\n') # don't use DOCTYPE. It's useless. see also: # http://tech.groups.yahoo.com/group/svg-developers/message/48562 # write stylesheets stylesheet_template = '<?xml-stylesheet href="%s" type="text/css" ' \ 'title="%s" alternate="%s" media="%s"?>\n' # removed map(), does not work with Python 3 for stylesheet in self._stylesheets: arr.append(stylesheet_template % stylesheet) xml_string = self.tostring() if pretty: # write easy readable XML file xml_string = pretty_xml(xml_string) arr.append(xml_string) return arr
def test_empty_string(self): result = pretty_xml("") self.assertEqual("", result)
def test_pretty_print(self): result = pretty_xml(XML_UGLY).split('\n') expect = XML_PRETTY.split('\n') for e, r in zip(expect, result): self.assertEqual(e, r)
def test_pretty_print(self): result = pretty_xml(XML_UGLY).split('\n') expect = XML_PRETTY.split('\n') # skip svg tag, order of attributes changes often by new Python releases for e, r in zip(expect[1:], result[1:]): self.assertEqual(e, r)