<commit_before># -*- coding: utf-8 -*-
"""Autodetection of docopt-style commands and subcommands.

Functions:
    setup_keyword: Adds a keyword to setuptools.setup to autodetect commands.
    egg_info_writer: Reads configuration from setup.cfg and writes out a new
        egg info file.
"""

from __future__ import absolute_import
from __future__ import unicode_literals

import ast
import collections
import json
import os.path
import typing

import setuptools
import six

from . import usage


_EntryPoint = collections.namedtuple(  # All data representing an entry point.
    '_EntryPoint', ('command', 'subcommand', 'callable'))


def setup_keyword(dist, _, value):
    # type: (setuptools.dist.Distribution, str, bool) -> None
    """Add autodetected commands as entry points.

    Args:
        dist: The distutils Distribution object for the project being
            installed.
        _: The keyword used in the setup function. Unused.
        value: The value set to the keyword in the setup function. If the value
            is not True, this function will do nothing.
    """
    if value is not True:
        return
    if dist.entry_points is None:
        dist.entry_points = {}
    for command, subcommands in six.iteritems(_get_commands(dist)):
        entry_point = '{command} = rcli.dispatcher:main'.format(
            command=command)
        entry_points = dist.entry_points.setdefault('console_scripts', [])
        if entry_point not in entry_points:
            entry_points.append(entry_point)
        dist.entry_points.setdefault('rcli', []).extend(subcommands)


def egg_info_writer(cmd, basename, filename):
    # type: (setuptools.command.egg_info.egg_info, str, str) -> None
    """Read rcli configuration and write it out to the egg info.
<commit_msg>support working with ini-style setup.py entry_points<commit_after># -*- coding: utf-8 -*-
"""Autodetection of docopt-style commands and subcommands.

Functions:
    setup_keyword: Adds a keyword to setuptools.setup to autodetect commands.
    egg_info_writer: Reads configuration from setup.cfg and writes out a new
        egg info file.
"""

from __future__ import absolute_import
from __future__ import unicode_literals

import ast
import collections
import configparser
import json
import os.path
import typing

import setuptools
import six

from . import usage


_EntryPoint = collections.namedtuple(  # All data representing an entry point.
    '_EntryPoint', ('command', 'subcommand', 'callable'))


def setup_keyword(dist, _, value):
    # type: (setuptools.dist.Distribution, str, bool) -> None
    """Add autodetected commands as entry points.

    Args:
        dist: The distutils Distribution object for the project being
            installed.
        _: The keyword used in the setup function. Unused.
        value: The value set to the keyword in the setup function. If the value
            is not True, this function will do nothing.
    """
    if value is not True:
        return
    if dist.entry_points is None:
        dist.entry_points = {}
    elif isinstance(dist.entry_points, str):
        config = configparser.ConfigParser()
        config.read_string(dist.entry_points)
        dist.entry_points = {k: ['='.join(t) for t in v]
                             for k, v in config.items()
                             if k != config.default_section}

    for command, subcommands in six.iteritems(_get_commands(dist)):
        entry_point = '{command} = rcli.dispatcher:main'.format(
            command=command)
        entry_points = dist.entry_points.setdefault('console_scripts', [])
        if entry_point not in entry_points:
            entry_points.append(entry_point)
        dist.entry_points.setdefault('rcli', []).extend(subcommands)


def egg_info_writer(cmd, basename, filename):
    # type: (setuptools.command.egg_info.egg_info, str, str) -> None
    """Read rcli configuration and write it out to the egg info.

{'fraction_non_alphanumeric': 0.08604060913705584, 'fraction_numerical': 0.0005076142131979696, 'mean_word_length': 3.050359712230216, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1412415', 'n_tokens_mistral': 1102, 'n_tokens_neox': 1079, 'n_words': 376}