################################################################################ Copyright (c) 2008 Zope Foundation and Contributors.# All Rights Reserved.## This software is subject to the provisions of the Zope Public License,# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS# FOR A PARTICULAR PURPOSE.###############################################################################"""ZConfig directive implementations for binding RelStorage to Zope"""from__future__importabsolute_importfromrelstorage.optionsimportOptionsfromrelstorage.storageimportRelStoragelogger=__import__('logging').getLogger(__name__)classBaseConfig(object):def__init__(self,config):self.config=configself.name=config.getSectionName()
[docs]classRelStorageFactory(BaseConfig):"""Open a storage configured via ZConfig"""defopen(self):config=self.config# Hoist the driver setting to the section we really want it.config.driver=config.adapter.config.driver# But don't remove it or otherwise mutate the config object;# that would prevent us from being correctly opened again.#config.adapter.config.driver = Noneoptions=Options.copy_valid_options(config)options.adapter=config.adapter# The adapter factories may modify the global options (or raise an exception)# if something at the top-level is specifically not allowed based on# their configuration.adapter=config.adapter.create(options)returnRelStorage(adapter,name=config.name,options=options)
classPostgreSQLAdapterFactory(BaseConfig):defcreate(self,options):from.adapters.postgresqlimportPostgreSQLAdapterreturnPostgreSQLAdapter(dsn=self.config.dsn,options=options)classOracleAdapterFactory(BaseConfig):defcreate(self,options):from.adapters.oracleimportOracleAdapterconfig=self.configreturnOracleAdapter(user=config.user,password=config.password,dsn=config.dsn,options=options)classMySQLAdapterFactory(BaseConfig):defcreate(self,options):from.adapters.mysqlimportMySQLAdapterparams={}forkeyinself.config.getSectionAttributes():ifkey=='driver':continuevalue=getattr(self.config,key)ifvalueisnotNone:params[key]=valuereturnMySQLAdapter(options=options,**params)classSQLitePragmas(BaseConfig):@propertydefpragmas(self):pragmas=self.config.pragmas# The keys could be repeated so the values are listspragmas={k:vfork,(v,)inpragmas.items()}# Ignore busy_timeout: That's the same thing as commit-lock-timeoutpragmas.pop('busy_timeout',None)forattrinself.config.getSectionAttributes():ifattr=='pragmas':continueval=getattr(self.config,attr)ifvalisnotNone:pragmas[attr]=valreturnpragmasclassSqlite3AdapterFactory(BaseConfig):defcreate(self,options):from.adapters.sqlite.adapterimportSqlite3Adapterifoptions.cache_local_dir:# A persistent cache makes absolutely no sense.# Disable.logger.info("Ignoring cache-local-dir setting.")deloptions.cache_local_dirifself.config.pragmas:pragmas=self.config.pragmas.pragmaselse:pragmas={}returnSqlite3Adapter(self.config.data_dir,pragmas=pragmas,options=options)