# Path: safe/definitions/hazard_classifications.py

# 
# Path: safe/gis/tools.py
# def load_layer(full_layer_uri_string, name=None, provider=None):
#     """Helper to load and return a single QGIS layer based on our layer URI.
# 
#     :param provider: The provider name to use if known to open the layer.
#         Default to None, we will try to guess it, but it's much better if you
#         can provide it.
#     :type provider:
# 
#     :param name: The name of the layer. If not provided, it will be computed
#         based on the URI.
#     :type name: basestring
# 
#     :param full_layer_uri_string: Layer URI, with provider type.
#     :type full_layer_uri_string: str
# 
#     :returns: tuple containing layer and its layer_purpose.
#     :rtype: (QgsMapLayer, str)
#     """
#     if provider:
#         # Cool !
#         layer_path = full_layer_uri_string
#     else:
#         #  Let's check if the driver is included in the path
#         layer_path, provider = decode_full_layer_uri(full_layer_uri_string)
# 
#         if not provider:
#             # Let's try to check if it's file based and look for a extension
#             if '|' in layer_path:
#                 clean_uri = layer_path.split('|')[0]
#             else:
#                 clean_uri = layer_path
#             is_file_based = os.path.exists(clean_uri)
#             if is_file_based:
#                 # Extract basename and absolute path
#                 file_name = os.path.split(layer_path)[
#                     -1]  # If path was absolute
#                 extension = os.path.splitext(file_name)[1]
#                 if extension in OGR_EXTENSIONS:
#                     provider = 'ogr'
#                 elif extension in GDAL_EXTENSIONS:
#                     provider = 'gdal'
#                 else:
#                     provider = None
# 
#     if not provider:
#         layer = load_layer_without_provider(layer_path)
#     else:
#         layer = load_layer_with_provider(layer_path, provider)
# 
#     if not layer or not layer.isValid():
#         message = 'Layer "%s" is not valid' % layer_path
#         LOGGER.debug(message)
#         raise InvalidLayerError(message)
# 
#     # Define the name
#     if not name:
#         source = layer.source()
#         if '|' in source:
#             clean_uri = source.split('|')[0]
#         else:
#             clean_uri = source
#         is_file_based = os.path.exists(clean_uri)
#         if is_file_based:
#             # Extract basename and absolute path
#             file_name = os.path.split(layer_path)[-1]  # If path was absolute
#             name = os.path.splitext(file_name)[0]
#         else:
#             # Might be a DB, take the DB name
#             source = QgsDataSourceUri(source)
#             name = source.table()
# 
#     if not name:
#         name = 'default'
# 
#     if qgis_version() >= 21800:
#         layer.setName(name)
#     else:
#         layer.setLayerName(name)
# 
#     # update the layer keywords
#     monkey_patch_keywords(layer)
#     layer_purpose = layer.keywords.get('layer_purpose')
# 
#     return layer, layer_purpose


import logging
import unittest
from safe.definitions.constants import INASAFE_TEST
from safe.definitions.exposure import (
    exposure_structure,
    exposure_population,
    exposure_road)
from safe.definitions.exposure_classifications import (
    generic_structure_classes,
    generic_road_classes)
from safe.definitions.hazard import (
    hazard_generic,
    hazard_earthquake,
    hazard_tsunami,
    hazard_cyclone)
from safe.definitions.hazard_classifications import (
    generic_hazard_classes,
    earthquake_mmi_scale,
    tsunami_hazard_classes,
    cyclone_au_bom_hazard_classes)
from safe.report.extractors.util import (
    layer_definition_type,
    layer_hazard_classification,
    resolve_from_dictionary,
    retrieve_exposure_classes_lists)
from safe.test.utilities import (
    standard_data_path,
    get_qgis_app)
from safe.gis.tools import load_layer
                    path=path))
                LOGGER.error('Expected {name}'.format(
                    **expected_definition))
                LOGGER.error('Actual {name}'.format(
                    **actual_definition))
                raise e

    # We are using multi hazard classification so this test will fail
    # the layer needs to run on impact function first or we can inject
    # the classification for this test.
    def test_layer_hazard_classification(self):
        """Test layer_hazard_classification method.

        .. versionadded:: 4.0
        """
        layer_paths = self.layer_paths_list
        expected_classifications = [
            generic_hazard_classes,
            earthquake_mmi_scale,
            tsunami_hazard_classes,
            cyclone_au_bom_hazard_classes,
            None,
            None,
            None,
            None,
        ]

        for layer_path, expected_classification in zip(
                layer_paths, expected_classifications):
            path = standard_data_path(*layer_path)

# Path: safe/definitions/hazard_classifications.py

