本文整理汇总了Python中couchdb.client.Server.create方法的典型用法代码示例。如果您正苦于以下问题:Python Server.create方法的具体用法?Python Server.create怎么用?Python Server.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类couchdb.client.Server
的用法示例。
在下文中一共展示了Server.create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CouchDBBackend
# 需要导入模块: from couchdb.client import Server [as 别名]
# 或者: from couchdb.client.Server import create [as 别名]
class CouchDBBackend(object):
@property
def db(self):
return self.server[self.db_name]
def __init__(self, config):
settings = config.registry.settings
self.config = config
self.server = Server(settings['backend.db_host'])
self.db_name = os.environ.get('DB_NAME', settings['backend.db_name'])
self.create_db_if_not_exist()
self.sync_views()
self.config.add_subscriber(self.add_db_to_request, NewRequest)
def delete_db(self):
del self.server[self.db_name]
def create_db_if_not_exist(self):
try:
self.server.create(self.db_name)
logger.debug('Creating and using db "%s"' % self.db_name)
except PreconditionFailed:
logger.debug('Using db "%s".' % self.db_name)
def sync_views(self):
ViewDefinition.sync_many(self.db, docs)
def add_db_to_request(self, event):
event.request.db = Database(self.db)
示例2: _open_couch_db
# 需要导入模块: from couchdb.client import Server [as 别名]
# 或者: from couchdb.client.Server import create [as 别名]
def _open_couch_db(dbname):
server = Server(url=self._couch_url)
try:
server[dbname]
except ResourceNotFound:
server.create(dbname)
return server[dbname]
示例3: CouchDBBackend
# 需要导入模块: from couchdb.client import Server [as 别名]
# 或者: from couchdb.client.Server import create [as 别名]
class CouchDBBackend(object):
@classmethod
def load_from_config(cls, config):
settings = config.registry.settings
return CouchDBBackend(
host=settings['backend.db_host'],
db_name=os.environ.get('DB_NAME', settings['backend.db_name']),
)
def __init__(self, host, db_name, id_generator):
self.server = Server(host)
self.db_name = db_name
try:
self.create_db_if_not_exist()
except socket.error as e:
raise CouchDBBackendConnectionError(
"Unable to connect to the CouchDB server: %s - %s" % (host, e))
self._db = self.server[self.db_name]
self.sync_views()
def delete_db(self):
del self.server[self.db_name]
def create_db_if_not_exist(self):
try:
self.server.create(self.db_name)
logger.info('Creating and using db "%s"' % self.db_name)
except PreconditionFailed:
logger.info('Using db "%s".' % self.db_name)
def sync_views(self):
ViewDefinition.sync_many(self.server[self.db_name], docs)
def __get_raw_user_token(self, user_id):
try:
return views.usertokens(self._db, key=user_id).rows[0].value
except IndexError:
raise UserIdNotFound(user_id)
def get_user_token(self, user_id):
"""Returns the information associated with a user token"""
usertoken = dict(**self.__get_raw_user_token(user_id))
return usertoken['token']
def add_token(self, user_id, token):
# Check that the token doesn't already exist.
try:
self.__get_raw_user_token(user_id)
raise UserIdAlreadyExist(user_id)
except UserIdNotFound:
pass
doc = dict(token=token, user_id=user_id, type='usertoken')
self._db.save(doc)
示例4: get_db
# 需要导入模块: from couchdb.client import Server [as 别名]
# 或者: from couchdb.client.Server import create [as 别名]
def get_db(self, db_uri, db_name):
server = Server(db_uri)
try:
db = server[db_name]
except ResourceNotFound:
db = server.create(db_name)
return db
示例5: couch_connect
# 需要导入模块: from couchdb.client import Server [as 别名]
# 或者: from couchdb.client.Server import create [as 别名]
def couch_connect():
server = Server()
try:
db = server.create('feedme')
except:
db = server['feedme']
return db
示例6: connector
# 需要导入模块: from couchdb.client import Server [as 别名]
# 或者: from couchdb.client.Server import create [as 别名]
def connector(collection):
# Make a server connection
svr = Server()
if collection not in svr:
return svr.create(collection)
else:
return svr[collection]
示例7: saveDocument
# 需要导入模块: from couchdb.client import Server [as 别名]
# 或者: from couchdb.client.Server import create [as 别名]
def saveDocument(self):
server = Server()
server.resource.credentials = ('secret', 'aedca29aed23e103f27f8e29dd6ec473')
# create database
db = server.create("new.job")
# associate NewJob to the db
NewJob.set_db(db)
self.save()
示例8: Create_Couchdb_Instance
# 需要导入模块: from couchdb.client import Server [as 别名]
# 或者: from couchdb.client.Server import create [as 别名]
def Create_Couchdb_Instance(dtbs_name):
server = Server("http://127.0.0.1:5984")
try:
db = server.create(dtbs_name)
except Exception:
db = server[dtbs_name]
return db
示例9: initDatabase
# 需要导入模块: from couchdb.client import Server [as 别名]
# 或者: from couchdb.client.Server import create [as 别名]
def initDatabase():
server = Server('http://localhost:5984')
del server['lazycrawler']
try:
db = server.create('lazycrawler')
except Exception:
db = server['lazycrawler']
return db
示例10: main
# 需要导入模块: from couchdb.client import Server [as 别名]
# 或者: from couchdb.client.Server import create [as 别名]
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
authn_policy = AuthTktAuthenticationPolicy(
'sosecret', hashalg='sha512')
authz_policy = ACLAuthorizationPolicy()
config = Configurator(settings=settings, root_factory=RootFactory)
config.set_authentication_policy(authn_policy)
config.set_authorization_policy(authz_policy)
dbserver = Server(url = settings['couchdb.url'])
if settings['couchdb.dbname'] not in dbserver:
dbserver.create(settings['couchdb.dbname'])
config.registry.settings['couchdb.server'] = dbserver
config.add_renderer(".html", "pyramid.mako_templating.renderer_factory")
config.add_static_view('static', 'static', cache_max_age=3600)
config.add_route('home', '/')
config.add_route('item', '/item/*traverse', factory=Container)
config.add_route('tag', '/tag/*traverse', factory=Tags)
config.scan()
return config.make_wsgi_app()
示例11: init_boards
# 需要导入模块: from couchdb.client import Server [as 别名]
# 或者: from couchdb.client.Server import create [as 别名]
def init_boards():
"""
:rtype : object
"""
server = Server()
try:
db = server.create("boards")
except Exception:
db = server["boards"]
return db
示例12: __init__
# 需要导入模块: from couchdb.client import Server [as 别名]
# 或者: from couchdb.client.Server import create [as 别名]
def __init__(self, db_uri):
local.application = self
server = Server(db_uri)
try:
db = server.create("urls")
except Exception:
db = server["urls"]
self.dispatch = SharedDataMiddleware(self.dispatch, {"/static": STATIC_PATH})
URL.db = db
示例13: __init__
# 需要导入模块: from couchdb.client import Server [as 别名]
# 或者: from couchdb.client.Server import create [as 别名]
def __init__(self, db_uri):
local.application = self
server = Server(db_uri)
try:
db = server.create('urls')
except:
db = server['urls']
self.dispatch = SharedDataMiddleware(self.dispatch, {
'/static': STATIC_PATH
})
URL.db = db
示例14: Connection
# 需要导入模块: from couchdb.client import Server [as 别名]
# 或者: from couchdb.client.Server import create [as 别名]
class Connection(object):
"""
Connect to CouchDb according to params in the settings.py file
and store that internally.
Access is made with this class cause it's a singleton.
"""
_inst = None
def __new__(cls, address=None, port=None, db_name=None, *args, **kwargs):
"""
Ensure we have only one instance for the connection.
"""
if not cls._inst:
cls._inst = object.__new__(cls, *args, **kwargs)
cls._inst.connect(address, port, db_name)
return cls._inst
def close(self):
Connection._inst = None
def connect(self, address=None, port=None, db_name=None):
"""
Connect to the CouchDB server and work on the databse mentioned in
the settngs.
"""
address = address or settings.SERVER_ADDRESS
self.url = "%s:%s/" % (address.rstrip("/"), port or settings.SERVER_PORT)
self.server = Server(self.url)
db_name = db_name or settings.DATABASE_NAME
try:
self.db = self.server.create(db_name)
except PreconditionFailed:
self.db = self.server[db_name]
def __unicode__(self):
return u"Connected on %s - working on %s" % (self.url, self.db.name)
def __str__(self):
return unicode(self)
def __repr__(self):
return repr(self.db)
示例15: TestCouchDBBackend
# 需要导入模块: from couchdb.client import Server [as 别名]
# 或者: from couchdb.client.Server import create [as 别名]
class TestCouchDBBackend(BackendTestBase, TestCase):
def setUp(self):
self.server = Server('http://localhost:5984')
self.db_name = 'test_%s' % uuid4()
self.server.create(self.db_name)
db = self.server[self.db_name]
ViewDefinition.sync_many(db, couchdb_views)
self.db = CouchDBDatabase(db, lambda: six.text_type(uuid4()))
super(TestCouchDBBackend, self).setUp()
def tearDown(self):
del self.server[self.db_name]
def test_server_unreachable(self):
config = mock.Mock()
config.registry = mock.Mock()
config.registry.settings = defaultdict(str)
config.registry.settings['backend.db_host'] = 'http://unreachable/'
config.registry.settings['backend.db_name'] = 'daybed'
with self.assertRaises(CouchDBBackendConnectionError):
CouchDBBackend(config)