8 """ |
8 """ |
9 |
9 |
10 import os |
10 import os |
11 import re |
11 import re |
12 import tokenize |
12 import tokenize |
|
13 import contextlib |
13 from io import StringIO |
14 from io import StringIO |
14 |
15 |
15 # CodeStyleCheckerDialog tries to import FixableCodeStyleIssues which fails |
16 # CodeStyleCheckerDialog tries to import FixableCodeStyleIssues which fails |
16 # under Python3. So ignore it. |
17 # under Python3. So ignore it. |
17 try: |
18 with contextlib.suppress(ImportError): |
18 import pycodestyle |
19 import pycodestyle |
19 except ImportError: |
|
20 pass |
|
21 |
20 |
22 FixableCodeStyleIssues = [ |
21 FixableCodeStyleIssues = [ |
23 "D111", "D112", "D121", "D131", "D141", "D142", |
22 "D111", "D112", "D121", "D131", "D141", "D142", |
24 "D143", "D144", "D145", |
23 "D143", "D144", "D145", |
25 "D221", "D222", "D231", "D242", "D243", "D244", |
24 "D221", "D222", "D231", "D242", "D243", "D244", |
207 # create a backup file before writing any changes |
206 # create a backup file before writing any changes |
208 if os.path.islink(self.__filename): |
207 if os.path.islink(self.__filename): |
209 bfn = '{0}~'.format(os.path.realpath(self.__filename)) |
208 bfn = '{0}~'.format(os.path.realpath(self.__filename)) |
210 else: |
209 else: |
211 bfn = '{0}~'.format(self.__filename) |
210 bfn = '{0}~'.format(self.__filename) |
212 try: |
211 with contextlib.suppress(OSError): |
213 os.remove(bfn) |
212 os.remove(bfn) |
214 except OSError: |
213 with contextlib.suppress(OSError): |
215 # if there was an error, ignore it |
|
216 pass |
|
217 try: |
|
218 os.rename(self.__filename, bfn) |
214 os.rename(self.__filename, bfn) |
219 except OSError: |
|
220 # if there was an error, ignore it |
|
221 pass |
|
222 |
215 |
223 txt = "".join(self.__source) |
216 txt = "".join(self.__source) |
224 try: |
217 try: |
225 enc = 'utf-8' if encoding == 'utf-8-bom' else encoding |
218 enc = 'utf-8' if encoding == 'utf-8-bom' else encoding |
226 txt = txt.encode(enc) |
219 txt = txt.encode(enc) |