def db(self): _db = self.params.get('db', self.options.get('DB', 1)) try: _db = int(_db) except (ValueError, TypeError): raise ConfigError("db value must be an integer") return _db
def _init(self, server, params): super(RedisClient, self).__init__(params) self._server = server self._params = params unix_socket_path = None if ':' in self.server: host, port = self.server.rsplit(':', 1) try: port = int(port) except (ValueError, TypeError): raise ConfigError("port value must be an integer") else: host, port = None, None unix_socket_path = self.server kwargs = { 'db': self.db, 'password': self.password, 'host': host, 'port': port, 'unix_socket_path': unix_socket_path, } connection_pool = pool.get_connection_pool( parser_class=self.parser_class, connection_pool_class=self.connection_pool_class, connection_pool_class_kwargs=self.connection_pool_class_kwargs, **kwargs) self._client = redis.StrictRedis(connection_pool=connection_pool, **kwargs)
def load_urls(self): #加载app if self.settings.INSTALLED_APPS: for app in self.settings.INSTALLED_APPS: app_urls = import_object(app + '.urls.urls') self.urls.extend(app_urls) else: raise ConfigError('load urls error,INSTALLED_APPS not found!') return self.urls
def connection_pool_class(self): cls = self.options.get('POOL_CLASS', 'redis.ConnectionPool') mod_path, cls_name = cls.rsplit('.', 1) try: mod = import_object(mod_path) pool_class = getattr(mod, cls_name) except (AttributeError, ImportError): raise ConfigError("Could not find connection pool class '%s'" % cls) return pool_class
def parser_class(self): cls = self.options.get('PARSER_CLASS', None) if cls is None: return DefaultParser mod_path, cls_name = cls.rsplit('.', 1) try: mod = import_object(mod_path) parser_class = getattr(mod, cls_name) except (AttributeError, ImportError): raise ConfigError("Could not find parser class '%s'" % cls) return parser_class
def __getattr__(self, item): setting = self._get_settings() if hasattr(setting, item): config = getattr(setting, item) elif hasattr(global_settings, item): config = getattr(global_settings, item) else: raise ConfigError('settings "%s" not exist!' % item) return storage(config) if type(config) is dict else config
def get_settings(self, name): if not hasattr(self, '._setting'): try: settings_env = os.environ.get("TORNGAS_PROJECT_NAME", '') # self.settings_module = global_setttings self.settings_module = import_object('.'.join([settings_env, options.setting])) except ImportError: self.settings_module = global_settings warnings.warn('settings file import error. using global settings now.') self._config = self.settings_module if hasattr(self._config, name): return getattr(self._config, name) elif hasattr(self._config, name): return getattr(self._config, name) else: raise ConfigError('settings "%s" not exist!' % name)
def connetion(self): if hasattr(SqlConnection, '_conn'): return SqlConnection._conn else: with SqlConnection._conn_lock: connection_pool = storage() connections = settings.DATABASE_CONNECTION config = settings.SQLALCHEMY_CONFIGURATION for connection_name, connection_item in connections.items(): master = [] slaves = [] kwargs = connection_item.get('kwargs', {}) connections_str = connection_item['connections'] for conn in connections_str: dburl = url.URL(drivername=conn['DRIVER'], username=conn['UID'], password=conn['PASSWD'], host=conn['HOST'], port=conn['PORT'], database=conn['DATABASE'], query=conn['QUERY']) if conn['ROLE'] == _CONNECTION_TYPE[0]: master.append(dburl) else: slaves.append(dburl) if not len(master): raise ConfigError( 'conn:%s ,master connection not found.' % connection_name) try: connection_pool[connection_name] = SQLAlchemy( config, master_url=master[0], slaves_url=slaves, **kwargs) except Exception: raise SqlConnection._conn = connection_pool return SqlConnection._conn
def load_urls(self, urls=None): if urls: self.urls = urls return self urls = [] if self.settings.INSTALLED_APPS: for app_name in self.settings.INSTALLED_APPS: app_urls = import_object(app_name + '.urls.urls') for url in app_urls: url.kwargs['current_appname'] = app_name url.name = '%s-%s' % ( app_name, url.name, ) urls.extend(app_urls) else: raise ConfigError('load urls error,INSTALLED_APPS not found!') self.urls = urls return self
def get_settings(self, name): """ :param name: 配置名 :return:配置项 """ if not hasattr(self, '._config'): global_setttings = import_object('torngas.global_settings') if self._get_settings_env(): try: settings_module = import_object(self._get_settings_env()) except ImportError: settings_module = global_setttings warnings.warn('config import error. but now,using global settings.') else: settings_module = global_setttings self._config = settings_module if hasattr(self._config, name): return getattr(self._config, name) elif hasattr(self._config, name): return getattr(self._config, name) else: raise ConfigError('config "%s" not exist!' % name)
# import path to a custom backend. BACKENDS = { 'memcached': 'memcached', 'localcache': 'localcache', 'file': 'filebased', 'dummy': 'dummy', 'redis': 'rediscache' } DEFAULT_CACHE_ALIAS = 'default' DEFAULT_REDIS_ALIAS = 'default_redis' DEFAULT_MEMCACHED_ALIAS = 'default_memcache' DEFAULT_DUMMY_ALIAS = 'dummy' DEFAULT_FILEBASED_ALIAS = 'filebased' if DEFAULT_CACHE_ALIAS not in settings_helper.settings.CACHES: raise ConfigError("You must define a '%s' cache" % DEFAULT_CACHE_ALIAS) def parse_backend_conf(backend, **kwargs): """ Helper function to parse the backend configuration that doesn't use the URI notation. """ # Try to get the CACHES entry for the given backend name first conf = settings_helper.settings.CACHES.get(backend, None) if conf is not None: args = conf.copy() args.update(kwargs) backend = args.pop('BACKEND') location = args.pop('LOCATION', '') return backend, location, args