def validateSourceURI(self, uri, factory): """ Makes sure the given uri is accessible for reading. Subclasses should call this method for any C{URI} they parse, in order to make sure they have the valid C{URI} on this given setup. @returns: The valid 'uri'. It might be different from the input. Sub-classes must use this for any URI they wish to read from. If no valid 'uri' can be found, None will be returned. @rtype: C{URI} or C{None} """ self.debug("uri:%s", uri) if not uri_is_valid(uri): self.warning("invalid URI") raise FormatterError("invalid URI", uri) # skip non local uri if not uri.split('://', 1)[0] in ["file"]: raise FormatterError("We can only handle file:// URI", uri) # first check the good old way if uri_is_valid(uri) and uri_is_reachable(uri): self.debug("URI is reachable") return uri self.debug("URI might have moved...") # else let's figure out if we have a compatible mapping for k, v in self.directorymapping.iteritems(): if uri.startswith(k): probable = uri.replace(k, v, 1) if uri_is_valid(probable) and uri_is_reachable(probable): return probable # else, let's fire the signal... self.emit('missing-uri', uri, factory) # and check again for k, v in self.directorymapping.iteritems(): self.debug("uri:%r, k:%r, v:%r", uri, k, v) if uri.startswith(k): probable = uri.replace(k, v, 1) if uri_is_valid(probable) and uri_is_reachable(probable): return probable # Houston, we have lost contact with mission://fail raise FormatterError("Couldn't find %s" % uri)
def saveProject(self, project, location, overwrite=False, backup=False): """ Saves the given project to the given location. @type project: L{Project} @param project: The Project to store. @type location: C{URI} @param location: The location where to store the project. Needs to be an absolute URI. @param overwrite: Whether to overwrite existing location. @type overwrite: C{bool} @raise FormatterURIError: If the location isn't a valid C{URI}. @raise FormatterOverwriteError: If the location already exists and overwrite is False. @raise FormatterSaveError: If the file couldn't be properly stored. """ if not uri_is_valid(location): raise FormatterURIError() if overwrite == False and uri_is_reachable(location): raise FormatterOverwriteError() if self._saveProject(project, location): if not backup: project.uri = location project.name = self._projectNameFromURI(location) self.emit("project-saved", project, location) return True return False
def saveProject(self, project, location, overwrite=False): """ Saves the given project to the given location. @type project: L{Project} @param project: The Project to store. @type location: C{URI} @param location: The location where to store the project. Needs to be an absolute URI. @param overwrite: Whether to overwrite existing location. @type overwrite: C{bool} @raise FormatterURIError: If the location isn't a valid C{URI}. @raise FormatterOverwriteError: If the location already exists and overwrite is False. @raise FormatterSaveError: If the file couldn't be properly stored. """ if not uri_is_valid(location): raise FormatterURIError() if overwrite == False and uri_is_reachable(location): raise FormatterOverwriteError() old_uri = project.uri if self._saveProject(project, location): project.uri = location project.name = self._projectNameFromURI(location) self.emit("project-saved", project, location) return True return False
def _validateUri(self, uri): # check if the location is # .. a uri # .. a valid uri # .. a reachable valid uri # FIXME : Allow subclasses to handle this for 'online' (non-file://) URI if not uri_is_valid(uri) or not uri_is_reachable(uri): raise FormatterURIError()
def _searchMissingFile(self, uri): """Search for a replacement for the specified file:// URI.""" for old_prefix, new_prefix in self.directorymapping.iteritems(): self.debug("uri:%r, k:%r, v:%r", uri, old_prefix, new_prefix) if uri.startswith(old_prefix): probable = uri.replace(old_prefix, new_prefix, 1) if uri_is_valid(probable) and uri_is_reachable(probable): return probable return None
def add_preview_request(self, dialogbox): """add a preview request """ uri = dialogbox.get_preview_uri() if uri is None or not uri_is_valid(uri): return self.log("Preview request for " + uri) self.clear_preview() self.current_selected_uri = uri if uri in self.preview_cache: # Already discovered self.log(uri + " already in cache") self.show_preview(uri) elif uri in self.preview_cache_errors: self.log(uri + " already in error cache") self.show_error(uri) else: self.log("Call discoverer for " + uri) self.discoverer.addUri(uri)
def validateSourceURI(self, uri, factory): """ Makes sure the given uri is accessible for reading. Subclasses should call this method for any C{URI} they parse, in order to make sure they have the valid C{URI} on this given setup. @returns: The valid 'uri'. It might be different from the input. Sub-classes must use this for any URI they wish to read from. If no valid 'uri' can be found, None will be returned. @rtype: C{URI} or C{None} """ self.debug("uri:%s", uri) if not uri_is_valid(uri): self.warning("invalid URI") raise FormatterError("invalid URI", uri) if uri_is_reachable(uri): self.debug("URI is reachable") return uri # The file might have been moved. probable = self._searchMissingFile(uri) if probable: # We already have a mapping which allowed us to find # the new position of the file. return probable # Inform the user that the file cannot be found. self.emit('missing-uri', uri, factory) # Check again, as emitting the missing-uri signal could result in # a new mapping in self.directorymapping. probable = self._searchMissingFile(uri) if probable: return probable # Houston, we have lost contact with mission://fail raise FormatterError("Couldn't find %s" % uri)