def create_cluster(settings): """ redis = create_cluster({ 'engine': 'nydus.db.backends.redis.Redis', 'router': 'nydus.db.routers.redis.PartitionRouter', 'hosts': { 0: {'db': 0}, 1: {'db': 1}, 2: {'db': 2}, } }) """ # Pull in our client if isinstance(settings["engine"], basestring): Conn = import_string(settings["engine"]) else: Conn = settings["engine"] # Pull in our router router = settings.get("router") if isinstance(router, basestring): router = import_string(router) elif router: router = router else: router = BaseRouter # Build the connection cluster return Cluster( router=router, hosts=dict( (conn_number, Conn(num=conn_number, **host_settings)) for conn_number, host_settings in settings["hosts"].iteritems() ), )
def create_cluster(settings): """ redis = create_cluster({ 'engine': 'nydus.db.backends.redis.Redis', 'router': 'nydus.db.routers.redis.PartitionRouter', 'hosts': { 0: {'db': 0}, 1: {'db': 1}, 2: {'db': 2}, } }) """ # Pull in our client if isinstance(settings['engine'], basestring): conn = import_string(settings['engine']) else: conn = settings['engine'] # Pull in our router router = settings.get('router') if isinstance(router, basestring): router = import_string(router)() elif router: router = router() else: router = BaseRouter() # Build the connection cluster return Cluster( router=router, hosts=dict( (conn_number, conn(num=conn_number, **host_settings)) for conn_number, host_settings in settings['hosts'].iteritems()), )
def create_cluster(settings): """ Creates a new Nydus cluster from the given settings. :param settings: Dictionary of the cluster settings. :returns: Configured instance of ``nydus.db.base.Cluster``. >>> redis = create_cluster({ >>> 'backend': 'nydus.db.backends.redis.Redis', >>> 'router': 'nydus.db.routers.redis.PartitionRouter', >>> 'defaults': { >>> 'host': 'localhost', >>> 'port': 6379, >>> }, >>> 'hosts': { >>> 0: {'db': 0}, >>> 1: {'db': 1}, >>> 2: {'db': 2}, >>> } >>> }) """ # Pull in our cluster cluster = settings.get('cluster') if not cluster: Cluster = BaseCluster elif isinstance(cluster, basestring): Cluster = import_string(cluster) else: Cluster = cluster # Pull in our client backend = settings.get('engine', settings.get('backend')) if isinstance(backend, basestring): Conn = import_string(backend) elif backend: Conn = backend else: raise KeyError('backend') # Pull in our router router = settings.get('router') if isinstance(router, basestring): router = import_string(router) elif router: router = router else: router = BaseRouter defaults = settings.get('defaults', {}) # Build the connection cluster return Cluster( router=router, hosts=dict( (conn_number, Conn(num=conn_number, **apply_defaults(host_settings, defaults))) for conn_number, host_settings in settings['hosts'].iteritems() ), )
def create_cluster(settings): """ Creates a new Nydus cluster from the given settings. :param settings: Dictionary of the cluster settings. :returns: Configured instance of ``nydus.db.base.Cluster``. >>> redis = create_cluster({ >>> 'backend': 'nydus.db.backends.redis.Redis', >>> 'router': 'nydus.db.routers.redis.PartitionRouter', >>> 'defaults': { >>> 'host': 'localhost', >>> 'port': 6379, >>> }, >>> 'hosts': { >>> 0: {'db': 0}, >>> 1: {'db': 1}, >>> 2: {'db': 2}, >>> } >>> }) """ # Pull in our client settings = copy.deepcopy(settings) backend = settings.pop('engine', settings.pop('backend', None)) if isinstance(backend, basestring): Conn = import_string(backend) elif backend: Conn = backend else: raise KeyError('backend') # Pull in our cluster cluster = settings.pop('cluster', None) if not cluster: Cluster = Conn.get_cluster() elif isinstance(cluster, basestring): Cluster = import_string(cluster) else: Cluster = cluster # Pull in our router router = settings.pop('router', None) if not router: Router = BaseRouter elif isinstance(router, basestring): Router = import_string(router) else: Router = router # Build the connection cluster return Cluster( router=Router, backend=Conn, **settings )
def create_cluster(settings): """ Creates a new Nydus cluster from the given settings. :param settings: Dictionary of the cluster settings. :returns: Configured instance of ``nydus.db.base.Cluster``. >>> redis = create_cluster({ >>> 'backend': 'nydus.db.backends.redis.Redis', >>> 'router': 'nydus.db.routers.redis.PartitionRouter', >>> 'defaults': { >>> 'host': 'localhost', >>> 'port': 6379, >>> }, >>> 'hosts': { >>> 0: {'db': 0}, >>> 1: {'db': 1}, >>> 2: {'db': 2}, >>> } >>> }) """ # Pull in our client settings = copy.deepcopy(settings) backend = settings.pop('engine', settings.pop('backend', None)) if isinstance(backend, basestring): Conn = import_string(backend) elif backend: Conn = backend else: raise KeyError('backend') # Pull in our cluster cluster = settings.pop('cluster', None) if not cluster: Cluster = Conn.get_cluster() elif isinstance(cluster, basestring): Cluster = import_string(cluster) else: Cluster = cluster # Pull in our router router = settings.pop('router', None) if not router: Router = BaseRouter elif isinstance(router, basestring): Router = import_string(router) else: Router = router # Build the connection cluster return Cluster(router=Router, backend=Conn, **settings)
def __init__(self, backend, name, host=None, port=None, test_name=None, user=None, password=None, options={}, **kwargs): """ Given an alias (which is defined in DATABASES), creates a new connection that proxies the original database engine. """ if isinstance(backend, basestring): backend = import_string(backend) self.backend = backend self.settings_dict = { 'HOST': host, 'PORT': port, 'NAME': name, 'TEST_NAME': test_name, 'OPTIONS': options, 'USER': user, 'PASSWORD': password, } self.wrapper = __import__('%s.base' % (backend.__name__, ), {}, {}, ['DatabaseWrapper']).DatabaseWrapper( self.settings_dict) super(DjangoDatabase, self).__init__(**kwargs)
def __init__(self, backend, name, host=None, port=None, test_name=None, user=None, password=None, options={}, **kwargs): """ Given an alias (which is defined in DATABASES), creates a new connection that proxies the original database engine. """ if isinstance(backend, basestring): backend = import_string(backend) self.backend = backend self.settings_dict = { 'HOST': host, 'PORT': port, 'NAME': name, 'TEST_NAME': test_name, 'OPTIONS': options, 'USER': user, 'PASSWORD': password, } self.wrapper = __import__('%s.base' % (backend.__name__,), {}, {}, ['DatabaseWrapper']).DatabaseWrapper(self.settings_dict) super(DjangoDatabase, self).__init__(**kwargs)