""" Module to control and define the drivers """ from tools import const from .masterDriver import MasterDriver from .KML import KML, ContainerKml from .SHAPE import SHAPE, ContainerSHP from .FIWARE import FIWARE from .PostgreSQL import PostgreSQL from .TIFF import TIFF, ContainerTiff from .ECW import ECW, ContainerEcw from .ZIP import ZIPGeneric, ZIPVector, ContainerZip from .JP2 import JP2, ContainerJP2 from .GJSON import GJSON, GJSONVirtual, ContainerGJson from .CSV import CSV, ContainerCsv from .EXCEL import EXCEL, ContainerExcel from .TOPOJSON import TOPOJSON, ContainerTopoJSON from .GEOPACKAGE import GEOPACKAGE, ContainerGeoPackage from . GML import GML, ContainerGML class Drivers: """ Class to control the drivers. """ driver_does_not_exist = type('DriverDoesNotExist', (Exception, ), {}) _drivers = { # Vector FORMATS const.KML_KEY: { const.VECTOR_KEY: KML, const.DEFAULT_KEY: KML, const.CONTAINER_KEY: ContainerKml }, const.GJSON_KEY: { const.VECTOR_KEY: GJSON, const.DEFAULT_KEY: GJSON, const.CONTAINER_KEY: ContainerGJson }, const.GJSON_VIRTUAL_KEY: { const.VECTOR_KEY: GJSONVirtual, const.DEFAULT_KEY: GJSONVirtual }, const.GML_KEY: { const.VECTOR_KEY: GML, const.DEFAULT_KEY: GML, const.CONTAINER_KEY: ContainerGML }, const.POSTGRESQL_KEY: { const.VECTOR_KEY: PostgreSQL, const.DEFAULT_KEY: PostgreSQL, const.THEMATIC_KEY: PostgreSQL }, const.SHAPE_KEY: { const.VECTOR_KEY: SHAPE, const.DEFAULT_KEY: SHAPE, const.CONTAINER_KEY: ContainerSHP, }, const.TOPOJSON_KEY: { const.VECTOR_KEY: TOPOJSON, const.DEFAULT_KEY: TOPOJSON, const.CONTAINER_KEY: ContainerTopoJSON, }, const.GEOPACKAGE_KEY: { const.VECTOR_KEY: GEOPACKAGE, const.DEFAULT_KEY: GEOPACKAGE, const.CONTAINER_KEY: ContainerGeoPackage, }, const.FIWARE_KEY: { const.VECTOR_KEY: FIWARE, const.DEFAULT_KEY: FIWARE}, # Thematic FORMATS const.CSV_KEY: { const.THEMATIC_KEY: CSV, const.VECTOR_KEY: CSV, const.DEFAULT_KEY: CSV, const.CONTAINER_KEY: ContainerCsv, }, const.EXCEL_KEY: { const.THEMATIC_KEY: EXCEL, const.DEFAULT_KEY: EXCEL, const.CONTAINER_KEY: ContainerExcel, }, # raster FORMATS const.GEOTIFF_KEY: { const.RASTER_KEY: TIFF, const.DEFAULT_KEY: TIFF, const.CONTAINER_KEY: ContainerTiff }, const.ECW_KEY: { const.RASTER_KEY: ECW, const.DEFAULT_KEY: ECW, const.CONTAINER_KEY: ContainerEcw }, const.JP2_KEY: { const.RASTER_KEY: JP2, const.DEFAULT_KEY: JP2, const.CONTAINER_KEY: ContainerJP2 }, # Generic const.ZIP_KEY: { const.VECTOR_KEY: ZIPVector, const.GENERIC_KEY: ZIPGeneric, const.DEFAULT_KEY: ZIPVector, const.CONTAINER_KEY: ContainerZip }} def get_by_name(self, name, _type=const.DEFAULT_KEY, container=False): """ Get driver implementation by name Parameters ---------- name: str Identification string of the drivers. _type: str Type of the driver. VECTOR, THEMATIC .... container: bool Flag to set if the driver must be a container driver Returns ------- MasterDriver: Selected drivers Raises ------ DriverDoesNotExist: The driver selected has not be found. """ if name not in list(self._drivers.keys()): raise self.driver_does_not_exist('Driver {} does not exists'. format(name)) _type = _type if not container else const.CONTAINER_KEY if _type not in self._drivers[name].keys(): raise self.driver_does_not_exist('Driver {} does not ' 'support {} type' .format(name, _type)) return self._drivers[name][_type]() def get_by_field(self, field, value, _type=const.DEFAULT_KEY): """ Get driver by a field that define it. format, gdal_format, name ... Parameters ---------- field: str Field of the driver value: str or float Value for the field to search _type: str Type of the driver. VECTOR, THEMATIC .... Returns ------- MasterDriver: Driver selected Raises ------ DriverDoesNotExist: The driver selected has not be found. """ for driver in self._drivers: field_value = self._drivers[driver][_type]().__getattribute__(field) if field_value == value: return driver raise self.driver_does_not_exist('Driver with "{}={}" does not exist' .format(field, value)) def driver_list(self): """ Method to list all drivers implemented. Returns ------- list: List of the drivers """ return list(self._drivers.keys()) def check_drivers(self): """ Method execute at startup to check if the drivers have all the necessary methods implemented Returns ------- None Raises ------ NotImplementedError: If any driver has not be well implemented. """ for driver in self.driver_list(): for _type in self._drivers[driver].keys(): for interface in MasterDriver.interface: try: driver_obj = self.get_by_name(driver, _type=_type) driver_obj.__getattribute__(interface)() except NotImplementedError: raise NotImplementedError(" Driver {}:{} has not been " "implemented well.\n" " Implement {} method". format(driver, _type, interface)) except Exception: continue print('Driver checked')