Esempio n. 1
0
def ensure_specifier_exists(db_spec):
    """Make sure a DB specifier exists, creating it if necessary."""
    local_match = LOCAL_RE.match(db_spec)
    remote_match = REMOTE_RE.match(db_spec)
    plain_match = PLAIN_RE.match(db_spec)
    if local_match:
        db_name = local_match.groupdict().get('database')
        server = shortcuts.get_server()
        if db_name not in server:
            server.create(db_name)
        return True
    elif remote_match:
        hostname, portnum, database = map(remote_match.groupdict().get,
            ('hostname', 'portnum', 'database'))
        server = shortcuts.get_server(
            server_url=('http://%s:%s' % (hostname, portnum)))
        if database not in server:
            server.create(database)
        return True
    elif plain_match:
        db_name = plain_match.groupdict().get('database')
        server = shortcuts.get_server()
        if db_name not in server:
            server.create(db_name)
        return True
    return False
Esempio n. 2
0
def get_server_from_db(db_string):
    """Return a CouchDB server instance from a database string."""
    local_match = PLAIN_RE.match(db_string)
    remote_match = URL_RE.match(db_string)
    # If this looks like a local specifier:
    if local_match:
        return shortcuts.get_server()
    elif remote_match:
        hostname, portnum, database = map(remote_match.groupdict().get,
            ('hostname', 'portnum', 'database'))
        local_url = settings._('COUCHDB_SERVER', 'http://127.0.0.1:5984/')
        localhost, localport = urlparse.urlparse(local_url)[1].split(':')
        # If it's the local server, then return a local specifier.
        if (localhost == hostname) and (localport == portnum):
            return shortcuts.get_server()
        return shortcuts.get_server(
            server_url=('http://%s:%s' % (hostname, portnum)))
    raise ValueError('Invalid database string: %r' % (db_string,))