Plugins/CheckerPlugins/Pep8/Pep8Fixer.py

Mon, 26 Dec 2011 19:31:22 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 26 Dec 2011 19:31:22 +0100
changeset 1509
c0b5e693b0eb
parent 1203
2e9f25df356f
child 2302
f29e9405c851
permissions
-rw-r--r--

Updated copyright for 2012.

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
1509
c0b5e693b0eb Updated copyright for 2012.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1203
diff changeset
3 # Copyright (c) 2011 - 2012 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 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a class to fix certain PEP 8 issues.
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
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 from PyQt4.QtCore import QObject
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 E5Gui import E5MessageBox
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 import Utilities
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18
853
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
19 Pep8FixableIssues = ["E101", "W191", "E201", "E202", "E203", "E211", "E221",
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
20 "E222", "E225", "E231", "E241", "E251", "E261", "E262",
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
21 "W291", "W292", "W293", "E301", "E302", "E303", "E304",
853
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
22 "W391", "W603"]
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
24
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 class Pep8Fixer(QObject):
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 Class implementing a fixer for certain PEP 8 issues.
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 def __init__(self, project, filename, sourceLines, fixCodes, inPlace):
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 Constructor
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 @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
34 @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
35 @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
36 (list of string)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 @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
38 string (string)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 @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
40 """
1131
7781e396c903 Changed the code to use super() to access the superclass.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1031
diff changeset
41 super().__init__()
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 self.__project = project
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 self.__filename = filename
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 self.__origName = ""
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 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
47 self.__fixCodes = [c.strip() for c in fixCodes.split(",") if c.strip()]
853
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
48 self.fixed = 0
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 if not inPlace:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 self.__origName = self.__filename
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
52 self.__filename = os.path.join(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
53 "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
54
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 self.__fixes = {
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
56 "E101": self.__fixTabs,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
57 "W191": self.__fixTabs,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
58 "E201": self.__fixWhitespaceAfter,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
59 "E202": self.__fixWhitespaceBefore,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
60 "E203": self.__fixWhitespaceBefore,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
61 "E211": self.__fixWhitespaceBefore,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
62 "E221": self.__fixWhitespaceAroundOperator,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
63 "E222": self.__fixWhitespaceAroundOperator,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
64 "E225": self.__fixMissingWhitespaceAroundOperator,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
65 "E231": self.__fixMissingWhitespaceAfter,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
66 "E241": self.__fixWhitespaceAroundOperator,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
67 "E251": self.__fixWhitespaceAroundEquals,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
68 "E261": self.__fixWhitespaceBeforeInline,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
69 "E262": self.__fixWhitespaceAfterInline,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
70 "W291": self.__fixWhitespace,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
71 "W292": self.__fixNewline,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
72 "W293": self.__fixWhitespace,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
73 "E301": self.__fixOneBlankLine,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
74 "E302": self.__fixTwoBlankLines,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
75 "E303": self.__fixTooManyBlankLines,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
76 "E304": self.__fixBlankLinesAfterDecorator,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
77 "W391": self.__fixTrailingBlankLines,
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
78 "W603": self.__fixNotEqual,
849
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.__modified = False
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 self.__stack = [] # these need to be fixed before the file is saved
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 # but after all inline fixes
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 def saveFile(self, encoding):
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 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
87
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 @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
89 @return flag indicating success (boolean)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 if not self.__modified:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 # no need to write
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 return True
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 # apply deferred fixes
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 self.__finalize()
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 txt = "".join(self.__source)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 try:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 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
101 except (IOError, Utilities.CodingError, UnicodeError) as err:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 E5MessageBox.critical(self,
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 self.trUtf8("Fix PEP 8 issues"),
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 self.trUtf8(
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 """<p>Could not save the file <b>{0}</b>."""
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 """ Skipping it.</p><p>Reason: {1}</p>""")\
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 .format(self.__filename, str(err))
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 )
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 return False
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 return True
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 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
114 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 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
116
1031
c0f795aa71a7 Fixed an issue in the PEP-8 fixer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 945
diff changeset
117 @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
118 @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
119 @param message message text (string)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 @return flag indicating an applied fix (boolean) and a message for
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 the fix (string)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 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
124
1031
c0f795aa71a7 Fixed an issue in the PEP-8 fixer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 945
diff changeset
125 if line <= len(self.__source) and \
c0f795aa71a7 Fixed an issue in the PEP-8 fixer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 945
diff changeset
126 (code in self.__fixCodes or len(self.__fixCodes) == 0) and \
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 code in self.__fixes:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 res = self.__fixes[code](code, line, pos)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 if res[0]:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 self.__modified = True
853
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
131 self.fixed += 1
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 else:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 res = (False, "")
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 return res
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 def __finalize(self):
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 Private method to apply all deferred fixes.
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 for code, line, pos in reversed(self.__stack):
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 self.__fixes[code](code, line, pos, apply=True)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 def __getEol(self):
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 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
147
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 @return eol string (string)
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 if self.__origName:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 fn = self.__origName
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 else:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 fn = self.__filename
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 self.__project.isOpen() and self.__project.isProjectFile(fn):
869
6d16246da792 Fixed an issue in the PEP-8 fixer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 853
diff changeset
156 eol = self.__project.getEolString()
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 else:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 eol = Utilities.linesep()
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 return eol
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 def __fixTabs(self, code, line, pos):
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 Private method to fix obsolete tab usage.
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 @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
166 @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
167 @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
168 @return flag indicating an applied fix (boolean) and a message for
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 the fix (string)
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 self.__source[line - 1] = self.__source[line - 1].replace("\t", " ")
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 return (True, self.trUtf8("Tab converted to 4 spaces."))
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 def __fixWhitespace(self, code, line, pos):
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 Private method to fix trailing whitespace.
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 @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
179 @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
180 @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
181 @return flag indicating an applied fix (boolean) and a message for
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 the fix (string)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 """
1203
2e9f25df356f Fix an issue in the PEP8 fixer dealing with win end of line markers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1131
diff changeset
184 self.__source[line - 1] = re.sub(r'[\t ]+(\r?)$', r"\1",
849
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 self.__source[line - 1])
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 return (True, self.trUtf8("Whitespace stripped from end of line."))
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 def __fixNewline(self, code, line, pos):
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 Private method to fix a missing newline at the end of file.
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 @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
193 @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
194 @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
195 @return flag indicating an applied fix (boolean) and a message for
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 the fix (string)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 self.__source[line - 1] += self.__getEol()
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 return (True, self.trUtf8("newline added to end of file."))
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 def __fixTrailingBlankLines(self, code, line, pos):
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 Private method to fix trailing blank lines.
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 @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
206 @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
207 @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
208 @return flag indicating an applied fix (boolean) and a message for
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 the fix (string)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 index = line - 1
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 while index:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 if self.__source[index].strip() == "":
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 del self.__source[index]
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215 index -= 1
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 else:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 break
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 return (True, self.trUtf8(
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219 "Superfluous trailing blank lines removed from end of file."))
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 def __fixNotEqual(self, code, line, pos):
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 Private method to fix the not equal notation.
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 @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
226 @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
227 @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
228 @return flag indicating an applied fix (boolean) and a message for
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 the fix (string)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 self.__source[line - 1] = self.__source[line - 1].replace("<>", "!=")
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 return (True, self.trUtf8("'<>' replaced by '!='."))
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 def __fixBlankLinesAfterDecorator(self, code, line, pos, apply=False):
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 Private method to fix superfluous blank lines after a function
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 decorator.
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 @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
240 @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
241 @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
242 @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
243 (boolean)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 @return flag indicating an applied fix (boolean) and a message for
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 the fix (string)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 if apply:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 index = line - 2
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 while index:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 if self.__source[index].strip() == "":
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 del self.__source[index]
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 index -= 1
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 else:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 break
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 else:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 self.__stack.append((code, line, pos))
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 return (True, self.trUtf8(
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 "Superfluous blank lines after function decorator removed."))
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 def __fixTooManyBlankLines(self, code, line, pos, apply=False):
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 Private method to fix superfluous blank lines.
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264 @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
265 @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
266 @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
267 @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
268 (boolean)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 @return flag indicating an applied fix (boolean) and a message for
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270 the fix (string)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 if apply:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 index = line - 3
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 while index:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 if self.__source[index].strip() == "":
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 del self.__source[index]
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 index -= 1
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278 else:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 break
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280 else:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 self.__stack.append((code, line, pos))
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282 return (True, self.trUtf8("Superfluous blank lines removed."))
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 def __fixOneBlankLine(self, code, line, pos, apply=False):
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
285 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286 Private method to fix the need for one blank line.
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
287
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
288 @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
289 @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
290 @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
291 @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
292 (boolean)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293 @return flag indicating an applied fix (boolean) and a message for
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
294 the fix (string)
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
295 """
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
296 if apply:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297 self.__source.insert(line - 1, self.__getEol())
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298 else:
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
299 self.__stack.append((code, line, pos))
996367a89673 Started to implement a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
300 return (True, self.trUtf8("One blank line inserted."))
851
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
301
853
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
302 def __fixTwoBlankLines(self, code, line, pos, apply=False):
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
303 """
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
304 Private method to fix the need for two blank lines.
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
305 """
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
306 # count blank lines
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
307 index = line - 1
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
308 blanks = 0
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
309 while index:
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
310 if self.__source[index - 1].strip() == "":
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
311 blanks += 1
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
312 index -= 1
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
313 else:
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
314 break
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
315 delta = blanks - 2
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
316
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
317 if apply:
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
318 line -= 1
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
319 if delta < 0:
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
320 # 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
321 while delta < 0:
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
322 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
323 delta += 1
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
324 elif delta > 0:
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
325 # delete superfluous blank lines
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
326 while delta > 0:
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
327 del self.__source[line - 1]
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
328 line -= 1
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
329 delta -= 1
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
330 else:
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
331 self.__stack.append((code, line, pos))
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
332
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
333 if delta < 0:
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
334 msg = self.trUtf8("%n blank line(s) inserted.", "", -delta)
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
335 elif delta > 0:
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
336 msg = self.trUtf8("%n superfluous lines removed", "", delta)
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
337 else:
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
338 msg = ""
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
339 return (True, msg)
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
340
851
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
341 def __fixWhitespaceAfter(self, code, line, pos, apply=False):
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
342 """
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
343 Private method to fix superfluous whitespace after '([{'.
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
344
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
345 @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
346 @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
347 @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
348 @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
349 (boolean)
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
350 @return flag indicating an applied fix (boolean) and a message for
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
351 the fix (string)
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
352 """
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
353 line = line - 1
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
354 pos = pos - 1
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
355 while self.__source[line][pos] in [" ", "\t"]:
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
356 self.__source[line] = self.__source[line][:pos] + \
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
357 self.__source[line][pos + 1:]
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
358 return (True, self.trUtf8("Superfluous whitespace removed."))
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
359
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
360 def __fixWhitespaceBefore(self, code, line, pos, apply=False):
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
361 """
852
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
362 Private method to fix superfluous whitespace before '}])',
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
363 ',;:' and '(['.
851
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
364
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
365 @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
366 @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
367 @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
368 @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
369 (boolean)
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
370 @return flag indicating an applied fix (boolean) and a message for
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
371 the fix (string)
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
372 """
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
373 line = line - 1
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
374 pos = pos - 1
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
375 while self.__source[line][pos] in [" ", "\t"]:
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
376 self.__source[line] = self.__source[line][:pos] + \
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
377 self.__source[line][pos + 1:]
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
378 pos -= 1
321d29b93238 Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 849
diff changeset
379 return (True, self.trUtf8("Superfluous whitespace removed."))
852
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
380
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
381 def __fixMissingWhitespaceAfter(self, code, line, pos, apply=False):
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
382 """
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
383 Private method to fix missing whitespace after ',;:'.
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
384
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
385 @param code code of the issue (string)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
386 @param line line number of the issue (integer)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
387 @param pos position inside line (integer)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
388 @keyparam apply flag indicating, that the fix should be applied
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
389 (boolean)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
390 @return flag indicating an applied fix (boolean) and a message for
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
391 the fix (string)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
392 """
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
393 line = line - 1
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
394 self.__source[line] = self.__source[line][:pos] + \
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
395 " " + \
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
396 self.__source[line][pos:]
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
397 return (True, self.trUtf8("Missing whitespace added."))
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
398
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
399 def __fixWhitespaceAroundOperator(self, code, line, pos, apply=False):
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
400 """
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
401 Private method to fix extraneous whitespace around operator.
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
402
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
403 @param code code of the issue (string)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
404 @param line line number of the issue (integer)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
405 @param pos position inside line (integer)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
406 @keyparam apply flag indicating, that the fix should be applied
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
407 (boolean)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
408 @return flag indicating an applied fix (boolean) and a message for
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
409 the fix (string)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
410 """
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
411 line = line - 1
853
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
412 while self.__source[line][pos - 1] in [" ", "\t"]:
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
413 self.__source[line] = self.__source[line][:pos - 1] + \
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
414 self.__source[line][pos:]
ec7dd115e26b Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 852
diff changeset
415 pos -= 1
852
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
416 return (True, self.trUtf8("Extraneous whitespace removed."))
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
417
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 869
diff changeset
418 def __fixMissingWhitespaceAroundOperator(self, code, line, pos,
852
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
419 apply=False):
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
420 """
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
421 Private method to fix missing whitespace after ',;:'.
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
422
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
423 @param code code of the issue (string)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
424 @param line line number of the issue (integer)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
425 @param pos position inside line (integer)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
426 @keyparam apply flag indicating, that the fix should be applied
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
427 (boolean)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
428 @return flag indicating an applied fix (boolean) and a message for
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
429 the fix (string)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
430 """
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
431 line = line - 1
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
432 pos = pos - 1
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
433 self.__source[line] = self.__source[line][:pos] + \
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
434 " " + \
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
435 self.__source[line][pos:]
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
436 return (True, self.trUtf8("Missing whitespace added."))
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
437
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
438 def __fixWhitespaceAroundEquals(self, code, line, pos, apply=False):
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
439 """
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
440 Private method to fix extraneous whitespace around keyword and
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
441 default parameter equals.
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
442
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
443 @param code code of the issue (string)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
444 @param line line number of the issue (integer)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
445 @param pos position inside line (integer)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
446 @keyparam apply flag indicating, that the fix should be applied
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
447 (boolean)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
448 @return flag indicating an applied fix (boolean) and a message for
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
449 the fix (string)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
450 """
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
451 line = line - 1
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
452 if self.__source[line][pos + 1] == " ":
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
453 self.__source[line] = self.__source[line][:pos + 1] + \
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
454 self.__source[line][pos + 2:]
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
455 if self.__source[line][pos - 1] == " ":
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
456 self.__source[line] = self.__source[line][:pos - 1] + \
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
457 self.__source[line][pos:]
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
458 return (True, self.trUtf8("Extraneous whitespace removed."))
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
459
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
460 def __fixWhitespaceBeforeInline(self, code, line, pos, apply=False):
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
461 """
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
462 Private method to fix missing whitespace before inline comment.
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
463
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
464 @param code code of the issue (string)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
465 @param line line number of the issue (integer)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
466 @param pos position inside line (integer)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
467 @keyparam apply flag indicating, that the fix should be applied
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
468 (boolean)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
469 @return flag indicating an applied fix (boolean) and a message for
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
470 the fix (string)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
471 """
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
472 line = line - 1
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
473 pos = pos - 1
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
474 if self.__source[line][pos] == " ":
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
475 count = 1
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
476 else:
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
477 count = 2
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
478 self.__source[line] = self.__source[line][:pos] + \
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
479 count * " " + \
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
480 self.__source[line][pos:]
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
481 return (True, self.trUtf8("Missing whitespace added."))
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
482
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
483 def __fixWhitespaceAfterInline(self, code, line, pos, apply=False):
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
484 """
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
485 Private method to fix whitespace after inline comment.
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
486
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
487 @param code code of the issue (string)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
488 @param line line number of the issue (integer)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
489 @param pos position inside line (integer)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
490 @keyparam apply flag indicating, that the fix should be applied
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
491 (boolean)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
492 @return flag indicating an applied fix (boolean) and a message for
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
493 the fix (string)
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
494 """
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
495 line = line - 1
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
496 if self.__source[line][pos] == " ":
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
497 pos += 1
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
498 while self.__source[line][pos] == " ":
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
499 self.__source[line] = self.__source[line][:pos] + \
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
500 self.__source[line][pos + 1:]
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
501 else:
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
502 self.__source[line] = self.__source[line][:pos] + \
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
503 " " + \
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
504 self.__source[line][pos:]
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
505 return (True, self.trUtf8(
257a6f80b96c Continued implementing a fixer for PEP 8 issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 851
diff changeset
506 "Whitespace after inline comment sign corrected."))

eric ide

mercurial