eric6/Plugins/CheckerPlugins/CodeStyleChecker/eradicate.py

Sat, 27 Feb 2021 12:08:23 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 27 Feb 2021 12:08:23 +0100
changeset 8138
169e65a6787c
parent 7980
2c3f14a3c595
permissions
-rw-r--r--

Shell: added functionality to show a prompt when the main client process has exited (e.g. a script ended).

7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
1 # Copyright (C) 2012-2018 Steven Myint
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2 #
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Permission is hereby granted, free of charge, to any person obtaining
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 # a copy of this software and associated documentation files (the
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5 # "Software"), to deal in the Software without restriction, including
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 # without limitation the rights to use, copy, modify, merge, publish,
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 # distribute, sublicense, and/or sell copies of the Software, and to
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 # permit persons to whom the Software is furnished to do so, subject to
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9 # the following conditions:
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 #
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 # The above copyright notice and this permission notice shall be included
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 # in all copies or substantial portions of the Software.
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 #
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 """Removes commented-out Python code."""
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
24 from __future__ import print_function
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
25 from __future__ import unicode_literals
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
26
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 import difflib
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 import io
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 import os
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 import re
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 import tokenize
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
33 __version__ = '2.0.0'
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
36 class Eradicator(object):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
37 """Eradicate comments."""
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
38 BRACKET_REGEX = re.compile(r'^[()\[\]{}\s]+$')
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
39 CODING_COMMENT_REGEX = re.compile(r'.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)')
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
40 DEF_STATEMENT_REGEX = re.compile(r"def .+\)[\s]+->[\s]+[a-zA-Z_][a-zA-Z0-9_]*:$")
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
41 FOR_STATEMENT_REGEX = re.compile(r"for [a-zA-Z_][a-zA-Z0-9_]* in .+:$")
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
42 HASH_NUMBER = re.compile(r'#[0-9]')
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
43 MULTILINE_ASSIGNMENT_REGEX = re.compile(r'^\s*\w+\s*=.*[(\[{]$')
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
44 PARTIAL_DICTIONARY_REGEX = re.compile(r'^\s*[\'"]\w+[\'"]\s*:.+[,{]\s*$')
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
45 PRINT_RETURN_REGEX = re.compile(r'^(print|return)\b\s*')
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
46 WITH_STATEMENT_REGEX = re.compile(r"with .+ as [a-zA-Z_][a-zA-Z0-9_]*:$")
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
48 CODE_INDICATORS = ['(', ')', '[', ']', '{', '}', ':', '=', '%',
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
49 'print', 'return', 'break', 'continue', 'import']
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
50 CODE_KEYWORDS = [r'elif\s+.*', 'else', 'try', 'finally', r'except\s+.*']
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
51 CODE_KEYWORDS_AGGR = CODE_KEYWORDS + [r'if\s+.*']
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
52 WHITESPACE_HASH = ' \t\v\n#'
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
54 DEFAULT_WHITELIST = (
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
55 r'pylint',
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
56 r'pyright',
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
57 r'noqa',
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
58 r'type:\s*ignore',
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
59 r'fmt:\s*(on|off)',
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
60 r'TODO',
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
61 r'FIXME',
7980
2c3f14a3c595 CodeStyleCheckerDialog: added a commented code whitelist patterns to the Miscellaneous Checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7978
diff changeset
62 r'XXX'
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
63 )
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
64 WHITELIST_REGEX = re.compile(r'|'.join(DEFAULT_WHITELIST), flags=re.IGNORECASE)
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
65
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
66 def comment_contains_code(self, line, aggressive=True):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
67 """Return True comment contains code."""
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
68 line = line.lstrip()
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
69 if not line.startswith('#'):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
70 return False
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
72 line = line.lstrip(self.WHITESPACE_HASH).strip()
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
73
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
74 # Ignore non-comment related hashes. For example, "# Issue #999".
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
75 if self.HASH_NUMBER.search(line):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
76 return False
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
77
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
78 # Ignore whitelisted comments
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
79 if self.WHITELIST_REGEX.search(line):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
80 return False
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
82 if self.CODING_COMMENT_REGEX.match(line):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
83 return False
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
85 # Check that this is possibly code.
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
86 for symbol in self.CODE_INDICATORS:
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
87 if symbol in line:
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
88 break
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
89 else:
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
90 return False
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
91
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
92 if self.multiline_case(line, aggressive=aggressive):
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 return True
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
95 for symbol in self.CODE_KEYWORDS_AGGR if aggressive else self.CODE_KEYWORDS:
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
96 if re.match(r'^\s*' + symbol + r'\s*:\s*$', line):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
97 return True
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
98
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
99 line = self.PRINT_RETURN_REGEX.sub('', line)
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
100
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
101 if self.PARTIAL_DICTIONARY_REGEX.match(line):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
102 return True
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
104 try:
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
105 compile(line, '<string>', 'exec')
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
106 except (SyntaxError, TypeError, UnicodeDecodeError):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
107 return False
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
108 else:
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
109 return True
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
110
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
111
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
112 def multiline_case(self, line, aggressive=True):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
113 """Return True if line is probably part of some multiline code."""
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
114 if aggressive:
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
115 for ending in ')]}':
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
116 if line.endswith(ending + ':'):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
117 return True
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
119 if line.strip() == ending + ',':
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
120 return True
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
121
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
122 # Check whether a function/method definition with return value
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
123 # annotation
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
124 if self.DEF_STATEMENT_REGEX.search(line):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
125 return True
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
126
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
127 # Check weather a with statement
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
128 if self.WITH_STATEMENT_REGEX.search(line):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
129 return True
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
130
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
131 # Check weather a for statement
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
132 if self.FOR_STATEMENT_REGEX.search(line):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
133 return True
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
134
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
135 if line.endswith('\\'):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
136 return True
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
137
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
138 if self.MULTILINE_ASSIGNMENT_REGEX.match(line):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
139 return True
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
140
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
141 if self.BRACKET_REGEX.match(line):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
142 return True
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
143
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 return False
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
147 def commented_out_code_line_numbers(self, source, aggressive=True):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
148 """Yield line numbers of commented-out code."""
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
149 sio = io.StringIO(source)
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
150 try:
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
151 for token in tokenize.generate_tokens(sio.readline):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
152 token_type = token[0]
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
153 start_row = token[2][0]
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
154 line = token[4]
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
156 if (token_type == tokenize.COMMENT and
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
157 line.lstrip().startswith('#') and
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
158 self.comment_contains_code(line, aggressive)):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
159 yield start_row
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
160 except (tokenize.TokenError, IndentationError):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
161 pass
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
164 def filter_commented_out_code(self, source, aggressive=True):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
165 """Yield code with commented out code removed."""
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
166 marked_lines = list(self.commented_out_code_line_numbers(source,
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
167 aggressive))
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
168 sio = io.StringIO(source)
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
169 previous_line = ''
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
170 for line_number, line in enumerate(sio.readlines(), start=1):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
171 if (line_number not in marked_lines or
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
172 previous_line.rstrip().endswith('\\')):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
173 yield line
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
174 previous_line = line
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
177 def fix_file(self, filename, args, standard_out):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
178 """Run filter_commented_out_code() on file."""
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
179 encoding = self.detect_encoding(filename)
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
180 with self.open_with_encoding(filename, encoding=encoding) as input_file:
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
181 source = input_file.read()
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
182
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
183 filtered_source = ''.join(self.filter_commented_out_code(source,
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
184 args.aggressive))
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
185
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
186 if source != filtered_source:
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
187 if args.in_place:
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
188 with self.open_with_encoding(filename, mode='w',
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
189 encoding=encoding) as output_file:
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
190 output_file.write(filtered_source)
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
191 else:
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
192 diff = difflib.unified_diff(
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
193 source.splitlines(),
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
194 filtered_source.splitlines(),
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
195 'before/' + filename,
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
196 'after/' + filename,
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
197 lineterm='')
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
198 standard_out.write('\n'.join(list(diff) + ['']))
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
199 return True
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
202 def open_with_encoding(self, filename, encoding, mode='r'):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
203 """Return opened file with a specific encoding."""
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
204 return io.open(filename, mode=mode, encoding=encoding,
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
205 newline='') # Preserve line endings
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
208 def detect_encoding(self, filename):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
209 """Return file encoding."""
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
210 try:
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
211 with open(filename, 'rb') as input_file:
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
212 from lib2to3.pgen2 import tokenize as lib2to3_tokenize
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
213 encoding = lib2to3_tokenize.detect_encoding(input_file.readline)[0]
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
215 # Check for correctness of encoding.
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
216 with self.open_with_encoding(filename, encoding) as input_file:
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
217 input_file.read()
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
219 return encoding
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
220 except (SyntaxError, LookupError, UnicodeDecodeError):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
221 return 'latin-1'
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
223 def update_whitelist(self, new_whitelist, extend_default=True):
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
224 """Updates the whitelist."""
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
225 if extend_default:
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
226 self.WHITELIST_REGEX = re.compile(
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
227 r'|'.join(list(self.DEFAULT_WHITELIST) + new_whitelist),
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
228 flags=re.IGNORECASE)
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
229 else:
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
230 self.WHITELIST_REGEX = re.compile(
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
231 r'|'.join(new_whitelist),
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
232 flags=re.IGNORECASE)
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 def main(argv, standard_out, standard_error):
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 """Main entry point."""
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 import argparse
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 parser = argparse.ArgumentParser(description=__doc__, prog='eradicate')
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 parser.add_argument('-i', '--in-place', action='store_true',
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 help='make changes to files instead of printing diffs')
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241 parser.add_argument('-r', '--recursive', action='store_true',
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242 help='drill down directories recursively')
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 parser.add_argument('-a', '--aggressive', action='store_true',
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 help='make more aggressive changes; '
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 'this may result in false positives')
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
246 parser.add_argument('-e', '--error', action="store_true",
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
247 help="Exit code based on result of check")
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 parser.add_argument('--version', action='version',
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 version='%(prog)s ' + __version__)
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
250 parser.add_argument('--whitelist', action="store",
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
251 help=(
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
252 'String of "#" separated comment beginnings to whitelist. '
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
253 'Single parts are interpreted as regex. '
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
254 'OVERWRITING the default whitelist: {}'
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
255 ).format(Eradicator.DEFAULT_WHITELIST))
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
256 parser.add_argument('--whitelist-extend', action="store",
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
257 help=(
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
258 'String of "#" separated comment beginnings to whitelist '
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
259 'Single parts are interpreted as regex. '
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
260 'Overwrites --whitelist. '
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
261 'EXTENDING the default whitelist: {} '
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
262 ).format(Eradicator.DEFAULT_WHITELIST))
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 parser.add_argument('files', nargs='+', help='files to format')
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 args = parser.parse_args(argv[1:])
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
267 eradicator = Eradicator()
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
268
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
269 if args.whitelist_extend:
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
270 eradicator.update_whitelist(args.whitelist_extend.split('#'), True)
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
271 elif args.whitelist:
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
272 eradicator.update_whitelist(args.whitelist.split('#'), False)
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
273
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 filenames = list(set(args.files))
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
275 change_or_error = False
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 while filenames:
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 name = filenames.pop(0)
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278 if args.recursive and os.path.isdir(name):
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 for root, directories, children in os.walk('{}'.format(name)):
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280 filenames += [os.path.join(root, f) for f in children
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 if f.endswith('.py') and
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282 not f.startswith('.')]
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283 directories[:] = [d for d in directories
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 if not d.startswith('.')]
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
285 else:
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286 try:
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
287 change_or_error = eradicator.fix_file(name, args=args, standard_out=standard_out) or change_or_error
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
288 except IOError as exception:
7040
f89952e5fc11 Code Style Checker: added check for commented code that should be removed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289 print('{}'.format(exception), file=standard_error)
7972
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
290 change_or_error = True
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
291 if change_or_error and args.error:
4fc11172df1b Code Style Checker: upgraded eradicate.py to version 2.0.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
292 return 1

eric ide

mercurial