def write_keywords(self, layer, keywords): """Write keywords for a datasource. This is a wrapper method that will 'do the right thing' to store keywords for the given datasource. In particular, if the datasource is remote (e.g. a database connection) it will write the keywords from the keywords store. :param layer: A QGIS QgsMapLayer instance. :type layer: QgsMapLayer :param keywords: A dict containing all the keywords to be written for the layer. :type keywords: dict :raises: UnsupportedProviderError """ try: flag = self.are_keywords_file_based(layer) except UnsupportedProviderError: raise source = layer.source() try: if flag: write_keywords_to_file(source, keywords) else: uri = self.normalize_uri(layer) self.write_keywords_for_uri(uri, keywords) return except: raise
def copy_keywords( self, source_layer, destination_file, extra_keywords=None): """Helper to copy the keywords file from a source to a target dataset. e.g.:: copyKeywords('foo.shp', 'bar.shp') Will result in the foo.keywords file being copied to bar.keyword. Optional argument extraKeywords is a dictionary with additional keywords that will be added to the destination file e.g:: copyKeywords('foo.shp', 'bar.shp', {'resolution': 0.01}) :param source_layer: A QGIS QgsMapLayer instance. :type source_layer: QgsMapLayer :param destination_file: The output filename that should be used to store the keywords in. It can be a .shp or a .keywords for example since the suffix will always be replaced with .keywords. :type destination_file: str :param extra_keywords: A dict containing all the extra keywords to be written for the layer. The written keywords will consist of any original keywords from the source layer's keywords file and and the extra keywords (which will replace the source layers keywords if the key is identical). :type extra_keywords: dict """ keywords = self.read_keywords(source_layer) if extra_keywords is None: extra_keywords = {} message = self.tr( 'Expected extraKeywords to be a dictionary. Got ' '%s' % str(type(extra_keywords))[1:-1]) verify(isinstance(extra_keywords, dict), message) # compute the output keywords file name destination_base = os.path.splitext(destination_file)[0] new_destination = destination_base + '.keywords' # write the extra keywords into the source dict try: for key in extra_keywords: keywords[key] = extra_keywords[key] write_keywords_to_file(new_destination, keywords) except Exception, e: message = self.tr( 'Failed to copy keywords file from : \n%s\nto\n%s: %s' % ( source_layer.source(), new_destination, str(e))) raise Exception(message)