def test_xml(self): # can read and write a .xml file converted into a Node object xmlfile = io.StringIO(u"""\ <root> <general> <a>1</a> <b>2</b> </general> <section1 param="xxx" /> <section2 param="yyy" /> </root> """) node = n.node_from_xml(xmlfile) outfile = io.BytesIO() n.node_to_xml(node, outfile) self.assertEqual( outfile.getvalue(), """\ <?xml version="1.0" encoding="utf-8"?> <root> <general> <a> 1 </a> <b> 2 </b> </general> <section1 param="xxx"/> <section2 param="yyy"/> </root> """)
def test_xml(self): # can read and write a .xml file converted into a Node object xmlfile = io.StringIO(u"""\ <root> <general> <a>1</a> <b>2</b> </general> <section1 param="xxx" /> <section2 param="yyy" /> </root> """) node = n.node_from_xml(xmlfile) outfile = io.BytesIO() n.node_to_xml(node, outfile) self.assertEqual(outfile.getvalue(), """\ <?xml version="1.0" encoding="utf-8"?> <root> <general> <a> 1 </a> <b> 2 </b> </general> <section1 param="xxx"/> <section2 param="yyy"/> </root> """)
def write(nodes, output=sys.stdout): """ Convert nodes into a NRML file. output must be a file object open in write mode. If you want to perform a consistency check, open it in read-write mode, then it will be read after creation and validated. :params nodes: an iterable over Node objects :params output: a file-like object in write or read-write mode """ root = Node('nrml', nodes=nodes) node_to_xml(root, output, {NAMESPACE: '', GML_NAMESPACE: 'gml:'}) if hasattr(output, 'mode') and '+' in output.mode: # read-write mode output.seek(0) read(output) # validate the written file
def write(nodes, output=sys.stdout, fmt="%8.4E"): """ Convert nodes into a NRML file. output must be a file object open in write mode. If you want to perform a consistency check, open it in read-write mode, then it will be read after creation and validated. :params nodes: an iterable over Node objects :params output: a file-like object in write or read-write mode """ root = Node("nrml", nodes=nodes) with writers.floatformat(fmt): node_to_xml(root, output, {NRML05: "", GML_NAMESPACE: "gml:"}) if hasattr(output, "mode") and "+" in output.mode: # read-write mode output.seek(0) read(output) # validate the written file
def write(nodes, output=sys.stdout, fmt='%8.4E'): """ Convert nodes into a NRML file. output must be a file object open in write mode. If you want to perform a consistency check, open it in read-write mode, then it will be read after creation and validated. :params nodes: an iterable over Node objects :params output: a file-like object in write or read-write mode """ root = Node('nrml', nodes=nodes) with writers.floatformat(fmt): node_to_xml(root, output, {NRML05: '', GML_NAMESPACE: 'gml:'}) if hasattr(output, 'mode') and '+' in output.mode: # read-write mode output.seek(0) read(output) # validate the written file
def write(nodes, output=sys.stdout, fmt='%10.7E', gml=True): """ Convert nodes into a NRML file. output must be a file object open in write mode. If you want to perform a consistency check, open it in read-write mode, then it will be read after creation and validated. :params nodes: an iterable over Node objects :params output: a file-like object in write or read-write mode """ root = Node('nrml', nodes=nodes) namespaces = {NRML05: ''} if gml: namespaces[GML_NAMESPACE] = 'gml:' with writers.floatformat(fmt): node_to_xml(root, output, namespaces) if hasattr(output, 'mode') and '+' in output.mode: # read-write mode output.seek(0) read(output) # validate the written file
def write(nodes, output=sys.stdout, fmt='%.7E', gml=True, xmlns=None): """ Convert nodes into a NRML file. output must be a file object open in write mode. If you want to perform a consistency check, open it in read-write mode, then it will be read after creation and validated. :params nodes: an iterable over Node objects :params output: a file-like object in write or read-write mode :param fmt: format used for writing the floats (default '%.7E') :param gml: add the http://www.opengis.net/gml namespace :param xmlns: NRML namespace like http://openquake.org/xmlns/nrml/0.4 """ root = Node('nrml', nodes=nodes) namespaces = {xmlns or NRML05: ''} if gml: namespaces[GML_NAMESPACE] = 'gml:' with writers.floatformat(fmt): node_to_xml(root, output, namespaces) if hasattr(output, 'mode') and '+' in output.mode: # read-write mode output.seek(0) read(output) # validate the written file