1"""Monkey-patching for pep8 and pycodestyle."""
2try:
3    import pep8
4except ImportError:
5    pep8 = None
6
7try:
8    import pycodestyle
9except ImportError:
10    pycodestyle = None
11
12from flake8_polyfill import version
13
14__all__ = ('monkey_patch',)
15
16modules = {
17    'pep8': [pep8],
18    'pycodestyle': [pycodestyle],
19    'all': [pep8, pycodestyle],
20}
21
22
23def monkey_patch(which):
24    """Monkey-patch the specified module with the appropriate stdin.
25
26    On Flake8 2.5 and lower, Flake8 would would monkey-patch
27    ``pep8.stdin_get_value`` for everyone. This avoided problems where
28    stdin might be exhausted.
29
30    On Flake8 2.6, Flake8 stopped patching ``pep8`` and started
31    monkey-patching ``pycodestyle.stdin_get_value``.
32
33    On Flake8 3.x, Flake8 has no need to monkey patch either ``pep8`` or
34    ``pycodestyle``.
35
36    This function accepts three parameters:
37
38    - pep8
39    - pycodestyle
40    - all
41
42    "all" is a special value that will monkey-patch both "pep8" and
43    "pycodestyle".
44
45    :param str which:
46        The name of the module to patch.
47    :returns:
48        Nothing.
49    :rtype:
50        NoneType
51    """
52    if (2, 0) <= version.version_info < (3, 0):
53        from flake8.engine import pep8 as _pep8
54        stdin_get_value = _pep8.stdin_get_value
55    elif (3, 0) <= version.version_info < (4, 0):
56        from flake8 import utils
57        stdin_get_value = utils.stdin_get_value
58
59    for module in modules[which]:
60        if module is None:
61            continue
62        module.stdin_get_value = stdin_get_value
63