[Python-modules-commits] [flake8-polyfill] 01/03: New upstream version 1.0.1

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Fri Jun 9 23:37:07 UTC 2017


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch master
in repository flake8-polyfill.

commit 00cb2ed2af6e9b2b8ab99fa8265435a64aaf982d
Author: Ghislain Antony Vaillant <ghisvail at gmail.com>
Date:   Fri Jun 9 15:42:55 2017 +0100

    New upstream version 1.0.1
---
 AUTHORS.rst                                       |   4 +
 CHANGELOG.rst                                     |  19 ++
 CONTRIBUTING.rst                                  |   0
 LICENSE                                           |  21 +++
 MANIFEST.in                                       |   8 +
 PKG-INFO                                          | 140 ++++++++++++++
 README.rst                                        | 118 ++++++++++++
 setup.cfg                                         |  12 ++
 setup.py                                          |  53 ++++++
 src/flake8_polyfill.egg-info/PKG-INFO             | 140 ++++++++++++++
 src/flake8_polyfill.egg-info/SOURCES.txt          |  19 ++
 src/flake8_polyfill.egg-info/dependency_links.txt |   1 +
 src/flake8_polyfill.egg-info/requires.txt         |   1 +
 src/flake8_polyfill.egg-info/top_level.txt        |   1 +
 src/flake8_polyfill/__init__.py                   |   4 +
 src/flake8_polyfill/options.py                    | 131 ++++++++++++++
 src/flake8_polyfill/stdin.py                      |  62 +++++++
 src/flake8_polyfill/version.py                    |   9 +
 tests/test_options.py                             | 211 ++++++++++++++++++++++
 tests/test_stdin.py                               |  83 +++++++++
 20 files changed, 1037 insertions(+)

