Example #1
0
def create_loss_map_writer(job_id, serialize_to, nrml_path, scenario):
    """Create a loss map writer observing the settings in the config file.

    :param job_id: the id of the job the curve belongs to.
    :type job_id: int
    :param serialize_to: where to serialize
    :type serialize_to: list of strings. Permitted values: 'db', 'xml'.
    :param nrml_path: the full path of the XML/NRML representation of the
        loss map.
    :type nrml_path: string
    :param scenario: Whether the loss map is scenario (True) or
        non-scenario (False)
    :type scenario: boolean
    :returns: None or an instance of
        :py:class:`output.risk.LossMapXMLWriter` or
        :py:class:`output.risk.LossMapDBWriter`
        :py:class:`output.risk.LossMapNonScenarioXMLWriter`
    """
    writers = []

    if 'db' in serialize_to:
        writers.append(LossMapDBWriter(nrml_path, job_id))

    if 'xml' in serialize_to:
        if scenario:
            writers.append(LossMapXMLWriter(nrml_path))
        else:
            writers.append(LossMapNonScenarioXMLWriter(nrml_path))
    return writer.compose_writers(writers)
Example #2
0
    def test_single_writer(self):

        class W:
            pass

        w = W()
        self.assertEqual(w, writer.compose_writers([w]))
Example #3
0
def create_loss_map_writer(job_id, serialize_to, nrml_path, deterministic):
    """Create a loss map writer observing the settings in the config file.

    :param job_id: the id of the job the curve belongs to.
    :type job_id: int
    :param serialize_to: where to serialize
    :type serialize_to: list of strings. Permitted values: 'db', 'xml'.
    :param nrml_path: the full path of the XML/NRML representation of the
        loss map.
    :type nrml_path: string
    :param deterministic: Whether the loss map is deterministic (True) or
        non-deterministic (False)
    :type deterministic: boolean
    :returns: None or an instance of
        :py:class:`output.risk.LossMapXMLWriter` or
        :py:class:`output.risk.LossMapDBWriter`
    """
    writers = []

    if 'db' in serialize_to:
        writers.append(LossMapDBWriter(get_db_session("reslt", "writer"),
                                       nrml_path,
                                       job_id))

    if 'xml' in serialize_to:
        if deterministic:
            writers.append(LossMapXMLWriter(nrml_path))
        else:
            # No XML schema for non-deterministic maps yet (see bug 805434)
            pass

    return writer.compose_writers(writers)
Example #4
0
def create_loss_map_writer(job_id, serialize_to, nrml_path, scenario):
    """Create a loss map writer observing the settings in the config file.

    :param job_id: the id of the job the curve belongs to.
    :type job_id: int
    :param serialize_to: where to serialize
    :type serialize_to: list of strings. Permitted values: 'db', 'xml'.
    :param nrml_path: the full path of the XML/NRML representation of the
        loss map.
    :type nrml_path: string
    :param scenario: Whether the loss map is scenario (True) or
        non-scenario (False)
    :type scenario: boolean
    :returns: None or an instance of
        :py:class:`output.risk.LossMapXMLWriter` or
        :py:class:`output.risk.LossMapDBWriter`
        :py:class:`output.risk.LossMapNonScenarioXMLWriter`
    """
    writers = []
    logs.LOG.debug("I'VE BEEN CALLED")
    if 'db' in serialize_to:
        writers.append(LossMapDBWriter(nrml_path, job_id))

    if 'xml' in serialize_to:
        if scenario:
            writers.append(LossMapXMLWriter(nrml_path))
        else:
            writers.append(LossMapNonScenarioXMLWriter(nrml_path))
    return writer.compose_writers(writers)
Example #5
0
    def test_multiple_writers(self):
        class W:
            pass

        ws = [W(), W()]

        w = writer.compose_writers(ws)

        self.assertTrue(isinstance(w, writer.CompositeWriter))
        self.assertEqual(list(w.writers), ws)
Example #6
0
    def test_multiple_writers(self):

        class W:
            pass

        ws = [W(), W()]

        w = writer.compose_writers(ws)

        self.assertTrue(isinstance(w, writer.CompositeWriter))
        self.assertEqual(list(w.writers), ws)
Example #7
0
def _create_writer(job_id, serialize_to, nrml_path, create_xml_writer, create_db_writer):
    """Common code for the functions below"""

    writers = []

    if "db" in serialize_to:
        assert job_id, "No job_id supplied"
        job_id = int(job_id)
        writers.append(create_db_writer(nrml_path, job_id))

    if "xml" in serialize_to:
        writers.append(create_xml_writer(nrml_path))

    return writer.compose_writers(writers)
Example #8
0
def _create_writer(job_id, serialize_to, nrml_path, create_xml_writer,
                   create_db_writer):
    """Common code for the functions below"""

    writers = []

    if 'db' in serialize_to:
        assert job_id, "No job_id supplied"
        job_id = int(job_id)
        writers.append(create_db_writer(nrml_path, job_id))

    if 'xml' in serialize_to and nrml_path:
        obj = create_xml_writer(nrml_path)
        writers.append(obj)

    return writer.compose_writers(writers)
