def read_file_keywords(layer_path, keyword=None): """Get metadata from the keywords file associated with a local file in the file system. .. note:: Requires a str representing a file path instance as parameter As opposed to readKeywordsFromLayer which takes a inasafe file object as parameter. .. seealso:: readKeywordsFromLayer :param: layer_path: a string representing a path to a layer (e.g. '/tmp/foo.shp', '/tmp/foo.tif') :type layer_path: str :param keyword: optional - the metadata keyword to retrieve e.g. 'title' :type keyword: str :return: A string containing the retrieved value for the keyword if the keyword argument is specified, otherwise the complete keywords dictionary is returned. :raises: KeywordNotFoundError, NoKeywordsFoundError, InvalidParameterError Note: * KeywordNotFoundError - occurs when the keyword is not recognised. * NoKeywordsFoundError - occurs when no keyword file exists. * InvalidParameterError - occurs when the layer does not exist. """ # check the source layer path is valid if not os.path.isfile(layer_path): message = tr('Cannot get keywords from a non-existent file.' '%s does not exist.' % layer_path) raise InvalidParameterError(message) # check there really is a keywords file for this layer keyword_file_path = os.path.splitext(layer_path)[0] keyword_file_path += '.keywords' if not os.path.isfile(keyword_file_path): #message = tr('No keywords file found for %s' % keyword_file_path) iface.messageBar().pushMessage("NoFileFound ",'No keywords file found for %s' % keyword_file_path,QgsMessageBar.CRITICAL,10) #raise NoKeywordsFoundError(message) # now get the requested keyword using the inasafe library dictionary = None try: dictionary = read_keywords(keyword_file_path) except Exception, e: message = \ tr('Keyword retrieval failed for %s (%s) \n %s' % ( keyword_file_path, keyword, str(e))) raise KeywordNotFoundError(message)
def read_file_keywords(layer_path, keyword=None): """Get metadata from the keywords file associated with a local file in the file system. .. note:: Requires a str representing a file path instance as parameter As opposed to readKeywordsFromLayer which takes a inasafe file object as parameter. .. seealso:: readKeywordsFromLayer :param: layer_path: a string representing a path to a layer (e.g. '/tmp/foo.shp', '/tmp/foo.tif') :type layer_path: str :param keyword: optional - the metadata keyword to retrieve e.g. 'title' :type keyword: str :return: A string containing the retrieved value for the keyword if the keyword argument is specified, otherwise the complete keywords dictionary is returned. :raises: KeywordNotFoundError, NoKeywordsFoundError, InvalidParameterError Note: * KeywordNotFoundError - occurs when the keyword is not recognised. * NoKeywordsFoundError - occurs when no keyword file exists. * InvalidParameterError - occurs when the layer does not exist. """ # check the source layer path is valid if not os.path.isfile(layer_path): message = tr('Cannot get keywords from a non-existent file.' '%s does not exist.' % layer_path) raise InvalidParameterError(message) # check there really is a keywords file for this layer keyword_file_path = os.path.splitext(layer_path)[0] keyword_file_path += '.keywords' if not os.path.isfile(keyword_file_path): message = tr('No keywords file found for %s' % keyword_file_path) raise NoKeywordsFoundError(message) # now get the requested keyword using the inasafe library dictionary = None try: dictionary = read_keywords(keyword_file_path) except Exception, e: message = \ tr('Keyword retrieval failed for %s (%s) \n %s' % ( keyword_file_path, keyword, str(e))) raise KeywordNotFoundError(message)
def read_file_keywords(theLayerPath, theKeyword=None): """Get metadata from the keywords file associated with a local file in the file system. .. note:: Requires a str representing a file path instance as parameter As opposed to readKeywordsFromLayer which takes a inasafe file object as parameter. .. seealso:: readKeywordsFromLayer Args: * theLayerPath - a string representing a path to a layer (e.g. '/tmp/foo.shp', '/tmp/foo.tif') * theKeyword - optional - the metadata keyword to retrieve e.g. 'title' Returns: A string containing the retrieved value for the keyword if the keyword argument is specified, otherwise the complete keywords dictionary is returned. Raises: KeywordNotFoundError if the keyword is not recognised. NoKeywordsFoundError if no keyword file exists. InvalidParameterError if the layer does not exist. """ # check the source layer path is valid if not os.path.isfile(theLayerPath): myMessage = tr('Cannot get keywords from a non-existent file.' '%s does not exist.' % theLayerPath) raise InvalidParameterError(myMessage) # check there really is a keywords file for this layer myKeywordFilePath = os.path.splitext(theLayerPath)[0] myKeywordFilePath += '.keywords' if not os.path.isfile(myKeywordFilePath): myMessage = tr('No keywords file found for %s' % myKeywordFilePath) raise NoKeywordsFoundError(myMessage) # now get the requested keyword using the inasafe library myDictionary = None try: myDictionary = read_keywords(myKeywordFilePath) except Exception, e: myMessage = \ tr('Keyword retrieval failed for %s (%s) \n %s' % ( myKeywordFilePath, theKeyword, str(e))) raise KeywordNotFoundError(myMessage)