Esempio n. 1
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 and description
                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)

            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
Esempio n. 2
0
    def __init__(self, src_model_path,
        mfd_bin_width=DEFAULT_MFD_BIN_WIDTH, owner_id=1, input_id=None):
        """
        :param src_model_path: path to a source model file
        :type src_model_path: str

        :param mfd_bin_width: Magnitude Frequency Distribution bin width
        :type mfd_bin_width: float


        :param owner_id: ID of an admin.organization entity in the database. By
            default, the default 'GEM Foundation' group will be used.
            Note(LB): This is kind of ugly and needs to be revisited later.

        :param int input_id: The database key of the uploaded input file from
            which this source was extracted. Please note that the `input_id`
            will only be supplied when uploading source model files via the
            GUI.
        """
        self.src_model_path = src_model_path
        self.mfd_bin_width = mfd_bin_width
        self.owner_id = owner_id
        self.input_id = input_id

        # Java SourceModelReader object
        java.jvm().java.lang.System.setProperty(
            "openquake.nrml.schema", xml.nrml_schema_file())
        self.src_reader = java.jclass('SourceModelReader')(
            self.src_model_path, self.mfd_bin_width)
Esempio n. 3
0
 def preloader(self, *args, **kwargs):
     """Validate job"""
     self.cache = java.jclass("KVS")(
             settings.KVS_HOST,
             settings.KVS_PORT)
     self.calc = java.jclass("LogicTreeProcessor")(
             self.cache, self.key)
     java.jvm().java.lang.System.setProperty("openquake.nrml.schema",
                                             xml.nrml_schema_file())
     return fn(self, *args, **kwargs)
Esempio n. 4
0
 def preloader(self, *args, **kwargs):
     """Validate job"""
     self.cache = java.jclass("KVS")(
             config.get("kvs", "host"),
             int(config.get("kvs", "port")))
     self.calc = java.jclass("LogicTreeProcessor")(
             self.cache, self.key)
     java.jvm().java.lang.System.setProperty("openquake.nrml.schema",
                                             xml.nrml_schema_file())
     return fn(self, *args, **kwargs)
Esempio 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):])
    def __init__(self, *args, **kwargs):
        """
        One-time setup stuff for this entire test case class.
        """
        super(NrmlModelLoaderTestCase, self).__init__(*args, **kwargs)

        self.src_reader = java.jclass('SourceModelReader')(
            TEST_SRC_FILE, db_loader.SourceModelLoader.DEFAULT_MFD_BIN_WIDTH)
        java.jvm().java.lang.System.setProperty("openquake.nrml.schema",
                                                xml.nrml_schema_file())
        self.sources = self.src_reader.read()
        self.simple, self.complex, self.area, self.point = self.sources
    def _serialize_test_helper(self, test_file, expected_tables):
        engine = db_utils.get_db_session("hzrdi", "writer").connection().engine
        java.jvm().java.lang.System.setProperty("openquake.nrml.schema",
                                                xml.nrml_schema_file())
        src_loader = db_loader.SourceModelLoader(test_file, engine)

        results = src_loader.serialize()

        # we should get a 3 item list of results
        self.assertEquals(3, len(results))

        # We expect there to have been 3 inserts.
        # The results are a list of dicts with a single key.
        # The key is the table name (including table space);
        # the value is the id (as an int) of the new record.

        # First, check that the results includes the 3 tables we expect:
        result_tables = [x.keys()[0] for x in results]

        self.assertEqual(expected_tables, result_tables)

        # Everything appears to be fine, but let's query the database to make
        # sure the expected records are there.
        # At this point, we're not going to check every single value; we just
        # want to make sure the records made it into the database.
        tables = src_loader.meta.tables

        # list of tuples of (table name, id)
        table_id_pairs = [x.items()[0] for x in results]

        for table_name, record_id in table_id_pairs:
            table = tables[table_name]

            # run a query against the table object to get a ResultProxy
            result_proxy = table.select(table.c.id == record_id).execute()

            # there should be 1 record here
            self.assertEqual(1, result_proxy.rowcount)

        # clean up db resources
        src_loader.close()
Esempio n. 8
0
    def _do_parse(self):
        """_parse implementation"""
        nrml_schema = etree.XMLSchema(etree.parse(nrml_schema_file()))
        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 and description
                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)

            elif event == 'end' and element.tag == '%sassetDefinition' % NRML:
                site_data = (_to_site(element),
                             self._to_site_attributes(element))
                del element
                yield site_data
Esempio n. 9
0
 def setUp(self):
     java.jvm().java.lang.System.setProperty("openquake.nrml.schema",
                                             xml.nrml_schema_file())
Esempio n. 10
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 XMLValidationError(nrml_schema.error_log.last_error)