def _parse_lt(self): # do the parsing, called at instantiation time to populate .values fkeys = [] nrml = node_from_xml(self.fname) for branching_level in nrml.logicTree: if len(branching_level) > 1: raise InvalidLogicTree( 'Branching level %s has multiple branchsets' % branching_level['branchingLevelID']) for branchset in branching_level: if branchset['uncertaintyType'] != 'gmpeModel': raise InvalidLogicTree( 'only uncertainties of type ' '"gmpeModel" are allowed in gmpe logic tree') fkey = branchset.attrib.get(self.branchset_filter) if fkey: fkeys.append(fkey) if fkey in self.filter_keys: weights = [] for branch in branchset: weight = Decimal(branch.uncertaintyWeight.text) weights.append(weight) branch_id = branch['branchID'] uncertainty = branch.uncertaintyModel.text.strip() self.values[fkey].append(uncertainty) yield BranchTuple( branchset, branch_id, uncertainty, weight) assert sum(weights) == 1, weights if len(fkeys) > len(set(fkeys)): raise InvalidLogicTree('Found duplicated %s=%s' % ( self.branchset_filter, fkeys))
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 _parse_lt(self): # do the parsing, called at instantiation time to populate .values fkeys = [] nrml = node_from_xml(self.fname) for branching_level in nrml.logicTree: if len(branching_level) > 1: raise InvalidLogicTree( 'Branching level %s has multiple branchsets' % branching_level['branchingLevelID']) for branchset in branching_level: if branchset['uncertaintyType'] != 'gmpeModel': raise InvalidLogicTree( 'only uncertainties of type ' '"gmpeModel" are allowed in gmpe logic tree') fkey = branchset.attrib.get(self.branchset_filter) if fkey: fkeys.append(fkey) if fkey in self.filter_keys: weights = [] for branch in branchset: weight = Decimal(branch.uncertaintyWeight.text) weights.append(weight) branch_id = branch['branchID'] uncertainty = branch.uncertaintyModel.text.strip() self.validate_gsim(uncertainty) self.values[fkey].append(uncertainty) yield BranchTuple( branchset, branch_id, uncertainty, weight) assert sum(weights) == 1, weights if len(fkeys) > len(set(fkeys)): raise InvalidLogicTree('Found duplicated %s=%s' % ( self.branchset_filter, fkeys))
def test_node_factory(self): class ValidNode(n.LiteralNode): "ValidNode test implementation. " validators = dict(a=float, b=int) self.assertEqual(ValidNode.__doc__, '''\ ValidNode test implementation. Known validators: a: float b: int''') 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, ValidNode) self.assertEqual(~node.general.a, 1.0) self.assertEqual(~node.general.b, 2) self.assertEqual(node.section1['param'], 'xxx') self.assertEqual( n.to_literal(node), ( 'root', {}, None, [('general', {}, None, [('a', {}, 1.0, []), ('b', {}, 2, [])]), ('section1', {'param': 'xxx'}, None, []), ('section2', {'param': 'yyy'}, None, [])]) )
def test_node_factory(self): class ValidNode(n.LiteralNode): "ValidNode test implementation. " validators = dict(a=float, b=int) self.assertEqual( ValidNode.__doc__, '''\ ValidNode test implementation. Known validators: a: float b: int''') 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, ValidNode) self.assertEqual(~node.general.a, 1.0) self.assertEqual(~node.general.b, 2) self.assertEqual(node.section1['param'], 'xxx') self.assertEqual( n.to_literal(node), ('root', {}, None, [('general', {}, None, [('a', {}, 1.0, []), ('b', {}, 2, [])]), ('section1', { 'param': 'xxx' }, None, []), ('section2', { 'param': 'yyy' }, None, [])]))
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 setUpClass(cls): nrmlfile = os.path.join(EXAMPLES, 'vm.xml') node = node_from_xml(nrmlfile)[0] tableset = Converter.from_node(node).tableset cls.widget = TripleTableWidget(tableset, nrmlfile) cls.tv0 = cls.widget.tv[0] cls.tv1 = cls.widget.tv[1] cls.tv2 = cls.widget.tv[2] cls.tm0 = cls.tv0.tableModel cls.tm1 = cls.tv1.tableModel cls.tm2 = cls.tv2.tableModel
def load_metadata(self, fileobj): """ Parse the metadata file and set the .metadata node and the .fieldnames list. """ try: self.metadata = node_from_xml(fileobj) except XMLSyntaxError as e: raise InvalidFile('%s:%s' % (fileobj.name, e)) try: self.fieldnames = converter(self.metadata).get_fields() except Exception as e: # get_fields can raise different kind of errors for invalid # metadata; see for instance `test_could_not_extract_fieldnames` raise InvalidFile('%s: could not extract fieldnames: %s' % (fileobj.name, e))
def from_nrml(cls, nrmlfile): """ Return a specialized Converter instance from a file or filename """ [node] = node_from_xml(nrmlfile) return cls.from_node(node)