Python mongoengine.connect() Examples

The following are 9 code examples of mongoengine.connect(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module mongoengine , or try the search function .
Example #1
Source File: tests.py    From state_machine with MIT License 5 votes vote down vote up
def establish_mongo_connection():
    mongo_name = os.environ.get('AASM_MONGO_DB_NAME', 'test_acts_as_state_machine')
    mongo_port = int(os.environ.get('AASM_MONGO_DB_PORT', 27017))
    mongoengine.connect(mongo_name, port=mongo_port) 
Example #2
Source File: database.py    From cascade-server with Apache License 2.0 5 votes vote down vote up
def connect():
    global serializer, fernet
    crypto_info = settings.load()['database']['crypto']
    mongo_host = settings.load()['database']['mongo'].get('host', '127.0.0.1')
    mongo_port = settings.load()['database']['mongo'].get('port', '27017')
    serializer = URLSafeSerializer(crypto_info['key'])
    fernet = Fernet(crypto_info['fernet'])
    mongoengine.connect(name, host=mongo_host, port=mongo_port, tz_aware=True) 
Example #3
Source File: models.py    From baleen with MIT License 5 votes vote down vote up
def connect(**kwargs):
    """
    Wrapper for mongoengine connect - connects with configuration details.
    """
    name = kwargs.pop('name', settings.database.name)
    host = kwargs.pop('host', settings.database.host)
    port = kwargs.pop('port', settings.database.port)

    return me.connect(name, host=host, port=port, **kwargs)

##########################################################################
## Models
########################################################################## 
Example #4
Source File: basemodel.py    From Zilliqa-Mining-Proxy with GNU General Public License v3.0 5 votes vote down vote up
def connect_to_db(config=None):
    """ connect_to_db at the begin of app initializing
    :param config: loaded config dict
    :return: None
    """
    uri = config.database["uri"]

    logging.critical(f"Connecting to {uri}")
    try:
        connect(host=uri)
        logging.critical("Database connected!")
    except MongoEngineConnectionError:
        logging.fatal("Failed connect to MongoDB!")
        raise 
Example #5
Source File: __init__.py    From discograph with MIT License 5 votes vote down vote up
def connect():
    import mongoengine
    return mongoengine.connect('discograph') 
Example #6
Source File: dump.py    From mykrobe with MIT License 5 votes vote down vote up
def run(parser, args):
    db_name = '%s-%s' % (DB_PREFIX, args.db_name)
    DB = connect(db_name)
    if args.verbose:
        logger.setLevel(level=logging.DEBUG)
    else:
        logger.setLevel(level=logging.INFO)
    al = AlleleGenerator(
        reference_filepath=args.reference_filepath,
        kmer=args.kmer)
    _variant_ids = get_non_singelton_variants(db_name)
    total = Variant.snps(id__in=_variant_ids).count()
    N = 100
    pages = math.ceil(total / N)
    for page in range(pages):
        logger.info("%i of %i - %f%%" %
                    (page*N, total, round(100*(page*N)/total, 2)))
        for variant in Variant.snps(id__in=_variant_ids).order_by("start").skip(N*page).limit(N):
            # for variant in Variant.snps().order_by("start"):
            variant_panel = make_variant_probe(al, variant, args.kmer, DB=DB)
            for i, ref in enumerate(variant_panel.refs):
                sys.stdout.write(
                    ">ref-%s?var_name=%snum_alts=%i&ref=%s&enum=%i\n" %
                    (variant_panel.variant.var_hash, variant.var_name[:100], len(
                        variant_panel.alts), variant_panel.variant.reference.id, i))
                sys.stdout.write("%s\n" % ref)
            for i, a in enumerate(variant_panel.alts):
                sys.stdout.write(">alt-%s?var_name=%s&enum=%i\n" %
                                 (variant_panel.variant.var_hash, variant.var_name[:100], i))
                sys.stdout.write("%s\n" % a) 
Example #7
Source File: schedulers.py    From celerybeat-mongo with Apache License 2.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        if hasattr(current_app.conf, "mongodb_scheduler_db"):
            db = current_app.conf.get("mongodb_scheduler_db")
        elif hasattr(current_app.conf, "CELERY_MONGODB_SCHEDULER_DB"):
            db = current_app.conf.CELERY_MONGODB_SCHEDULER_DB
        else:
            db = "celery"

        if hasattr(current_app.conf, "mongodb_scheduler_connection_alias"):
            alias = current_app.conf.get('mongodb_scheduler_connection_alias')
        elif hasattr(current_app.conf, "CELERY_MONGODB_SCHEDULER_CONNECTION_ALIAS"):
            alias = current_app.conf.CELERY_MONGODB_SCHEDULER_CONNECTION_ALIAS
        else:
            alias = "default"

        if hasattr(current_app.conf, "mongodb_scheduler_url"):
            host = current_app.conf.get('mongodb_scheduler_url')
        elif hasattr(current_app.conf, "CELERY_MONGODB_SCHEDULER_URL"):
            host = current_app.conf.CELERY_MONGODB_SCHEDULER_URL
        else:
            host = None

        self._mongo = mongoengine.connect(db, host=host, alias=alias)

        if host:
            logger.info("backend scheduler using %s/%s:%s",
                        host, db, self.Model._get_collection().name)
        else:
            logger.info("backend scheduler using %s/%s:%s",
                        "mongodb://localhost", db, self.Model._get_collection().name)
        self._schedule = {}
        self._last_updated = None
        Scheduler.__init__(self, *args, **kwargs)
        self.max_interval = (kwargs.get('max_interval')
                             or self.app.conf.CELERYBEAT_MAX_LOOP_INTERVAL or 5) 
Example #8
Source File: __init__.py    From Arsenal with GNU General Public License v3.0 4 votes vote down vote up
def create_app(**config_overrides):
    """
    Creates a flask application with the desired configuration settings
    and connects it to the database.
    """
    app = Flask(__name__)

    # Initialize logging
    _configure_logging()

    # Initialize configuration
    app.config.from_object("teamserver.config")
    app.config["MODE"] = MODE

    app.config["MONGODB_SETTINGS"] = {"db": DB_NAME, "host": DB_HOST, "port": DB_PORT}
    if DB_USER and DB_PASS:
        app.config["MONGODB_SETTINGS"]["username"] = DB_USER
        app.config["MONGODB_SETTINGS"]["password"] = DB_PASS

    app.config["CELERY_BROKER_URL"] = CELERY_BROKER_URL
    app.config["CELERY_RESULT_BACKEND"] = CELERY_RESULT_BACKEND

    # Override configuration options
    app.config.update(config_overrides)

    # Initialize DEBUG
    if MODE.upper() == "DEBUG":
        # Enable debug logging
        app.logger.setLevel(logging.DEBUG)

        # Enable profiling
        from werkzeug.contrib.profiler import ProfilerMiddleware

        app.config["PROFILE"] = True
        app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[50], profile_dir=PROFILE_DIR)

        # Enable mongodb debug toolbar
        from flask_debugtoolbar import DebugToolbarExtension

        app.config["DEBUG_TB_PANELS"] = ["flask_mongoengine.panels.MongoDebugPanel"]
        app.debug_toolbar = DebugToolbarExtension(app)
    else:
        app.logger.setLevel(logging.WARNING)

    # Initialize the database
    try:
        DB.init_app(app)
    except MongoEngineConnectionError as conn_err:
        print(conn_err)
        sys.exit("Could not connect to database.")

    # Import endpoints
    from teamserver.router import API

    app.register_blueprint(API)

    app.logger.info(f"Initialized Arsenal Teamserver [{MODE}]")
    return app 
