def testSaveProject(self): uri = "file://" + os.path.abspath("testproject.xptv") uri2 = "file://" + os.path.abspath("testproject2.xptv") path = gst.uri_get_location(uri) path2 = gst.uri_get_location(uri2) # unlink any existing project files try: os.unlink(path) os.unlink(path2) except OSError: pass # save a project self.failUnless(self.manager.newBlankProject()) self.failUnless(self.manager.saveProject( self.manager.current, uri, True)) self.failUnless(uri_is_reachable(uri)) # wait a bit time.sleep(1.0) # save project under new path self.failUnless(self.manager.saveProject( self.manager.current, uri2, True)) self.failUnless(uri_is_reachable(uri2)) # make sure the old path and the new path have different mtime mtime = os.path.getmtime(path) mtime2 = os.path.getmtime(path2) self.failUnless(mtime < mtime2) # wait a bit more time.sleep(1.0) # save project again under new path (by omitting uri arg) self.failUnless(self.manager.saveProject( self.manager.current, overwrite=True)) # regression test for bug 594396 # make sure we didn't save to the old URI self.failUnlessEqual(mtime, os.path.getmtime(path)) # make sure we did save to the new URI self.failUnless(mtime2 < os.path.getmtime(path2)) # unlink any existing project files try: os.unlink(path) os.unlink(path2) except OSError: pass
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 testBackupProject(self): uri = "file://" + os.path.abspath("testproject.xptv") # Create and save the project self.manager.newBlankProject() self.manager.saveProject(self.manager.current, uri, True) # Save the backup self.manager._saveBackupCb(self.manager.current, uri) backup_uri = self.manager._makeBackupURI(uri) self.failUnless(uri_is_reachable(uri)) self.failUnless(uri_is_reachable(backup_uri)) # When closing it should clean the backup self.manager.closeRunningProject() self.failUnless(not uri_is_reachable(backup_uri)) # unlink any existing project files try: os.unlink(uri) os.unlink(backup_uri) except OSError: pass
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)