Exemplo n.º 1
0
 def __init__(self, config, opts, args):
     """ :See: `wtf.services.ServiceInterface.__init__` """
     section = config.memcache
     servertokens = tuple(section.servers)
     pools = []
     for server in servertokens:
         key = u'memcache %s' % server
         if key in config:
             subsection = config[key]
         else:
             subsection = section
         server = _util.parse_socket_spec(server, _memcache.DEFAULT_PORT)
         pools.append(_memcache.MemcacheConnectionPool(
             subsection('maxconn', section('maxconn', 0)),
             subsection('maxcached', section('maxcached', 0)),
             server,
             weight=subsection('weight', section('weight', None)),
             timeout=subsection('timeout', section('timeout', 2.6)),
         ))
     max_age = unicode(section('max_age', u'')) or None
     if max_age is not None:
         max_age = int(max_age)
     self._mc = GlobalMemcache(pools,
         max_age,
         section('grace_time', None),
         section('retry_time', None),
         section('compress_threshold', None),
         section('padded', None),
         section('split', None),
         unicode(section('prefix', u'')).encode('utf-8'),
         section('largest_slab', None),
     )
Exemplo n.º 2
0
    def __init__(self, config, opts, args):
        """ :See: `session.StorageFactoryInterface.__init__` """
        # pylint: disable = E1103, R0912, R0915
        try:
            section = config['session:sharedance']
        except KeyError:
            section = dict()

        if 'timeout' in section:
            timeout = max(0.0, float(section.timeout))
        else:
            timeout = self._DEFAULT_TIMEOUT

        if 'weight' in section:
            weight = max(0, int(section.weight))
        else:
            weight = 1

        if 'compress_threshold' in section:
            compress_threshold = unicode(section.compress_threshold) and \
                int(section.compress_threshold) or None
        else:
            compress_threshold = 128

        if 'server' in section:
            servers = []
            for spec in section.server:
                key = 'session:sharedance %s' % spec
                spec = _util.parse_socket_spec(unicode(spec),
                    default_port=_sharedance.DEFAULT_PORT)
                if key in config:
                    subtimeout = \
                        max(0.0, float(config[key]('timeout', timeout)))
                    subweight = max(0, int(config[key]('weight', weight)))
                else:
                    subtimeout, subweight = timeout, weight
                servers.append(_sharedance.SharedanceConnector(
                    spec, compress_threshold=compress_threshold,
                    timeout=subtimeout, weight=subweight, magic=True
                ))
        else:
            servers = [_sharedance.SharedanceConnector(
                (self._DEFAULT_HOST, _sharedance.DEFAULT_PORT),
                compress_threshold=compress_threshold,
                timeout=timeout, weight=1, magic=True
            )]

        self._sd = _sharedance.Sharedance(servers)

        if 'refresh' in section:
            refresh = unicode(section.refresh)
        else:
            refresh = u'auto'
        if refresh == u'auto':
            self._refresh = 60
        else:
            self._refresh = int(refresh)

        if 'cookie' in section:
            cookie = section.cookie
        else:
            cookie = dict().get
        sign = cookie('sign', u'') or None
        if sign:
            sign = sign.encode('ascii').decode('base64')
        domain = cookie('domain', u'') or None
        if domain:
            if domain.startswith(u'.'):
                domain = (u'x' + domain).encode('idna')[1:]
            else:
                domain = domain.encode('idna')
        self._cookie = dict(
            name=cookie('name', u's').encode('ascii'),
            max_age=int(cookie('max_age', 0)) or None,
            path=unicode(cookie('path', u'/')).encode('ascii'),
            domain=domain,
            sign=sign,
        )