Example #9
Source File: probe_generation.py    From mykrobe with MIT License 4 votes vote down vote up
def make_variant_probe(al, variant, kmer, DB=None, no_backgrounds=False):
    if no_backgrounds:
        context = []
    else:
        if DB is not None:
            try:
                context = get_context(variant.start, kmer)
            except:
                DB = None
                context = []
                logger.warning(
                    "Could not connect to database. Continuing without using backgrounds")
        else:
            context = []
    if context:
        logger.debug(
            "Found %i variants in context of %s" %
            (len(context), variant))
    variant_probe = None
    contexts_seen_together = seen_together(context)
    alts = []
    for context in contexts_seen_together:
        if context:
            logger.debug("Processing variant:%s with context:%s" % (
                variant, ",".join([str(c) for c in context])))
        else:
            logger.debug("Processing variant:%s " % (variant))
        try:
            panel = al.create(variant, context)
        except ValueError as e:
            pass
            logger.warning("Failed to process variant:%s context:%s. %s" % (
                variant, ",".join([str(c) for c in context]), str(e)))
        else:
            if variant_probe is not None:
                variant_probe.alts.extend(panel.alts)
                variant_probe.refs.extend(panel.refs)
            else:
                variant_probe = panel
    if variant_probe:
        variant_probe.alts = unique(variant_probe.alts)
        variant_probe.refs = unique(variant_probe.refs)
    return variant_probe