Beispiel #1
0
    def make(cls, backend=None, name=None, **kwargs):
        '''Create a new :class:`Backend` from a *backend* connection string
which is of the form::

    scheme:://host?params

For example, a parameter-less local backend is::

    local://

A redis backend could be::

    redis://127.0.0.1:6379?db=1&password=bla

:param backend: the connection string, if not supplied ``local://`` is used.
:param kwargs: additional key-valued parameters used by the :class:`Backend`
    during initialisation.
    '''
        if isinstance(backend, cls):
            return backend
        backend = backend or 'local://'
        scheme, address, params = parse_connection_string(backend,
                                                          default_port=0)
        if scheme == 'local':
            if address[0] == '0.0.0.0':
                address = ('', )
        path = cls.path_from_scheme(scheme)
        module = import_module(path)
        bcls = getattr(module, cls.__name__)
        con_str = bcls.get_connection_string(scheme, address, params, name)
        kwargs['name'] = name
        params.update(kwargs)
        if 'timeout' in params:
            params['timeout'] = int(params['timeout'])
        return bcls(scheme, con_str, **params)
Beispiel #2
0
    def make(cls, backend=None, name=None, **kwargs):
        """Create a new :class:`Backend` from a *backend* connection string
which is of the form::

    scheme:://host?params

For example, a parameter-less local backend is::

    local://

A redis backend could be::

    redis://127.0.0.1:6379?db=1&password=bla

:param backend: the connection string, if not supplied ``local://`` is used.
:param kwargs: additional key-valued parameters used by the :class:`Backend`
    during initialisation.
    """
        if isinstance(backend, cls):
            return backend
        backend = backend or "local://"
        scheme, address, params = parse_connection_string(backend, default_port=0)
        if scheme == "local":
            if address[0] == "0.0.0.0":
                address = ("",)
        path = cls.path_from_scheme(scheme)
        module = import_module(path)
        bcls = getattr(module, cls.__name__)
        con_str = bcls.get_connection_string(scheme, address, params, name)
        kwargs["name"] = name
        params.update(kwargs)
        if "timeout" in params:
            params["timeout"] = int(params["timeout"])
        return bcls(scheme, con_str, **params)
Beispiel #3
0
 def from_connection_string(self, connection_string, **kw):
     scheme, address, params = parse_connection_string(connection_string)
     if scheme == 'redis':
         params.update(kw)
         return self.redis(address, **params)
     else:
         raise ValueError('Use "redis" as connection string schema')
Beispiel #4
0
 def from_connection_string(self, connection_string, **kw):
     scheme, address, params = parse_connection_string(connection_string)
     if scheme == 'redis':
         params.update(kw)
         return self.redis(address, **params)
     else:
         raise ValueError('Use "redis" as connection string schema')
Beispiel #5
0
 def backend(cls, namespace):
     if namespace:
         backend = cls.cfg.backend_server or 'redis://127.0.0.1:6379'
         scheme, address, params = parse_connection_string(backend)
         params['namespace'] = namespace
         return get_connection_string(scheme, address, params)
     else:
         return cls.cfg.backend_server
Beispiel #6
0
 def backend(cls, namespace):
     if namespace:
         backend = cls.cfg.backend_server or 'redis://127.0.0.1:6379'
         scheme, address, params = parse_connection_string(backend)
         params['namespace'] = namespace
         return get_connection_string(scheme, address, params)
     else:
         return cls.cfg.backend_server
Beispiel #7
0
 def test_parse_tcp_with_https_and_params(self):
     scheme, address, params = parse_connection_string(
         'https://127.0.0.1:6439?db=3&bla=foo')
     self.assertEqual(scheme, 'https')
     self.assertEqual(address, ('127.0.0.1', 6439))
     self.assertEqual(params, {'db': '3', 'bla': 'foo'})
Beispiel #8
0
 def test_parse_unix(self):
     scheme, address, params = parse_connection_string('unix:bla.foo')
     self.assertEqual(scheme, '')
     self.assertEqual(address, 'bla.foo')
     self.assertEqual(params, {})
Beispiel #9
0
 def test_parse_unix_with_scheme(self):
     scheme, address, params = parse_connection_string(
         'redis://unix:bla.foo')
     self.assertEqual(scheme, 'redis')
     self.assertEqual(address, 'bla.foo')
     self.assertEqual(params, {})
Beispiel #10
0
 def test_parse_tcp_with_scheme_and_params(self):
     scheme, address, params = parse_connection_string('redis://:6439?db=3')
     self.assertEqual(scheme, 'redis')
     self.assertEqual(address, ('', 6439))
     self.assertEqual(params, {'db': '3'})
Beispiel #11
0
 def test_parse_tcp_default(self):
     scheme, address, params = parse_connection_string('127.0.0.1', 8095)
     self.assertEqual(scheme, '')
     self.assertEqual(address, ('127.0.0.1', 8095))
     self.assertEqual(params, {})
Beispiel #12
0
 def test_parse_unix(self):
     scheme, address, params = parse_connection_string('unix:bla.foo')
     self.assertEqual(scheme, '')
     self.assertEqual(address, 'bla.foo')
     self.assertEqual(params, {})
Beispiel #13
0
 def test_parse_tcp_with_scheme_and_params(self):
     scheme, address, params = parse_connection_string('redis://:6439?db=3')
     self.assertEqual(scheme, 'redis')
     self.assertEqual(address, ('', 6439))
     self.assertEqual(params, {'db': '3'})
Beispiel #14
0
 def test_parse_tcp_with_https_and_params(self):
     scheme, address, params = parse_connection_string(
         'https://127.0.0.1:6439?db=3&bla=foo')
     self.assertEqual(scheme, 'https')
     self.assertEqual(address, ('127.0.0.1', 6439))
     self.assertEqual(params, {'db': '3', 'bla': 'foo'})
Beispiel #15
0
 def test_parse_unix_with_scheme(self):
     scheme, address, params = parse_connection_string(
         'redis://unix:bla.foo')
     self.assertEqual(scheme, 'redis')
     self.assertEqual(address, 'bla.foo')
     self.assertEqual(params, {})
Beispiel #16
0
 def test_parse_tcp(self):
     scheme, address, params = parse_connection_string("127.0.0.1:8050")
     self.assertEqual(scheme, "")
     self.assertEqual(address, ("127.0.0.1", 8050))
     self.assertEqual(params, {})
Beispiel #17
0
 def test_parse_tcp_default(self):
     scheme, address, params = parse_connection_string('127.0.0.1', 8095)
     self.assertEqual(scheme, '')
     self.assertEqual(address, ('127.0.0.1', 8095))
     self.assertEqual(params, {})
Beispiel #18
0
 def test_parse_tcp_with_scheme_and_params(self):
     scheme, address, params = parse_connection_string("redis://:6439?db=3")
     self.assertEqual(scheme, "redis")
     self.assertEqual(address, ("", 6439))
     self.assertEqual(params, {"db": "3"})
Beispiel #19
0
 def test_parse_tcp_with_https_and_params(self):
     scheme, address, params = parse_connection_string("https://127.0.0.1:6439?db=3&bla=foo")
     self.assertEqual(scheme, "https")
     self.assertEqual(address, ("127.0.0.1", 6439))
     self.assertEqual(params, {"db": "3", "bla": "foo"})