How to use the pymongo.uri_parser.parse_uri function in pymongo

To help you get started, we’ve selected a few pymongo examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github MongoEngine / flask-mongoengine / flask_mongoengine / connection.py View on Github external
if k.startswith('MONGODB_'):
            k = k[len('MONGODB_'):]
        k = k.lower()
        resolved_settings[k] = v

    # Handle uri style connections
    if "://" in resolved_settings.get('host', ''):
        # this section pulls the database name from the URI
        # PyMongo requires URI to start with mongodb:// to parse
        # this workaround allows mongomock to work
        uri_to_check = resolved_settings['host']

        if uri_to_check.startswith('mongomock://'):
            uri_to_check = uri_to_check.replace('mongomock://', 'mongodb://')

        uri_dict = uri_parser.parse_uri(uri_to_check)
        resolved_settings['db'] = uri_dict['database']

    # Add a default name param or use the "db" key if exists
    if resolved_settings.get('db'):
        resolved_settings['name'] = resolved_settings.pop('db')
    else:
        resolved_settings['name'] = 'test'

    # Add various default values.
    resolved_settings['alias'] = resolved_settings.get('alias', mongoengine.DEFAULT_CONNECTION_NAME)  # TODO do we have to specify it here? MongoEngine should take care of that
    resolved_settings['host'] = resolved_settings.get('host', 'localhost')  # TODO this is the default host in pymongo.mongo_client.MongoClient, we may not need to explicitly set a default here
    resolved_settings['port'] = resolved_settings.get('port', 27017)  # TODO this is the default port in pymongo.mongo_client.MongoClient, we may not need to explicitly set a default here

    # Default to ReadPreference.PRIMARY if no read_preference is supplied
    resolved_settings['read_preference'] = resolved_settings.get('read_preference', ReadPreference.PRIMARY)
github mongodb / mongo-python-driver / test / test_uri_parser.py View on Github external
parse_uri("mongodb://%2Ftmp%2Fmongodb-27017.sock,"
                                   "example2.com:27017/test.yield_historical"
                                   ".sock"))

        res = copy.deepcopy(orig)
        res['nodelist'] = [("example2.com", 27017)]
        res.update({'database': 'test', 'collection': 'yield_historical.sock'})
        self.assertEqual(res,
                         parse_uri("mongodb://example2.com:27017"
                                   "/test.yield_historical.sock"))

        res = copy.deepcopy(orig)
        res['nodelist'] = [("/tmp/mongodb-27017.sock", None)]
        res.update({'database': 'test', 'collection': 'mongodb-27017.sock'})
        self.assertEqual(res,
                         parse_uri("mongodb://%2Ftmp%2Fmongodb-27017.sock"
                                   "/test.mongodb-27017.sock"))

        res = copy.deepcopy(orig)
        res['nodelist'] = [('/tmp/mongodb-27020.sock', None),
                           ("::1", 27017),
                           ("2001:0db8:85a3:0000:0000:8a2e:0370:7334", 27018),
                           ("192.168.0.212", 27019),
                           ("localhost", 27018)]
        self.assertEqual(res, parse_uri("mongodb://%2Ftmp%2Fmongodb-27020.sock"
                                        ",[::1]:27017,[2001:0db8:"
                                        "85a3:0000:0000:8a2e:0370:7334],"
                                        "192.168.0.212:27019,localhost",
                                        27018))

        res = copy.deepcopy(orig)
        res.update({'username': 'fred', 'password': 'foobar'})
github mongodb / mongo-python-driver / test / test_uri_parser.py View on Github external
"85a3:0000:0000:8a2e:0370:7334],"
                                        "192.168.0.212:27019,localhost",
                                        27018))

        res = copy.deepcopy(orig)
        res.update({'username': 'fred', 'password': 'foobar'})
        res.update({'database': 'test', 'collection': 'yield_historical.in'})
        self.assertEqual(res,
                         parse_uri("mongodb://fred:foobar@localhost/"
                                   "test.yield_historical.in"))

        res = copy.deepcopy(orig)
        res['database'] = 'test'
        res['collection'] = 'name/with "delimiters'
        self.assertEqual(
            res, parse_uri("mongodb://localhost/test.name/with \"delimiters"))

        res = copy.deepcopy(orig)
        res['options'] = {
            'readpreference': ReadPreference.SECONDARY.mongos_mode
        }
        self.assertEqual(res, parse_uri(
            "mongodb://localhost/?readPreference=secondary"))

        # Various authentication tests
        res = copy.deepcopy(orig)
        res['options'] = {'authmechanism': 'MONGODB-CR'}
        res['username'] = 'user'
        res['password'] = 'password'
        self.assertEqual(res,
                         parse_uri("mongodb://user:password@localhost/"
                                   "?authMechanism=MONGODB-CR"))
github janezkranjc / twitter-tap / twitter_tap / tap.py View on Github external
if not ACCESS_TOKEN:
            logger.warning("No access token provided in options. Obtaining one now...")
            token_getter = Twython(CONSUMER_KEY, CONSUMER_SECRET, oauth_version=2)
            ACCESS_TOKEN = token_getter.obtain_access_token()
            logger.warning("Access token: " + ACCESS_TOKEN)

        twitter = Twython(CONSUMER_KEY, access_token=ACCESS_TOKEN)

        if not args.output:
            try:
                client = pymongo.MongoClient(MONGODB_URI)
            except:
                logger.fatal("Couldn't connect to MongoDB. Please check your --db argument settings.")
                sys.exit(1)

            parsed_dburi = pymongo.uri_parser.parse_uri(MONGODB_URI)
            db = client[parsed_dburi['database']]

            queries = db[args.queries_collection]
            tweets = db[args.tweets_collection]

            queries.ensure_index(
                [("query", pymongo.ASCENDING), ("geocode", pymongo.ASCENDING), ("lang", pymongo.ASCENDING)],
                unique=True)
            tweets.ensure_index("id", direction=pymongo.DESCENDING, unique=True)
            tweets.ensure_index([("coordinates.coordinates", pymongo.GEO2D), ])

            if not clean_since_id:
                current_query = queries.find_one({'query': query, 'geocode': geocode, 'lang': lang})
            else:
                current_query = None
            if current_query:
