Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support flake8 3.x onwards compatibility #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion flake8_filename/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from flake8_filename import rules
import optparse

__version__ = '1.0.0'

Expand All @@ -15,6 +16,23 @@ class FilenameChecker(object):
max_check = 50
filename_checks = dict.fromkeys(["filename_check{}".format(x) for x in range(min_check, max_check)], {})

@classmethod
def register_options(cls, parser, *args, **kwargs):
"""Options handler to support cross compatibility.

Args:
parser (OptionsManager):
"""
try:
# Flake8 3.x registration
parser.add_option(*args, **kwargs)
except (optparse.OptionError, TypeError):
# Flake8 2.x registration
parse_from_config = kwargs.pop('parse_from_config', False)
option = parser.add_option(*args, **kwargs)
if parse_from_config:
parser.config_options.append(option.get_opt_string().lstrip('-'))

@classmethod
def add_options(cls, parser):
"""Required by flake8
Expand All @@ -26,7 +44,7 @@ def add_options(cls, parser):
kwargs = {'action': 'store', 'default': '', 'parse_from_config': True,
'comma_separated_list': True}
for num in range(cls.min_check, cls.max_check):
parser.add_option(None, "--filename_check{}".format(num), **kwargs)
cls.register_options(parser, "--filename_check{}".format(num), **kwargs)

@classmethod
def parse_options(cls, options):
Expand Down