当前位置: 首页>>代码示例>>Python>>正文


Python Server.all_dbs方法代码示例

本文整理汇总了Python中couchdbkit.Server.all_dbs方法的典型用法代码示例。如果您正苦于以下问题:Python Server.all_dbs方法的具体用法?Python Server.all_dbs怎么用?Python Server.all_dbs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在couchdbkit.Server的用法示例。


在下文中一共展示了Server.all_dbs方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: CouchDBServer

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import all_dbs [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)
开发者ID:Sliim,项目名称:faraday,代码行数:31,代码来源:couchdb.py

示例2: test_create_and_get_db

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import all_dbs [as 别名]
    def test_create_and_get_db(self):
        couch_manager = CouchDbManager(uri=CONF.getCouchURI())
        couch_manager.createDb(self.dbname)

        self.assertNotEquals(
            couch_manager.getDb(self.dbname),
            None,
            "Db %s shouldn't be None" % self.dbname)

        server = Server(uri=CONF.getCouchURI())
        self.assertIn(
            self.dbname,
            server.all_dbs(),
            "Db %s should be in the db list" % self.dbname)
开发者ID:BecomingMore,项目名称:faraday,代码行数:16,代码来源:persistencemanager.py

示例3: index

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import all_dbs [as 别名]
def index(request):
    server = Server(settings.COUCHDB_SERVER)
    databases = [server.get_or_create_db(db).info() for db in server.all_dbs()]

    create_database_form = CreateDatabaseForm(request.POST or None)
    if create_database_form.is_valid():
        database_name = create_database_form.cleaned_data["name"]
        return HttpResponseRedirect(reverse("cushion_database",
                                            args=(database_name,)))

    return render_to_response("cushion/index.html",
                              {"title": "CouchDB",
                               "server": server,
                               "databases": databases,
                               "form": create_database_form},
                              context_instance=RequestContext(request))
开发者ID:huddlej,项目名称:cushion,代码行数:18,代码来源:views.py

示例4: CouchDbManager

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import all_dbs [as 别名]
class CouchDbManager(AbstractPersistenceManager):
    """
    This is a couchdb manager for the workspace,
    it will load from the couchdb databases
    """
    def __init__(self, uri):
        super(CouchDbManager, self).__init__()
        getLogger(self).debug(
            "Initializing CouchDBManager for url [%s]" % uri)
        self._lostConnection = False
        self.__uri = uri
        self.__serv = NoConectionServer()
        self._available = False
        try:
            if uri is not None:
                self.testCouchUrl(uri)
                url = urlparse(uri)
                getLogger(self).debug(
                    "Setting user,pass %s %s" % (url.username, url.password))
                self.__serv = Server(uri=uri)
                self.__serv.resource_class.credentials = (url.username, url.password)
                self._available = True
                self.pushReports()
                self._loadDbs()
        except:
            getLogger(self).warn("No route to couchdb server on: %s" % uri)
            getLogger(self).debug(traceback.format_exc())

    #@trap_timeout
    def _create(self, name):
        db = self.__serv.create_db(name.lower())
        return CouchDbConnector(db)

    #@trap_timeout
    def _delete(self, name):
        self.__serv.delete_db(name)

    #@trap_timeout
    def _loadDbs(self):
        conditions = lambda x: not x.startswith("_") and x != 'reports'
        for dbname in filter(conditions, self.__serv.all_dbs()):
            if dbname not in self.dbs.keys():
                getLogger(self).debug(
                    "Asking for dbname[%s], registering for lazy initialization" % dbname)
                self.dbs[dbname] = lambda x: self._loadDb(x)

    def _loadDb(self, dbname):
        db = self.__serv.get_db(dbname)
        seq = db.info()['update_seq']
        self.dbs[dbname] = CouchDbConnector(db, seq_num=seq) 
        return self.dbs[dbname]


    #@trap_timeout
    def pushReports(self):
        vmanager = ViewsManager()
        reports = os.path.join(os.getcwd(), "views", "reports")
        workspace = self.__serv.get_or_create_db("reports")
        vmanager.addView(reports, workspace)
        return self.__uri + "/reports/_design/reports/index.html"

    def lostConnectionResolv(self):
        self._lostConnection = True
        self.__dbs.clear()
        self.__serv = NoConectionServer()

    def reconnect(self):
        ret_val = False
        ur = self.__uri
        if CouchDbManager.testCouch(ur):
            self.__serv = Server(uri = ur)
            self.__dbs.clear()
            self._lostConnection = False
            ret_val = True

        return ret_val

    @staticmethod
    def testCouch(uri):
        if uri is not None:
            host, port = None, None
            try:
                import socket
                url = urlparse(uri)
                proto = url.scheme
                host = url.hostname
                port = url.port

                port = port if port else socket.getservbyname(proto)
                s = socket.socket()
                s.settimeout(1)
                s.connect((host, int(port)))
            except:
                return False
            #getLogger(CouchdbManager).info("Connecting Couch to: %s:%s" % (host, port))
            return True

    def testCouchUrl(self, uri):
        if uri is not None:
            url = urlparse(uri)
