Plugins/CheckerPlugins/Pep8/Pep8Fixer.py

Mon, 16 Sep 2013 19:59:46 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 16 Sep 2013 19:59:46 +0200
changeset 2921
9e8431cbd2cf
parent 2912
9ff696796092
child 2923
01ac1f364b38
permissions
-rw-r--r--

Added a TODO to the PEP-8 fixer to extend it to fix some PEP-257 issues as well.

849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
2302
f29e9405c851 Updated copyright for 2013.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
3 # Copyright (c) 2011 - 2013 Detlev Offenbach <detlev@die-offenbachs.de>
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
2912
9ff696796092 Changed some display and source docu strings in the code style checker to make them more universal because the scope goes beyond PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2895
diff changeset
7 Module implementing a class to fix certain code style issues.
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import os
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 import re
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
12 import tokenize
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
13 import io
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 from PyQt4.QtCore import QObject
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 from E5Gui import E5MessageBox
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
19 from . import pep8
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
20
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 import Utilities
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22
2921
9e8431cbd2cf Added a TODO to the PEP-8 fixer to extend it to fix some PEP-257 issues as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2912
diff changeset
23 # TODO: add fixers for some docstring issues
9e8431cbd2cf Added a TODO to the PEP-8 fixer to extend it to fix some PEP-257 issues as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2912
diff changeset
24
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
25 Pep8FixableIssues = ["E101", "E111", "E121", "E122", "E123", "E124",
2895
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
26 "E125", "E126", "E127", "E128", "E133", "E201",
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
27 "E202", "E203", "E211", "E221", "E222", "E223",
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
28 "E224", "E225", "E226", "E227", "E228", "E231",
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
29 "E241", "E242", "E251", "E261", "E262", "E271",
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
30 "E272", "E273", "E274", "E301", "E302", "E303",
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
31 "E304", "E401", "E501", "E502", "E701", "E702",
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
32 "E703", "E711", "E712",
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
33 "N804", "N805", "N806",
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
34 "W191", "W291", "W292", "W293", "W391", "W603",
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
35 ]
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
37
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 class Pep8Fixer(QObject):
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 """
2912
9ff696796092 Changed some display and source docu strings in the code style checker to make them more universal because the scope goes beyond PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2895
diff changeset
40 Class implementing a fixer for certain code style issues.
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 """
2876
bfa39cf40277 Extended the PEP-8 dialog to select fixable issues manually (i.e. fix only the selected issues).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2875
diff changeset
42 def __init__(self, project, filename, sourceLines, fixCodes, noFixCodes,
bfa39cf40277 Extended the PEP-8 dialog to select fixable issues manually (i.e. fix only the selected issues).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2875
diff changeset
43 maxLineLength, inPlace):
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 Constructor
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 @param project reference to the project object (Project)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 @param filename name of the file to be fixed (string)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 @param sourceLines list of source lines including eol marker
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 (list of string)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 @param fixCodes list of codes to be fixed as a comma separated
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 string (string)
2876
bfa39cf40277 Extended the PEP-8 dialog to select fixable issues manually (i.e. fix only the selected issues).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2875
diff changeset
53 @param noFixCodes list of codes not to be fixed as a comma
bfa39cf40277 Extended the PEP-8 dialog to select fixable issues manually (i.e. fix only the selected issues).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2875
diff changeset
54 separated string (string)
bfa39cf40277 Extended the PEP-8 dialog to select fixable issues manually (i.e. fix only the selected issues).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2875
diff changeset
55 @param maxLineLength maximum allowed line length (integer)
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 @param inPlace flag indicating to modify the file in place (boolean)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 """
1131
7781e396c903 Changed the code to use super() to access the superclass.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1031
diff changeset
58 super().__init__()
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 self.__project = project
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 self.__filename = filename
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 self.__origName = ""
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 self.__source = sourceLines[:] # save a copy
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 self.__fixCodes = [c.strip() for c in fixCodes.split(",") if c.strip()]
2883
3c99a1db1506 Finished the PEP-8 line shortener.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2882
diff changeset
65 self.__noFixCodes = [
3c99a1db1506 Finished the PEP-8 line shortener.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2882
diff changeset
66 c.strip() for c in noFixCodes.split(",") if c.strip()]
2876
bfa39cf40277 Extended the PEP-8 dialog to select fixable issues manually (i.e. fix only the selected issues).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2875
diff changeset
67 self.__maxLineLength = maxLineLength
853
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
68 self.fixed = 0
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
70 self.__reindenter = None
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
71 self.__eol = ""
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
72 self.__indentWord = self.__getIndentWord()
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
73
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 if not inPlace:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 self.__origName = self.__filename
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
76 self.__filename = os.path.join(
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
77 os.path.dirname(self.__filename),
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 "fixed_" + os.path.basename(self.__filename))
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 self.__fixes = {
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
81 "E101": self.__fixE101,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
82 "E111": self.__fixE101,
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
83 "E121": self.__fixE121,
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
84 "E122": self.__fixE122,
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
85 "E123": self.__fixE123,
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
86 "E124": self.__fixE121,
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
87 "E125": self.__fixE125,
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
88 "E126": self.__fixE126,
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
89 "E127": self.__fixE127,
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
90 "E128": self.__fixE127,
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
91 "E133": self.__fixE126,
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
92 "E201": self.__fixE201,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
93 "E202": self.__fixE201,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
94 "E203": self.__fixE201,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
95 "E211": self.__fixE201,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
96 "E221": self.__fixE221,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
97 "E222": self.__fixE221,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
98 "E223": self.__fixE221,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
99 "E224": self.__fixE221,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
100 "E225": self.__fixE221,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
101 "E226": self.__fixE221,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
102 "E227": self.__fixE221,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
103 "E228": self.__fixE221,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
104 "E231": self.__fixE231,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
105 "E241": self.__fixE221,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
106 "E242": self.__fixE221,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
107 "E251": self.__fixE251,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
108 "E261": self.__fixE261,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
109 "E262": self.__fixE261,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
110 "E271": self.__fixE221,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
111 "E272": self.__fixE221,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
112 "E273": self.__fixE221,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
113 "E274": self.__fixE221,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
114 "E301": self.__fixE301,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
115 "E302": self.__fixE302,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
116 "E303": self.__fixE303,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
117 "E304": self.__fixE304,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
118 "E401": self.__fixE401,
2882
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
119 "E501": self.__fixE501,
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
120 "E502": self.__fixE502,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
121 "E701": self.__fixE701,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
122 "E702": self.__fixE702,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
123 "E703": self.__fixE702,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
124 "E711": self.__fixE711,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
125 "E712": self.__fixE711,
2895
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
126 "N804": self.__fixN804,
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
127 "N805": self.__fixN804,
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
128 "N806": self.__fixN806,
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
129 "W191": self.__fixE101,
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
130 "W291": self.__fixW291,
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
131 "W292": self.__fixW292,
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
132 "W293": self.__fixW291,
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
133 "W391": self.__fixW391,
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
134 "W603": self.__fixW603,
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 }
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 self.__modified = False
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
137 self.__stackLogical = [] # these need to be fixed before the file
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
138 # is saved but after all other inline
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
139 # fixes. These work with logical lines.
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
140 self.__stack = [] # these need to be fixed before the file
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
141 # is saved but after all inline fixes
2882
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
142
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
143 self.__multiLineNumbers = None
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
144 self.__docLineNumbers = None
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
145
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
146 self.__lastID = 0
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 def saveFile(self, encoding):
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 Public method to save the modified file.
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 @param encoding encoding of the source file (string)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 @return flag indicating success (boolean)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 if not self.__modified:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 # no need to write
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 return True
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 txt = "".join(self.__source)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 try:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 Utilities.writeEncodedFile(self.__filename, txt, encoding)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 except (IOError, Utilities.CodingError, UnicodeError) as err:
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
163 E5MessageBox.critical(
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
164 self,
2912
9ff696796092 Changed some display and source docu strings in the code style checker to make them more universal because the scope goes beyond PEP-8.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2895
diff changeset
165 self.trUtf8("Fix Code Style Issues"),
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 self.trUtf8(
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 """<p>Could not save the file <b>{0}</b>."""
2876
bfa39cf40277 Extended the PEP-8 dialog to select fixable issues manually (i.e. fix only the selected issues).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2875
diff changeset
168 """ Skipping it.</p><p>Reason: {1}</p>""")
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
169 .format(self.__filename, str(err))
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 )
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 return False
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 return True
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174
2878
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
175 def __codeMatch(self, code):
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
176 """
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
177 Private method to check, if the code should be fixed.
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
178
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
179 @param code to check (string)
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
180 @return flag indicating it should be fixed (boolean)
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
181 """
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
182 def mutualStartswith(a, b):
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
183 """
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
184 Local helper method to compare the beginnings of two strings
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
185 against each other.
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
186
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
187 @return flag indicating that one string starts with the other
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
188 (boolean)
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
189 """
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
190 return b.startswith(a) or a.startswith(b)
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
191
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
192 if self.__noFixCodes:
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
193 for noFixCode in [c.strip() for c in self.__noFixCodes]:
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
194 if mutualStartswith(code.lower(), noFixCode.lower()):
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
195 return False
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
196
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
197 if self.__fixCodes:
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
198 for fixCode in [c.strip() for c in self.__fixCodes]:
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
199 if mutualStartswith(code.lower(), fixCode.lower()):
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
200 return True
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
201 return False
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
202
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
203 return True
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
204
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 def fixIssue(self, line, pos, message):
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 Public method to fix the fixable issues.
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208
1031
c0f795aa71a7 Fixed an issue in the PEP-8 fixer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 945
diff changeset
209 @param line line number of issue (integer)
c0f795aa71a7 Fixed an issue in the PEP-8 fixer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 945
diff changeset
210 @param pos character position of issue (integer)
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 @param message message text (string)
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
212 @return value indicating an applied/deferred fix (-1, 0, 1),
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
213 a message for the fix (string) and an ID for a deferred
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
214 fix (integer)
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 code = message.split(None, 1)[0].strip()
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217
1031
c0f795aa71a7 Fixed an issue in the PEP-8 fixer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 945
diff changeset
218 if line <= len(self.__source) and \
2878
61042247f793 Added a button to the PEP-8 dialog to reset the default values.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2876
diff changeset
219 self.__codeMatch(code) and \
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 code in self.__fixes:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 res = self.__fixes[code](code, line, pos)
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
222 if res[0] == 1:
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 self.__modified = True
853
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
224 self.fixed += 1
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 else:
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
226 res = (0, "", 0)
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 return res
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
230 def finalize(self):
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 """
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
232 Public method to apply all deferred fixes.
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 """
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
234 results = {}
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
235
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
236 # step 1: do fixes operating on logical lines first
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
237 for id_, code, line, pos in self.__stackLogical:
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
238 res, msg, _ = self.__fixes[code](code, line, pos, apply=True)
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
239 if res == 1:
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
240 self.__modified = True
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
241 self.fixed += 1
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
242 results[id_] = (res, msg)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
243
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
244 # step 2: do fixes that change the number of lines
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
245 for id_, code, line, pos in reversed(self.__stack):
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
246 res, msg, _ = self.__fixes[code](code, line, pos, apply=True)
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
247 if res == 1:
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
248 self.__modified = True
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
249 self.fixed += 1
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
250 results[id_] = (res, msg)
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
251
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
252 return results
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
253
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
254 def __getID(self):
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
255 """
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
256 Private method to get the ID for a deferred fix.
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
257
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
258 @return ID for a deferred fix (integer)
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
259 """
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
260 self.__lastID += 1
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
261 return self.__lastID
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 def __getEol(self):
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 Private method to get the applicable eol string.
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267 @return eol string (string)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 """
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
269 if not self.__eol:
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
270 if self.__origName:
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
271 fn = self.__origName
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
272 else:
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
273 fn = self.__filename
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
274
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
275 if self.__project.isOpen() and self.__project.isProjectFile(fn):
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
276 self.__eol = self.__project.getEolString()
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
277 else:
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
278 self.__eol = Utilities.linesep()
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
279 return self.__eol
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
280
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
281 def __findLogical(self):
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
282 """
2883
3c99a1db1506 Finished the PEP-8 line shortener.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2882
diff changeset
283 Private method to extract the index of all the starts and ends of
3c99a1db1506 Finished the PEP-8 line shortener.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2882
diff changeset
284 lines.
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
285
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
286 @return tuple containing two lists of integer with start and end tuples
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
287 of lines
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
288 """
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
289 logical_start = []
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
290 logical_end = []
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
291 last_newline = True
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
292 sio = io.StringIO("".join(self.__source))
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
293 parens = 0
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
294 for t in tokenize.generate_tokens(sio.readline):
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
295 if t[0] in [tokenize.COMMENT, tokenize.DEDENT,
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
296 tokenize.INDENT, tokenize.NL,
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
297 tokenize.ENDMARKER]:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
298 continue
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
299 if not parens and t[0] in [tokenize.NEWLINE, tokenize.SEMI]:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
300 last_newline = True
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
301 logical_end.append((t[3][0] - 1, t[2][1]))
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
302 continue
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
303 if last_newline and not parens:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
304 logical_start.append((t[2][0] - 1, t[2][1]))
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
305 last_newline = False
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
306 if t[0] == tokenize.OP:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
307 if t[1] in '([{':
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
308 parens += 1
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
309 elif t[1] in '}])':
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
310 parens -= 1
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
311 return logical_start, logical_end
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
312
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
313 def __getLogical(self, line, pos):
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
314 """
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
315 Private method to get the logical line corresponding to the given
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
316 position.
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
317
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
318 @param line line number of the issue (integer)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
319 @param pos position inside line (integer)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
320 @return tuple of a tuple of two integers giving the start of the
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
321 logical line, another tuple of two integers giving the end
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
322 of the logical line and a list of strings with the original
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
323 source lines
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
324 """
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
325 try:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
326 (logical_start, logical_end) = self.__findLogical()
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
327 except (SyntaxError, tokenize.TokenError):
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
328 return None
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
329
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
330 line = line - 1
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
331 ls = None
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
332 le = None
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
333 for i in range(0, len(logical_start)):
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
334 x = logical_end[i]
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
335 if x[0] > line or (x[0] == line and x[1] > pos):
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
336 le = x
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
337 ls = logical_start[i]
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
338 break
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
339 if ls is None:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
340 return None
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
341
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
342 original = self.__source[ls[0]:le[0] + 1]
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
343 return ls, le, original
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
344
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
345 def __getIndentWord(self):
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
346 """
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
347 Private method to determine the indentation type.
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
349 @return string to be used for an indentation (string)
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
350 """
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
351 sio = io.StringIO("".join(self.__source))
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
352 indentWord = " " # default in case of failure
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
353 try:
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
354 for token in tokenize.generate_tokens(sio.readline):
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
355 if token[0] == tokenize.INDENT:
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
356 indentWord = token[1]
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
357 break
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
358 except (SyntaxError, tokenize.TokenError):
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
359 pass
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
360 return indentWord
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
361
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
362 def __getIndent(self, line):
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
363 """
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
364 Private method to get the indentation string.
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
365
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
366 @param line line to determine the indentation string from (string)
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
367 @return indentation string (string)
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
368 """
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
369 return line.replace(line.lstrip(), "")
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
370
2882
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
371 def __multilineStringLines(self):
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
372 """
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
373 Private method to determine the line numbers that are within multi line
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
374 strings and these which are part of a documentation string.
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
375
2883
3c99a1db1506 Finished the PEP-8 line shortener.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2882
diff changeset
376 @return tuple of a set of line numbers belonging to a multi line
3c99a1db1506 Finished the PEP-8 line shortener.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2882
diff changeset
377 string and a set of line numbers belonging to a multi line
3c99a1db1506 Finished the PEP-8 line shortener.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2882
diff changeset
378 documentation string (tuple of two set of integer)
2882
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
379 """
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
380 if self.__multiLineNumbers is None:
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
381 source = "".join(self.__source)
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
382 sio = io.StringIO(source)
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
383 self.__multiLineNumbers = set()
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
384 self.__docLineNumbers = set()
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
385 previousTokenType = ''
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
386 try:
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
387 for t in tokenize.generate_tokens(sio.readline):
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
388 tokenType = t[0]
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
389 startRow = t[2][0]
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
390 endRow = t[3][0]
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
391
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
392 if (tokenType == tokenize.STRING and startRow != endRow):
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
393 if previousTokenType != tokenize.INDENT:
2883
3c99a1db1506 Finished the PEP-8 line shortener.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2882
diff changeset
394 self.__multiLineNumbers |= set(
3c99a1db1506 Finished the PEP-8 line shortener.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2882
diff changeset
395 range(startRow, 1 + endRow))
2882
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
396 else:
2883
3c99a1db1506 Finished the PEP-8 line shortener.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2882
diff changeset
397 self.__docLineNumbers |= set(
3c99a1db1506 Finished the PEP-8 line shortener.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2882
diff changeset
398 range(startRow, 1 + endRow))
2882
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
399
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
400 previousTokenType = tokenType
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
401 except (SyntaxError, tokenize.TokenError):
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
402 pass
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
403
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
404 return self.__multiLineNumbers, self.__docLineNumbers
9b97bc92fdda Continued implementing the PEP-8 line shortener class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2880
diff changeset
405
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
406 def __fixReindent(self, line, pos, logical):
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
407 """
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
408 Private method to fix a badly indented line.
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
409
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
410 This is done by adding or removing from its initial indent only.
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
411
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
412 @param line line number of the issue (integer)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
413 @param pos position inside line (integer)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
414 @return flag indicating a change was done (boolean)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
415 """
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
416 assert logical
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
417 ls, _, original = logical
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
418
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
419 rewrapper = Pep8IndentationWrapper(original)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
420 valid_indents = rewrapper.pep8Expected()
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
421 if not rewrapper.rel_indent:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
422 return False
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
423
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
424 if line > ls[0]:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
425 # got a valid continuation line number
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
426 row = line - ls[0] - 1
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
427 # always pick the first option for this
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
428 valid = valid_indents[row]
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
429 got = rewrapper.rel_indent[row]
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
430 else:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
431 return False
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
432
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
433 line1 = ls[0] + row
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
434 # always pick the expected indent, for now.
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
435 indent_to = valid[0]
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
436
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
437 if got != indent_to:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
438 orig_line = self.__source[line1]
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
439 new_line = ' ' * (indent_to) + orig_line.lstrip()
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
440 if new_line == orig_line:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
441 return False
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
442 else:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
443 self.__source[line1] = new_line
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
444 return True
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
445 else:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
446 return False
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
447
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
448 def __fixWhitespace(self, line, offset, replacement):
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
449 """
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
450 Private method to correct whitespace at the given offset.
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
451
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
452 @param line line to be corrected (string)
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
453 @param offset offset within line (integer)
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
454 @param replacement replacement string (string)
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
455 @return corrected line
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
456 """
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
457 left = line[:offset].rstrip(" \t")
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
458 right = line[offset:].lstrip(" \t")
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
459 if right.startswith("#"):
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
460 return line
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
461 else:
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
462 return left + replacement + right
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
463
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
464 def __fixE101(self, code, line, pos):
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
465 """
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
466 Private method to fix obsolete tab usage and indentation errors
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
467 (E101, E111, W191).
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
468
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
469 @param code code of the issue (string)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
470 @param line line number of the issue (integer)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
471 @param pos position inside line (integer)
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
472 @return value indicating an applied/deferred fix (-1, 0, 1),
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
473 a message for the fix (string) and an ID for a deferred
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
474 fix (integer)
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
475 """
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
476 if self.__reindenter is None:
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
477 self.__reindenter = Pep8Reindenter(self.__source)
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
478 self.__reindenter.run()
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
479 fixedLine = self.__reindenter.fixedLine(line - 1)
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
480 if fixedLine is not None and fixedLine != self.__source[line - 1]:
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
481 self.__source[line - 1] = fixedLine
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
482 if code in ["E101", "W191"]:
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
483 msg = self.trUtf8("Tab converted to 4 spaces.")
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
484 else:
2883
3c99a1db1506 Finished the PEP-8 line shortener.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2882
diff changeset
485 msg = self.trUtf8(
3c99a1db1506 Finished the PEP-8 line shortener.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2882
diff changeset
486 "Indentation adjusted to be a multiple of four.")
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
487 return (1, msg, 0)
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
488 else:
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
489 return (0, "", 0)
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
490
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
491 def __fixE121(self, code, line, pos, apply=False):
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
492 """
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
493 Private method to fix the indentation of continuation lines and
2895
4a44d92757f9 Implemented some fixers for wrong naming conventions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2894
diff changeset
494 closing brackets (E121, E124).
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
495
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
496 @param code code of the issue (string)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
497 @param line line number of the issue (integer)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
498 @param pos position inside line (integer)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
499 @keyparam apply flag indicating, that the fix should be applied
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
500 (boolean)
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
501 @return value indicating an applied/deferred fix (-1, 0, 1),
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
502 a message for the fix (string) and an ID for a deferred
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
503 fix (integer)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
504 """
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
505 if apply:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
506 logical = self.__getLogical(line, pos)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
507 if logical:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
508 # Fix by adjusting initial indent level.
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
509 changed = self.__fixReindent(line, pos, logical)
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
510 if changed:
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
511 if code == "E121":
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
512 msg = self.trUtf8(
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
513 "Indentation of continuation line corrected.")
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
514 elif code == "E124":
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
515 msg = self.trUtf8(
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
516 "Indentation of closing bracket corrected.")
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
517 return (1, msg, 0)
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
518 return (0, "", 0)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
519 else:
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
520 id = self.__getID()
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
521 self.__stackLogical.append((id, code, line, pos))
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
522 return (-1, "", id)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
523
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
524 def __fixE122(self, code, line, pos, apply=False):
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
525 """
2883
3c99a1db1506 Finished the PEP-8 line shortener.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2882
diff changeset
526 Private method to fix a missing indentation of continuation lines
3c99a1db1506 Finished the PEP-8 line shortener.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2882
diff changeset
527 (E122).
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
528
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
529 @param code code of the issue (string)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
530 @param line line number of the issue (integer)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
531 @param pos position inside line (integer)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
532 @keyparam apply flag indicating, that the fix should be applied
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
533 (boolean)
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
534 @return value indicating an applied/deferred fix (-1, 0, 1),
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
535 a message for the fix (string) and an ID for a deferred
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
536 fix (integer)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
537 """
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
538 if apply:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
539 logical = self.__getLogical(line, pos)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
540 if logical:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
541 # Fix by adding an initial indent.
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
542 modified = self.__fixReindent(line, pos, logical)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
543 if not modified:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
544 # fall back to simple method
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
545 line = line - 1
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
546 text = self.__source[line]
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
547 indentation = self.__getIndent(text)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
548 self.__source[line] = indentation + \
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
549 self.__indentWord + text.lstrip()
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
550 return (
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
551 1,
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
552 self.trUtf8(
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
553 "Missing indentation of continuation line corrected."),
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
554 0)
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
555 return (0, "", 0)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
556 else:
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
557 id = self.__getID()
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
558 self.__stackLogical.append((id, code, line, pos))
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
559 return (-1, "", id)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
560
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
561 def __fixE123(self, code, line, pos, apply=False):
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
562 """
2883
3c99a1db1506 Finished the PEP-8 line shortener.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2882
diff changeset
563 Private method to fix the indentation of a closing bracket lines
3c99a1db1506 Finished the PEP-8 line shortener.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2882
diff changeset
564 (E123).
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
565
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
566 @param code code of the issue (string)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
567 @param line line number of the issue (integer)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
568 @param pos position inside line (integer)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
569 @keyparam apply flag indicating, that the fix should be applied
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
570 (boolean)
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
571 @return value indicating an applied/deferred fix (-1, 0, 1),
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
572 a message for the fix (string) and an ID for a deferred
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
573 fix (integer)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
574 """
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
575 if apply:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
576 logical = self.__getLogical(line, pos)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
577 if logical:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
578 # Fix by deleting whitespace to the correct level.
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
579 logicalLines = logical[2]
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
580 row = line - 1
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
581 text = self.__source[row]
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
582 newText = self.__getIndent(logicalLines[0]) + text.lstrip()
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
583 if newText == text:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
584 # fall back to slower method
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
585 changed = self.__fixReindent(line, pos, logical)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
586 else:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
587 self.__source[row] = newText
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
588 changed = True
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
589 if changed:
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
590 return (1, self.trUtf8(
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
591 "Closing bracket aligned to opening bracket."),
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
592 0)
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
593 return (0, "", 0)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
594 else:
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
595 id = self.__getID()
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
596 self.__stackLogical.append((id, code, line, pos))
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
597 return (-1, "", id)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
598
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
599 def __fixE125(self, code, line, pos, apply=False):
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
600 """
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
601 Private method to fix the indentation of continuation lines not
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
602 distinguishable from next logical line (E125).
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
603
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
604 @param code code of the issue (string)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
605 @param line line number of the issue (integer)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
606 @param pos position inside line (integer)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
607 @keyparam apply flag indicating, that the fix should be applied
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
608 (boolean)
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
609 @return value indicating an applied/deferred fix (-1, 0, 1),
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
610 a message for the fix (string) and an ID for a deferred
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
611 fix (integer)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
612 """
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
613 if apply:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
614 logical = self.__getLogical(line, pos)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
615 if logical:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
616 # Fix by adjusting initial indent level.
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
617 modified = self.__fixReindent(line, pos, logical)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
618 if not modified:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
619 row = line - 1
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
620 text = self.__source[row]
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
621 self.__source[row] = self.__getIndent(text) + \
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
622 self.__indentWord + text.lstrip()
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
623 return (1, self.trUtf8("Indentation level changed."), 0)
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
624 return (0, "", 0)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
625 else:
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
626 id = self.__getID()
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
627 self.__stackLogical.append((id, code, line, pos))
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
628 return (-1, "", id)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
629
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
630 def __fixE126(self, code, line, pos, apply=False):
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
631 """
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
632 Private method to fix over-indented/under-indented hanging
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
633 indentation (E126, E133).
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
634
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
635 @param code code of the issue (string)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
636 @param line line number of the issue (integer)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
637 @param pos position inside line (integer)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
638 @keyparam apply flag indicating, that the fix should be applied
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
639 (boolean)
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
640 @return value indicating an applied/deferred fix (-1, 0, 1),
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
641 a message for the fix (string) and an ID for a deferred
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
642 fix (integer)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
643 """
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
644 if apply:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
645 logical = self.__getLogical(line, pos)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
646 if logical:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
647 # Fix by deleting whitespace to the left.
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
648 logicalLines = logical[2]
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
649 row = line - 1
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
650 text = self.__source[row]
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
651 newText = self.__getIndent(logicalLines[0]) + \
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
652 self.__indentWord + text.lstrip()
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
653 if newText == text:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
654 # fall back to slower method
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
655 changed = self.__fixReindent(line, pos, logical)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
656 else:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
657 self.__source[row] = newText
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
658 changed = True
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
659 if changed:
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
660 return (1, self.trUtf8(
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
661 "Indentation level of hanging indentation changed."),
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
662 0)
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
663 return (0, "", 0)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
664 else:
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
665 id = self.__getID()
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
666 self.__stackLogical.append((id, code, line, pos))
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
667 return (-1, "", id)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
668
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
669 def __fixE127(self, code, line, pos, apply=False):
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
670 """
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
671 Private method to fix over/under indented lines (E127, E128).
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
672
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
673 @param code code of the issue (string)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
674 @param line line number of the issue (integer)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
675 @param pos position inside line (integer)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
676 @keyparam apply flag indicating, that the fix should be applied
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
677 (boolean)
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
678 @return value indicating an applied/deferred fix (-1, 0, 1),
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
679 a message for the fix (string) and an ID for a deferred
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
680 fix (integer)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
681 """
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
682 if apply:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
683 logical = self.__getLogical(line, pos)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
684 if logical:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
685 # Fix by inserting/deleting whitespace to the correct level.
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
686 logicalLines = logical[2]
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
687 row = line - 1
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
688 text = self.__source[row]
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
689 newText = text
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
690
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
691 if logicalLines[0].rstrip().endswith('\\'):
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
692 newText = self.__getIndent(logicalLines[0]) + \
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
693 self.__indentWord + text.lstrip()
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
694 else:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
695 startIndex = None
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
696 for symbol in '([{':
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
697 if symbol in logicalLines[0]:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
698 foundIndex = logicalLines[0].find(symbol) + 1
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
699 if startIndex is None:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
700 startIndex = foundIndex
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
701 else:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
702 startIndex = min(startIndex, foundIndex)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
703
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
704 if startIndex is not None:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
705 newText = startIndex * ' ' + text.lstrip()
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
706
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
707 if newText == text:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
708 # fall back to slower method
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
709 changed = self.__fixReindent(line, pos, logical)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
710 else:
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
711 self.__source[row] = newText
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
712 changed = True
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
713 if changed:
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
714 return (1, self.trUtf8("Visual indentation corrected."), 0)
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
715 return (0, "", 0)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
716 else:
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
717 id = self.__getID()
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
718 self.__stackLogical.append((id, code, line, pos))
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
719 return (-1, "", id)
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
720
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
721 def __fixE201(self, code, line, pos):
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
722 """
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
723 Private method to fix extraneous whitespace (E201, E202,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
724 E203, E211).
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
725
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
726 @param code code of the issue (string)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
727 @param line line number of the issue (integer)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
728 @param pos position inside line (integer)
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
729 @return value indicating an applied/deferred fix (-1, 0, 1),
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
730 a message for the fix (string) and an ID for a deferred
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
731 fix (integer)
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
732 """
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
733 line = line - 1
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
734 text = self.__source[line]
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
735
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
736 if '"""' in text or "'''" in text or text.rstrip().endswith('\\'):
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
737 return (0, "", 0)
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
738
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
739 newText = self.__fixWhitespace(text, pos, '')
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
740 if newText == text:
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
741 return (0, "", 0)
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
742
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
743 self.__source[line] = newText
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
744 return (1, self.trUtf8("Extraneous whitespace removed."), 0)
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
745
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
746 def __fixE221(self, code, line, pos):
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
747 """
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
748 Private method to fix extraneous whitespace around operator or
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
749 keyword (E221, E222, E223, E224, E225, E226, E227, E228, E241,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
750 E242, E271, E272, E273, E274).
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
751
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
752 @param code code of the issue (string)
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
753 @param line line number of the issue (integer)
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
754 @param pos position inside line (integer)
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
755 @return value indicating an applied/deferred fix (-1, 0, 1),
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
756 a message for the fix (string) and an ID for a deferred
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
757 fix (integer)
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
758 """
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
759 line = line - 1
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
760 text = self.__source[line]
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
761
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
762 if '"""' in text or "'''" in text or text.rstrip().endswith('\\'):
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
763 return (0, "", 0)
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
764
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
765 newText = self.__fixWhitespace(text, pos, ' ')
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
766 if newText == text:
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
767 return (0, "", 0)
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
768
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
769 self.__source[line] = newText
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
770 if code in ["E225", "E226", "E227", "E228"]:
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
771 return (1, self.trUtf8("Missing whitespace added."), 0)
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
772 else:
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
773 return (1, self.trUtf8("Extraneous whitespace removed."), 0)
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
774
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
775 def __fixE231(self, code, line, pos):
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
776 """
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
777 Private method to fix missing whitespace after ',;:'.
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
778
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
779 @param code code of the issue (string)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
780 @param line line number of the issue (integer)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
781 @param pos position inside line (integer)
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
782 @return value indicating an applied/deferred fix (-1, 0, 1),
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
783 a message for the fix (string) and an ID for a deferred
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
784 fix (integer)
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
785 """
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
786 line = line - 1
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
787 pos = pos + 1
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
788 self.__source[line] = self.__source[line][:pos] + \
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
789 " " + self.__source[line][pos:]
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
790 return (1, self.trUtf8("Missing whitespace added."), 0)
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
791
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
792 def __fixE251(self, code, line, pos):
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
793 """
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
794 Private method to fix extraneous whitespace around keyword and
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
795 default parameter equals (E251).
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
796
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
797 @param code code of the issue (string)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
798 @param line line number of the issue (integer)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
799 @param pos position inside line (integer)
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
800 @return value indicating an applied/deferred fix (-1, 0, 1),
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
801 a message for the fix (string) and an ID for a deferred
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
802 fix (integer)
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
803 """
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
804 line = line - 1
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
805 text = self.__source[line]
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
806
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
807 # This is necessary since pep8 sometimes reports columns that goes
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
808 # past the end of the physical line. This happens in cases like,
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
809 # foo(bar\n=None)
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
810 col = min(pos, len(text) - 1)
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
811 if text[col].strip():
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
812 newText = text
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
813 else:
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
814 newText = text[:col].rstrip() + text[col:].lstrip()
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
815
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
816 # There could be an escaped newline
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
817 #
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
818 # def foo(a=\
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
819 # 1)
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
820 if newText.endswith(('=\\\n', '=\\\r\n', '=\\\r')):
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
821 self.__source[line] = newText.rstrip("\n\r \t\\")
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
822 self.__source[line + 1] = self.__source[line + 1].lstrip()
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
823 else:
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
824 self.__source[line] = newText
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
825 return (1, self.trUtf8("Extraneous whitespace removed."), 0)
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
826
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
827 def __fixE261(self, code, line, pos):
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
828 """
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
829 Private method to fix whitespace before or after inline comment
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
830 (E261, E262).
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
831
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
832 @param code code of the issue (string)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
833 @param line line number of the issue (integer)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
834 @param pos position inside line (integer)
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
835 @return value indicating an applied/deferred fix (-1, 0, 1),
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
836 a message for the fix (string) and an ID for a deferred
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
837 fix (integer)
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
838 """
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
839 line = line - 1
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
840 text = self.__source[line]
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
841 left = text[:pos].rstrip(' \t#')
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
842 right = text[pos:].lstrip(' \t#')
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
843 newText = left + (" # " + right if right.strip() else right)
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
844 self.__source[line] = newText
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
845 return (1, self.trUtf8("Whitespace around comment sign corrected."), 0)
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
846
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
847 def __fixE301(self, code, line, pos, apply=False):
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
848 """
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
849 Private method to fix the need for one blank line (E301).
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
850
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
851 @param code code of the issue (string)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
852 @param line line number of the issue (integer)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
853 @param pos position inside line (integer)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
854 @keyparam apply flag indicating, that the fix should be applied
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
855 (boolean)
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
856 @return value indicating an applied/deferred fix (-1, 0, 1),
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
857 a message for the fix (string) and an ID for a deferred
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
858 fix (integer)
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
859 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
860 if apply:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
861 self.__source.insert(line - 1, self.__getEol())
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
862 return (1, self.trUtf8("One blank line inserted."), 0)
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
863 else:
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
864 id = self.__getID()
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
865 self.__stack.append((id, code, line, pos))
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
866 return (-1, "", id)
851
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
867
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
868 def __fixE302(self, code, line, pos, apply=False):
853
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
869 """
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
870 Private method to fix the need for two blank lines (E302).
2875
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
871
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
872 @param code code of the issue (string)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
873 @param line line number of the issue (integer)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
874 @param pos position inside line (integer)
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
875 @keyparam apply flag indicating, that the fix should be applied
1267f0663801 Continued implementing PEP-8 fixer methods.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2868
diff changeset
876 (boolean)
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
877 @return value indicating an applied/deferred fix (-1, 0, 1),
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
878 a message for the fix (string) and an ID for a deferred
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
879 fix (integer)
853
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
880 """
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
881 if apply:
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
882 # count blank lines
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
883 index = line - 1
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
884 blanks = 0
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
885 while index:
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
886 if self.__source[index - 1].strip() == "":
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
887 blanks += 1
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
888 index -= 1
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
889 else:
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
890 break
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
891 delta = blanks - 2
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
892
853
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
893 line -= 1
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
894 if delta < 0:
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
895 # insert blank lines (one or two)
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
896 while delta < 0:
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
897 self.__source.insert(line, self.__getEol())
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
898 delta += 1
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
899 changed = True
853
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
900 elif delta > 0:
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
901 # delete superfluous blank lines
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
902 while delta > 0:
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
903 del self.__source[line - 1]
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
904 line -= 1
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
905 delta -= 1
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
906 changed = True
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
907 else:
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
908 changed = False
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
909
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
910 if changed:
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
911 if delta < 0:
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
912 msg = self.trUtf8(
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
913 "%n blank line(s) inserted.", "", -delta)
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
914 elif delta > 0:
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
915 msg = self.trUtf8(
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
916 "%n superfluous lines removed", "", delta)
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
917 else:
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
918 msg = ""
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
919 return (1, msg, 0)
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
920 return (0, "", 0)
853
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
921 else:
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
922 id = self.__getID()
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
923 self.__stack.append((id, code, line, pos))
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
924 return (-1, "", id)
853
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
925
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
926 def __fixE303(self, code, line, pos, apply=False):
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
927 """
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
928 Private method to fix superfluous blank lines (E303).
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
929
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
930 @param code code of the issue (string)
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
931 @param line line number of the issue (integer)
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
932 @param pos position inside line (integer)
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
933 @keyparam apply flag indicating, that the fix should be applied
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
934 (boolean)
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
935 @return value indicating an applied/deferred fix (-1, 0, 1),
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
936 a message for the fix (string) and an ID for a deferred
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
937 fix (integer)
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
938 """
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
939 if apply:
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
940 index = line - 3
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
941 while index:
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
942 if self.__source[index].strip() == "":
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
943 del self.__source[index]
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
944 index -= 1
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
945 else:
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
946 break
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
947 return (1, self.trUtf8("Superfluous blank lines removed."), 0)
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
948 else:
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
949 id = self.__getID()
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
950 self.__stack.append((id, code, line, pos))
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
951 return (-1, "", id)
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
952
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
953 def __fixE304(self, code, line, pos, apply=False):
851
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
954 """
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
955 Private method to fix superfluous blank lines after a function
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
956 decorator (E304).
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
957
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
958 @param code code of the issue (string)
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
959 @param line line number of the issue (integer)
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
960 @param pos position inside line (integer)
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
961 @keyparam apply flag indicating, that the fix should be applied
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
962 (boolean)
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
963 @return value indicating an applied/deferred fix (-1, 0, 1),
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
964 a message for the fix (string) and an ID for a deferred
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
965 fix (integer)
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
966 """
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
967 if apply:
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
968 index = line - 2
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
969 while index:
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
970 if self.__source[index].strip() == "":
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
971 del self.__source[index]
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
972 index -= 1
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
973 else:
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
974 break
2892
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
975 return (1, self.trUtf8(
b601ede6dbbf Fixed a few issues in the PEP-8 fixer and made it PEP-8 compliant.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2891
diff changeset
976 "Superfluous blank lines after function decorator removed."),
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
977 0)
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
978 else:
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
979 id = self.__getID()
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
980 self.__stack.append((id, code, line, pos))
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
981 return (-1, "", id)
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
982
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
983 def __fixE401(self, code, line, pos, apply=False):
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
984 """
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
985 Private method to fix multiple imports on one line (E401).
851
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
986
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
987 @param code code of the issue (string)
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
988 @param line line number of the issue (integer)
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
989 @param pos position inside line (integer)
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
990 @keyparam apply flag indicating, that the fix should be applied
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
991 (boolean)
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
992 @return value indicating an applied/deferred fix (-1, 0, 1),
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
993 a message for the fix (string) and an ID for a deferred
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
994 fix (integer)
851
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
995 """
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
996 if apply:
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
997 line = line - 1
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
998 text = self.__source[line]
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
999 if not text.lstrip().startswith("import"):
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
1000 return (0, "", 0)
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
1001
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
1002 # pep8 (1.3.1) reports false positive if there is an import
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
1003 # statement followed by a semicolon and some unrelated
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
1004 # statement with commas in it.
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
1005 if ';' in text:
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
1006 return (0, "", 0)
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
1007
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
1008 newText = text[:pos].rstrip("\t ,") + self.__getEol() + \
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
1009 self.__getIndent(text) + "import " + text[pos:].lstrip("\t ,")
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
1010 self.__source[line] = newText
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
1011 return (1, self.trUtf8("Imports were put on separate lines."), 0)
2868
8d30ec21e9c7 Extended the PEP8 fixer and adjusted it to the updated PEP8 checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
1012 else:
2891
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
1013 id = self.__getID()
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2890
diff changeset
1014 self.__stack.append((id, code, line, pos))
6843b7d23279 Changed the PEP-8 fixer and dialog to decouple the indication of the fixed status from the generation of the PEP-8 status.
Detlev Offenbach &#