Example #9
0
def create_loss_curve_writer(job_id, serialize_to, nrml_path, curve_mode):
    """Create a loss curve writer observing the settings in the config file.

    If no writer is available for the given curve_mode and settings, returns
    None.

    :param job_id: the id of the job the curve belongs to.
    :type job_id: int
    :param serialize_to: where to serialize
    :type serialize_to: list of strings. Permitted values: 'db', 'xml'.
    :param str nrml_path: the full path of the XML/NRML representation of the
        hazard map.
    :param str curve_mode: one of 'loss', 'loss_ratio'
    :returns: None or an instance of
        :py:class:`output.risk.LossCurveXMLWriter`,
        :py:class:`output.risk.LossCurveDBWriter`,
        :py:class:`output.risk.LossRatioCurveXMLWriter`
    """

    assert curve_mode in ('loss', 'loss_ratio', 'insured_loss_curve',
        'insured_loss_ratio_curve')

    writers = []

    if 'db' in serialize_to:
        if job_id is None:
            raise RuntimeError("No job_id supplied")
        job_id = int(job_id)

        if curve_mode == 'loss':
            writers.append(LossCurveDBWriter(nrml_path, job_id))
        elif curve_mode in ('loss_ratio', 'insured_loss_curve',
                            'insured_loss_ratio_curve'):
            # We are non interested in storing loss ratios in the db
            pass

    if 'xml' in serialize_to:
        if curve_mode in ('loss_ratio', 'insured_loss_ratio_curve'):
            writer_class = LossRatioCurveXMLWriter
        elif curve_mode  in ('loss', 'insured_loss_curve'):
            writer_class = LossCurveXMLWriter

        writers.append(writer_class(nrml_path))

    return writer.compose_writers(writers)
Example #10
0
def create_loss_curve_writer(job_id, serialize_to, nrml_path, curve_mode):
    """Create a loss curve writer observing the settings in the config file.

    If no writer is available for the given curve_mode and settings, returns
    None.

    :param job_id: the id of the job the curve belongs to.
    :type job_id: int
    :param serialize_to: where to serialize
    :type serialize_to: list of strings. Permitted values: 'db', 'xml'.
    :param str nrml_path: the full path of the XML/NRML representation of the
        hazard map.
    :param str curve_mode: one of 'loss', 'loss_ratio'
    :returns: None or an instance of
        :py:class:`output.risk.LossCurveXMLWriter`,
        :py:class:`output.risk.LossCurveDBWriter`,
        :py:class:`output.risk.LossRatioCurveXMLWriter`
    """

    assert curve_mode in ('loss', 'loss_ratio', 'insured_loss_curve',
                          'insured_loss_ratio_curve')

    writers = []

    if 'db' in serialize_to:
        if job_id is None:
            raise RuntimeError("No job_id supplied")
        job_id = int(job_id)

        if curve_mode == 'loss':
            writers.append(LossCurveDBWriter(nrml_path, job_id))
        elif curve_mode in ('loss_ratio', 'insured_loss_curve',
                            'insured_loss_ratio_curve'):
            # We are non interested in storing loss ratios in the db
            pass

    if 'xml' in serialize_to:
        if curve_mode in ('loss_ratio', 'insured_loss_ratio_curve'):
            writer_class = LossRatioCurveXMLWriter
        elif curve_mode in ('loss', 'insured_loss_curve'):
            writer_class = LossCurveXMLWriter

        writers.append(writer_class(nrml_path))

    return writer.compose_writers(writers)
Example #11
0
def _create_writer(job_id, serialize_to, nrml_path, create_xml_writer,
                   create_db_writer, multistage_serialization=False):
    """Common code for the functions below"""

    writers = []

    if 'db' in serialize_to:
        assert job_id, "No job_id supplied"
        job_id = int(job_id)
        writers.append(create_db_writer(nrml_path, job_id))

    if 'xml' in serialize_to and nrml_path:
        if multistage_serialization:
            obj = _XML_SERIALIZER_CACHE[(job_id, nrml_path)]
            if obj is None:
                obj = create_xml_writer(nrml_path)
                _XML_SERIALIZER_CACHE[(job_id, nrml_path)] = obj
        else:
            obj = create_xml_writer(nrml_path)
        writers.append(obj)

    return writer.compose_writers(writers)
Example #12
0
def _create_writer(job_id, serialize_to, nrml_path, create_xml_writer,
                   create_db_writer, multistage_serialization=False):
    """Common code for the functions below"""

    writers = []

    if 'db' in serialize_to:
        assert job_id, "No job_id supplied"
        job_id = int(job_id)
        writers.append(create_db_writer(nrml_path, job_id))

    if 'xml' in serialize_to and nrml_path:
        if multistage_serialization:
            obj = _XML_SERIALIZER_CACHE[(job_id, nrml_path)]
            if obj is None:
                obj = create_xml_writer(nrml_path)
                _XML_SERIALIZER_CACHE[(job_id, nrml_path)] = obj
        else:
            obj = create_xml_writer(nrml_path)
        writers.append(obj)

    return writer.compose_writers(writers)
Example #13
0
 def test_writer_is_none(self):
     self.assertEqual(None, writer.compose_writers([None]))
Example #14
0
 def test_empty(self):
     self.assertEqual(None, writer.compose_writers([]))
Example #15
0
 def test_empty(self):
     self.assertEqual(None, writer.compose_writers([]))
Example #16
0
 def test_writer_is_none(self):
     self.assertEqual(None, writer.compose_writers([None]))
Example #17
0
    def test_single_writer(self):
        class W:
            pass

        w = W()
        self.assertEqual(w, writer.compose_writers([w]))