#.........这里部分代码省略.........
开发者ID:Gr3yR0n1n,项目名称:faraday,代码行数:103,代码来源:persistence_managers.py

示例5: CouchdbManager

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import all_dbs [as 别名]
class CouchdbManager(PersistenceManager):
    """ This is a couchdb manager for the workspace, it will load from the 
    couchdb databases"""
    def __init__(self, uri):
        self._last_seq_ack = 0
        model.api.log("Initializing CouchDBManager for url [%s]" % uri)
        self._lostConnection = False
        self.__uri = uri
        self.__dbs = {} 
        self.__seq_nums = {}
        self.__serv = NoConectionServer()
        self.mutex = threading.Lock()
        self._available = False
        try:
            self.testCouchUrl(uri)
            self.__serv = Server(uri = uri)
            self._available = True
        except:
            model.api.log("No route to couchdb server on: %s" % uri)

    def isAvailable(self):
        return self._available

    def lostConnectionResolv(self): 
        self._lostConnection = True
        self.__dbs.clear()
        self.__serv = NoConectionServer()

    def reconnect(self):
        ret_val = False
        ur = self.__uri
        if CouchdbManager.testCouch(ur):
            self.__serv = Server(uri = ur)
            self.__dbs.clear()
            self._lostConnection = False
            ret_val = True

        return ret_val



    @staticmethod
    def testCouch(uri):
        host, port = None, None
        try:
            import socket
            proto, netloc, _, _, _ = urlsplit(uri)
            host, port = splitport(netloc)
            port = port if port else socket.getservbyname(proto)
            s = socket.socket()
            s.settimeout(1)
            s.connect((host, int(port)))
        except:
            return False
        model.api.log("Connecting Couch to: %s:%s" % (host, port))
        return True



    def testCouchUrl(self, uri):
        _, netloc, _, _, _ = urlsplit(uri)
        host, port = splitport(netloc)
        self.test(host, int(port))

    def test(self, address, port):
        import socket
        s = socket.socket()
        s.settimeout(1)
        s.connect((address, port))


    @trap_timeout
    def getWorkspacesNames(self):
        return filter(lambda x: not x.startswith("_"), self.__serv.all_dbs())

    def workspaceExists(self, name):
        return name in self.getWorkspacesNames()


    @trap_timeout
    def addWorkspace(self, aWorkspace):
        self.__serv.create_db(aWorkspace.lower())
        return self.__getDb(aWorkspace)

    @trap_timeout
    def addDocument(self, aWorkspaceName, documentId, aDocument):
        self.incrementSeqNumber(aWorkspaceName)
        self.__getDb(aWorkspaceName)[documentId] = aDocument

    @trap_timeout
    def saveDocument(self, aWorkspaceName, aDocument):
        self.incrementSeqNumber(aWorkspaceName)
        model.api.log("Saving document in remote workspace %s" % aWorkspaceName)
        self.__getDb(aWorkspaceName).save_doc(aDocument, use_uuids = True, force_update = True)

    @trap_timeout
    def __getDb(self, aWorkspaceName): 
        aWorkspaceName = aWorkspaceName.lower()
        model.api.log("Getting workspace [%s]" % aWorkspaceName)
        workspacedb = self.__dbs.get(aWorkspaceName, self.__serv.get_db(aWorkspaceName))
#.........这里部分代码省略.........
开发者ID:Behk,项目名称:faraday,代码行数:103,代码来源:workspace.py

示例6: Server

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import all_dbs [as 别名]
if not server_urls:
    logging.error("Please supply one or more URL's for CouchDB instances to carry out maintenance for.")
    sys.exit(1)

for server_url in server_urls:
    try:
        server = Server(server_url)
    except:
        logging.error("Could not connect to CouchDB at %s", server_url)
        continue
    logging.info("Connected to CouchDB server at %s", server_url)

    logging.info("Getting list of databases...")
    try:
        db_names = server.all_dbs()
    except:
        logging.error("Could not get list of databases for CouchDB at %s", server_url)
        continue
    logging.info("Databases: %s", ", ".join(db_names))

    for db_name in db_names:
        if db_name.startswith('_'):
            logging.info("Skipping database starting with '_': %s", db_name)
            continue

        logging.info("Database: %s", db_name)
        db = server.get_or_create_db(db_name)
	
        logging.info("Starting compaction for database %s...", db_name)
        try:	
开发者ID:vpro,项目名称:couchdb_maintenance,代码行数:32,代码来源:couchdb_maintenance.py

