def create(self, overwrite=False, defaultDataPath=None): """ Creates the repository configuration. @param overwrite: Flag indicating that an existing configuration can be removed before creating the new one. The default is C{False}. @type overwrite: C{bool} @param defaultDataPath: Optional path to a data root collection which is created as well. The path is located on the same file system as the configuration. An existing data root collection is not replaced. @type defaultDataPath: C{unicode} @raise ConfigurationError: Indicating problem on repository configuration creation. """ if self.isManagedRepository: try: if self._configurationCollection.exists(): if not overwrite: errorMessage = "The repository configuration '%s' " % self.repositoryConfigurationUri \ + "already exists and is not replaced by default." raise ConfigurationError(errorMessage) self._configurationCollection.delete() self._configurationCollection.createCollection(True) dataFileStorer = self._configurationCollection.fileSystem.createFileStorer( defaultDataPath) if not dataFileStorer.exists(): dataFileStorer.createCollection(True) except PersistenceError, error: raise ConfigurationError(error.message) else: self._dataModelHandler.create() self._dataStoreHandler.create(dataFileStorer.uri) self._iconHandler.create() self._scriptHandler.create()
def store(self): """ Stores the preferences. @raise ConfigurationError: Indicating problems on storage. """ try: if not self._fileStorer.exists(): self._fileStorer.createResource() stream = self._streamWriterClass(StringIO()) self._preferences.connections = list() for connectionUri in self._connectionOrder: connection = self._connections[connectionUri] if connection.password is None: encryptedPassword = None else: encryptedPassword = base64.encodestring(connection.password) copiedConnection = preferences.connection(connection.url, connection.username, encryptedPassword, connection.useLdap, connection.ldapServerUri, connection.ldapBaseDn, connection.useLucene, connection.luceneIndexUri, connection.defaultDataStore, connection.defaultArchiveStore, connection.defaultOfflineStore) if not copiedConnection.url is None: self._preferences.addConnections(copiedConnection) self._preferences.__dict__.update(self.__dict__) try: self._preferences.export(stream, 0) except ExpatError: raise ConfigurationError("Cannot persist preferences configuration.") stream.seek(0) self._fileStorer.writeData(stream) except PersistenceError, error: raise ConfigurationError("Unable to store preferences file.\nReason: '%s'" % error.message)
def load(self): """ Loads the data store configuration. """ try: if self._fileStorer.exists(): stream = self._fileStorer.readData() else: raise ConfigurationError( "The data store configuration does not exist.") except PersistenceError, error: raise ConfigurationError( "Cannot access the data store configuration.\nReason: '%s'" % error.message)
def load(self): """ Loads the data model. @raise ConfigurationError: Indicating problems on data model loading. """ try: if self._fileStorer.exists(): stream = self._fileStorer.readData() else: raise ConfigurationError("The data model does not exists.") except PersistenceError, error: raise ConfigurationError("Cannot load data model. Reason: '%s'" % error.message)
def create(self): """ Creates directory for script extensions. """ try: self._sourceFileStorer.createCollection() except PersistenceError, error: raise ConfigurationError("Cannot create script extensions collection. Reason: '%s'" % error.message)
def createDataStore(self, name=None, storeType=None, iconName="dataStore", url=None, isDefault=False, owner=None): """ Creates a data store configuration for the given type. @param name: Identifier of the data store configuration. @type name: C{unicode} @param storeType: Type of the data store configuration. @type storeType: C{unicode} """ dataStoreName = self._determineUniqueDataStoreName(name) if storeType is None: dataStore = _typeDataStoreClassMap[constants.DEFAULT_STORE]( dataStoreName, constants.DEFAULT_STORE, iconName, url, isDefault, owner) else: if storeType in _typeDataStoreClassMap: dataStore = _typeDataStoreClassMap[storeType](dataStoreName, storeType, iconName, url, isDefault, owner) else: raise ConfigurationError( "The data store type '%s' is not supported." % storeType) return dataStore
def load(self): """ Initializes everything with the installed icons. """ try: directoryFileStorer = createFileStorer("file:///" + LOCAL_INSTALLED_ICONS_DIRECTORY_PATH) except PersistenceError, error: raise ConfigurationError("Cannot parse local icon directory.\nReason:'%s'", error.message)
def removeIcon(self, icon): """ Removes an icon. @param icon: Icon to remove. @type icon: C{Icon} """ try: smallIconFileStorer = self._targetFileStorer.getChild(icon.smallName) if smallIconFileStorer.exists(): smallIconFileStorer.delete() largeIconFileStorer = self._targetFileStorer.getChild(icon.largeName) if largeIconFileStorer.exists(): largeIconFileStorer.delete() self._iconRegistry.unregister(self._location, icon) smallIconFileStorer = self._sourceFileStorer.getChild(icon.smallName) if smallIconFileStorer.exists(): smallIconFileStorer.delete() largeIconFileStorer = self._sourceFileStorer.getChild(icon.largeName) if largeIconFileStorer.exists(): largeIconFileStorer.delete() except PersistenceError, error: raise ConfigurationError("Cannot remove icon '%s'. Reason: '%s'" % (icon.baseName, error.message))
def parseIconDirectory(directoryFileStorer): """ Parses the given directory for icon files. @param directoryFileStorer: Allows access to the directory containing the icons. @type directoryFileStorer: L{FileStorer<datafinder.persistence.factory.FileStorer>} @return: List of icons. @rtype: C{list} of C{Icon} """ try: result = list() children = directoryFileStorer.getChildren() childrenNames = [child.name for child in children] smallSuffixLength = len(SMALL_ICONFILENAME_SUFFIX) for fileStorer in children: if fileStorer.name.endswith(SMALL_ICONFILENAME_SUFFIX): baseName = fileStorer.name[:len(fileStorer.name) - smallSuffixLength] largeIconFileName = baseName + LARGE_ICONFILENAME_SUFFIX if largeIconFileName in childrenNames: result.append(Icon(baseName, fileStorer.name, largeIconFileName, directoryFileStorer.identifier)) else: result.append(Icon(baseName, fileStorer.name, fileStorer.name, directoryFileStorer.identifier)) return result except PersistenceError, error: raise ConfigurationError("Cannot load the local installed icons. Reason: '%s'" % error.message)
def _registerScripts(self): """ Registers all retrieved scripts. """ scripts = list() try: children = self._targetFileStorer.getChildren() except PersistenceError, error: raise ConfigurationError("Cannot register repository-specific script extensions.\nReason: '%s'" % error.message)
def testConnectRepository(self): """ Test for the connectRepository method. """ self._repositoryMock.error = ConfigurationError("") self.assertRaises(ScriptApiError, repository.connectRepository, "", "", "", "")
def __exists(self): """ Getter of the existence flag. """ if self._exists is None: raise ConfigurationError( "Repository configuration is not initialized.") else: return self._exists
def testStore(self): """ Tests the storing of the repository configuration. """ self._configuration.store() self._dataModelHandlerMock.error = ConfigurationError("") self.assertRaises(ConfigurationError, self._configuration.store) self._configuration._configurationCollection = None # Test for unmanaged repository self._configuration.store()
def testDisconnectRepository(self): """ Test for the disconnectRepository method. """ self._repositoryManagerInstanceMock.error = ConfigurationError("") self.assertRaises(ScriptApiError, repository.disconnectRepository, repository.RepositoryDescription)
def create(self): """ Creates icon location of the repository. @raise ConfigurationError: Indicating problems on creation. """ try: self._sourceFileStorer.createCollection() except PersistenceError, error: raise ConfigurationError("Cannot create icon collection. Reason: '%s'" % error.message)
def create(self): """ Creates a new data model. @raise ConfigurationError: Indicating problems on creation. """ try: self._fileStorer.createResource() self.store() except PersistenceError, error: raise ConfigurationError("Cannot create the data model. Reason: '%s'" % error.message)
def removeRelation(self, name): """ Removes the relation. @param name: Name of the relation. @type name: C{unicode} """ if name == ROOT_RELATION_NAME: raise ConfigurationError("The removal of the root relation is not allowed.") if name in self._relations: del self._relations[name]
def addIcon(self, iconBaseName, localBaseDirectoryPath): """ Adds a new icon. @param iconBaseName: Base name of the new icon. @type iconBaseName: C{unicode} @param localBaseDirectoryPath: Path to the base directory for small and large icon representation. @type localBaseDirectoryPath: C{unicode} @raise ConfigurationError: Indicating problems on icon importing. """ try: str(iconBaseName) except UnicodeEncodeError: raise ConfigurationError("Currently only icon file names in ASCII encoding are supported.") else: try: baseDirectoryFileStorer = createFileStorer("file:///" + localBaseDirectoryPath) smallIconFileName = iconBaseName + SMALL_ICONFILENAME_SUFFIX largeIconFileName = iconBaseName + LARGE_ICONFILENAME_SUFFIX smallIconFileStorer = baseDirectoryFileStorer.getChild(smallIconFileName) largeIconFileStorer = baseDirectoryFileStorer.getChild(largeIconFileName) if not smallIconFileStorer.exists(): raise ConfigurationError("The small icon file '%s' cannot be found." % smallIconFileName) if not largeIconFileStorer.exists(): raise ConfigurationError("The large icon file '%s' cannot be found." % largeIconFileName) for smallIconDestination in [self._sourceFileStorer, self._targetFileStorer]: smallIconDestinationFileStorer = smallIconDestination.getChild(smallIconFileName) self._copy(smallIconFileStorer, smallIconDestinationFileStorer) for largeIconDestination in [self._sourceFileStorer, self._targetFileStorer]: largeIconDestinationFileStorer = largeIconDestination.getChild(largeIconFileName) self._copy(largeIconFileStorer, largeIconDestinationFileStorer) self._iconRegistry.register(self._location, [Icon(iconBaseName, smallIconFileName, largeIconFileName, localBaseDirectoryPath)]) except PersistenceError, error: raise ConfigurationError("Cannot add icon '%s'. Reason: '%s'" % (iconBaseName, error.message))
def load(self): """ Stores the icons in the target directory and register corresponding icons. @raise ConfigurationError: Indicating problems. """ self.__logger.info("Loading icons...") try: if not self._targetFileStorer.exists(): self._targetFileStorer.createCollection(True) except PersistenceError, error: raise ConfigurationError("Cannot prepare target icon location.\nReason: '%s'" % error.message)
def store(self): """ Stores the data store configuration. """ persistedDataStores = self._createPersistedDatastores() stream = self._streamWriterClass(StringIO()) persistedDataStores.export(stream, 0) stream.seek(0) try: self._fileStorer.writeData(stream) except PersistenceError, error: raise ConfigurationError( "Cannot store the data store configuration\nReason: '%s'" % error.message)
def executePreferences(self): """ Execute the preferences page. """ if self.hasPreferences: try: fileObject = self._baseDirFileStorer.getChild( constants.PREFERENCES_PAGE_MODULE_NAME).readData() except PersistenceError, error: raise ConfigurationError( "Cannot access script.\nReason: '%s'" % error.message) else: _loadModule(fileObject, constants.PREFERENCES_PAGE_MODULE_NAME, self._logger)
def addScript(self, scriptUri): """ Adds a new script. @param scriptUri: URI identifying the new script extension. @type scriptUri: C{unicode} @raise ConfigurationError: Indicating problems on icon importing. """ try: scriptFileStorer = createFileStorer(scriptUri) if not scriptFileStorer.exists(): raise ConfigurationError("The script '%s' cannot be found." % scriptUri) script = createScript(scriptFileStorer) for scriptDestination in [self._sourceFileStorer, self._targetFileStorer]: scriptDestinationFileStorer = scriptDestination.getChild(scriptFileStorer.name) self._copy(scriptFileStorer, scriptDestinationFileStorer) script = createScript(self._targetFileStorer.getChild(scriptFileStorer.name), self._location) self._scriptRegistry.register(self._location, [script]) except PersistenceError, error: raise ConfigurationError("Cannot add script '%s'. Reason: '%s'" % (scriptUri, error.message))
def testLoad(self): """ Tests the initialization of the script registry. """ self._createScriptMock.value = _ScriptMock(uri="uri") self._registry.load() self.assertEquals(len(self._registry.scripts), 1) self._createScriptMock.error = ConfigurationError("") self._registry.load() self.assertEquals(len(self._registry.scripts), 0) self._createFileStorerMock.error = PersistenceError("") self._registry.load() self.assertEquals(len(self._registry.scripts), 0)
def delete(self): """ Deletes the repository configuration. @raise ConfigurationError: Indicating problem on deletion. """ if self.isManagedRepository: try: if self._configurationCollection.exists(): self._configurationCollection.delete() except PersistenceError, error: errorMessage = "Cannot delete the repository configuration.\nReason: '%s'" % error.message raise ConfigurationError(errorMessage)
def load(self): """ Loads the scripts in the target directory and registers corresponding scripts. @raise ConfigurationError: Indicating problems. """ self.__logger.info("Loading scripts...") try: if not self._targetFileStorer.exists(): self._targetFileStorer.createCollection(True) except PersistenceError, error: raise ConfigurationError("Cannot access target location of repository-specific " \ + "script extensions.\nReason: '%s'" % error.message)
def load(self): """ Initializes configuration. @raise ConfigurationError: Indicating problem on configuration loading. """ if self.isManagedRepository: try: exists = self._configurationCollection.exists() except PersistenceError, error: raise ConfigurationError( "Cannot determine repository configuration existence.\nReason: '%s'" % error.message) else: if not exists: errorMessage = "The repository configuration '%s' does not exist." % self.repositoryConfigurationUri raise ConfigurationError(errorMessage) else: self._scriptHandler.load() self._dataModelHandler.load() self._dataStoreHandler.load() self._iconHandler.load()
def _parseScript(self): """ Parse the script and extract the meta data. """ dataStream = self._fileStorer.readData() try: try: line = dataStream.readline() while line: line = line.strip() self._parse(line) line = dataStream.readline() finally: dataStream.close() except IOError: raise ConfigurationError("Cannot read script data.")
def release(self): """ Releases acquired resources. @raise ConfigurationError: Indicating problem on resource release. """ if self.isManagedRepository: try: self._configurationCollection.fileSystem.release() self._dataStoreAccessManager.release() except PersistenceError, error: raise ConfigurationError( "Problem on releasing acquired configuration resources. Reason: '%s'" % error.message)
def importDataStores(self, localFilePath): """ Imports the data store configuration from a local file. @param localFilePath: Path to file on the local file system. @type localFilePath: C{unicode} """ try: localFileStorer = createFileStorer("file:///" + localFilePath) binaryStream = localFileStorer.readData() except PersistenceError, error: raise ConfigurationError( "Cannot import data store configuration.\nReason: '%s'" % error.message)
def store(self): """ Stores the current data model state. @raise ConfigurationError: Indicating problems on data model storage. """ persistedDatamodel = self._createPersistedDatamodel() stream = self._streamWriterClass(StringIO()) persistedDatamodel.export(stream, 0) stream.seek(0) try: self._fileStorer.writeData(stream) except PersistenceError, error: raise ConfigurationError("Cannot store the data model.\nReason: '%s'" % error.message)