本文整理汇总了Python中couchdbkit.Server.get_or_create_db方法的典型用法代码示例。如果您正苦于以下问题:Python Server.get_or_create_db方法的具体用法?Python Server.get_or_create_db怎么用?Python Server.get_or_create_db使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类couchdbkit.Server
的用法示例。
在下文中一共展示了Server.get_or_create_db方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import get_or_create_db [as 别名]
def main():
#arguments parser
parser = argparse.ArgumentParser(prog='pushCwe', epilog="Example: ./%(prog)s.py")
parser.add_argument('-c', '--couchdburi', action='store', type=str,
dest='couchdb',default="http://127.0.0.1:5984",
help='Couchdb URL (default http://127.0.0.1:5984)')
#arguments put in variables
args = parser.parse_args()
#default value from ENV COUCHDB
couchdb = os.environ.get('COUCHDB')
#Else from argument
if not couchdb:
couchdb = args.couchdb
__serv = Server(uri = couchdb)
# reports = os.path.join(os.getcwd(), "views", "reports")
workspace = __serv.get_or_create_db("cwe")
# designer.push(reports, workspace, atomic = False)
with open('data/cwe.csv', 'r') as csvfile:
cwereader = csv.reader(csvfile, delimiter=',')
header = cwereader.next()
for cwe in cwereader:
cwe_doc = dict(zip(header, cwe))
workspace.save_doc(cwe_doc)
示例2: CouchDBServer
# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import get_or_create_db [as 别名]
class CouchDBServer(object):
def __init__(self):
self.__get_server_uri()
self.__authenticate()
self.__connect()
def __get_server_uri(self):
couchdb_port = config.couchdb.port if config.couchdb.protocol == 'http' else config.couchdb.ssl_port
self.__couchdb_uri = "%s://%s:%s" % (config.couchdb.protocol, config.couchdb.host, couchdb_port)
def __authenticate(self):
user, passwd = config.couchdb.user, config.couchdb.password
if all((user, passwd)):
auth = restkit.BasicAuth(user, passwd)
self.__auth_resource = CouchdbResource(filters=[auth])
else:
self.__auth_resource = None
def __connect(self):
self.__server = Server(uri=self.__couchdb_uri, resource_instance=self.__auth_resource)
def list_workspaces(self):
return filter(is_usable_workspace, self.__server.all_dbs())
def get_workspace_handler(self, ws_name):
return self.__server.get_db(ws_name)
def get_or_create_db(self, ws_name):
return self.__server.get_or_create_db(ws_name)
示例3: __init__
# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import get_or_create_db [as 别名]
def __init__(self, databases):
""" initialize couchdbkit handler with COUCHDB_DATABASES
settings """
self.__dict__ = self.__shared_state__
# create databases sessions
for app_name, uri in databases:
try:
if isinstance(uri, tuple):
# case when you want to specify server uri
# and database name specifically. usefull
# when you proxy couchdb on some path
server_uri, dbname = uri
else:
server_uri, dbname = uri.rsplit("/", 1)
except ValueError:
raise ValueError("couchdb uri [%s:%s] invalid" % (
app_name, uri))
res = CouchdbResource(server_uri, timeout=COUCHDB_TIMEOUT)
server = Server(server_uri, resource_instance=res)
app_label = app_name.split('.')[-1]
self._databases[app_label] = server.get_or_create_db(dbname)
示例4: main
# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import get_or_create_db [as 别名]
def main(views_directory, server_uri):
directory = os.path.abspath(views_directory)
server = Server(server_uri)
db = server.get_or_create_db("v1")
loader = FileSystemDocsLoader(directory)
loader.sync(db, debug=True, verbose=True)
示例5: CouchClient
# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import get_or_create_db [as 别名]
class CouchClient(object):
def __init__(self,config):
try:
# First figure out which document template we are using here.
if config.get_param('template') not in ['pdst','tslp','random']:
raise Exception('Template parameter not recognized. Try: pdst tslp random')
self.server = Server(uri='http://' + \
config.get_param('server') + \
':' + config.get_param('port'))
self.db = self.server.get_or_create_db(config.get_param('db'))
self. docu_class = None
if config.get_param('template') == 'tslp':
self.docu_class = TSLPDocument
if config.get_param('template') == 'pdst':
self.docu_class = PDSTDocument
elif config.get_param('template') == 'random':
self.docu_class = DemoDocument
self.docu_class.set_db(self.db)
# OK, if that all worked, consider the object initialized wrt couch,
# and store the other params we need to iterate.
self.maxcounts = int(config.get_param('total'))
self.increment = int(config.get_param('increment'))
self.verbose = config.get_param('verbose')
except Exception, msg:
raise Exception(msg)
示例6: uploadFile
# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import get_or_create_db [as 别名]
def uploadFile(fname, uri, dbname):
print 'Upload contents of %s to %s/%s' % (fname, uri, dbname)
# Connect to the database
theServer = Server(uri)
db = theServer.get_or_create_db(dbname)
# Loop on file for upload
reader = DictReader(open(fname, 'rU'), dialect = 'excel')
# For bulk upload
docs = list()
checkpoint = 100
for doc in reader:
# Convert strings that are really numbers into ints and floats
newdoc = parseDoc(doc)
# Check if doc already exists in the DB
# If it already exists, update it
#if db.doc_exist(newdoc.get('_id')):
# newdoc['_rev'] = db.get_rev(newdoc.get('_id'))
docs.append(newdoc)
if len(docs) % checkpoint == 0:
docs = upload(db, docs)
# Upload the lasr batch
docs = upload(db, docs)
示例7: _list_couch_docs
# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import get_or_create_db [as 别名]
def _list_couch_docs(self, db_name="dmscouch_test"):
"""Downloads all the documents that are currently in CouchDB now"""
docs = {}
server = Server()
db = server.get_or_create_db(db_name)
r = db.view("dmscouch/all", include_docs=True)
for row in r:
docs[row["doc"]["_id"]] = row["doc"]
return docs
示例8: SPARQLBinBackend
# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import get_or_create_db [as 别名]
class SPARQLBinBackend(object):
# init with URL of CouchDB server and database name and credentials
def __init__(self, serverURL, dbname, username, pwd):
self.serverURL = serverURL
self.dbname = dbname
self.username = username
self.pwd = pwd
self.server = Server(self.serverURL, filters=[BasicAuth(self.username, self.pwd)])
# adds a document to the database
def add(self, entry):
try:
db = self.server.get_or_create_db(self.dbname)
PasteEntry.set_db(db)
doc = PasteEntry(
endpoint=entry["endpoint"], querystr=entry["querystr"], timestamp=datetime.datetime.utcnow()
)
doc.save()
logging.debug("Adding entry with ID %s" % doc["_id"])
return doc["_id"]
except Exception as err:
logging.error("Error while adding entry: %s" % err)
return None
# finds a document via its ID in the database
def find(self, eid):
try:
db = self.server.get_or_create_db(self.dbname)
if db.doc_exist(eid):
ret = db.get(eid)
# don't expose details, clean up
ret.pop("doc_type")
ret.pop("_rev")
ret.pop("_id")
return (True, ret)
else:
return (False, None)
except Exception as err:
logging.error("Error while looking up entry: %s" % err)
return (False, None)
示例9: init_db
# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import get_or_create_db [as 别名]
def init_db(uri, dbname, main_db=True):
"""Returns a db object and syncs the design documents on demand.
If main_db is set to true then all models will use that one by default.
"""
server = Server(uri)
db = server.get_or_create_db(dbname)
if main_db:
Document.set_db(db)
return db
示例10: getDatabase
# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import get_or_create_db [as 别名]
def getDatabase(servname, dbn):
try:
serv = Server(servname)
db = serv.get_or_create_db(dbn)
except:
try: #if we cannot connect to the given database, we try the local database before giving up
print 'unable to connect to', servname
print 'trying local database'
serv = Server('http://localhost:5984')
db = serv.get_or_create_db(dbn)
except Exception as e:
print e
raise e
print 'connected to', db.uri
return db, serv
示例11: setup
# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import get_or_create_db [as 别名]
def setup(dbstring, path):
server_uri, db_name, docid = parse_uri(dbstring)
if "@" in server_uri:
username, password, server_uri = parse_auth(server_uri)
server = Server(server_uri)
server.res.add_authorization(httpc.BasicAuth((uri.username, uri.password)))
else:
server = Server(server_uri)
db = server.get_or_create_db("couchdbproxy")
loader = FileSystemDocsLoader(path)
loader.sync(db, verbose=True)
示例12: RevCountResolutionTestCase
# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import get_or_create_db [as 别名]
class RevCountResolutionTestCase(ConflictResolverTestCase):
"""
This tests resolution that is based on rev_count, given
the time difference between two conflicting versions
is less than configuration.SESSION_LAG_TIME, we can't
rely on timestamps due to server time skew.
So we decide the winning document based on the number
of modifications, assuming the one with the largest
number of modifs should be the current one.
"""
def _runTest(self):
self.s.replicate(self.database_name, 'http://localhost:5984/'+self.repdb_name)
self.s = Server('http://localhost:5984')
self.repdb = self.s.get_or_create_db(self.repdb_name)
self.replicated_doc = self.repdb.get(self.docid)
# increasing the revision log (add 6 more revisions)
for i in range(6):
self.replicated_doc['text'] = 'bigger revision number'
self.replicated_doc['timestamp'] = str(int(round(time.time())))
self.replicated_doc['rev_count'] = str(int(self.replicated_doc['rev_count']) + 1)
self.repdb.save_doc(self.replicated_doc)
# create the conflict, change the same
# text field of the original at the source database.
master_db = self.s.get_or_create_db(self.database_name)
doc = master_db.get(self.docid)
doc['text'] = 'smaller revision number'
doc['timestamp'] = str(int(round(time.time())))
doc['rev_count'] = str(int(doc['rev_count']) + 1)
master_db.save_doc(doc)
self.s.replicate('http://localhost:5984/'+self.repdb_name, self.database_name)
doc = self.db.get(self.docid)
self.assertEqual(doc['text'], 'bigger revision number')
start_time = time.time()
while (self.db.get(self.docid, conflicts=True).has_key('_conflicts')):
pass
end_time = time.time()
print "Time to conflicts clear: %s" % (end_time - start_time)
def runTest(self):
for i in range(10):
self._runTest()
示例13: _list_couch_docs
# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import get_or_create_db [as 别名]
def _list_couch_docs(self, db_name='dmscouch_test'):
"""Downloads all the documents that are currently in CouchDB now"""
docs = {}
server = Server()
db = server.get_or_create_db(db_name)
r = db.view(
'dmscouch/all',
include_docs=True,
)
for row in r:
docs[row['doc']['_id']] = row['doc']
return docs
示例14: CloudantPool
# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import get_or_create_db [as 别名]
class CloudantPool(object):
def __init__(self, nworkers, maxdepth, tsleep, timeout, uri, dbname):
self.tstart = time.time()
self.nworkers = nworkers
self.maxdepth=maxdepth
self.tsleep= tsleep
self.timeout = timeout
self.uri=uri
self.dbname=dbname
self.counter = 0
#make sure DB exists
self.server = Server(self.uri)
self.db = self.server.get_or_create_db(self.dbname)
print self.db.info()
#create worker pool
self.inputQ = Queue.Queue()
self.workers = list()
print '\nUse:\t%i workers' % self.nworkers
for i in range(0,self.nworkers):
#each worker gets its own connection
thd = thread.start_new_thread(worker, (i,self.timeout, self.uri, self.dbname, self.inputQ) )
self.workers.append(thd)
def pushDoc(self, doc):
#put it on the queue
self.inputQ.put(doc)
self.counter += 1
if self.counter%100==0:
print 'pushing doc:%i with depth %i' % (self.counter, self.inputQ.qsize())
#keep the work queue from getting too backed up
while (self.inputQ.qsize()>self.maxdepth):
print 'qdepth = at %i sleep(%f)' % (self.inputQ.qsize(), self.tsleep)
sys.stdout.flush()
time.sleep(self.tsleep)
def flush(self):
print '\nFinal Queue Flush'
print 'size:\t', self.inputQ.qsize()
sys.stdout.flush()
self.inputQ.join()
print 'done flushing'
self.tstop = time.time()
self.rate = float(self.counter)/float(self.tstop-self.tstart)
print 'Saved %i documents in %i seconds for %f docs/sec' % (self.counter, self.tstop-self.tstart, self.rate)
def cleanup(self):
for thd in enumerate():
print thd
示例15: upload_file
# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import get_or_create_db [as 别名]
def upload_file(fname, uri, dbname):
print 'Upload contents of %s to %s/%s' % (fname, uri, dbname)
theServer = Server(uri, filters=[BasicAuth(cloudant_username, cloudant_password)])
db = theServer.get_or_create_db(dbname)
reader = DictReader(open(fname, 'rU'), dialect = 'excel')
docs = list()
checkpoint = 100
for doc in reader:
newdoc = parse_doc(doc)
docs.append(newdoc)
if len(docs)%checkpoint==0:
docs = upload(db,docs)
docs = upload(db,docs)