46 class CodeStyleFixer(object): |
46 class CodeStyleFixer(object): |
47 """ |
47 """ |
48 Class implementing a fixer for certain code style issues. |
48 Class implementing a fixer for certain code style issues. |
49 """ |
49 """ |
50 def __init__(self, filename, sourceLines, fixCodes, noFixCodes, |
50 def __init__(self, filename, sourceLines, fixCodes, noFixCodes, |
51 maxLineLength, inPlace, eol): |
51 maxLineLength, inPlace, eol, backup=False): |
52 """ |
52 """ |
53 Constructor |
53 Constructor |
54 |
54 |
55 @param filename name of the file to be fixed (string) |
55 @param filename name of the file to be fixed (string) |
56 @param sourceLines list of source lines including eol marker |
56 @param sourceLines list of source lines including eol marker |
60 @param noFixCodes list of codes not to be fixed as a comma |
60 @param noFixCodes list of codes not to be fixed as a comma |
61 separated string (string) |
61 separated string (string) |
62 @param maxLineLength maximum allowed line length (integer) |
62 @param maxLineLength maximum allowed line length (integer) |
63 @param inPlace flag indicating to modify the file in place (boolean) |
63 @param inPlace flag indicating to modify the file in place (boolean) |
64 @param eol end of line character(s) (string) |
64 @param eol end of line character(s) (string) |
|
65 @param backup flag indicating to create a backup before fixing |
|
66 anything (boolean) |
65 """ |
67 """ |
66 super(CodeStyleFixer, self).__init__() |
68 super(CodeStyleFixer, self).__init__() |
67 |
69 |
68 self.__filename = filename |
70 self.__filename = filename |
69 self.__origName = "" |
71 self.__origName = "" |
76 |
78 |
77 self.__reindenter = None |
79 self.__reindenter = None |
78 self.__indentWord = self.__getIndentWord() |
80 self.__indentWord = self.__getIndentWord() |
79 |
81 |
80 if inPlace: |
82 if inPlace: |
81 # TODO: Do a backup before any changes depending on |
83 self.__createBackup = backup |
82 # 'CreateBackupFile' editor config setting. |
|
83 pass |
|
84 else: |
84 else: |
85 self.__origName = self.__filename |
85 self.__origName = self.__filename |
86 self.__filename = os.path.join( |
86 self.__filename = os.path.join( |
87 os.path.dirname(self.__filename), |
87 os.path.dirname(self.__filename), |
88 "fixed_" + os.path.basename(self.__filename)) |
88 "fixed_" + os.path.basename(self.__filename)) |
|
89 self.__createBackup = False |
89 self.__eol = eol |
90 self.__eol = eol |
90 |
91 |
91 self.__fixes = { |
92 self.__fixes = { |
92 "D111": self.__fixD111, |
93 "D111": self.__fixD111, |
93 "D112": self.__fixD112, |
94 "D112": self.__fixD112, |
186 |
187 |
187 if not self.__modified: |
188 if not self.__modified: |
188 # no need to write |
189 # no need to write |
189 return |
190 return |
190 |
191 |
|
192 if self.__createBackup: |
|
193 # create a backup file before writing any changes |
|
194 if os.path.islink(self.__filename): |
|
195 bfn = '{0}~'.format(os.path.realpath(self.__filename)) |
|
196 else: |
|
197 bfn = '{0}~'.format(self.__filename) |
|
198 try: |
|
199 os.remove(bfn) |
|
200 except EnvironmentError: |
|
201 # if there was an error, ignore it |
|
202 pass |
|
203 try: |
|
204 os.rename(self.__filename, bfn) |
|
205 except EnvironmentError: |
|
206 # if there was an error, ignore it |
|
207 pass |
|
208 |
191 txt = "".join(self.__source) |
209 txt = "".join(self.__source) |
192 try: |
210 try: |
193 if sys.version_info[0] == 3: |
211 if sys.version_info[0] == 3: |
194 txt = txt.encode(encoding) |
212 txt = txt.encode(encoding) |
195 if encoding == 'utf-8-bom': |
213 if encoding == 'utf-8-bom': |