示例7: CouchdbManager

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import all_dbs [as 别名]
class CouchdbManager(PersistenceManager):
    """ This is a couchdb manager for the workspace, it will load from the 
    couchdb databases"""
    def __init__(self, uri):
        self._last_seq_ack = 0
        getLogger(self).debug("Initializing CouchDBManager for url [%s]" % uri)
        self._lostConnection = False
        self.__uri = uri
        self.__dbs = {} 
        self.__seq_nums = {}
        self.__serv = NoConectionServer()
        self.mutex = threading.Lock()
        self._available = False

        #setting the doc types to load from couch
        def get_types(subclasses):
            if len(subclasses):
                head = subclasses[0]
                tail = []
                if len(subclasses[1:]):
                    tail = subclasses[1:]
                return get_types(head.__subclasses__()) + [head.class_signature] + get_types(tail)
            return []
        self._model_object_types = get_types([ModelObject])
        try:
            if uri is not None:
                self.testCouchUrl(uri)
                url = urlparse(uri)
                getLogger(self).debug("Setting user,pass %s %s" % (url.username, url.password))
                self.__serv = Server(uri=uri)
                #print dir(self.__serv)
                self.__serv.resource_class.credentials = (url.username, url.password)
                self._available = True
        except:
            getLogger(self).warn("No route to couchdb server on: %s" % uri)
            getLogger(self).debug(traceback.format_exc())

    def isAvailable(self):
        return self._available

    def lostConnectionResolv(self): 
        self._lostConnection = True
        self.__dbs.clear()
        self.__serv = NoConectionServer()

    def reconnect(self):
        ret_val = False
        ur = self.__uri
        if CouchdbManager.testCouch(ur):
            self.__serv = Server(uri = ur)
            self.__dbs.clear()
            self._lostConnection = False
            ret_val = True

        return ret_val

    @staticmethod
    def testCouch(uri):
        if uri is not None:
            host, port = None, None
            try:
                import socket
                url = urlparse(uri)
                proto = url.scheme
                host = url.hostname
                port = url.port

                port = port if port else socket.getservbyname(proto)
                s = socket.socket()
                s.settimeout(1)
                s.connect((host, int(port)))
            except:
                return False
            getLogger(CouchdbManager).info("Connecting Couch to: %s:%s" % (host, port))
            return True

    def testCouchUrl(self, uri):
        if uri is not None:
            url = urlparse(uri)
            proto = url.scheme
            host = url.hostname
            port = url.port
            self.test(host, int(port))

    def test(self, address, port):
        import socket
        s = socket.socket()
        s.settimeout(1)
        s.connect((address, port))


    @trap_timeout
    def getWorkspacesNames(self):
        return filter(lambda x: not x.startswith("_"), self.__serv.all_dbs())

    def workspaceExists(self, name):
        return name in self.getWorkspacesNames()


    @trap_timeout
#.........这里部分代码省略.........
开发者ID:0x24bin,项目名称:BurpSuite,代码行数:103,代码来源:all.py

示例8: tearDown

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import all_dbs [as 别名]
 def tearDown(self):
     server = Server(uri=CONF.getCouchURI())
     if self.dbname in server.all_dbs():
         server.delete_db(self.dbname)
开发者ID:BecomingMore,项目名称:faraday,代码行数:6,代码来源:persistencemanager.py

