def normalize_uri(self, layer): """Normalize URI from layer source. URI can be in a form of QgsDataSourceURI which is related to RDBMS source or general URI like CSV URI :param layer : A layer :type layer : QgsMapLayer :return: normalized URI to be hashed :raise: AttributeError if providerType not recognized """ # there are different method to extract unique uri based on # layer type try: provider_type = str(layer.providerType()) except AttributeError: raise UnsupportedProviderError( 'Could not determine type for provider: %s' % layer.__class__.__name__) source = layer.source() if provider_type == 'postgres': # Use QgsDataSourceURI to parse RDBMS datasource # create unique uri based on host, schema, and tablename datasource_uri = QgsDataSourceURI(source) normalized_uri = ':'.join([ datasource_uri.host(), datasource_uri.schema(), datasource_uri.table()]) elif provider_type == 'delimitedtext': # Use urlparse to parse delimitedtext uri # create unique uri based on protocol, host, and path general_uri = urlparse(source) normalized_uri = ':'.join([ general_uri.scheme, general_uri.netloc, general_uri.path]) else: normalized_uri = source return normalized_uri
def are_keywords_file_based(self, layer): """Check if keywords should be read/written to file or our keywords db. Determine which keyword lookup system to use (file base or cache db) based on the layer's provider type. True indicates we should use the datasource as a file and look for a keywords file, False and we look in the keywords db. :param layer: The layer which want to know how the keywords are stored. :type layer: QgsMapLayer :returns: True if keywords are stored in a file next to the dataset, else False if the dataset is remove e.g. a database. :rtype: bool :raises: UnsupportedProviderError """ try: provider_type = str(layer.providerType()) except AttributeError: raise UnsupportedProviderError( 'Could not determine type for provider: %s' % layer.__class__.__name__) provider_dict = { 'ogr': True, 'gdal': True, 'gpx': False, 'wms': False, 'spatialite': False, 'delimitedtext': False, 'postgres': False } file_based_keywords = False if provider_type in provider_dict: file_based_keywords = provider_dict[provider_type] return file_based_keywords