Ejemplo n.º 1
0
    def _get(self, filename, xpath):
        schema = etree.XMLSchema(file=nrml_schema_file())
        parser = etree.XMLParser(schema=schema)

        tree = etree.parse(filename, parser=parser)

        return tree.getroot().find(xpath % {'ns': NRML_NS}).text
Ejemplo n.º 2
0
    def _parse_bcr_map(self, filename):
        self.assertTrue(os.path.exists(filename))
        schema = etree.XMLSchema(file=nrml_schema_file())
        parser = etree.XMLParser(schema=schema)
        tree = etree.parse(filename, parser=parser)

        bcrnodes = tree.getroot().findall(
            '{%(ns)s}riskResult/{%(ns)s}benefitCostRatioMap/{%(ns)s}BCRNode' %
            {'ns': NRML}
        )
        result = {}

        for bcrnode in bcrnodes:
            [site] = bcrnode.findall('{%s}site/{%s}Point/{%s}pos' %
                                     (NRML, GML, GML))
            assets = {}
            valuenodes = bcrnode.findall('{%s}benefitCostRatioValue' % NRML)
            for valuenode in valuenodes:
                values = []
                for tag in ('expectedAnnualLossOriginal',
                            'expectedAnnualLossRetrofitted',
                            'benefitCostRatio'):
                    [node] = valuenode.findall('{%s}%s' % (NRML, tag))
                    values.append(float(node.text))
                assets[valuenode.attrib['assetRef']] = tuple(values)
            result[tuple(map(float, site.text.split()))] = assets

        return result
Ejemplo n.º 3
0
def jvm():
    """
    Return the jpype module, after guaranteeing the JVM is running and
    the classpath has been loaded properly.
    """
    jarpaths = (os.path.abspath(
                    os.path.join(os.path.dirname(__file__), "../dist")),
                '/usr/share/java')

    if not jpype.isJVMStarted():
        max_mem = get_jvm_max_mem()
        jpype.startJVM(jpype.getDefaultJVMPath(),
            "-Xmx%sM" % max_mem,
            "-Djava.ext.dirs=%s:%s" % jarpaths,
            # setting Schema path here is ugly, but it's better than
            # doing it before all XML parsing calls
            "-Dopenquake.nrml.schema=%s" % nrml.nrml_schema_file(),
            # force the default Xerces parser configuration, otherwise
            # some random system-installed JAR might override it
            "-Dorg.apache.xerces.xni.parser.XMLParserConfiguration=" \
                "org.apache.xerces.parsers.XIncludeAwareParserConfiguration")

        _init_logs()

    return jpype
Ejemplo n.º 4
0
    def _do_parse(self):
        """parser implementation"""
        # The tags we expect to see:
        ffs_tag = '%sffs' % xml.NRML
        ffc_tag = '%sffc' % xml.NRML
        ffd_tag = '%sffd' % xml.NRML

        first_ffs = True

        schema = etree.XMLSchema(etree.parse(nrml_schema_file()))
        parse_args = dict(source=self.file, events=("end",), schema=schema)

        for _, element in etree.iterparse(**parse_args):
            # start: fragility model
            if element.tag == ffs_tag:
                if first_ffs:
                    # parse the "fragilityModel" element data only once
                    self._parse_model(element.getparent())
                    first_ffs = False
                self.ffs = dict(type=None)
                self.ffs["type"] = element.get('type')
                taxonomy = element.find('%staxonomy' % xml.NRML)
                assert taxonomy is not None, "taxonomy not set"
                self.ffs["taxonomy"] = taxonomy.text.strip()
                tag, func = ((ffd_tag, self._parse_ffd) if self.discrete
                             else (ffc_tag, self._parse_ffc))
                for child in element.iterchildren(tag=tag):
                    yield func(child)
Ejemplo n.º 5
0
 def __init__(self, path):
     producer.FileProducer.__init__(self, path)
     nrml_schema = etree.XMLSchema(etree.parse(nrml_schema_file()))
     self.vuln_model = etree.parse(self.path).getroot()
     if not nrml_schema.validate(self.vuln_model):
         raise xml.XMLValidationError(nrml_schema.error_log.last_error, path)
     model_el = self.vuln_model.getchildren()[0]
     if model_el.tag != "%svulnerabilityModel" % NRML:
         raise xml.XMLMismatchError(path, "vulnerabilityModel", str(model_el.tag)[len(NRML) :])
Ejemplo n.º 6
0
def validates_against_xml_schema(xml_instance_path, schema_path=nrml.nrml_schema_file()):
    """
    Checks whether an XML file validates against an XML Schema

    :param xml_instance_path: XML document path
    :param schema_path: XML schema path
    :returns: boolean success value
    """
    xml_doc = etree.parse(xml_instance_path)
    xmlschema = etree.XMLSchema(etree.parse(schema_path))
    return xmlschema.validate(xml_doc)