# 
# Path: safe/gis/tools.py
# def load_layer(full_layer_uri_string, name=None, provider=None):
#     """Helper to load and return a single QGIS layer based on our layer URI.
# 
#     :param provider: The provider name to use if known to open the layer.
#         Default to None, we will try to guess it, but it's much better if you
#         can provide it.
#     :type provider:
# 
#     :param name: The name of the layer. If not provided, it will be computed
#         based on the URI.
#     :type name: basestring
# 
#     :param full_layer_uri_string: Layer URI, with provider type.
#     :type full_layer_uri_string: str
# 
#     :returns: tuple containing layer and its layer_purpose.
#     :rtype: (QgsMapLayer, str)
#     """
#     if provider:
#         # Cool !
#         layer_path = full_layer_uri_string
#     else:
#         #  Let's check if the driver is included in the path
#         layer_path, provider = decode_full_layer_uri(full_layer_uri_string)
# 
#         if not provider:
#             # Let's try to check if it's file based and look for a extension
#             if '|' in layer_path:
#                 clean_uri = layer_path.split('|')[0]
#             else:
#                 clean_uri = layer_path
#             is_file_based = os.path.exists(clean_uri)
#             if is_file_based:
#                 # Extract basename and absolute path
#                 file_name = os.path.split(layer_path)[
#                     -1]  # If path was absolute
#                 extension = os.path.splitext(file_name)[1]
#                 if extension in OGR_EXTENSIONS:
#                     provider = 'ogr'
#                 elif extension in GDAL_EXTENSIONS:
#                     provider = 'gdal'
#                 else:
#                     provider = None
# 
#     if not provider:
#         layer = load_layer_without_provider(layer_path)
#     else:
#         layer = load_layer_with_provider(layer_path, provider)
# 
#     if not layer or not layer.isValid():
#         message = 'Layer "%s" is not valid' % layer_path
#         LOGGER.debug(message)
#         raise InvalidLayerError(message)
# 
#     # Define the name
#     if not name:
#         source = layer.source()
#         if '|' in source:
#             clean_uri = source.split('|')[0]
#         else:
#             clean_uri = source
#         is_file_based = os.path.exists(clean_uri)
#         if is_file_based:
#             # Extract basename and absolute path
#             file_name = os.path.split(layer_path)[-1]  # If path was absolute
#             name = os.path.splitext(file_name)[0]
#         else:
#             # Might be a DB, take the DB name
#             source = QgsDataSourceUri(source)
#             name = source.table()
# 
#     if not name:
#         name = 'default'
# 
#     if qgis_version() >= 21800:
#         layer.setName(name)
#     else:
#         layer.setLayerName(name)
# 
#     # update the layer keywords
#     monkey_patch_keywords(layer)
#     layer_purpose = layer.keywords.get('layer_purpose')
# 
#     return layer, layer_purpose


# Path: safe/report/test/test_util.py
import logging
import unittest
from safe.definitions.constants import INASAFE_TEST
from safe.definitions.exposure import (
    exposure_structure,
    exposure_population,
    exposure_road)
from safe.definitions.exposure_classifications import (
    generic_structure_classes,
    generic_road_classes)
from safe.definitions.hazard import (
    hazard_generic,
    hazard_earthquake,
    hazard_tsunami,
    hazard_cyclone)
from safe.definitions.hazard_classifications import (
    generic_hazard_classes,
    earthquake_mmi_scale,
    tsunami_hazard_classes,
    cyclone_au_bom_hazard_classes)
from safe.report.extractors.util import (
    layer_definition_type,
    layer_hazard_classification,
    resolve_from_dictionary,
    retrieve_exposure_classes_lists)
from safe.test.utilities import (
    standard_data_path,
    get_qgis_app)
from safe.gis.tools import load_layer

                    path=path))
                LOGGER.error('Expected {name}'.format(
                    **expected_definition))
                LOGGER.error('Actual {name}'.format(
                    **actual_definition))
                raise e

    # We are using multi hazard classification so this test will fail
    # the layer needs to run on impact function first or we can inject
    # the classification for this test.
    def test_layer_hazard_classification(self):
        """Test layer_hazard_classification method.

        .. versionadded:: 4.0
        """
        layer_paths = self.layer_paths_list
        expected_classifications = [
            generic_hazard_classes,
            earthquake_mmi_scale,
            tsunami_hazard_classes,
            cyclone_au_bom_hazard_classes,
            None,
            None,
            None,
            None,
        ]

        for layer_path, expected_classification in zip(
                layer_paths, expected_classifications):
            path = standard_data_path(*layer_path)

            layer, _ = load_layer(path)