本文整理汇总了Python中beaker.container.NamespaceManager类的典型用法代码示例。如果您正苦于以下问题:Python NamespaceManager类的具体用法?Python NamespaceManager怎么用?Python NamespaceManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NamespaceManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, namespace, url=None, data_dir=None, lock_dir=None, **params):
NamespaceManager.__init__(self, namespace)
if not url:
raise MissingCacheParameter("url is required")
if lock_dir:
self.lock_dir = lock_dir
elif data_dir:
self.lock_dir = data_dir + "/container_tcd_lock"
else:
self.lock_dir = None
if self.lock_dir:
verify_directory(self.lock_dir)
conn_params = {}
parts = url.split('?', 1)
url = parts[0]
if len(parts) > 1:
conn_params = dict(p.split('=', 1) for p in parts[1].split('&'))
host, port = url.split(':', 1)
self.open_connection(host, int(port), **conn_params)
示例2: __init__
def __init__(self, namespace, url=None, data_dir=None, lock_dir=None, **params):
NamespaceManager.__init__(self, namespace)
if not url:
raise MissingCacheParameter("url is required")
if lock_dir:
self.lock_dir = lock_dir
elif data_dir:
self.lock_dir = data_dir + "/container_tcd_lock"
else:
self.lock_dir = None
if self.lock_dir:
verify_directory(self.lock_dir)
self.url = url
# parse the url properly using urlparse
url = urlparse.urlparse(url)
conn_params = {}
parts = url.path.split('?', 1)
path = parts[0]
if len(parts) > 1:
conn_params = dict(p.split('=', 1) for p in parts[1].split('&'))
self.host = url.hostname
self.port = url.port
self.username = url.username
self.password = url.password
self.scheme = url.scheme
self.open_connection(self.host, self.port, **conn_params)
示例3: __init__
def __init__(self, namespace, url,
memcache_module='auto',
data_dir=None, lock_dir=None,
**kw):
NamespaceManager.__init__(self, namespace)
_memcache_module = _client_libs[memcache_module]
if not url:
raise MissingCacheParameter("url is required")
if lock_dir:
self.lock_dir = lock_dir
elif data_dir:
self.lock_dir = data_dir + "/container_mcd_lock"
if self.lock_dir:
verify_directory(self.lock_dir)
# Check for pylibmc namespace manager, in which case client will be
# instantiated by subclass __init__, to handle behavior passing to the
# pylibmc client
if not _is_configured_for_pylibmc(memcache_module, _memcache_module):
self.mc = MemcachedNamespaceManager.clients.get(
(memcache_module, url),
_memcache_module.Client,
url.split(';'))
示例4: __init__
def __init__(self, namespace, url=None, data_dir=None, lock_dir=None, expire=None, **params):
NamespaceManager.__init__(self, namespace)
if not url:
raise MissingCacheParameter("url is required")
if lock_dir:
self.lock_dir = lock_dir
elif data_dir:
self.lock_dir = data_dir + "/container_tcd_lock"
if hasattr(self, 'lock_dir'):
verify_directory(self.lock_dir)
# Specify the serializer to use (pickle or json?)
self.serializer = params.pop('serializer', 'pickle')
self._expiretime = int(expire) if expire else None
conn_params = {}
parts = url.split('?', 1)
url = parts[0]
if len(parts) > 1:
conn_params = dict(p.split('=', 1) for p in parts[1].split('&'))
host, port = url.split(':', 1)
self.open_connection(host, int(port), **conn_params)
示例5: __init__
def __init__(self, namespace, url=None, data_dir=None, lock_dir=None, **params):
NamespaceManager.__init__(self, namespace)
if not url:
raise MissingCacheParameter("url is required")
if lock_dir:
self.lock_dir = lock_dir
elif data_dir:
self.lock_dir = data_dir + "/container_tcd_lock"
if hasattr(self, 'lock_dir'):
verify_directory(self.lock_dir)
conn_params = {}
parts = url.split('?', 1)
url = parts[0]
if len(parts) > 1:
conn_params = dict(p.split('=', 1) for p in parts[1].split('&'))
netloc = urlparse(url).netloc
parts = netloc.split(':')
port = parts.pop()
if port:
port = int(port)
host = ':'.join(parts)
self.open_connection(host, port, **conn_params)
示例6: __init__
def __init__(self, namespace, url, memcache_module='auto', data_dir=None,
lock_dir=None, **kw):
NamespaceManager.__init__(self, namespace)
if not url:
raise MissingCacheParameter("url is required")
if lock_dir:
self.lock_dir = lock_dir
elif data_dir:
self.lock_dir = data_dir + "/container_mcd_lock"
if self.lock_dir:
verify_directory(self.lock_dir)
if bmemcached is None:
raise ImportError('`bmemcached` is required.')
auth_args = []
username = kw.get('username', None)
password = kw.get('password', None)
if username:
auth_args.append(username)
if password:
auth_args.append(password)
logging.warn('WFT')
logging.warn((bmemcached, auth_args))
self.mc = MemcachedNamespaceManager.clients.get((memcache_module, url),
bmemcached.Client, url.split(';'), *auth_args)
示例7: __init__
def __init__(self, namespace, bind, table, data_dir=None, lock_dir=None,
**kwargs):
"""Create a namespace manager for use with a database table via
SQLAlchemy.
``bind``
SQLAlchemy ``Engine`` or ``Connection`` object
``table``
SQLAlchemy ``Table`` object in which to store namespace data.
This should usually be something created by ``make_cache_table``.
"""
NamespaceManager.__init__(self, namespace)
if lock_dir is not None:
self.lock_dir = lock_dir
elif data_dir is None:
raise MissingCacheParameter('data_dir or lock_dir is required')
else:
self.lock_dir = data_dir + '/container_db_lock'
verify_directory(self.lock_dir)
self.bind = self.__class__.binds.get(str(bind.url), lambda: bind)
self.table = self.__class__.tables.get('%s:%s' % (bind.url, table.name),
lambda: table)
self.hash = {}
self._is_new = False
self.loaded = False
示例8: __init__
def __init__(self, namespace, url=None, data_dir=None,
lock_dir=None, skip_pickle=False,
sparse_collection=False, **params):
NamespaceManager.__init__(self, namespace)
if not url:
raise MissingCacheParameter("MongoDB url is required")
if skip_pickle:
log.info("Disabling pickling for namespace: %s" % self.namespace)
self._pickle = False
if sparse_collection:
log.info(("Separating data to one row per key (sparse collection)"
" for ns %s .") % self.namespace)
self._sparse = True
# Temporarily uses a local copy of the functions until pymongo upgrades to new parser code
url_components = parse_uri(url)
host_list = url_components['nodelist']
username = url_components['username']
password = url_components['password']
collection = url_components['collection']
database = url_components['database']
options = url_components['options']
if database and host_list:
data_key = "mongodb:%s" % (database)
else:
raise MissingCacheParameter("Invalid Cache URL. Cannot parse.")
# Key will be db + collection
if lock_dir:
self.lock_dir = lock_dir
elif data_dir:
self.lock_dir = data_dir + "/container_mongodb_lock"
if hasattr(self, 'lock_dir'):
verify_directory(self.lock_dir)
def _create_mongo_conn():
host_uri = 'mongodb://'
for x in host_list:
host_uri += '%s:%s' % x
log.info("Host URI: %s" % host_uri)
conn = Connection(url, slave_okay=options.get('slaveok', False))
db = conn[database]
if username:
log.info("Attempting to authenticate %s/%s " % (username,
password))
if not db.authenticate(username, password):
raise InvalidCacheBackendError('Cannot authenticate to '
' MongoDB.')
return db[collection]
self.mongo = MongoDBNamespaceManager.clients.get(
data_key,
_create_mongo_conn)
示例9: __init__
def __init__(self, namespace, url=None, data_dir=None,
lock_dir=None, skip_pickle=False,
sparse_collection=False, use_file_lock=False, **params):
NamespaceManager.__init__(self, namespace)
if not url:
raise MissingCacheParameter("MongoDB url is required")
if skip_pickle:
log.info("Disabling pickling for namespace: %s" % self.namespace)
self._pickle = False
if sparse_collection:
log.info("Separating data to one row per key (sparse collection) for ns %s ." % self.namespace)
self._sparse = True
if use_file_lock:
log.info("Enabling file_locks for namespace: %s" % self.namespace)
self._use_file_lock = True
# Temporarily uses a local copy of the functions until pymongo upgrades to new parser code
(host_list, database, username, password, collection, options) = _parse_uri(url)
if database and host_list:
data_key = "mongodb:%s" % (database)
else:
raise MissingCacheParameter("Invalid Cache URL. Cannot parse.")
# Key will be db + collection
if lock_dir:
self.lock_dir = lock_dir
elif data_dir:
self.lock_dir = data_dir + "/container_mongodb_lock"
elif self._use_file_lock:
raise ImproperlyConfigured("Neither data_dir nor lock_dir are specified, while use_file_lock is set to True")
if self._use_file_lock and self.lock_dir:
verify_directory(self.lock_dir)
def _create_mongo_conn():
host_uri = 'mongodb://'
for x in host_list:
host_uri += '%s:%s' % x
log.info("Host URI: %s" % host_uri)
conn = Connection(host_uri, slave_okay=options.get('slaveok', False))
db = conn[database]
if username:
log.info("Attempting to authenticate %s/%s " % (username, password))
if not db.authenticate(username, password):
raise InvalidCacheBackendError('Cannot authenticate to '
' MongoDB.')
return db[collection]
self.mongo = MongoDBNamespaceManager.clients.get(data_key,
_create_mongo_conn)
示例10: __init__
def __init__(self, namespace, url, sa_opts=None, optimistic=False,
table_name='beaker_cache', data_dir=None, lock_dir=None,
**params):
"""Creates a database namespace manager
``url``
A SQLAlchemy database URL
``sa_opts``
A dictionary of SQLAlchemy keyword options to initialize the engine
with.
``optimistic``
Use optimistic session locking, note that this will result in an
additional select when updating a cache value to compare version
numbers.
``table_name``
The table name to use in the database for the cache.
"""
NamespaceManager.__init__(self, namespace, **params)
if sa_opts is None:
sa_opts = {}
if lock_dir is not None:
self.lock_dir = lock_dir
elif data_dir is None:
raise MissingCacheParameter("data_dir or lock_dir is required")
else:
self.lock_dir = data_dir + "/container_db_lock"
verify_directory(self.lock_dir)
# Check to see if the table's been created before
table_key = url + str(sa_opts) + table_name
def make_cache():
# Check to see if we have a connection pool open already
meta_key = url + str(sa_opts)
def make_meta():
if url.startswith('mysql') and not sa_opts:
sa_opts['poolclass'] = pool.QueuePool
engine = sa.create_engine(url, **sa_opts)
meta = sa.BoundMetaData(engine)
return meta
meta = DatabaseNamespaceManager.metadatas.get(meta_key, make_meta)
# Create the table object and cache it now
cache = sa.Table(table_name, meta,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('namespace', sa.String(255), nullable=False),
sa.Column('key', sa.String(255), nullable=False),
sa.Column('value', sa.BLOB(), nullable=False),
sa.UniqueConstraint('namespace', 'key')
)
cache.create(checkfirst=True)
return cache
self.cache = DatabaseNamespaceManager.tables.get(table_key, make_cache)
示例11: __init__
def __init__(self, namespace, url=None, data_dir=None,
lock_dir=None, skip_pickle=False,
sparse_collection=False, **params):
NamespaceManager.__init__(self, namespace)
if not url:
raise MissingCacheParameter("MongoDB url is required")
if skip_pickle:
log.info("Disabling pickling for namespace: %s" % self.namespace)
_pickle = False
if sparse_collection:
log.info("Separating data to one row per key (sparse collection) for ns %s ." % self.namespace)
self._sparse = True
conn_params = parse_mongo_url(url)
if conn_params['database'] and conn_params['host'] and \
conn_params['collection']:
data_key = "mongodb:%s#%s" % (conn_params['database'],
conn_params['collection'])
else:
raise MissingCacheParameter("Invalid Cache URL. Cannot parse"
" host, database and/or "
" collection name.")
conn_params['slave_okay'] = params.get('slave_okay') == 'True'
# Key will be db + collection
if lock_dir:
self.lock_dir = lock_dir
elif data_dir:
self.lock_dir = data_dir + "/container_mongodb_lock"
if self.lock_dir:
verify_directory(self.lock_dir)
def _create_mongo_conn():
conn = pymongo.connection.Connection(conn_params['host'],\
conn_params['port'], slave_okay=conn_params['slave_okay'])
db = conn[conn_params['database']]
if conn_params['username'] and conn_params['password']:
log.info("Attempting to authenticate %s/%s " %
(conn_params['username'], conn_params['password']))
if not db.authenticate(conn_params['username'],
conn_params['password']):
raise InvalidCacheBackendError('Cannot authenticate to '
' MongoDB.')
return db[conn_params['collection']]
self.mongo = MongoDBNamespaceManager.clients.get(data_key,
_create_mongo_conn)
示例12: __init__
def __init__(self, namespace, url, data_dir=None, lock_dir=None, **params):
NamespaceManager.__init__(self, namespace, **params)
if lock_dir is not None:
self.lock_dir = lock_dir
elif data_dir is None:
raise MissingCacheParameter("data_dir or lock_dir is required")
else:
self.lock_dir = data_dir + "/container_mcd_lock"
verify_directory(self.lock_dir)
self.mc = MemcachedNamespaceManager.clients.get(url, lambda: memcache.Client(url.split(';'), debug=0))
示例13: __init__
def __init__(self, namespace, url=None, data_dir=None, lock_dir=None, **params):
NamespaceManager.__init__(self, namespace)
if not url:
raise MissingCacheParameter("url is required")
if lock_dir:
self.lock_dir = lock_dir
elif data_dir:
self.lock_dir = data_dir + "/container_mcd_lock"
if self.lock_dir:
verify_directory(self.lock_dir)
self.mc = MemcachedNamespaceManager.clients.get(url, memcache.Client, url.split(';'))
示例14: __init__
def __init__(self, namespace, url=None, data_dir=None, lock_dir=None, **params):
NamespaceManager.__init__(self, namespace)
if not url:
raise MissingCacheParameter("url is required")
if lock_dir:
self.lock_dir = lock_dir
elif data_dir:
self.lock_dir = data_dir + "/container_tcd_lock"
if self.lock_dir:
verify_directory(self.lock_dir)
host, port = url.split(':')
self.open_connection(host, int(port))
示例15: __init__
def __init__(self, namespace, url, memcache_module="auto", data_dir=None, lock_dir=None, **kw):
NamespaceManager.__init__(self, namespace)
_memcache_module = _client_libs[memcache_module]
if not url:
raise MissingCacheParameter("url is required")
if lock_dir:
self.lock_dir = lock_dir
elif data_dir:
self.lock_dir = data_dir + "/container_mcd_lock"
if self.lock_dir:
verify_directory(self.lock_dir)
self.mc = MemcachedNamespaceManager.clients.get((memcache_module, url), _memcache_module.Client, url.split(";"))