Ejemplo n.º 7
0
    def get_xmlschema(cls):
        """
        Create (if needed) and return ``etree.XMLSchema`` object
        for verifying nrml-files correctness.

        Once created schema object is cached in ``_xmlschema``
        class attribute.
        """
        if not cls._xmlschema:
            cls._xmlschema = etree.XMLSchema(file=nrml_schema_file())
        return cls._xmlschema
Ejemplo n.º 8
0
    def _do_parse(self):
        """_parse implementation"""
        nrml_schema = etree.XMLSchema(etree.parse(nrml_schema_file()))
        level = 0
        for event, element in etree.iterparse(
                self.file, events=('start', 'end'), schema=nrml_schema):

            if event == 'start' and element.tag == \
                    '%sexposureList' % NRML:
                # we need to get the exposureList id, description and
                # asset category
                exp_id = element.get('%sid' % GML)
                self._current_meta['listID'] = str(exp_id)

                desc = element.find('%sdescription' % GML)
                if desc is not None:
                    self._current_meta['listDescription'] = str(desc.text)

                taxsrc = element.find('%staxonomySource' % NRML)
                if taxsrc is not None:
                    self._current_meta['taxonomySource'] = str(taxsrc.text)

                asset_category = str(element.get('assetCategory'))
                self._current_meta['assetCategory'] = asset_category

                # type and unit for area, contents cost, retrofitting cost
                # and structural cost.
                attrs = ("areaType", "areaUnit", "cocoType", "cocoUnit",
                         "recoType", "recoUnit", "stcoType", "stcoUnit")
                for attr_name in attrs:
                    attr_value = element.get(attr_name)
                    if attr_value is not None:
                        self._current_meta[attr_name] = attr_value

            elif event == 'start' and level < 2:
                # check that the first child of the root element is an
                # exposure portfolio
                if level == 1 and element.tag != '%sexposureModel' % NRML:
                    raise xml.XMLMismatchError(
                        self.file.name, str(element.tag)[len(NRML):],
                        'exposureModel')

                level += 1

            elif event == 'end' and element.tag == '%sassetDefinition' % NRML:
                site_data = (_to_site(element), _to_occupancy(element),
                             self._to_site_attributes(element))
                del element
                yield site_data
Ejemplo n.º 9
0
    def _do_parse(self):
        """_parse implementation"""
        nrml_schema = etree.XMLSchema(etree.parse(nrml_schema_file()))
        level = 0
        for event, element in etree.iterparse(
                self.file, events=('start', 'end'), schema=nrml_schema):

            if event == 'start' and element.tag == \
                    '%sexposureList' % NRML:
                # we need to get the exposureList id, description and
                # asset category
                exp_id = element.get('%sid' % GML)
                self._current_meta['listID'] = str(exp_id)

                desc = element.find('%sdescription' % GML)
                if desc is not None:
                    self._current_meta['listDescription'] = str(desc.text)

                asset_category = str(element.get('assetCategory'))
                self._current_meta['assetCategory'] = asset_category

                unit = str(element.get('unit'))
                self._current_meta['unit'] = unit

            elif event == 'start' and level < 2:
                # check that the first child of the root element is an
                # exposure portfolio
                if level == 1 and element.tag != '%sexposurePortfolio' % NRML:
                    raise xml.XMLMismatchError(
                        self.file.name, str(element.tag)[len(NRML):],
                        'exposurePortfolio')

                level += 1

            elif event == 'end' and element.tag == '%sassetDefinition' % NRML:
                site_data = (_to_site(element),
                             self._to_site_attributes(element))
                del element
                yield site_data
Ejemplo n.º 10
0
from openquake import xml
from openquake.db import models
from openquake.output import risk as risk_output

from tests.utils import helpers


LOSS_XML_OUTPUT_FILE = 'loss-curves.xml'
LOSS_RATIO_XML_OUTPUT_FILE = 'loss-ratio-curves.xml'

SINGLE_LOSS_XML_OUTPUT_FILE = 'loss-curves-single.xml'
SINGLE_LOSS_RATIO_XML_OUTPUT_FILE = 'loss-ratio-curves-single.xml'

LOSS_XML_FAIL_OUTPUT_FILE = 'loss-curves-fail.xml'

NRML_SCHEMA_PATH = nrml.nrml_schema_file()

TEST_LOSS_CURVE = shapes.Curve(
    [(0.0, 0.44), (256.0, 0.23), (512.0, 0.2), (832.0, 0.16), (1216.0, 0.06)])

TEST_LOSS_RATIO_CURVE = shapes.Curve(
    [(0.0, 0.89), (0.2, 0.72), (0.4, 0.45), (0.6, 0.22), (0.8, 0.17),
     (1.0, 0.03)])


class LossOutputTestCase(unittest.TestCase):
    """Confirm that XML output from risk engine is valid against schema,
    as well as correct given the inputs."""

    def setUp(self):
        self.loss_curve_path = helpers.get_output_path(LOSS_XML_OUTPUT_FILE)