示例9: CouchDbManager

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import all_dbs [as 别名]
class CouchDbManager(AbstractPersistenceManager):
    """
    This is a couchdb manager for the workspace,
    it will load from the couchdb databases
    """
    def __init__(self, uri, couch_exception_callback):
        super(CouchDbManager, self).__init__()
        getLogger(self).debug(
            "Initializing CouchDBManager for url [%s]" % uri)
        self._lostConnection = False
        self.__uri = uri
        self._available = False
        self.couch_exception_callback = couch_exception_callback
        test_couch_thread = threading.Thread(target=self.continuosly_check_connection)
        test_couch_thread.daemon = True
        test_couch_thread.start()
        try:
            if uri is not None:
                self.testCouchUrl(uri)
                url = urlparse(uri)
                getLogger(self).debug(
                    "Setting user,pass %s %s" % (url.username, url.password))
                self.__serv = Server(uri=uri)
                self.__serv.resource_class.credentials = (url.username, url.password)
                self._available = True
                self.pushReports()
                self._loadDbs()
        except:
            getLogger(self).warn("No route to couchdb server on: %s" % uri)
            getLogger(self).debug(traceback.format_exc())

    def continuosly_check_connection(self):
        """Intended to use on a separate thread. Call module-level
        function testCouch every second to see if response to the server_uri
        of the DB is still 200. Call the exception_callback if we can't access
        the server three times in a row.
        """
        tolerance = 0
        server_uri = self.__uri
        while True:
            time.sleep(1)
            test_was_successful = test_couch(server_uri)
            if test_was_successful:
                tolerance = 0
            else:
                tolerance += 1
                if tolerance == 3:
                    self.couch_exception_callback()
                    return False  # kill the thread if something went wrong

    def _create(self, name):
        db = self.__serv.create_db(name.lower())
        return CouchDbConnector(db)

    def _delete(self, name):
        self.__serv.delete_db(name)

    def _loadDbs(self):

        def conditions(database):
            begins_with_underscore = database.startswith("_")
            is_blacklisted = database in CONST_BLACKDBS
            return not begins_with_underscore and not is_blacklisted

        try:
            for dbname in filter(conditions, self.__serv.all_dbs()):
                if dbname not in self.dbs.keys():
                    getLogger(self).debug(
                        "Asking for dbname[%s], registering for lazy initialization" % dbname)
                    self.dbs[dbname] = lambda x: self._loadDb(x)
        except restkit.errors.RequestError as req_error:
            getLogger(self).error("Couldn't load databases. "
                                  "The connection to the CouchDB was probably lost. ")

    def _loadDb(self, dbname):
        db = self.__serv.get_db(dbname)
        seq = db.info()['update_seq']
        self.dbs[dbname] = CouchDbConnector(db, seq_num=seq)
        return self.dbs[dbname]

    def refreshDbs(self):
        """Refresh databases using inherited method. On exception, asume
        no databases are available.
        """
        try:
            return AbstractPersistenceManager.refreshDbs()
        except:
            return []

    def pushReports(self):
        vmanager = ViewsManager()
        reports = os.path.join(os.getcwd(), "views", "reports")
        try:
            workspace = self.__serv.get_or_create_db("reports")
            vmanager.addView(reports, workspace)
        except:
            getLogger(self).warn(
                "Reports database couldn't be uploaded. You need to be an admin to do it")
        return self.__uri + "/reports/_design/reports/index.html"

#.........这里部分代码省略.........
开发者ID:FreedomCoder,项目名称:faraday,代码行数:103,代码来源:persistence_managers.py

示例10: ClientServerTestCase

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import all_dbs [as 别名]
class ClientServerTestCase(unittest.TestCase):
    def setUp(self):
        self.couchdb = CouchdbResource()
        self.Server = Server()

    def tearDown(self):
        try:
            del self.Server["couchdbkit_test"]
            del self.Server["couchdbkit/test"]
        except:
            pass

    def testGetInfo(self):
        info = self.Server.info()
        self.assertIn("version", info)

    def testCreateDb(self):
        res = self.Server.create_db("couchdbkit_test")
        self.assertIsInstance(res, Database)
        all_dbs = self.Server.all_dbs()
        self.assertIn("couchdbkit_test", all_dbs)
        del self.Server["couchdbkit_test"]
        res = self.Server.create_db("couchdbkit/test")
        self.assertIn("couchdbkit/test", self.Server.all_dbs())
        del self.Server["couchdbkit/test"]

    def testGetOrCreateDb(self):
        # create the database
        gocdb = self.Server.get_or_create_db("get_or_create_db")
        self.assertEqual(gocdb.dbname, "get_or_create_db")
        self.assertIn("get_or_create_db", self.Server)
        self.Server.delete_db("get_or_create_db")
        # get the database (already created)
        self.assertNotIn("get_or_create_db", self.Server)
        db = self.Server.create_db("get_or_create_db")
        self.assertIn("get_or_create_db", self.Server)
        gocdb = self.Server.get_or_create_db("get_or_create_db")
        self.assertEqual(db.dbname, gocdb.dbname)
        self.Server.delete_db("get_or_create_db")

    def testCreateInvalidDbName(self):
        def create_invalid():
            res = self.Server.create_db("123ab")

        self.assertRaises(ValueError, create_invalid)

    def testServerLen(self):
        res = self.Server.create_db("couchdbkit_test")
        self.assertGreaterEqual(len(self.Server), 1)
        self.assertTrue(self.Server)
        del self.Server["couchdbkit_test"]

    def testServerContain(self):
        res = self.Server.create_db("couchdbkit_test")
        self.assertIn("couchdbkit_test", self.Server)
        del self.Server["couchdbkit_test"]

    def testGetUUIDS(self):
        uuid = self.Server.next_uuid()
        self.assertIsInstance(uuid, basestring)
        self.assertEqual(len(self.Server._uuids), 999)
        uuid2 = self.Server.next_uuid()
        self.assertNotEqual(uuid, uuid2)
        self.assertEqual(len(self.Server._uuids), 998)
开发者ID:georgemarshall,项目名称:couchdbkit,代码行数:66,代码来源:test_client.py


注:本文中的couchdbkit.Server.all_dbs方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。