diff --git a/AUTHORS.rst b/AUTHORS.rst
new file mode 100644
index 0000000..85d48f3
--- /dev/null
+++ b/AUTHORS.rst
@@ -0,0 +1,4 @@
+Creator
+=======
+
+* Ian Cordasco
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
new file mode 100644
index 0000000..3154289
--- /dev/null
+++ b/CHANGELOG.rst
@@ -0,0 +1,19 @@
+1.0.1 (2016-07-19)
+------------------
+
+- Fix some packaging bugs
+
+1.0.0 (2016-07-19)
+------------------
+
+- Release Initial Version of flake8-polyfill
+
+- Includes:
+
+  * ``flake8_polyfill.options`` for Flake8 2.x/3.x compatibility handling
+
+  * ``flake8_polyfill.stdin`` for Flake8 2.x stdin monkey patching that can
+    also be used on Flake8 3.x
+
+  * ``flake8_polyfill.version`` which can be used to retrieve a tuple to
+    test Flake8's version information on all versions of Flake8
diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst
new file mode 100644
index 0000000..e69de29
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..d32256f
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+== Flake8 License (MIT) ==
+
+Copyright (C) 2016 Ian Cordasco <graffatcolmingov at gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/MANIFEST.in b/MANIFEST.in
new file mode 100644
index 0000000..96b10e7
--- /dev/null
+++ b/MANIFEST.in
@@ -0,0 +1,8 @@
+include *.rst
+include CONTRIBUTORS.txt
+include LICENSE
+global-exclude *.pyc
+recursive-include docs *.rst *.py
+recursive-include tests *.py *.ini *.rst *_diff
+recursive-include src *.py
+prune docs/build/
diff --git a/PKG-INFO b/PKG-INFO
new file mode 100644
index 0000000..6a58d1b
--- /dev/null
+++ b/PKG-INFO
@@ -0,0 +1,140 @@
+Metadata-Version: 1.1
+Name: flake8-polyfill
+Version: 1.0.1
+Summary: Polyfill package for Flake8 plugins
+Home-page: https://gitlab.com/pycqa/flake8
+Author: Ian Cordasco
+Author-email: graffatcolmingov at gmail.com
+License: MIT
+Description: =============================
+         Polyfill for Flake8 Plugins
+        =============================
+        
+        ``flake8-polyfill`` is a package that provides some compatibility helpers for
+        Flake8 plugins that intend to support Flake8 2.x and 3.x simultaneously.
+        
+        
+        Installation
+        ============
+        
+        .. code-block:: bash
+        
+            pip install flake8-polyfill
+        
+        
+        Usage
+        =====
+        
+        Option Handling
+        ---------------
+        
+        One problem area with compatibility with Flake8 2.x and 3.x is the registering
+        options and receiving the parsed values.
+        
+        Flake8 3.0 added extra parameters to the ``add_option`` method which don't
+        have the same effect on Flake8 2.x. To accomodate the change, this polyfill
+        module allows you to do:
+        
+        .. code-block:: python
+        
+            from flake8_polyfill import options
+        
+            class MyFlake8Plugin(object):
+                @classmethod
+                def add_options(cls, parser):
+                    options.register(parser, '--my-long-option-name',
+                                     parse_from_config=True,
+                                     comma_separated_list=True,
+                                     default='...',
+                                     help='...')
+                    options.register(parser, '-m', '--my-other-long-option-name',
+                                     parse_from_config=True,
+                                     normalize_paths=True,
+                                     default='...',
+                                     help='...')
+        
+                @classmethod
+                def parse_options(cls, values):
+                    cls.my_long_option_name = values.my_long_option_name
+                    cls.my_other_long_option_name = values.my_other_long_option_name
+        
+        And have the code work the same way on both versions.
+        
+        Retrieving Standard In
+        ----------------------
+        
+        Until Flake8 2.6, getting the code on standard in from a plugin has been
+        simple:
+        
+        .. code-block:: python
+        
+            import pep8
+        
+            stdin = pep8.get_stdin_value()
+        
+        In 2.6 you now have to know whether to use ``pep8`` or ``pycodestyle`` since
+        Flake8 2.6 made a hard change to ``pycodestyle``.
+        
+        The reason you need to know which module to use is because standard in can be
+        exhausted and Flake8 does some work to cache the value so that call always
+        returns the desired data.
+        
+        In 3.0, Flake8 no longer monkey-patches those modules.
+        
+        To accommodate this, this package provides:
+        
+        .. code-block:: python
+        
+            from flake8_polyfill import stdin
+        
+            stdin.monkey_patch('all')
+            stdin.monkey_patch('pep8')
+            stdin.monkey_patch('pycodestyle')
+        
+        This allows you to have the polyfill module monkey-patch what you want so it
+        is always monkey-patched. It will also do so in an intelligent way.
+        
+        Version Comparison
+        ------------------
+        
+        Flake8 2.x did not include an object that would allow for easy version
+        comparison. Flake8 3.0, however, added a ``__version_info__`` attribute. For
+        consistency, Flake8 Polyfill will turn 2.x's version string into a tuple
+        suitable for comparison.
+        
+        .. code-block:: python
+        
+            from flake8_polyfill import version
+        
+            if (2, 4) <= version.version_info < (2, 6):
+                # ...
+            elif (2, 6) <= version.version_info < (3, 0):
+                # ...
+            elif (3, 0) <= version.version_info < (4, 0):
+                # ...
+        
+        
+        License
+        =======
+        
+        MIT
+        
+        
+        Creator
+        =======
+        
+        Ian Cordasco
+        
+Platform: UNKNOWN
+Classifier: Environment :: Console
+Classifier: Framework :: Flake8
+Classifier: Intended Audience :: Developers
+Classifier: License :: OSI Approved :: MIT License
+Classifier: Programming Language :: Python
+Classifier: Programming Language :: Python :: 2
+Classifier: Programming Language :: Python :: 2.7
+Classifier: Programming Language :: Python :: 3
+Classifier: Programming Language :: Python :: 3.4
+Classifier: Programming Language :: Python :: 3.5
+Classifier: Topic :: Software Development :: Libraries :: Python Modules
+Classifier: Topic :: Software Development :: Quality Assurance
diff --git a/README.rst b/README.rst
new file mode 100644
index 0000000..529ecd5
--- /dev/null
+++ b/README.rst
@@ -0,0 +1,118 @@
+=============================
+ Polyfill for Flake8 Plugins
+=============================
+
+``flake8-polyfill`` is a package that provides some compatibility helpers for
+Flake8 plugins that intend to support Flake8 2.x and 3.x simultaneously.
+
+
+Installation
+============
+
+.. code-block:: bash
+
+    pip install flake8-polyfill
+
+
+Usage
+=====
+
+Option Handling
+---------------
+
+One problem area with compatibility with Flake8 2.x and 3.x is the registering
+options and receiving the parsed values.
+
+Flake8 3.0 added extra parameters to the ``add_option`` method which don't
+have the same effect on Flake8 2.x. To accomodate the change, this polyfill
+module allows you to do:
+
+.. code-block:: python
+
+    from flake8_polyfill import options
+
+    class MyFlake8Plugin(object):
+        @classmethod
+        def add_options(cls, parser):
+            options.register(parser, '--my-long-option-name',
+                             parse_from_config=True,
+                             comma_separated_list=True,
+                             default='...',
+                             help='...')
+            options.register(parser, '-m', '--my-other-long-option-name',
+                             parse_from_config=True,
+                             normalize_paths=True,
+                             default='...',
+                             help='...')
+
+        @classmethod
+        def parse_options(cls, values):
+            cls.my_long_option_name = values.my_long_option_name
+            cls.my_other_long_option_name = values.my_other_long_option_name
+
+And have the code work the same way on both versions.
+
+Retrieving Standard In
+----------------------
+
+Until Flake8 2.6, getting the code on standard in from a plugin has been
+simple:
+
+.. code-block:: python
+
+    import pep8
+
+    stdin = pep8.get_stdin_value()
+
+In 2.6 you now have to know whether to use ``pep8`` or ``pycodestyle`` since
+Flake8 2.6 made a hard change to ``pycodestyle``.
+
+The reason you need to know which module to use is because standard in can be
+exhausted and Flake8 does some work to cache the value so that call always
+returns the desired data.
+
+In 3.0, Flake8 no longer monkey-patches those modules.
+
+To accommodate this, this package provides:
+
+.. code-block:: python
+
+    from flake8_polyfill import stdin
+
+    stdin.monkey_patch('all')
+    stdin.monkey_patch('pep8')
+    stdin.monkey_patch('pycodestyle')
+
+This allows you to have the polyfill module monkey-patch what you want so it
+is always monkey-patched. It will also do so in an intelligent way.
+
+Version Comparison
+------------------
+
+Flake8 2.x did not include an object that would allow for easy version
+comparison. Flake8 3.0, however, added a ``__version_info__`` attribute. For
+consistency, Flake8 Polyfill will turn 2.x's version string into a tuple
+suitable for comparison.
+
+.. code-block:: python
+
+    from flake8_polyfill import version
+
+    if (2, 4) <= version.version_info < (2, 6):
+        # ...
+    elif (2, 6) <= version.version_info < (3, 0):
+        # ...
+    elif (3, 0) <= version.version_info < (4, 0):
+        # ...
+
+
+License
+=======
+
+MIT
+
+
+Creator
+=======
+
+Ian Cordasco
diff --git a/setup.cfg b/setup.cfg
new file mode 100644
index 0000000..4d00bac
--- /dev/null
+++ b/setup.cfg
@@ -0,0 +1,12 @@
+[bdist_wheel]
+universal = 1
+
+[pytest]
+norecursedirs = .git .* *.egg* old docs dist build
+addopts = -rw
+
+[egg_info]
+tag_build = 
+tag_date = 0
+tag_svn_revision = 0
+
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..c110d99
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,53 @@
+# -*- coding: utf-8 -*-
+"""Packaging logic for Flake8's polyfill."""
+import io
+import os
+import sys
+
+import setuptools
+
+sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
+
+import flake8_polyfill  # noqa
+
+requires = ['flake8']
+
+
+def get_long_description():
+    """Generate a long description from the README file."""
+    descr = []
+    for fname in ('README.rst',):
+        with io.open(fname, encoding='utf-8') as f:
+            descr.append(f.read())
+    return '\n\n'.join(descr)
+
+
+setuptools.setup(
+    name='flake8-polyfill',
+    license='MIT',
+    version=flake8_polyfill.__version__,
+    description='Polyfill package for Flake8 plugins',
+    long_description=get_long_description(),
+    author='Ian Cordasco',
+    author_email='graffatcolmingov at gmail.com',
+    url='https://gitlab.com/pycqa/flake8',
+    package_dir={'': 'src'},
+    packages=[
+        'flake8_polyfill',
+    ],
+    install_requires=requires,
+    classifiers=[
+        "Environment :: Console",
+        "Framework :: Flake8",
+        "Intended Audience :: Developers",
+        "License :: OSI Approved :: MIT License",
+        "Programming Language :: Python",
+        "Programming Language :: Python :: 2",
+        "Programming Language :: Python :: 2.7",
+        "Programming Language :: Python :: 3",
+        "Programming Language :: Python :: 3.4",
+        "Programming Language :: Python :: 3.5",
+        "Topic :: Software Development :: Libraries :: Python Modules",
+        "Topic :: Software Development :: Quality Assurance",
+    ],
+)
diff --git a/src/flake8_polyfill.egg-info/PKG-INFO b/src/flake8_polyfill.egg-info/PKG-INFO
new file mode 100644
index 0000000..6a58d1b
--- /dev/null
+++ b/src/flake8_polyfill.egg-info/PKG-INFO
@@ -0,0 +1,140 @@
+Metadata-Version: 1.1
+Name: flake8-polyfill
+Version: 1.0.1
+Summary: Polyfill package for Flake8 plugins
+Home-page: https://gitlab.com/pycqa/flake8
+Author: Ian Cordasco
+Author-email: graffatcolmingov at gmail.com
+License: MIT
+Description: =============================
+         Polyfill for Flake8 Plugins
+        =============================
+        
+        ``flake8-polyfill`` is a package that provides some compatibility helpers for
+        Flake8 plugins that intend to support Flake8 2.x and 3.x simultaneously.
+        
+        
+        Installation
+        ============
+        
+        .. code-block:: bash
+        
+            pip install flake8-polyfill
+        
+        
+        Usage
+        =====
+        
+        Option Handling
+        ---------------
+        
+        One problem area with compatibility with Flake8 2.x and 3.x is the registering
+        options and receiving the parsed values.
+        
+        Flake8 3.0 added extra parameters to the ``add_option`` method which don't
+        have the same effect on Flake8 2.x. To accomodate the change, this polyfill
+        module allows you to do:
+        
+        .. code-block:: python
+        
+            from flake8_polyfill import options
+        
+            class MyFlake8Plugin(object):
+                @classmethod
+                def add_options(cls, parser):
+                    options.register(parser, '--my-long-option-name',
+                                     parse_from_config=True,
+                                     comma_separated_list=True,
+                                     default='...',
+                                     help='...')
+                    options.register(parser, '-m', '--my-other-long-option-name',
+                                     parse_from_config=True,
+                                     normalize_paths=True,
+                                     default='...',
+                                     help='...')
+        
+                @classmethod
+                def parse_options(cls, values):
+                    cls.my_long_option_name = values.my_long_option_name
+                    cls.my_other_long_option_name = values.my_other_long_option_name
+        
+        And have the code work the same way on both versions.
+        
+        Retrieving Standard In
+        ----------------------
+        
+        Until Flake8 2.6, getting the code on standard in from a plugin has been
+        simple:
+        
+        .. code-block:: python
+        
+            import pep8
+        
+            stdin = pep8.get_stdin_value()
+        
+        In 2.6 you now have to know whether to use ``pep8`` or ``pycodestyle`` since
+        Flake8 2.6 made a hard change to ``pycodestyle``.
+        
+        The reason you need to know which module to use is because standard in can be
+        exhausted and Flake8 does some work to cache the value so that call always
+        returns the desired data.
+        
+        In 3.0, Flake8 no longer monkey-patches those modules.
+        
+        To accommodate this, this package provides:
+        
+        .. code-block:: python
+        
+            from flake8_polyfill import stdin
+        
+            stdin.monkey_patch('all')
+            stdin.monkey_patch('pep8')
+            stdin.monkey_patch('pycodestyle')
+        
+        This allows you to have the polyfill module monkey-patch what you want so it
+        is always monkey-patched. It will also do so in an intelligent way.
+        
+        Version Comparison
+        ------------------
+        
+        Flake8 2.x did not include an object that would allow for easy version
+        comparison. Flake8 3.0, however, added a ``__version_info__`` attribute. For
+        consistency, Flake8 Polyfill will turn 2.x's version string into a tuple
+        suitable for comparison.
+        
+        .. code-block:: python
+        
+            from flake8_polyfill import version
+        
+            if (2, 4) <= version.version_info < (2, 6):
+                # ...
+            elif (2, 6) <= version.version_info < (3, 0):
+                # ...
+            elif (3, 0) <= version.version_info < (4, 0):
+                # ...
+        
+        
+        License
+        =======
+        
+        MIT
+        
+        
+        Creator
+        =======
+        
+        Ian Cordasco
+        
+Platform: UNKNOWN
+Classifier: Environment :: Console
+Classifier: Framework :: Flake8
+Classifier: Intended Audience :: Developers
+Classifier: License :: OSI Approved :: MIT License
+Classifier: Programming Language :: Python
+Classifier: Programming Language :: Python :: 2
+Classifier: Programming Language :: Python :: 2.7
+Classifier: Programming Language :: Python :: 3
+Classifier: Programming Language :: Python :: 3.4
+Classifier: Programming Language :: Python :: 3.5
+Classifier: Topic :: Software Development :: Libraries :: Python Modules
+Classifier: Topic :: Software Development :: Quality Assurance
diff --git a/src/flake8_polyfill.egg-info/SOURCES.txt b/src/flake8_polyfill.egg-info/SOURCES.txt
new file mode 100644
index 0000000..45158d1
--- /dev/null
+++ b/src/flake8_polyfill.egg-info/SOURCES.txt
@@ -0,0 +1,19 @@
+AUTHORS.rst
+CHANGELOG.rst
+CONTRIBUTING.rst
+LICENSE
+MANIFEST.in
+README.rst
+setup.cfg
+setup.py
+src/flake8_polyfill/__init__.py
+src/flake8_polyfill/options.py
+src/flake8_polyfill/stdin.py
+src/flake8_polyfill/version.py
+src/flake8_polyfill.egg-info/PKG-INFO
+src/flake8_polyfill.egg-info/SOURCES.txt
+src/flake8_polyfill.egg-info/dependency_links.txt
+src/flake8_polyfill.egg-info/requires.txt
+src/flake8_polyfill.egg-info/top_level.txt
+tests/test_options.py
+tests/test_stdin.py
\ No newline at end of file
diff --git a/src/flake8_polyfill.egg-info/dependency_links.txt b/src/flake8_polyfill.egg-info/dependency_links.txt
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/src/flake8_polyfill.egg-info/dependency_links.txt
@@ -0,0 +1 @@
+
diff --git a/src/flake8_polyfill.egg-info/requires.txt b/src/flake8_polyfill.egg-info/requires.txt
new file mode 100644
index 0000000..3930480
--- /dev/null
+++ b/src/flake8_polyfill.egg-info/requires.txt
@@ -0,0 +1 @@
+flake8
diff --git a/src/flake8_polyfill.egg-info/top_level.txt b/src/flake8_polyfill.egg-info/top_level.txt
new file mode 100644
index 0000000..bab065f
--- /dev/null
+++ b/src/flake8_polyfill.egg-info/top_level.txt
@@ -0,0 +1 @@
+flake8_polyfill
diff --git a/src/flake8_polyfill/__init__.py b/src/flake8_polyfill/__init__.py
new file mode 100644
index 0000000..57fa838
--- /dev/null
+++ b/src/flake8_polyfill/__init__.py
@@ -0,0 +1,4 @@
+"""The polyfill package for Flake8 plugins."""
+
+__version__ = '1.0.1'
+__version_info__ = tuple(int(i) for i in __version__.split('.') if i.isdigit())
diff --git a/src/flake8_polyfill/options.py b/src/flake8_polyfill/options.py
new file mode 100644
index 0000000..9684b72
--- /dev/null
+++ b/src/flake8_polyfill/options.py
@@ -0,0 +1,131 @@
+"""Option handling polyfill for Flake8 2.x and 3.x."""
+import optparse
+import os
+
+
+def register(parser, *args, **kwargs):
+    r"""Register an option for the Option Parser provided by Flake8.
+
+    :param parser:
+        The option parser being used by Flake8 to handle command-line options.
+    :param \*args:
+        Positional arguments that you might otherwise pass to ``add_option``.
+    :param \*\*kwargs:
+        Keyword arguments you might otherwise pass to ``add_option``.
+    """
+    try:
+        # Flake8 3.x registration
+        parser.add_option(*args, **kwargs)
+    except (optparse.OptionError, TypeError):
+        # Flake8 2.x registration
+        # Pop Flake8 3 parameters out of the kwargs so they don't cause a
+        # conflict.
+        parse_from_config = kwargs.pop('parse_from_config', False)
+        comma_separated_list = kwargs.pop('comma_separated_list', False)
+        normalize_paths = kwargs.pop('normalize_paths', False)
+        # In the unlikely event that the developer has specified their own
+        # callback, let's pop that and deal with that as well.
+        preexisting_callback = kwargs.pop('callback', None)
+        callback = generate_callback_from(comma_separated_list,
+                                          normalize_paths,
+                                          preexisting_callback)
+
+        if callback:
+            kwargs['callback'] = callback
+            kwargs['action'] = 'callback'
+
+        # We've updated our args and kwargs and can now rather confidently
+        # call add_option.
+        option = parser.add_option(*args, **kwargs)
+        if parse_from_config:
+            parser.config_options.append(option.get_opt_string().lstrip('-'))
+
+
+def parse_comma_separated_list(value):
+    """Parse a comma-separated list.
+
+    :param value:
+        String or list of strings to be parsed and normalized.
+    :returns:
+        List of values with whitespace stripped.
+    :rtype:
+        list
+    """
+    if not value:
+        return []
+
+    if not isinstance(value, (list, tuple)):
+        value = value.split(',')
+
+    return [item.strip() for item in value]
+
+
+def normalize_path(path, parent=os.curdir):
+    """Normalize a single-path.
+
+    :returns:
+        The normalized path.
+    :rtype:
+        str
+    """
+    # NOTE(sigmavirus24): Using os.path.sep allows for Windows paths to
+    # be specified and work appropriately.
+    separator = os.path.sep
+    if separator in path:
+        path = os.path.abspath(os.path.join(parent, path))
+    return path.rstrip(separator)
+
+
+def generate_callback_from(comma_separated_list, normalize_paths,
+                           preexisting_callback):
+    """Generate a callback from parameters provided for the option.
+
+    This uses composition to handle mixtures of the flags provided as well as
+    callbacks specified by the user.
+    """
+    if comma_separated_list and normalize_paths:
+        callback_list = [comma_separated_callback,
+                         normalize_paths_callback]
+        if preexisting_callback:
+            callback_list.append(preexisting_callback)
+        callback = compose_callbacks(*callback_list)
+    elif comma_separated_list:
+        callback = comma_separated_callback
+        if preexisting_callback:
+            callback = compose_callbacks(callback, preexisting_callback)
+    elif normalize_paths:
+        callback = normalize_paths_callback
+        if preexisting_callback:
+            callback = compose_callbacks(callback, preexisting_callback)
+    elif preexisting_callback:
+        callback = preexisting_callback
+    else:
+        callback = None
+    return callback
+
+
+def compose_callbacks(*callback_functions):
+    """Compose the callbacks provided as arguments."""
+    def _callback(option, opt_str, value, parser, *args, **kwargs):
+        """Callback that encompasses the other callbacks."""
+        for callback in callback_functions:
+            callback(option, opt_str, value, parser, *args, **kwargs)
+
+    return _callback
+
+
+def comma_separated_callback(option, opt_str, value, parser):
+    """Parse the value into a comma-separated list."""
+    value = getattr(parser.values, option.dest, value)
+    comma_separated_list = parse_comma_separated_list(value)
+    setattr(parser.values, option.dest, comma_separated_list)
+
+
+def normalize_paths_callback(option, opt_str, value, parser):
+    """Normalize the path(s) value."""
+    value = getattr(parser.values, option.dest, value)
+    if isinstance(value, list):
+        normalized = [normalize_path(s) for s in value]
+    else:
+        normalized = normalize_path(value)
+    setattr(parser.values, option.dest, normalized)
diff --git a/src/flake8_polyfill/stdin.py b/src/flake8_polyfill/stdin.py
new file mode 100644
index 0000000..0c9bab3
--- /dev/null
+++ b/src/flake8_polyfill/stdin.py
@@ -0,0 +1,62 @@
+"""Monkey-patching for pep8 and pycodestyle."""
+try:
+    import pep8
+except ImportError:
+    pep8 = None
+
+try:
+    import pycodestyle
+except ImportError:
+    pycodestyle = None
+
+from flake8_polyfill import version
+
+__all__ = ('monkey_patch',)
+
+modules = {
+    'pep8': [pep8],
+    'pycodestyle': [pycodestyle],
+    'all': [pep8, pycodestyle],
+}
+
+
+def monkey_patch(which):
+    """Monkey-patch the specified module with the appropriate stdin.
+
+    On Flake8 2.5 and lower, Flake8 would would monkey-patch
+    ``pep8.stdin_get_value`` for everyone. This avoided problems where
+    stdin might be exhausted.
+
+    On Flake8 2.6, Flake8 stopped patching ``pep8`` and started
+    monkey-patching ``pycodestyle.stdin_get_value``.
+
+    On Flake8 3.x, Flake8 has no need to monkey patch either ``pep8`` or
+    ``pycodestyle``.
+
+    This function accepts three parameters:
+
+    - pep8
+    - pycodestyle
+    - all
+
+    "all" is a special value that will monkey-patch both "pep8" and
+    "pycodestyle".
+
+    :param str which:
+        The name of the module to patch.
+    :returns:
+        Nothing.
+    :rtype:
+        NoneType
+    """
+    if (2, 0) <= version.version_info < (3, 0):
+        from flake8.engine import pep8 as _pep8
+        stdin_get_value = _pep8.stdin_get_value
+    elif (3, 0) <= version.version_info < (4, 0):
+        from flake8 import utils
+        stdin_get_value = utils.stdin_get_value
+
+    for module in modules[which]:
+        if module is None:
+            continue
+        module.stdin_get_value = stdin_get_value
diff --git a/src/flake8_polyfill/version.py b/src/flake8_polyfill/version.py
new file mode 100644
index 0000000..4c58bda
--- /dev/null
+++ b/src/flake8_polyfill/version.py
@@ -0,0 +1,9 @@
+"""Version information for Flake8 2.x and 3.x."""
+
+import flake8
+
+version_info = getattr(flake8, '__version_info__', None)
+if version_info is None:
+    version_info = tuple(
+        int(i) for i in flake8.__version__.split('.') if i.isdigit()
+    )
diff --git a/tests/test_options.py b/tests/test_options.py
new file mode 100644
index 0000000..ee3b7ca
--- /dev/null
+++ b/tests/test_options.py
@@ -0,0 +1,211 @@
+"""Tests for our options submodule."""
+import optparse
+import os
+
+import mock
+import pytest
+
+from flake8_polyfill import options
+
+
+ at pytest.mark.parametrize("value,expected", [
+    ("E123,\n\tW234,\n    E206", ["E123", "W234", "E206"]),
+    ("E123,W234,E206", ["E123", "W234", "E206"]),
+    (["E123", "W234", "E206"], ["E123", "W234", "E206"]),
+    (["E123", "\n\tW234", "\n    E206"], ["E123", "W234", "E206"]),
+])
+def test_parse_comma_separated_list(value, expected):
+    """Verify that similar inputs produce identical outputs."""
+    assert options.parse_comma_separated_list(value) == expected
+
+
+ at pytest.mark.parametrize("value,expected", [
+    ("flake8", "flake8"),
+    ("../flake8", os.path.abspath("../flake8")),
+    ("flake8/", os.path.abspath("flake8")),
+])
+def test_normalize_path(value, expected):
+    """Verify that we normalize paths provided to the tool."""
+    assert options.normalize_path(value) == expected
+
+
+def test_generate_callback_from_nothing():
+    """Assert that do not create a new callback for nothing."""
+    callback = options.generate_callback_from(comma_separated_list=False,
+                                              normalize_paths=False,
+                                              preexisting_callback=None)
+    assert callback is None
+
+
+def test_generate_callback_from_returns_preexisting():
+    """Assert we return the original callback.
+
+    If the value isn't comma separated or a path but we have a callback,
+    show that we return it unadulterated.
+    """
+    def preexisting_callback():
+        pass
+    callback = options.generate_callback_from(
+        comma_separated_list=False,
+        normalize_paths=False,
+        preexisting_callback=preexisting_callback,
+    )
+    assert callback is preexisting_callback
+
+
+def test_generate_callback_from_comma_separated_list():
+    """Assert we just return the comma-separated callback."""
+    callback = options.generate_callback_from(comma_separated_list=True,
+                                              normalize_paths=False,
+                                              preexisting_callback=None)
+    assert callback is options.comma_separated_callback
+
+
+def test_generate_callback_from_normalize_paths():
+    """Assert we just return the noramlize_paths callback."""
+    callback = options.generate_callback_from(comma_separated_list=False,
+                                              normalize_paths=True,
+                                              preexisting_callback=None)
+    assert callback is options.normalize_paths_callback
+
+
+filename = 'file.py'
+partial_path = 'path/file.py'
+abspath = os.path.abspath('path/file.py')
+multiple_paths = '{},{}'.format(filename, partial_path)
+parsed_multiple_paths = [filename, abspath]
+
+
+ at pytest.mark.parametrize('values, parsed_value, expected_value', [
+    ({}, filename, filename),
+    ({}, partial_path, abspath),
+    ({'exclude': filename}, filename, filename),
+    ({'exclude': filename}, partial_path, filename),
+    ({'exclude': partial_path}, partial_path, abspath),
+    ({'exclude': partial_path}, filename, abspath),
+    ({'exclude': [filename, partial_path]}, multiple_paths,
+        parsed_multiple_paths),
+])
+def test_normalize_paths_callback(values, parsed_value, expected_value):
+    """Assert our normalize_paths_callback behaves the right way."""
+    dest = 'exclude'
+    opt_str = '--exclude'
+    option = optparse.Option(opt_str, dest=dest)
+    parser = mock.Mock(values=optparse.Values(values))
+    options.normalize_paths_callback(option, opt_str, parsed_value, parser)
+    assert getattr(parser.values, dest) == expected_value
+
+
+single_code = 'E123'
+parsed_single_code = ['E123']
+multi_code = 'E123,E234,W504'
+parsed_multi_code = ['E123', 'E234', 'W504']
+
+
+ at pytest.mark.parametrize('values, parsed_value, expected_value', [
+    ({}, single_code, parsed_single_code),
+    ({}, multi_code, parsed_multi_code),
+    ({'select': single_code}, single_code, parsed_single_code),
+    ({'select': single_code}, multi_code, parsed_single_code),
+    ({'select': multi_code}, multi_code, parsed_multi_code),
+    ({'select': multi_code}, single_code, parsed_multi_code),
+])
+def test_comma_separated_callback(values, parsed_value, expected_value):
+    """Assert our comma_separated_callback behaves the right way."""
+    dest = 'select'
+    opt_str = '--{}'.format(dest)
+    option = optparse.Option(opt_str, dest=dest)
... 183 lines suppressed ...

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/flake8-polyfill.git



More information about the Python-modules-commits mailing list