<commit_before>#
# Copyright (C) 2011 - 2015 <NAME> <<EMAIL>>
# License: MIT
#
"""A module to aggregate config parser (loader/dumper) backends.
"""
import itertools
import operator
import pkg_resources

import anyconfig.backend.ini_
import anyconfig.backend.json_
import anyconfig.backend.xml_
import anyconfig.backend.yaml_
import anyconfig.compat
import anyconfig.utils


_CPs = [p for p in [anyconfig.backend.ini_.IniConfigParser,
                    anyconfig.backend.json_.JsonConfigParser,
                    anyconfig.backend.xml_.XmlConfigParser,
                    anyconfig.backend.yaml_.YamlConfigParser] if p.supports()]

for e in pkg_resources.iter_entry_points("anyconfig_backends"):
    try:
        _CPs.append(e.load())
    except ImportError:
        continue


def cmp_cps(lhs, rhs):
    """Compare config parsers by these priorities.
    """
    return anyconfig.compat.cmp(lhs.priority(), rhs.priority())


def fst(tpl):
    """
    >>> fst((0, 1))
    0
    """
    return tpl[0]


def snd(tpl):
    """
    >>> snd((0, 1))
    1
    """
    return tpl[1]


def groupby_key(xs, kf):
    """An wrapper around itertools.groupby
    """
    return itertools.groupby(sorted(xs, key=kf), key=kf)


def uniq(iterable):
    """
    >>> uniq([1, 2, 3, 1, 2])
    [1, 2, 3]
    """
    acc = []
    for x in iterable:
        if x not in acc:
            acc.append(x)

    return acc


def list_parsers_by_type(cps=_CPs):
    """
    :return: List (generator) of (config_type, [config_parser])
    """
    return ((t, sorted(p, key=operator.methodcaller("priority"))) for t, p
            in groupby_key(cps, operator.methodcaller("type")))


def _list_xppairs(xps):
    """List config parsers by priority.
    """
    return sorted((snd(xp) for xp in xps),
                  key=operator.methodcaller("priority"))


def list_parsers_by_extension(cps=_CPs):
    """
    :return: List (generator) of (config_ext, [config_parser])
    """
    cps_by_ext = anyconfig.utils.concat(([(x, p) for x in p.extensions()] for p
                                         in cps))

    return ((x, _list_xppairs(xps)) for x, xps in groupby_key(cps_by_ext, fst))


def find_by_file(config_file, cps=_CPs):
    """
    Find config parser by file's extension.

    :param config_file: Config file path
    """
    ext = anyconfig.utils.get_file_extension(config_file)
    for x, ps in list_parsers_by_extension(cps):
        if x == ext:
            return ps[-1]

    return None


def find_by_type(cptype, cps=_CPs):
    """
    Find config parser by file's extension.

    :param cptype: Config file's type
    """
    for t, ps in list_parsers_by_type(cps):
        if t == cptype:
            return ps[-1]

    return None


def list_types(cps=_CPs):
    """List available config types.
    """
    return sorted(uniq(t for t, ps in list_parsers_by_type(cps)))

# vim:sw=4:ts=4:et:<commit_msg>make a list of parser with catching ImportError excs<commit_after>#
# Copyright (C) 2011 - 2015 <NAME> <<EMAIL>>
# License: MIT
#
"""A module to aggregate config parser (loader/dumper) backends.
"""
import itertools
import operator
import pkg_resources

import anyconfig.backend.ini_
PARSERS = [anyconfig.backend.ini_.IniConfigParser]  # It should be supported.

try:
    import anyconfig.backend.json_
    PARSERS.append(anyconfig.backend.json_.JsonConfigParser)
except ImportError:
    pass

try:
    import anyconfig.backend.xml_
    if anyconfig.backend.xml_.XmlConfigParser.supports():
        PARSERS.append(anyconfig.backend.xml_.XmlConfigParser)
except ImportError:
    pass

try:
    import anyconfig.backend.yaml_
    if anyconfig.backend.yaml_.YamlConfigParser.supports():
        PARSERS.append(anyconfig.backend.yaml_.YamlConfigParser)
except ImportError:
    pass

import anyconfig.compat
import anyconfig.utils


for e in pkg_resources.iter_entry_points("anyconfig_backends"):
    try:
        PARSERS.append(e.load())
    except ImportError:
        continue


def cmp_cps(lhs, rhs):
    """Compare config parsers by these priorities.
    """
    return anyconfig.compat.cmp(lhs.priority(), rhs.priority())


def fst(tpl):
    """
    >>> fst((0, 1))
    0
    """
    return tpl[0]


def snd(tpl):
    """
    >>> snd((0, 1))
    1
    """
    return tpl[1]


def groupby_key(xs, kf):
    """An wrapper around itertools.groupby
    """
    return itertools.groupby(sorted(xs, key=kf), key=kf)


def uniq(iterable):
    """
    >>> uniq([1, 2, 3, 1, 2])
    [1, 2, 3]
    """
    acc = []
    for x in iterable:
        if x not in acc:
            acc.append(x)

    return acc


def list_parsers_by_type(cps=PARSERS):
    """
    :return: List (generator) of (config_type, [config_parser])
    """
    return ((t, sorted(p, key=operator.methodcaller("priority"))) for t, p
            in groupby_key(cps, operator.methodcaller("type")))


def _list_xppairs(xps):
    """List config parsers by priority.
    """
    return sorted((snd(xp) for xp in xps),
                  key=operator.methodcaller("priority"))


def list_parsers_by_extension(cps=PARSERS):
    """
    :return: List (generator) of (config_ext, [config_parser])
    """
    cps_by_ext = anyconfig.utils.concat(([(x, p) for x in p.extensions()] for p
                                         in cps))

    return ((x, _list_xppairs(xps)) for x, xps in groupby_key(cps_by_ext, fst))


def find_by_file(config_file, cps=PARSERS):
    """
    Find config parser by file's extension.

    :param config_file: Config file path
    """
    ext = anyconfig.utils.get_file_extension(config_file)
    for x, ps in list_parsers_by_extension(cps):
        if x == ext:
            return ps[-1]

    return None


def find_by_type(cptype, cps=PARSERS):
    """
    Find config parser by file's extension.

    :param cptype: Config file's type
    """
    for t, ps in list_parsers_by_type(cps):
        if t == cptype:
            return ps[-1]

    return None


def list_types(cps=PARSERS):
    """List available config types.
    """
    return sorted(uniq(t for t, ps in list_parsers_by_type(cps)))

# vim:sw=4:ts=4:et:
{'fraction_non_alphanumeric': 0.12750246143747948, 'fraction_numerical': 0.009189366590088613, 'mean_word_length': 2.8357457520453115, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 5}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7460262', 'n_tokens_mistral': 2151, 'n_tokens_neox': 2083, 'n_words': 507}