def connect(cfg): """connect to mongo database :param cfg: Dictionary containing configuration for MongoDB connection :type cfg: dict """ args = { 'tz_aware': cfg.get('tz_aware', False), 'replicaset': cfg.get('replicaset', cfg.get('replica_set')) } if 'uri' in cfg: args['host'] = cfg['uri'] else: args['host'] = cfg.get('host', 'localhost') args['port'] = int(cfg.get('port', 27017)) client = MotorClient(*args) if cfg.get('user'): yield client[cfg['db']].authenticate(cfg['user'], cfg['password']) if cfg.get('read_preference'): read_preference = cfg['read_preference'].upper() client.read_preference = getattr( pymongo.read_preferences.ReadPreference, read_preference) raise gen.Return(client)
def connect(cfg): """connect to mongo database :param cfg: Dictionary containing configuration for MongoDB connection :type cfg: dict """ LOG.info("connecting to %s", cfg['host']) client = MotorClient(host=cfg.get('host', 'localhost'), port=cfg.get('port', 27017), tz_aware=cfg.get('tz_aware', False), replicaset=cfg.get('replicaset', cfg.get('replica_set'))) if cfg.get('user'): yield client[cfg['db']].authenticate(cfg['user'], cfg['password']) if cfg.get('read_preference'): read_preference = cfg['read_preference'].upper() client.read_preference = getattr( pymongo.read_preferences.ReadPreference, read_preference) raise gen.Return(client)
def connect(cfg): """connect to mongo database :param cfg: Dictionary containing configuration for MongoDB connection :type cfg: dict """ args = { 'tz_aware': cfg.get('tz_aware', False), 'replicaset': cfg.get('replicaset', cfg.get('replica_set')) } if 'uri' in cfg: args['host'] = cfg['uri'] else: args['host'] = cfg.get('host', 'localhost') args['port'] = int(cfg.get('port', 27017)) client = MotorClient(*args) if cfg.get('user'): yield client[cfg['db']].authenticate(cfg['user'], cfg['password']) if cfg.get('read_preference'): read_preference = cfg['read_preference'].upper() client.read_preference = getattr(pymongo.read_preferences.ReadPreference, read_preference) raise gen.Return(client)