github openstack / ceilometer / ceilometer / event / storage / impl_mongodb.py View on Github external
def __init__(self, conf, url):
        super(Connection, self).__init__(conf, url)

        # NOTE(jd) Use our own connection pooling on top of the Pymongo one.
        # We need that otherwise we overflow the MongoDB instance with new
        # connection since we instantiate a Pymongo client each time someone
        # requires a new storage connection.
        self.conn = self.CONNECTION_POOL.connect(conf, url)

        # Require MongoDB 2.4 to use $setOnInsert
        if self.conn.server_info()['versionArray'] < [2, 4]:
            raise storage.StorageBadVersion("Need at least MongoDB 2.4")

        connection_options = pymongo.uri_parser.parse_uri(url)
        self.db = getattr(self.conn, connection_options['database'])
        if connection_options.get('username'):
            self.db.authenticate(connection_options['username'],
                                 connection_options['password'])

        # NOTE(jd) Upgrading is just about creating index, so let's do this
        # on connection to be sure at least the TTL is correctly updated if
        # needed.
        self.upgrade()
github mongolab / mongoctl / mongoctl / mongo_uri_tools.py View on Github external
def parse_mongo_uri(uri):
    try:
        uri_obj = uri_parser.parse_uri(uri)
        # validate uri
        nodes = uri_obj["nodelist"]
        for node in nodes:
            host = node[0]
            if not host:
                raise Exception("URI '%s' is missing a host." % uri)

        return MongoUriWrapper(uri_obj)
    except errors.ConfigurationError, e:
        raise Exception("Malformed URI '%s'. %s" % (uri, e))

    except Exception, e:
        raise Exception("Unable to parse mongo uri '%s'."
                        " Cause: %s" % (e, uri))
github DataDog / integrations-core / mongo / datadog_checks / mongo / mongo.py View on Github external
def _parse_uri(self, server, sanitize_username=False):
        """
        Parses a MongoDB-formatted URI (e.g. mongodb://user:pass@server/db) and returns parsed elements
        and a sanitized URI.
        """
        parsed = pymongo.uri_parser.parse_uri(server)

        username = parsed.get('username')
        password = parsed.get('password')
        db_name = parsed.get('database')
        nodelist = parsed.get('nodelist')
        auth_source = parsed.get('options', {}).get('authsource')

        # Remove password (and optionally username) from sanitized server URI.
        # To ensure that the `replace` works well, we first need to url-decode the raw server string
        # since the password parsed by pymongo is url-decoded
        decoded_server = urllib.unquote_plus(server)
        clean_server_name = decoded_server.replace(password, "*" * 5) if password else decoded_server

        if sanitize_username and username:
            username_pattern = u"{}[@:]".format(re.escape(username))
            clean_server_name = re.sub(username_pattern, "", clean_server_name)
github MolSSI / QCFractal / qcfractal / storage_sockets / mongo_socket.py View on Github external
"collections": True,
            "options": True,
            "results": True,
            "molecules": False,
            "procedures": False,
            "service_queue": False,
            "task_queue": False,
            "users": True,
            "queue_managers": True,
        }

        self._lower_results_index = ["method", "basis", "options", "program"]

        # Build MongoClient
        self.client = pymongo.MongoClient(uri)
        expanded_uri = pymongo.uri_parser.parse_uri(uri)
        if expanded_uri["password"] is not None:
            self.client = pymongo.MongoClient(uri, authMechanism=authMechanism, authSource=authSource)
        else:
            self.client = pymongo.MongoClient(uri)
        self._url, self._port = expanded_uri["nodelist"][0]

        try:
            version_array = self.client.server_info()['versionArray']

            if tuple(version_array) < (3, 2):
                raise RuntimeError
        except AttributeError:
            raise RuntimeError(
                "Could not detect MongoDB version at URL {}. It may be a very old version or installed incorrectly. "
                "Choosing to stop instead of assuming version is at least 3.2.".format(url))
        except RuntimeError:
github hansonkd / FlaskBootstrapSecurity / flask_application / config.py View on Github external
def mongo_from_uri(uri):
        config = parse_uri(uri)
        conn_settings = {
            'db': config['database'],
            'username': config['username'],
            'password': config['password'],
            'host': config['nodelist'][0][0],
            'port': config['nodelist'][0][1]
        }
        return conn_settings
github glotzerlab / signac / signac / common / host.py View on Github external
from pymongo.uri_parser import parse_uri
    auth_m = hostcfg.get('auth_mechanism', 'none')
    if auth_m == 'SCRAM-SHA-1':
        uri = hostcfg['url']
        if isinstance(uri, list):
            uri = ','.join(uri)
        if 'username' not in hostcfg and not parse_uri(uri)['username']:
            username = SESSION_USERNAME_CACHE.get(_host_id(hostcfg))
            if username:
                hostcfg['username'] = username
            else:
                SESSION_USERNAME_CACHE[_host_id(hostcfg)] = \
                    hostcfg['username'] = _input(
                        "Username ({}): ".format(getpass.getuser()),
                        getpass.getuser())
        if 'password' not in hostcfg and not parse_uri(uri)['password']:
            hostcfg['password'] = get_credentials(hostcfg)
    return hostcfg