<commit_before>"""
Check if expected databases and hardware are alive.
"""
import os
import logging
from collections import namedtuple
import requests
import epics
import pymongo
import pyOlog
import pyOlog.conf


logger = logging.getLogger(__name__)


def connect_mds_mongodb():
    import metadatastore.conf
    _connect_mongodb('metadatastore', metadatastore.conf.connection_config)


def connect_fs_mongodb():
    import filestore.conf
    _connect_mongodb('filestore', filestore.conf.connection_config)


def _connect_mongodb(name, connection_config):
    cc = connection_config
    logger.debug("Attempting to connect to %s mongodb at host %s "
                 "and port %d", name, cc['host'], cc['port'])
    try:
        mc = pymongo.MongoClient(cc['host'], cc['port'])
    except Exception:
        logger.debug("Failed to connect to %s mongodb.", name)
        raise
    else:
        logger.debug("Successfully connected to %s mongodb.", name)
    logger.debug("Attemping to find %s database called %s in mongodb.", name,
                 cc['database'])
    if cc['database'] in mc.database_names():
        logger.debug("Database for %s successfully found.", name)
    else:
        logger.debug("Database for %s not found.", name)
        raise RuntimeError("Database for %s called %s not found." %
                           (name, cc['database']))


def connect_olog():
    paths = pyOlog.conf.Config.conf_files
    found_file = False
    for path in paths:
        logger.debug("Attemping to find olog config file at path %s", path)
        if os.path.isfile(path):
            logger.debug("Olog config file successfully found.")
            found_file = True
            break
        else:
            logger.debug("Olog config file not found.")
    if not found_file:
        logger.debug("Olog config file was not found at any of "
                     "these paths: %r", paths)
        raise RuntimeError("Olog config file was not found at any of "
                           "these paths: %r" % paths)
    logger.debug("Attempting to create OlogClient.")
    try:
        pyOlog.OlogClient()
    except Exception:
        logger.debug("Failed to create OlogClient.")
        raise RuntimeError("Failed to create OlogClient.")
    else:<commit_msg>REF: Make pyOlog an optional dep.<commit_after>"""
Check if expected databases and hardware are alive.
"""
import os
import logging
from collections import namedtuple
import requests
import epics
import pymongo


logger = logging.getLogger(__name__)


def connect_mds_mongodb():
    import metadatastore.conf
    _connect_mongodb('metadatastore', metadatastore.conf.connection_config)


def connect_fs_mongodb():
    import filestore.conf
    _connect_mongodb('filestore', filestore.conf.connection_config)


def _connect_mongodb(name, connection_config):
    cc = connection_config
    logger.debug("Attempting to connect to %s mongodb at host %s "
                 "and port %d", name, cc['host'], cc['port'])
    try:
        mc = pymongo.MongoClient(cc['host'], cc['port'])
    except Exception:
        logger.debug("Failed to connect to %s mongodb.", name)
        raise
    else:
        logger.debug("Successfully connected to %s mongodb.", name)
    logger.debug("Attemping to find %s database called %s in mongodb.", name,
                 cc['database'])
    if cc['database'] in mc.database_names():
        logger.debug("Database for %s successfully found.", name)
    else:
        logger.debug("Database for %s not found.", name)
        raise RuntimeError("Database for %s called %s not found." %
                           (name, cc['database']))


def connect_olog():
    import pyOlog
    import pyOlog.conf
    paths = pyOlog.conf.Config.conf_files
    found_file = False
    for path in paths:
        logger.debug("Attemping to find olog config file at path %s", path)
        if os.path.isfile(path):
            logger.debug("Olog config file successfully found.")
            found_file = True
            break
        else:
            logger.debug("Olog config file not found.")
    if not found_file:
        logger.debug("Olog config file was not found at any of "
                     "these paths: %r", paths)
        raise RuntimeError("Olog config file was not found at any of "
                           "these paths: %r" % paths)
    logger.debug("Attempting to create OlogClient.")
    try:
        pyOlog.OlogClient()
    except Exception:
        logger.debug("Failed to create OlogClient.")
        raise RuntimeError("Failed to create OlogClient.")
    else:
{'fraction_non_alphanumeric': 0.09973753280839895, 'fraction_numerical': 0.0, 'mean_word_length': 2.6322478157267675, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1756490', 'n_tokens_mistral': 1311, 'n_tokens_neox': 1268, 'n_words': 412}