commit 5d7f24c5dd6b34dfaccd534707d018e2889bb31e Author: Neel Chauhan neel@neelc.org Date: Wed Oct 26 20:35:15 2016 -0400
Switch to 'import pycodestyle' and add fallback mechanism --- stem/util/test_tools.py | 74 ++++++++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 29 deletions(-)
diff --git a/stem/util/test_tools.py b/stem/util/test_tools.py index 9a9f7f6..450e18a 100644 --- a/stem/util/test_tools.py +++ b/stem/util/test_tools.py @@ -11,9 +11,9 @@ Helper functions for testing. clean_orphaned_pyc - delete *.pyc files without corresponding *.py
is_pyflakes_available - checks if pyflakes is available - is_pep8_available - checks if pep8 is available + is_pycodestyle_available - checks if pycodestyle is available
- stylistic_issues - checks for PEP8 and other stylistic issues + stylistic_issues - checks for pycodestyle and other stylistic issues pyflakes_issues - static checks for problems via pyflakes """
@@ -26,7 +26,7 @@ import stem.util.conf import stem.util.system
CONFIG = stem.util.conf.config_dict('test', { - 'pep8.ignore': [], + 'pycodestyle.ignore': [], 'pyflakes.ignore': [], 'exclude_paths': [], }) @@ -34,7 +34,7 @@ CONFIG = stem.util.conf.config_dict('test', {
class Issue(collections.namedtuple('Issue', ['line_number', 'message', 'line'])): """ - Issue encountered by pyflakes or pep8. + Issue encountered by pyflakes or pycodestyle.
:var int line_number: line number the issue occured on :var str message: description of the issue @@ -86,6 +86,18 @@ def clean_orphaned_pyc(paths):
return orphaned_pyc
+def module_exists(module_name): + """ + Checks if a module exists + + :returns: **True** if module exists and **False** otherwise + """ + try: + mod = __import__(module_name) + except ImportError: + return False + else: + return True
def is_pyflakes_available(): """ @@ -102,44 +114,45 @@ def is_pyflakes_available(): return False
-def is_pep8_available(): +def is_pycodestyle_available(): """ - Checks if pep8 is availalbe. + Checks if pycodestyle is availalbe.
- :returns: **True** if we can use pep8 and **False** otherwise + :returns: **True** if we can use pycodestyle and **False** otherwise """
- try: - import pep8 - - if not hasattr(pep8, 'BaseReport'): - raise ImportError() - - return True - except ImportError: + if module_exists('pycodestyle'): + import pycodestyle + elif module_exists('pep8'): + import pep8 as pycodestyle + else: return False
+ if not hasattr(pycodestyle, 'BaseReport'): + return False + else: + return True
def stylistic_issues(paths, check_newlines = False, check_exception_keyword = False, prefer_single_quotes = False): """ - Checks for stylistic issues that are an issue according to the parts of PEP8 - we conform to. You can suppress PEP8 issues by making a 'test' configuration - that sets 'pep8.ignore'. + Checks for stylistic issues that are an issue according to the parts of + pycodestyle we conform to. You can suppress pycodestyle issues by making a + 'test' configuration that sets 'pycodestyle.ignore'.
For example, with a 'test/settings.cfg' of...
::
- # PEP8 compliance issues that we're ignoreing... + # pycodestyle compliance issues that we're ignoreing... # # * E111 and E121 four space indentations # * E501 line is over 79 characters
- pep8.ignore E111 - pep8.ignore E121 - pep8.ignore E501 + pycodestyle.ignore E111 + pycodestyle.ignore E121 + pycodestyle.ignore E501
- pep8.ignore run_tests.py => E402: import stem.util.enum + pycodestyle.ignore run_tests.py => E402: import stem.util.enum
... you can then run tests with...
@@ -182,7 +195,7 @@ def stylistic_issues(paths, check_newlines = False, check_exception_keyword = Fa ignore_rules = [] ignore_for_file = []
- for rule in CONFIG['pep8.ignore']: + for rule in CONFIG['pycodestyle.ignore']: if '=>' in rule: path, rule_entry = rule.split('=>', 1)
@@ -199,10 +212,13 @@ def stylistic_issues(paths, check_newlines = False, check_exception_keyword = Fa
return False
- if is_pep8_available(): - import pep8 + if is_pycodestyle_available(): + if module_exists('pycodestyle'): + import pycodestyle + elif module_exists('pep8'): + import pep8 as pycodestyle
- class StyleReport(pep8.BaseReport): + class StyleReport(pycodestyle.BaseReport): def __init__(self, options): super(StyleReport, self).__init__(options)
@@ -215,7 +231,7 @@ def stylistic_issues(paths, check_newlines = False, check_exception_keyword = Fa if not is_ignored(self.filename, code, line): issues.setdefault(self.filename, []).append(Issue(line_number, text, line))
- style_checker = pep8.StyleGuide(ignore = ignore_rules, reporter = StyleReport) + style_checker = pycodestyle.StyleGuide(ignore = ignore_rules, reporter = StyleReport) style_checker.check_files(list(_python_files(paths)))
if check_newlines or check_exception_keyword: @@ -269,7 +285,7 @@ def pyflakes_issues(paths): ::
pyflakes.ignore stem/util/test_tools.py => 'pyflakes' imported but unused - pyflakes.ignore stem/util/test_tools.py => 'pep8' imported but unused + pyflakes.ignore stem/util/test_tools.py => 'pycodestyle' imported but unused
If a 'exclude_paths' was set in our test config then we exclude any absolute paths matching those regexes.
tor-commits@lists.torproject.org