Example #1
0
def mongo_defaults(mongo_config_file=MONGO_CONFIG_FILE,
                   private_config_file=PRIVATE_CONFIG_FILE,
                   **kwargs):
    """
    Returns mongo configuration with following precedence

    1- if passed in arguments: db, host, port, data_map, collection_suffix - use that
    2- if defined in private_config file, use that. mongo_db, mongo_host, mongo_port,
    3- if defined in mongo_config file, use that.
    3- otherwise use defaults from DEFAULT_MONGO_SPECS

    :return: mongo db, hostname, port, data_map, collection_suffix
    """
    defaults_dict = DEFAULT_MONGO_PARAMS

    try:
        with open(mongo_config_file) as file_to_parse:
            yaml_dict_mongo_config = yaml.load(file_to_parse)
    except:
        yaml_dict_mongo_config = {}

    try:
        with open(private_config_file) as file_to_parse:
            yaml_dict_private_config = yaml.load(file_to_parse)
    except:
        yaml_dict_private_config = {}

    yaml_dict = {}
    for arg_name in LIST_OF_MONGO_PARAMS:
        yaml_arg_name = 'mongo_' + arg_name

        # Start with defaults
        arg_value = defaults_dict[arg_name]
        # Overwrite with mongo config
        arg_value = get_safe_from_dict(yaml_dict_mongo_config, arg_name,
                                       arg_value)
        # Overwrite with private config
        arg_value = get_safe_from_dict(yaml_dict_private_config, arg_name,
                                       arg_value)
        # Overwrite with kwargs
        arg_value = get_safe_from_dict(kwargs, arg_name, arg_value)

        # Write
        yaml_dict[arg_name] = arg_value

    # Get from dictionary
    mongo_db = yaml_dict['db']
    hostname = yaml_dict['host']
    port = DEFAULT_MONGO_PORT

    return mongo_db, hostname, port
def ib_defaults(**kwargs):
    """
    Returns ib configuration with following precedence
    1- if passed in arguments: ipaddress, port, idoffset - use that
    2- if defined in private_config file, use that. ib_ipaddress, ib_port, ib_idoffset
    3 - if defined in system defaults file, use that
    4- otherwise use defaults DEFAULT_IB_IPADDRESS, DEFAULT_IB_PORT, DEFAULT_IB_IDOFFSET

    :return: mongo db, hostname, port
    """

    param_names_with_prefix = [
        "ib_" + arg_name for arg_name in LIST_OF_IB_PARAMS
    ]
    config_dict = get_list_of_private_then_default_key_values(
        param_names_with_prefix)

    yaml_dict = {}
    for arg_name in LIST_OF_IB_PARAMS:
        yaml_arg_name = "ib_" + arg_name

        # Start with config (precedence: private config, then system config)
        arg_value = config_dict[yaml_arg_name]
        # Overwrite with kwargs
        arg_value = get_safe_from_dict(kwargs, arg_name, arg_value)

        # Write
        yaml_dict[arg_name] = arg_value

    # Get from dictionary
    ipaddress = yaml_dict.get("ipaddress", DEFAULT_IB_IPADDRESS)
    port = yaml_dict.get("port", DEFAULT_IB_PORT)
    idoffset = yaml_dict.get("idoffset", DEFAULT_IB_IDOFFSET)

    return ipaddress, port, idoffset
def mongo_defaults(**kwargs):
    """
    Returns mongo configuration with following precedence

    1- if passed in arguments: db, host - use that
    2- if defined in private_config file, use that. mongo_db, mongo_host
    3- if defined in system defaults file, use that: mongo_db, mongo_host

    :return: mongo db, hostname, port
    """
    param_names_with_prefix = [
        "mongo_" + arg_name for arg_name in LIST_OF_MONGO_PARAMS]
    config_dict = get_list_of_private_then_default_key_values(
        param_names_with_prefix)

    yaml_dict = {}
    for arg_name in LIST_OF_MONGO_PARAMS:
        yaml_arg_name = "mongo_" + arg_name

        # Start with config (precedence: private config, then system config)
        arg_value = config_dict[yaml_arg_name]
        # Overwrite with kwargs
        arg_value = get_safe_from_dict(kwargs, arg_name, arg_value)

        # Write
        yaml_dict[arg_name] = arg_value

    # Get from dictionary
    mongo_db = yaml_dict["db"]
    hostname = yaml_dict["host"]
    port = DEFAULT_MONGO_PORT

    return mongo_db, hostname, port