25 from __future__ import unicode_literals |
25 from __future__ import unicode_literals |
26 |
26 |
27 import difflib |
27 import difflib |
28 import io |
28 import io |
29 import os |
29 import os |
|
30 import sys |
30 import re |
31 import re |
31 import tokenize |
32 import tokenize |
32 |
33 |
33 __version__ = '2.2.0' |
34 try: |
|
35 detect_encoding = tokenize.detect_encoding |
|
36 except AttributeError: |
|
37 from lib2to3.pgen2 import tokenize as lib2to3_tokenize |
|
38 detect_encoding = lib2to3_tokenize.detect_encoding |
|
39 |
|
40 __version__ = '2.3.0' |
34 |
41 |
35 |
42 |
36 class Eradicator(object): |
43 class Eradicator(object): |
37 """Eradicate comments.""" |
44 """Eradicate comments.""" |
38 BRACKET_REGEX = re.compile(r'^[()\[\]{}\s]+$') |
45 BRACKET_REGEX = re.compile(r'^[()\[\]{}\s]+$') |
211 |
218 |
212 def detect_encoding(self, filename): |
219 def detect_encoding(self, filename): |
213 """Return file encoding.""" |
220 """Return file encoding.""" |
214 try: |
221 try: |
215 with open(filename, 'rb') as input_file: |
222 with open(filename, 'rb') as input_file: |
216 from lib2to3.pgen2 import tokenize as lib2to3_tokenize |
223 encoding = detect_encoding(input_file.readline)[0] |
217 encoding = lib2to3_tokenize.detect_encoding(input_file.readline)[0] |
|
218 |
224 |
219 # Check for correctness of encoding. |
225 # Check for correctness of encoding. |
220 with self.open_with_encoding(filename, encoding) as input_file: |
226 with self.open_with_encoding(filename, encoding) as input_file: |
221 input_file.read() |
227 input_file.read() |
222 |
228 |
234 self.WHITELIST_REGEX = re.compile( |
240 self.WHITELIST_REGEX = re.compile( |
235 r'|'.join(new_whitelist), |
241 r'|'.join(new_whitelist), |
236 flags=re.IGNORECASE) |
242 flags=re.IGNORECASE) |
237 |
243 |
238 |
244 |
239 def main(argv, standard_out, standard_error): |
245 def main(argv=sys.argv, standard_out=sys.stdout, standard_error=sys.stderr): |
240 """Main entry point.""" |
246 """Main entry point.""" |
241 import argparse |
247 import argparse |
242 parser = argparse.ArgumentParser(description=__doc__, prog='eradicate') |
248 parser = argparse.ArgumentParser(description=__doc__, prog='eradicate') |
243 parser.add_argument('-i', '--in-place', action='store_true', |
249 parser.add_argument('-i', '--in-place', action='store_true', |
244 help='make changes to files instead of printing diffs') |
250 help='make changes to files instead of printing diffs') |
290 try: |
296 try: |
291 change_or_error = eradicator.fix_file(name, args=args, standard_out=standard_out) or change_or_error |
297 change_or_error = eradicator.fix_file(name, args=args, standard_out=standard_out) or change_or_error |
292 except IOError as exception: |
298 except IOError as exception: |
293 print('{}'.format(exception), file=standard_error) |
299 print('{}'.format(exception), file=standard_error) |
294 change_or_error = True |
300 change_or_error = True |
|
301 |
295 if change_or_error and args.error: |
302 if change_or_error and args.error: |
296 return 1 |
303 return 1 |
|
304 |
|
305 |
|
306 if __name__ == '__main__': |
|
307 main() |