<commit_before>#
# Copyright (C) 2012 <NAME> <<EMAIL>>
# License: MIT
#
import anyconfig.Bunch as B
import anyconfig.backend.backends as Backends
import anyconfig.backend.json_ as BJ

import logging
import os.path


def __find_parser(config_path, forced_type=None):
    """
    @param config_path: Configuration file path
    @param forced_type: Forced configuration parser type
    """
    if forced_type is not None:
        cparser = Backends.find_by_type(forced_type)
        if not cparser:
            logging.error(
                "No parser found for given type: " + forced_type
            )
    else:
        cparser = Backends.find_by_file(config_path)
        if not cparser:
            logging.error(
                "No parser found for given file: " + config_path
            )

    if cparser:
        logging.info("Using config parser: " + str(cparser))

    return cparser


def load(config_path, forced_type=None, **kwargs):
    """
    @param config_path: Configuration file path
    @param forced_type: Forced configuration parser type
    """
    cparser = __find_parser(config_path, forced_type)
    if not cparser:
        return None

    logging.info("Loading: " + config_path)
    return cparser.load(config_path, **kwargs)


def loads(paths=[], update=B.ST_MERGE_DICTS):
    """
    @param paths: Configuration file path list
    @param update: Update merging strategy to use.
        see also: anyconfig.Bunch.update
    """
    config = B.Bunch()
    for p in paths:
        config.update(load(p), update)

    return config


def dump(data, config_path, forced_type=None):
    cparser = __find_parser(config_path, forced_type)
    if not cparser or not getattr(cparser, "dump", False):
        logging.warn(
            "Dump method not implemented. Fallback to JsonConfigParser"
        )
        cparser = BJ.JsonConfigParser()
        config_path = os.path.splitext(config_path)[0] + ".json"

    logging.debug("Save to: " + config_path)
    cparser.dump(data, config_path)

# vim:sw=4:ts=4:et:<commit_msg>made a function to find parser public<commit_after>#
# Copyright (C) 2012 <NAME> <ssato @ redhat.com>
# License: MIT
#
import anyconfig.Bunch as B
import anyconfig.backend.backends as Backends
import anyconfig.backend.json_ as BJ

import logging
import os.path


def find_parser(config_path, forced_type=None):
    """
    @param config_path: Configuration file path
    @param forced_type: Forced configuration parser type
    """
    if forced_type is not None:
        cparser = Backends.find_by_type(forced_type)
        if not cparser:
            logging.error(
                "No parser found for given type: " + forced_type
            )
    else:
        cparser = Backends.find_by_file(config_path)
        if not cparser:
            logging.error(
                "No parser found for given file: " + config_path
            )

    if cparser:
        logging.info("Using config parser: " + str(cparser))

    return cparser


def load(config_path, forced_type=None, **kwargs):
    """
    @param config_path: Configuration file path
    @param forced_type: Forced configuration parser type
    """
    cparser = find_parser(config_path, forced_type)
    if not cparser:
        return None

    logging.info("Loading: " + config_path)
    return cparser.load(config_path, **kwargs)


def loads(paths=[], update=B.ST_MERGE_DICTS):
    """
    @param paths: Configuration file path list
    @param update: Update merging strategy to use.
        see also: anyconfig.Bunch.update
    """
    config = B.Bunch()
    for p in paths:
        config.update(load(p), update)

    return config


def dump(data, config_path, forced_type=None):
    cparser = find_parser(config_path, forced_type)
    if not cparser or not getattr(cparser, "dump", False):
        logging.warn(
            "Dump method not implemented. Fallback to JsonConfigParser"
        )
        cparser = BJ.JsonConfigParser()
        config_path = os.path.splitext(config_path)[0] + ".json"

    logging.debug("Save to: " + config_path)
    cparser.dump(data, config_path)

# vim:sw=4:ts=4:et:
{'fraction_non_alphanumeric': 0.09676635059567226, 'fraction_numerical': 0.0034038414782397277, 'mean_word_length': 2.7952029520295203, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '882684', 'n_tokens_mistral': 1279, 'n_tokens_neox': 1242, 'n_words': 344}