def test_xml(self): # can read and write a .xml file converted into a Node object xmlfile = cStringIO.StringIO("""\ <root> <general> <a>1</a> <b>2</b> </general> <section1 param="xxx" /> <section2 param="yyy" /> </root> """) node = n.node_from_xml(xmlfile) outfile = cStringIO.StringIO() 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 = cStringIO.StringIO("""\ <root> <general> <a>1</a> <b>2</b> </general> <section1 param="xxx" /> <section2 param="yyy" /> </root> """) node = n.node_from_xml(xmlfile) outfile = cStringIO.StringIO() 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 save(self, basename): """ Save the table as a pair of files .mdata and .csv. :param basename: pathname where to save the table :returns: the .mdata and .csv filenames (with suffix) """ name = basename + ('__' + self.suffix if self.suffix else '') with open(name + '.mdata', 'w') as mdatafile, \ open(name + '.csv', 'w') as csvfile: # save metadata node_to_xml(self.metadata, mdatafile) # save data cw = csv.writer(csvfile) cw.writerow(self.fieldnames) cw.writerows(self.rows) return mdatafile.name, csvfile.name
def test_nrml(self): # can read and write a NRML file converted into a Node object xmlfile = cStringIO.StringIO("""\ <?xml version='1.0' encoding='utf-8'?> <nrml xmlns="http://openquake.org/xmlns/nrml/0.4" xmlns:gml="http://www.opengis.net/gml"> <exposureModel id="my_exposure_model_for_population" category="population" taxonomySource="fake population datasource"> <description> Sample population </description> <assets> <asset id="asset_01" number="7" taxonomy="IT-PV"> <location lon="9.15000" lat="45.16667" /> </asset> <asset id="asset_02" number="7" taxonomy="IT-CE"> <location lon="9.15333" lat="45.12200" /> </asset> </assets> </exposureModel> </nrml> """) root = n.node_from_nrml(xmlfile) outfile = cStringIO.StringIO() n.node_to_xml(root, outfile) self.assertEqual(outfile.getvalue(), """\ <?xml version="1.0" encoding="utf-8"?> <nrml xmlns="http://openquake.org/xmlns/nrml/0.4" xmlns:gml="http://www.opengis.net/gml" > <exposureModel category="population" id="my_exposure_model_for_population" taxonomySource="fake population datasource" > <description> Sample population </description> <assets> <asset id="asset_01" number="7" taxonomy="IT-PV" > <location lat="45.16667" lon="9.15000"/> </asset> <asset id="asset_02" number="7" taxonomy="IT-CE" > <location lat="45.12200" lon="9.15333"/> </asset> </assets> </exposureModel> </nrml> """)
def test_nrml(self): # can read and write a NRML file converted into a Node object xmlfile = cStringIO.StringIO("""\ <?xml version='1.0' encoding='utf-8'?> <nrml xmlns="http://openquake.org/xmlns/nrml/0.4" xmlns:gml="http://www.opengis.net/gml"> <exposureModel id="my_exposure_model_for_population" category="population" taxonomySource="fake population datasource"> <description> Sample population </description> <assets> <asset id="asset_01" number="7" taxonomy="IT-PV"> <location lon="9.15000" lat="45.16667" /> </asset> <asset id="asset_02" number="7" taxonomy="IT-CE"> <location lon="9.15333" lat="45.12200" /> </asset> </assets> </exposureModel> </nrml> """) root = n.node_from_nrml(xmlfile) outfile = cStringIO.StringIO() n.node_to_xml(root, outfile) self.assertEqual( outfile.getvalue(), """\ <?xml version="1.0" encoding="utf-8"?> <nrml xmlns="http://openquake.org/xmlns/nrml/0.4" xmlns:gml="http://www.opengis.net/gml" > <exposureModel category="population" id="my_exposure_model_for_population" taxonomySource="fake population datasource" > <description> Sample population </description> <assets> <asset id="asset_01" number="7" taxonomy="IT-PV" > <location lat="45.16667" lon="9.15000"/> </asset> <asset id="asset_02" number="7" taxonomy="IT-CE" > <location lat="45.12200" lon="9.15333"/> </asset> </assets> </exposureModel> </nrml> """)