autopep8.py 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  1. #!/usr/bin/env python
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining
  4. # a copy of this software and associated documentation files (the
  5. # "Software"), to deal in the Software without restriction, including
  6. # without limitation the rights to use, copy, modify, merge, publish,
  7. # distribute, sublicense, and/or sell copies of the Software, and to
  8. # permit persons to whom the Software is furnished to do so, subject to
  9. # the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included
  12. # in all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  18. # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  19. # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  20. # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. """
  22. A tool that automatically formats Python code to conform to the PEP 8 style
  23. guide.
  24. """
  25. from __future__ import print_function
  26. import copy
  27. import os
  28. import re
  29. import sys
  30. import inspect
  31. import codecs
  32. import locale
  33. try:
  34. from StringIO import StringIO
  35. except ImportError:
  36. from io import StringIO
  37. import token
  38. import tokenize
  39. from optparse import OptionParser
  40. from subprocess import Popen, PIPE
  41. from difflib import unified_diff
  42. import tempfile
  43. from distutils.version import StrictVersion
  44. try:
  45. import pep8
  46. if StrictVersion(pep8.__version__) < StrictVersion('1.3a2'):
  47. pep8 = None
  48. except ImportError:
  49. pep8 = None
  50. __version__ = '0.8.1'
  51. PEP8_BIN = 'pep8'
  52. CR = '\r'
  53. LF = '\n'
  54. CRLF = '\r\n'
  55. MAX_LINE_WIDTH = 79
  56. def open_with_encoding(filename, encoding, mode='r'):
  57. """Return opened file with a specific encoding."""
  58. try:
  59. # Python 3
  60. return open(filename, mode=mode, encoding=encoding)
  61. except TypeError:
  62. # Python 2
  63. return codecs.open(filename, mode=mode, encoding=encoding)
  64. def detect_encoding(filename):
  65. """Return file encoding."""
  66. try:
  67. # Python 3
  68. try:
  69. with open(filename, 'rb') as input_file:
  70. encoding = tokenize.detect_encoding(input_file.readline)[0]
  71. # Check for correctness of encoding
  72. import io
  73. with io.TextIOWrapper(input_file, encoding) as wrapper:
  74. wrapper.read()
  75. return encoding
  76. except (SyntaxError, LookupError, UnicodeDecodeError):
  77. return 'latin-1'
  78. except AttributeError:
  79. # Python 2
  80. encoding = 'utf-8'
  81. try:
  82. # Check for correctness of encoding
  83. with open_with_encoding(filename, encoding) as input_file:
  84. input_file.read()
  85. except UnicodeDecodeError:
  86. encoding = 'latin-1'
  87. return encoding
  88. def read_from_filename(filename, readlines=False):
  89. """Return contents of file."""
  90. with open_with_encoding(filename,
  91. encoding=detect_encoding(filename)) as input_file:
  92. return input_file.readlines() if readlines else input_file.read()
  93. class FixPEP8(object):
  94. """Fix invalid code.
  95. Fixer methods are prefixed "fix_". The _fix_source() method looks for these
  96. automatically.
  97. The fixer method can take either one or two arguments (in addition to
  98. self). The first argument is "result", which is the error information from
  99. pep8. The second argument, "logical", is required only for logical-line
  100. fixes.
  101. The fixer method can return the list of modified lines or None. An empty
  102. list would mean that no changes were made. None would mean that only the
  103. line reported in the pep8 error was modified. Note that the modified line
  104. numbers that are returned are indexed at 1. This typically would correspond
  105. with the line number reported in the pep8 error information.
  106. [fixed method list]
  107. - e111
  108. - e121,e122,e123,e124,e125,e126,e127,e128
  109. - e201,e202,e203
  110. - e211
  111. - e221,e222,e223,e224,e225
  112. - e231
  113. - e251
  114. - e261,e262
  115. - e271,e272,e273,e274
  116. - e301,e302,e303
  117. - e401
  118. - e502
  119. - e701,e702
  120. - e711
  121. - e721
  122. - w291,w293
  123. - w391
  124. - w602,w603,w604
  125. """
  126. def __init__(self, filename, options, contents=None):
  127. self.filename = filename
  128. if contents is None:
  129. self.source = read_from_filename(filename, readlines=True)
  130. else:
  131. sio = StringIO(contents)
  132. self.source = sio.readlines()
  133. self.original_source = copy.copy(self.source)
  134. self.newline = find_newline(self.source)
  135. self.options = options
  136. self.indent_word = _get_indentword(''.join(self.source))
  137. self.logical_start = None
  138. self.logical_end = None
  139. # method definition
  140. self.fix_e111 = self.fix_e101
  141. self.fix_e128 = self.fix_e127
  142. self.fix_e202 = self.fix_e201
  143. self.fix_e203 = self.fix_e201
  144. self.fix_e211 = self.fix_e201
  145. self.fix_e221 = self.fix_e271
  146. self.fix_e222 = self.fix_e271
  147. self.fix_e223 = self.fix_e271
  148. self.fix_e241 = self.fix_e271
  149. self.fix_e242 = self.fix_e224
  150. self.fix_e261 = self.fix_e262
  151. self.fix_e272 = self.fix_e271
  152. self.fix_e273 = self.fix_e271
  153. self.fix_e274 = self.fix_e271
  154. self.fix_w191 = self.fix_e101
  155. def _fix_source(self, results):
  156. completed_lines = set()
  157. for result in sorted(results, key=_priority_key):
  158. if result['line'] in completed_lines:
  159. continue
  160. fixed_methodname = 'fix_%s' % result['id'].lower()
  161. if hasattr(self, fixed_methodname):
  162. fix = getattr(self, fixed_methodname)
  163. is_logical_fix = len(inspect.getargspec(fix).args) > 2
  164. if is_logical_fix:
  165. # Do not run logical fix if any lines have been modified.
  166. if completed_lines:
  167. continue
  168. logical = self._get_logical(result)
  169. if not logical:
  170. continue
  171. modified_lines = fix(result, logical)
  172. else:
  173. modified_lines = fix(result)
  174. if modified_lines:
  175. completed_lines.update(modified_lines)
  176. elif modified_lines == []: # Empty list means no fix
  177. if self.options.verbose >= 2:
  178. print(
  179. 'Not fixing {f} on line {l}'.format(
  180. f=result['id'], l=result['line']),
  181. file=sys.stderr)
  182. else: # We assume one-line fix when None
  183. completed_lines.add(result['line'])
  184. else:
  185. if self.options.verbose >= 3:
  186. print("'%s' is not defined." % fixed_methodname,
  187. file=sys.stderr)
  188. info = result['info'].strip()
  189. print('%s:%s:%s:%s' % (self.filename,
  190. result['line'],
  191. result['column'],
  192. info),
  193. file=sys.stderr)
  194. def fix(self):
  195. """Return a version of the source code with PEP 8 violations fixed."""
  196. if pep8:
  197. pep8_options = {
  198. 'ignore':
  199. self.options.ignore and self.options.ignore.split(','),
  200. 'select':
  201. self.options.select and self.options.select.split(','),
  202. }
  203. results = _execute_pep8(pep8_options, self.source)
  204. else:
  205. if self.options.verbose:
  206. print('Running in compatibility mode. Consider '
  207. 'upgrading to the latest pep8.',
  208. file=sys.stderr)
  209. results = _spawn_pep8((['--ignore=' + self.options.ignore]
  210. if self.options.ignore else []) +
  211. (['--select=' + self.options.select]
  212. if self.options.select else []) +
  213. [self.filename])
  214. if self.options.verbose:
  215. print('{n} issues to fix'.format(
  216. n=len(results)), file=sys.stderr)
  217. self._fix_source(filter_results(source=''.join(self.source),
  218. results=results))
  219. return ''.join(self.source)
  220. def fix_e101(self, _):
  221. """Reindent all lines."""
  222. reindenter = Reindenter(self.source, self.newline)
  223. modified_line_numbers = reindenter.run()
  224. if modified_line_numbers:
  225. self.source = reindenter.fixed_lines()
  226. return modified_line_numbers
  227. else:
  228. return []
  229. def find_logical(self, force=False):
  230. # make a variable which is the index of all the starts of lines
  231. if not force and self.logical_start is not None:
  232. return
  233. logical_start = []
  234. logical_end = []
  235. last_newline = True
  236. sio = StringIO(''.join(self.source))
  237. parens = 0
  238. for t in tokenize.generate_tokens(sio.readline):
  239. if t[0] in [tokenize.COMMENT, tokenize.DEDENT,
  240. tokenize.INDENT, tokenize.NL,
  241. tokenize.ENDMARKER]:
  242. continue
  243. if not parens and t[0] in [
  244. tokenize.NEWLINE, tokenize.SEMI
  245. ]:
  246. last_newline = True
  247. logical_end.append((t[3][0] - 1, t[2][1]))
  248. continue
  249. if last_newline and not parens:
  250. logical_start.append((t[2][0] - 1, t[2][1]))
  251. last_newline = False
  252. if t[0] == tokenize.OP:
  253. if t[1] in '([{':
  254. parens += 1
  255. elif t[1] in '}])':
  256. parens -= 1
  257. self.logical_start = logical_start
  258. self.logical_end = logical_end
  259. def _get_logical(self, result):
  260. """Return the logical line corresponding to the result.
  261. Assumes input is already E702-clean.
  262. """
  263. try:
  264. self.find_logical()
  265. except (IndentationError, tokenize.TokenError):
  266. return None
  267. row = result['line'] - 1
  268. col = result['column'] - 1
  269. ls = None
  270. le = None
  271. for i in range(0, len(self.logical_start), 1):
  272. x = self.logical_end[i]
  273. if x[0] > row or (x[0] == row and x[1] > col):
  274. le = x
  275. ls = self.logical_start[i]
  276. break
  277. if ls is None:
  278. return None
  279. original = self.source[ls[0]:le[0] + 1]
  280. return ls, le, original
  281. def _fix_reindent(self, result, logical, fix_distinct=False):
  282. """Fix a badly indented line.
  283. This is done by adding or removing from its initial indent only.
  284. """
  285. if not logical:
  286. return []
  287. ls, _, original = logical
  288. try:
  289. rewrapper = Wrapper(original, hard_wrap=MAX_LINE_WIDTH)
  290. except (tokenize.TokenError, IndentationError):
  291. return []
  292. valid_indents = rewrapper.pep8_expected()
  293. if not rewrapper.rel_indent:
  294. return []
  295. if result['line'] > ls[0]:
  296. # got a valid continuation line number from pep8
  297. row = result['line'] - ls[0] - 1
  298. # always pick the first option for this
  299. valid = valid_indents[row]
  300. got = rewrapper.rel_indent[row]
  301. else:
  302. # Line number from pep8 isn't a continuation line. Instead,
  303. # compare our own function's result, look for the first mismatch,
  304. # and just hope that we take fewer than 100 iterations to finish.
  305. for row in range(0, len(original), 1):
  306. valid = valid_indents[row]
  307. got = rewrapper.rel_indent[row]
  308. if valid != got:
  309. break
  310. line = ls[0] + row
  311. # always pick the expected indent, for now.
  312. indent_to = valid[0]
  313. if fix_distinct and indent_to == 4:
  314. if len(valid) == 1:
  315. return []
  316. else:
  317. indent_to = valid[1]
  318. if got != indent_to:
  319. orig_line = self.source[line]
  320. new_line = ' ' * (indent_to) + orig_line.lstrip()
  321. if new_line == orig_line:
  322. return []
  323. else:
  324. self.source[line] = new_line
  325. return [line + 1] # Line indexed at 1
  326. else:
  327. return []
  328. def fix_e121(self, result, logical):
  329. """Fix indentation to be a multiple of four."""
  330. # Fix by adjusting initial indent level.
  331. return self._fix_reindent(result, logical)
  332. def fix_e122(self, result, logical):
  333. """Add absent indentation for hanging indentation."""
  334. # Fix by adding an initial indent.
  335. return self._fix_reindent(result, logical)
  336. def fix_e123(self, result, logical):
  337. """Align closing bracket to match opening bracket."""
  338. # Fix by deleting whitespace to the correct level.
  339. if not logical:
  340. return []
  341. logical_lines = logical[2]
  342. line_index = result['line'] - 1
  343. original_line = self.source[line_index]
  344. fixed_line = (_get_indentation(logical_lines[0]) +
  345. original_line.lstrip())
  346. if fixed_line == original_line:
  347. # Fall back to slower method.
  348. return self._fix_reindent(result, logical)
  349. else:
  350. self.source[line_index] = fixed_line
  351. def fix_e124(self, result, logical):
  352. """Align closing bracket to match visual indentation."""
  353. # Fix by inserting whitespace before the closing bracket.
  354. return self._fix_reindent(result, logical)
  355. def fix_e125(self, result, logical):
  356. """Indent to distinguish line from next logical line."""
  357. # Fix by indenting the line in error to the next stop.
  358. modified_lines = self._fix_reindent(result, logical, fix_distinct=True)
  359. if modified_lines:
  360. return modified_lines
  361. else:
  362. # Fallback
  363. line_index = result['line'] - 1
  364. original_line = self.source[line_index]
  365. self.source[line_index] = self.indent_word + original_line
  366. def fix_e126(self, result, logical):
  367. """Fix over-indented hanging indentation."""
  368. # fix by deleting whitespace to the left
  369. if not logical:
  370. return []
  371. logical_lines = logical[2]
  372. line_index = result['line'] - 1
  373. original = self.source[line_index]
  374. fixed = (_get_indentation(logical_lines[0]) +
  375. self.indent_word + original.lstrip())
  376. if fixed == original:
  377. # Fallback to slower method.
  378. return self._fix_reindent(result, logical)
  379. else:
  380. self.source[line_index] = fixed
  381. def fix_e127(self, result, logical):
  382. """Fix visual indentation."""
  383. # Fix by inserting/deleting whitespace to the correct level.
  384. modified_lines = self._align_visual_indent(result, logical)
  385. if modified_lines:
  386. return modified_lines
  387. else:
  388. # Fallback to slower method.
  389. return self._fix_reindent(result, logical)
  390. def _align_visual_indent(self, result, logical):
  391. """Correct visual indent.
  392. This includes over (E127) and under (E128) indented lines.
  393. """
  394. if not logical:
  395. return []
  396. logical_lines = logical[2]
  397. line_index = result['line'] - 1
  398. original = self.source[line_index]
  399. fixed = original
  400. if '(' in logical_lines[0]:
  401. fixed = logical_lines[0].find('(') * ' ' + original.lstrip()
  402. elif logical_lines[0].rstrip().endswith('\\'):
  403. fixed = (_get_indentation(logical_lines[0]) +
  404. self.indent_word + original.lstrip())
  405. else:
  406. return []
  407. if fixed == original:
  408. return []
  409. else:
  410. self.source[line_index] = fixed
  411. def fix_e201(self, result):
  412. """Remove extraneous whitespace."""
  413. line_index = result['line'] - 1
  414. target = self.source[line_index]
  415. offset = result['column'] - 1
  416. # When multiline strings are involved, pep8 reports the error as
  417. # being at the start of the multiline string, which doesn't work
  418. # for us.
  419. if '"""' in target or "'''" in target:
  420. return []
  421. fixed = fix_whitespace(target,
  422. offset=offset,
  423. replacement='')
  424. if fixed == target:
  425. return []
  426. else:
  427. self.source[line_index] = fixed
  428. def fix_e224(self, result):
  429. """Remove extraneous whitespace around operator."""
  430. target = self.source[result['line'] - 1]
  431. offset = result['column'] - 1
  432. fixed = target[:offset] + target[offset:].replace('\t', ' ')
  433. self.source[result['line'] - 1] = fixed
  434. def fix_e225(self, result):
  435. """Fix missing whitespace around operator."""
  436. target = self.source[result['line'] - 1]
  437. offset = result['column'] - 1
  438. fixed = target[:offset] + ' ' + target[offset:]
  439. # Only proceed if non-whitespace characters match.
  440. # And make sure we don't break the indentation.
  441. if (fixed.replace(' ', '') == target.replace(' ', '') and
  442. _get_indentation(fixed) == _get_indentation(target)):
  443. self.source[result['line'] - 1] = fixed
  444. else:
  445. return []
  446. def fix_e231(self, result):
  447. """Add missing whitespace."""
  448. line_index = result['line'] - 1
  449. target = self.source[line_index]
  450. offset = result['column']
  451. fixed = target[:offset] + ' ' + target[offset:]
  452. self.source[line_index] = fixed
  453. def fix_e251(self, result):
  454. """Remove whitespace around parameter '=' sign."""
  455. line_index = result['line'] - 1
  456. target = self.source[line_index]
  457. # This is necessary since pep8 sometimes reports columns that goes
  458. # past the end of the physical line. This happens in cases like,
  459. # foo(bar\n=None)
  460. c = min(result['column'] - 1,
  461. len(target) - 1)
  462. if target[c].strip():
  463. fixed = target
  464. else:
  465. fixed = target[:c].rstrip() + target[c:].lstrip()
  466. # There could be an escaped newline
  467. #
  468. # def foo(a=\
  469. # 1)
  470. if (fixed.endswith('=\\\n') or
  471. fixed.endswith('=\\\r\n') or
  472. fixed.endswith('=\\\r')):
  473. self.source[line_index] = fixed.rstrip('\n\r \t\\')
  474. self.source[line_index + 1] = \
  475. self.source[line_index + 1].lstrip()
  476. return [line_index + 1, line_index + 2] # Line indexed at 1
  477. self.source[result['line'] - 1] = fixed
  478. def fix_e262(self, result):
  479. """Fix spacing after comment hash."""
  480. target = self.source[result['line'] - 1]
  481. offset = result['column']
  482. code = target[:offset].rstrip(' \t#')
  483. comment = target[offset:].lstrip(' \t#')
  484. fixed = code + (' # ' + comment if comment.strip()
  485. else self.newline)
  486. self.source[result['line'] - 1] = fixed
  487. def fix_e271(self, result):
  488. """Fix extraneous whitespace around keywords."""
  489. line_index = result['line'] - 1
  490. target = self.source[line_index]
  491. offset = result['column'] - 1
  492. fixed = fix_whitespace(target,
  493. offset=offset,
  494. replacement=' ')
  495. if fixed == target:
  496. return []
  497. else:
  498. self.source[line_index] = fixed
  499. def fix_e301(self, result):
  500. """Add missing blank line."""
  501. cr = self.newline
  502. self.source[result['line'] - 1] = cr + self.source[result['line'] - 1]
  503. def fix_e302(self, result):
  504. """Add missing 2 blank lines."""
  505. add_linenum = 2 - int(result['info'].split()[-1])
  506. cr = self.newline * add_linenum
  507. self.source[result['line'] - 1] = cr + self.source[result['line'] - 1]
  508. def fix_e303(self, result):
  509. """Remove extra blank lines."""
  510. delete_linenum = int(result['info'].split('(')[1].split(')')[0]) - 2
  511. delete_linenum = max(1, delete_linenum)
  512. # We need to count because pep8 reports an offset line number if there
  513. # are comments.
  514. cnt = 0
  515. line = result['line'] - 2
  516. modified_lines = []
  517. while cnt < delete_linenum:
  518. if line < 0:
  519. break
  520. if not self.source[line].strip():
  521. self.source[line] = ''
  522. modified_lines.append(1 + line) # Line indexed at 1
  523. cnt += 1
  524. line -= 1
  525. return modified_lines
  526. def fix_e304(self, result):
  527. """Remove blank line following function decorator."""
  528. line = result['line'] - 2
  529. if not self.source[line].strip():
  530. self.source[line] = ''
  531. def fix_e401(self, result):
  532. """Put imports on separate lines."""
  533. line_index = result['line'] - 1
  534. target = self.source[line_index]
  535. offset = result['column'] - 1
  536. if not target.lstrip().startswith('import'):
  537. return []
  538. # pep8 (1.3.1) reports false positive if there is an import statement
  539. # followed by a semicolon and some unrelated statement with commas in
  540. # it.
  541. if ';' in target:
  542. return []
  543. indentation = target.split('import ')[0]
  544. fixed = (target[:offset].rstrip('\t ,') + self.newline +
  545. indentation + 'import ' + target[offset:].lstrip('\t ,'))
  546. self.source[line_index] = fixed
  547. def fix_e501(self, result):
  548. """Try to make lines fit within 79 characters."""
  549. line_index = result['line'] - 1
  550. target = self.source[line_index]
  551. indent = _get_indentation(target)
  552. source = target[len(indent):]
  553. sio = StringIO(target)
  554. # Check for multiline string.
  555. try:
  556. tokens = list(tokenize.generate_tokens(sio.readline))
  557. except (tokenize.TokenError, IndentationError):
  558. multi_line_candidate = break_multi_line(
  559. target, newline=self.newline, indent_word=self.indent_word)
  560. if multi_line_candidate:
  561. self.source[line_index] = multi_line_candidate
  562. return
  563. else:
  564. return []
  565. # Prefer
  566. # my_long_function_name(
  567. # x, y, z, ...)
  568. #
  569. # over
  570. # my_long_function_name(x, y,
  571. # z, ...)
  572. candidate0 = _shorten_line(tokens, source, target, indent,
  573. self.indent_word, newline=self.newline,
  574. reverse=False)
  575. candidate1 = _shorten_line(tokens, source, target, indent,
  576. self.indent_word, newline=self.newline,
  577. reverse=True)
  578. if candidate0 and candidate1:
  579. if candidate0.split(self.newline)[0].endswith('('):
  580. self.source[line_index] = candidate0
  581. else:
  582. self.source[line_index] = candidate1
  583. elif candidate0:
  584. self.source[line_index] = candidate0
  585. elif candidate1:
  586. self.source[line_index] = candidate1
  587. else:
  588. # Otherwise both don't work
  589. return []
  590. def fix_e502(self, result):
  591. """Remove extraneous escape of newline."""
  592. line_index = result['line'] - 1
  593. target = self.source[line_index]
  594. self.source[line_index] = target.rstrip('\n\r \t\\') + self.newline
  595. def fix_e701(self, result):
  596. """Put colon-separated compound statement on separate lines."""
  597. line_index = result['line'] - 1
  598. target = self.source[line_index]
  599. c = result['column']
  600. fixed_source = (target[:c] + self.newline +
  601. _get_indentation(target) + self.indent_word +
  602. target[c:].lstrip('\n\r \t\\'))
  603. self.source[result['line'] - 1] = fixed_source
  604. def fix_e702(self, result, logical):
  605. """Put semicolon-separated compound statement on separate lines."""
  606. logical_lines = logical[2]
  607. line_index = result['line'] - 1
  608. target = self.source[line_index]
  609. if target.rstrip().endswith('\\'):
  610. # Normalize '1; \\\n2' into '1; 2'.
  611. self.source[line_index] = target.rstrip('\n \r\t\\')
  612. self.source[line_index + 1] = self.source[line_index + 1].lstrip()
  613. return [line_index + 1, line_index + 2]
  614. if target.rstrip().endswith(';'):
  615. self.source[line_index] = target.rstrip('\n \r\t;') + self.newline
  616. return
  617. offset = result['column'] - 1
  618. first = target[:offset].rstrip(';').rstrip()
  619. second = (_get_indentation(logical_lines[0]) +
  620. target[offset:].lstrip(';').lstrip())
  621. self.source[line_index] = first + self.newline + second
  622. def fix_e711(self, result):
  623. """Fix comparison."""
  624. line_index = result['line'] - 1
  625. target = self.source[line_index]
  626. offset = result['column'] - 1
  627. right_offset = offset + 2
  628. if right_offset >= len(target):
  629. return []
  630. left = target[:offset].rstrip()
  631. center = target[offset:right_offset]
  632. right = target[right_offset:].lstrip()
  633. if not right.startswith('None'):
  634. return []
  635. if center.strip() == '==':
  636. new_center = 'is'
  637. elif center.strip() == '!=':
  638. new_center = 'is not'
  639. else:
  640. return []
  641. self.source[line_index] = ' '.join([left, new_center, right])
  642. def fix_e721(self, _):
  643. """Switch to use isinstance()."""
  644. return self.refactor('idioms')
  645. def fix_w291(self, result):
  646. """Remove trailing whitespace."""
  647. fixed_line = self.source[result['line'] - 1].rstrip()
  648. self.source[result['line'] - 1] = '%s%s' % (fixed_line, self.newline)
  649. def fix_w293(self, result):
  650. """Remove trailing whitespace on blank line."""
  651. assert not self.source[result['line'] - 1].strip()
  652. self.source[result['line'] - 1] = self.newline
  653. def fix_w391(self, _):
  654. """Remove trailing blank lines."""
  655. blank_count = 0
  656. for line in reversed(self.source):
  657. line = line.rstrip()
  658. if line:
  659. break
  660. else:
  661. blank_count += 1
  662. original_length = len(self.source)
  663. self.source = self.source[:original_length - blank_count]
  664. return range(1, 1 + original_length)
  665. def refactor(self, fixer_name, ignore=None):
  666. """Return refactored code using lib2to3.
  667. Skip if ignore string is produced in the refactored code.
  668. """
  669. from lib2to3 import pgen2
  670. try:
  671. new_text = refactor_with_2to3(''.join(self.source),
  672. fixer_name=fixer_name)
  673. except (pgen2.parse.ParseError,
  674. UnicodeDecodeError, UnicodeEncodeError):
  675. return []
  676. try:
  677. original = unicode(''.join(self.source).strip(), 'utf-8')
  678. except (NameError, TypeError):
  679. original = ''.join(self.source).strip()
  680. if original == new_text.strip():
  681. return []
  682. else:
  683. if ignore:
  684. if ignore in new_text and ignore not in ''.join(self.source):
  685. return []
  686. original_length = len(self.source)
  687. self.source = [new_text]
  688. return range(1, 1 + original_length)
  689. def fix_w601(self, _):
  690. """Replace the {}.has_key() form with 'in'."""
  691. return self.refactor('has_key')
  692. def fix_w602(self, _):
  693. """Fix deprecated form of raising exception."""
  694. return self.refactor('raise',
  695. ignore='with_traceback')
  696. def fix_w603(self, _):
  697. """Replace <> with !=."""
  698. return self.refactor('ne')
  699. def fix_w604(self, _):
  700. """Replace backticks with repr()."""
  701. return self.refactor('repr')
  702. def find_newline(source):
  703. """Return type of newline used in source."""
  704. cr, lf, crlf = 0, 0, 0
  705. for s in source:
  706. if CRLF in s:
  707. crlf += 1
  708. elif CR in s:
  709. cr += 1
  710. elif LF in s:
  711. lf += 1
  712. _max = max(cr, crlf, lf)
  713. if _max == lf:
  714. return LF
  715. elif _max == crlf:
  716. return CRLF
  717. elif _max == cr:
  718. return CR
  719. else:
  720. return LF
  721. def _get_indentword(source):
  722. """Return indentation type."""
  723. sio = StringIO(source)
  724. indent_word = ' ' # Default in case source has no indentation
  725. try:
  726. for t in tokenize.generate_tokens(sio.readline):
  727. if t[0] == token.INDENT:
  728. indent_word = t[1]
  729. break
  730. except (tokenize.TokenError, IndentationError):
  731. pass
  732. return indent_word
  733. def _get_indentation(line):
  734. """Return leading whitespace."""
  735. if line.strip():
  736. non_whitespace_index = len(line) - len(line.lstrip())
  737. return line[:non_whitespace_index]
  738. else:
  739. return ''
  740. def _analyze_pep8result(result):
  741. tmp = result.split(':')
  742. filename = tmp[0]
  743. line = int(tmp[1])
  744. column = int(tmp[2])
  745. info = ' '.join(result.split()[1:])
  746. pep8id = info.lstrip().split()[0]
  747. return dict(id=pep8id, filename=filename, line=line,
  748. column=column, info=info)
  749. def _get_difftext(old, new, filename):
  750. diff = unified_diff(old, new, 'original/' + filename, 'fixed/' + filename)
  751. return ''.join(diff)
  752. def _priority_key(pep8_result):
  753. """Key for sorting PEP8 results.
  754. Global fixes should be done first. This is important for things
  755. like indentation.
  756. """
  757. priority = ['e101', 'e111', 'w191', # Global fixes
  758. 'e701', # Fix multiline colon-based before semicolon based
  759. 'e702', # Break multiline statements early
  760. 'e225', 'e231', # things that make lines longer
  761. 'e201', # Remove extraneous whitespace before breaking lines
  762. 'e501', # before we break lines
  763. ]
  764. key = pep8_result['id'].lower()
  765. if key in priority:
  766. return priority.index(key)
  767. else:
  768. # Lowest priority
  769. return len(priority)
  770. def _shorten_line(tokens, source, target, indentation, indent_word, newline,
  771. reverse=False):
  772. """Separate line at OPERATOR."""
  773. max_line_width_minus_indentation = MAX_LINE_WIDTH - len(indentation)
  774. if reverse:
  775. tokens.reverse()
  776. for tkn in tokens:
  777. # Don't break on '=' after keyword as this violates PEP 8.
  778. if token.OP == tkn[0] and tkn[1] != '=':
  779. offset = tkn[2][1] + 1
  780. if reverse:
  781. if offset > (max_line_width_minus_indentation -
  782. len(indent_word)):
  783. continue
  784. else:
  785. if (len(target.rstrip()) - offset >
  786. (max_line_width_minus_indentation -
  787. len(indent_word))):
  788. continue
  789. first = source[:offset - len(indentation)]
  790. second_indent = indentation
  791. if first.rstrip().endswith('('):
  792. second_indent += indent_word
  793. elif '(' in first:
  794. second_indent += ' ' * (1 + first.find('('))
  795. else:
  796. second_indent += indent_word
  797. second = (second_indent +
  798. source[offset - len(indentation):].lstrip())
  799. if not second.strip():
  800. continue
  801. # Don't modify if lines are not short enough
  802. if len(first) > max_line_width_minus_indentation:
  803. continue
  804. if len(second) > MAX_LINE_WIDTH: # Already includes indentation
  805. continue
  806. # Do not begin a line with a comma
  807. if second.lstrip().startswith(','):
  808. continue
  809. # Do end a line with a dot
  810. if first.rstrip().endswith('.'):
  811. continue
  812. if tkn[1] in '+-*/':
  813. fixed = first + ' \\' + newline + second
  814. else:
  815. fixed = first + newline + second
  816. if check_syntax(fixed):
  817. return indentation + fixed
  818. return None
  819. def fix_whitespace(line, offset, replacement):
  820. """Replace whitespace at offset and return fixed line."""
  821. # Replace escaped newlines too
  822. left = line[:offset].rstrip('\n\r \t\\')
  823. right = line[offset:].lstrip('\n\r \t\\')
  824. if right.startswith('#'):
  825. return line
  826. else:
  827. return left + replacement + right
  828. def _spawn_pep8(pep8_options):
  829. """Execute pep8 via subprocess.Popen."""
  830. for path in os.environ['PATH'].split(':'):
  831. if os.path.exists(os.path.join(path, PEP8_BIN)):
  832. cmd = ([os.path.join(path, PEP8_BIN)] +
  833. pep8_options)
  834. p = Popen(cmd, stdout=PIPE)
  835. output = p.communicate()[0].decode('utf-8')
  836. return [_analyze_pep8result(l)
  837. for l in output.splitlines()]
  838. raise Exception("'%s' is not found." % PEP8_BIN)
  839. def _execute_pep8(pep8_options, source):
  840. """Execute pep8 via python method calls."""
  841. class QuietReport(pep8.BaseReport):
  842. """Version of checker that does not print."""
  843. def __init__(self, options):
  844. super(QuietReport, self).__init__(options)
  845. self.__full_error_results = []
  846. def error(self, line_number, offset, text, _):
  847. """Collect errors."""
  848. code = super(QuietReport, self).error(line_number, offset, text, _)
  849. if code:
  850. self.__full_error_results.append(
  851. dict(id=code, line=line_number,
  852. column=offset + 1, info=text))
  853. def full_error_results(self):
  854. """Return error results in detail.
  855. Results are in the form of a list of dictionaries. Each dictionary
  856. contains 'id', 'line', 'column', and 'info'.
  857. """
  858. return self.__full_error_results
  859. checker = pep8.Checker('', lines=source,
  860. reporter=QuietReport, **pep8_options)
  861. checker.check_all()
  862. return checker.report.full_error_results()
  863. class Reindenter(object):
  864. """Reindents badly-indented code to uniformly use four-space indentation.
  865. Released to the public domain, by Tim Peters, 03 October 2000.
  866. """
  867. def __init__(self, input_text, newline):
  868. self.newline = newline
  869. self.find_stmt = 1 # next token begins a fresh stmt?
  870. self.level = 0 # current indent level
  871. # Raw file lines.
  872. self.raw = input_text
  873. self.after = None
  874. self.string_content_line_numbers = multiline_string_lines(
  875. ''.join(self.raw))
  876. # File lines, rstripped & tab-expanded. Dummy at start is so
  877. # that we can use tokenize's 1-based line numbering easily.
  878. # Note that a line is all-blank iff it is a newline.
  879. self.lines = []
  880. for line_number, line in enumerate(self.raw, start=1):
  881. # Do not modify if inside a multi-line string.
  882. if line_number in self.string_content_line_numbers:
  883. self.lines.append(line)
  884. else:
  885. # Only expand leading tabs.
  886. self.lines.append(_get_indentation(line).expandtabs() +
  887. line.strip() + newline)
  888. self.lines.insert(0, None)
  889. self.index = 1 # index into self.lines of next line
  890. # List of (lineno, indentlevel) pairs, one for each stmt and
  891. # comment line. indentlevel is -1 for comment lines, as a
  892. # signal that tokenize doesn't know what to do about them;
  893. # indeed, they're our headache!
  894. self.stats = []
  895. def run(self):
  896. """Fix indentation and return modified line numbers.
  897. Line numbers are indexed at 1.
  898. """
  899. tokens = tokenize.generate_tokens(self.getline)
  900. try:
  901. for t in tokens:
  902. self.tokeneater(*t)
  903. except (tokenize.TokenError, IndentationError):
  904. return set()
  905. # Remove trailing empty lines.
  906. lines = self.lines
  907. while lines and lines[-1] == self.newline:
  908. lines.pop()
  909. # Sentinel.
  910. stats = self.stats
  911. stats.append((len(lines), 0))
  912. # Map count of leading spaces to # we want.
  913. have2want = {}
  914. # Program after transformation.
  915. after = self.after = []
  916. # Copy over initial empty lines -- there's nothing to do until
  917. # we see a line with *something* on it.
  918. i = stats[0][0]
  919. after.extend(lines[1:i])
  920. for i in range(len(stats) - 1):
  921. thisstmt, thislevel = stats[i]
  922. nextstmt = stats[i + 1][0]
  923. have = _leading_space_count(lines[thisstmt])
  924. want = thislevel * 4
  925. if want < 0:
  926. # A comment line.
  927. if have:
  928. # An indented comment line. If we saw the same
  929. # indentation before, reuse what it most recently
  930. # mapped to.
  931. want = have2want.get(have, - 1)
  932. if want < 0:
  933. # Then it probably belongs to the next real stmt.
  934. for j in range(i + 1, len(stats) - 1):
  935. jline, jlevel = stats[j]
  936. if jlevel >= 0:
  937. if have == _leading_space_count(lines[jline]):
  938. want = jlevel * 4
  939. break
  940. if want < 0: # Maybe it's a hanging
  941. # comment like this one,
  942. # in which case we should shift it like its base
  943. # line got shifted.
  944. for j in range(i - 1, -1, -1):
  945. jline, jlevel = stats[j]
  946. if jlevel >= 0:
  947. want = (have + _leading_space_count(
  948. after[jline - 1]) -
  949. _leading_space_count(lines[jline]))
  950. break
  951. if want < 0:
  952. # Still no luck -- leave it alone.
  953. want = have
  954. else:
  955. want = 0
  956. assert want >= 0
  957. have2want[have] = want
  958. diff = want - have
  959. if diff == 0 or have == 0:
  960. after.extend(lines[thisstmt:nextstmt])
  961. else:
  962. for line_number, line in enumerate(lines[thisstmt:nextstmt],
  963. start=thisstmt):
  964. if line_number in self.string_content_line_numbers:
  965. after.append(line)
  966. elif diff > 0:
  967. if line == self.newline:
  968. after.append(line)
  969. else:
  970. after.append(' ' * diff + line)
  971. else:
  972. remove = min(_leading_space_count(line), -diff)
  973. after.append(line[remove:])
  974. if self.raw == self.after:
  975. return set()
  976. else:
  977. return (set(range(1, 1 + len(self.raw))) -
  978. self.string_content_line_numbers)
  979. def fixed_lines(self):
  980. return self.after
  981. def getline(self):
  982. """Line-getter for tokenize."""
  983. if self.index >= len(self.lines):
  984. line = ""
  985. else:
  986. line = self.lines[self.index]
  987. self.index += 1
  988. return line
  989. def tokeneater(self, token_type, _, start, __, line):
  990. """Line-eater for tokenize."""
  991. sline = start[0]
  992. if token_type == tokenize.NEWLINE:
  993. # A program statement, or ENDMARKER, will eventually follow,
  994. # after some (possibly empty) run of tokens of the form
  995. # (NL | COMMENT)* (INDENT | DEDENT+)?
  996. self.find_stmt = 1
  997. elif token_type == tokenize.INDENT:
  998. self.find_stmt = 1
  999. self.level += 1
  1000. elif token_type == tokenize.DEDENT:
  1001. self.find_stmt = 1
  1002. self.level -= 1
  1003. elif token_type == tokenize.COMMENT:
  1004. if self.find_stmt:
  1005. self.stats.append((sline, -1))
  1006. # but we're still looking for a new stmt, so leave
  1007. # find_stmt alone
  1008. elif token_type == tokenize.NL:
  1009. pass
  1010. elif self.find_stmt:
  1011. # This is the first "real token" following a NEWLINE, so it
  1012. # must be the first token of the next program statement, or an
  1013. # ENDMARKER.
  1014. self.find_stmt = 0
  1015. if line: # not endmarker
  1016. self.stats.append((sline, self.level))
  1017. class Wrapper(object):
  1018. """Class for functions relating to continuation lines and line folding.
  1019. Each instance operates on a single logical line.
  1020. """
  1021. SKIP_TOKENS = frozenset([
  1022. tokenize.COMMENT, tokenize.NL, tokenize.INDENT,
  1023. tokenize.DEDENT, tokenize.NEWLINE, tokenize.ENDMARKER
  1024. ])
  1025. def __init__(self, physical_lines, hard_wrap=79, soft_wrap=72):
  1026. if type(physical_lines) != list:
  1027. physical_lines = physical_lines.splitlines(keepends=True)
  1028. self.lines = physical_lines
  1029. self.index = 0
  1030. self.hard_wrap = hard_wrap
  1031. self.soft_wrap = soft_wrap
  1032. self.tokens = list()
  1033. self.rel_indent = None
  1034. sio = StringIO(''.join(physical_lines))
  1035. for t in tokenize.generate_tokens(sio.readline):
  1036. if not len(self.tokens) and t[0] in self.SKIP_TOKENS:
  1037. continue
  1038. if t[0] != tokenize.ENDMARKER:
  1039. self.tokens.append(t)
  1040. self.logical_line = self.build_tokens_logical(self.tokens)
  1041. def build_tokens_logical(self, tokens):
  1042. """Build a logical line from a list of tokens.
  1043. Return the logical line and a list of (offset, token) tuples. Does
  1044. not mute strings like the version in pep8.py.
  1045. """
  1046. # from pep8.py with minor modifications
  1047. logical = []
  1048. previous = None
  1049. for t in tokens:
  1050. token_type, text = t[0:2]
  1051. if token_type in self.SKIP_TOKENS:
  1052. continue
  1053. if previous:
  1054. end_line, end = previous[3]
  1055. start_line, start = t[2]
  1056. if end_line != start_line: # different row
  1057. prev_text = self.lines[end_line - 1][end - 1]
  1058. if prev_text == ',' or (prev_text not in '{[('
  1059. and text not in '}])'):
  1060. logical.append(' ')
  1061. elif end != start: # different column
  1062. fill = self.lines[end_line - 1][end:start]
  1063. logical.append(fill)
  1064. logical.append(text)
  1065. previous = t
  1066. logical_line = ''.join(logical)
  1067. assert logical_line.lstrip() == logical_line
  1068. assert logical_line.rstrip() == logical_line
  1069. return logical_line
  1070. def pep8_expected(self):
  1071. """Replicate logic in pep8.py, to know what level to indent things to.
  1072. Return a list of lists; each list represents valid indent levels for
  1073. the line in question, relative from the initial indent. However, the
  1074. first entry is the indent level which was expected.
  1075. """
  1076. # What follows is an adjusted version of
  1077. # pep8.py:continuation_line_indentation. All of the comments have been
  1078. # stripped and the 'yield' statements replaced with 'pass'.
  1079. tokens = self.tokens
  1080. if not tokens:
  1081. return
  1082. first_row = tokens[0][2][0]
  1083. nrows = 1 + tokens[-1][2][0] - first_row
  1084. # here are the return values
  1085. valid_indents = [list()] * nrows
  1086. indent_level = tokens[0][2][1]
  1087. valid_indents[0].append(indent_level)
  1088. if nrows == 1:
  1089. # bug, really.
  1090. return valid_indents
  1091. indent_next = self.logical_line.endswith(':')
  1092. row = depth = 0
  1093. parens = [0] * nrows
  1094. self.rel_indent = rel_indent = [0] * nrows
  1095. indent = [indent_level]
  1096. indent_chances = {}
  1097. last_indent = (0, 0)
  1098. last_token_multiline = None
  1099. for token_type, text, start, end, _ in self.tokens:
  1100. newline = row < start[0] - first_row
  1101. if newline:
  1102. row = start[0] - first_row
  1103. newline = (not last_token_multiline and
  1104. token_type not in (tokenize.NL, tokenize.NEWLINE))
  1105. if newline:
  1106. # This is where the differences start. Instead of looking at
  1107. # the line and determining whether the observed indent matches
  1108. # our expectations, we decide which type of indentation is in
  1109. # use at the given indent level, and return the offset. This
  1110. # algorithm is susceptible to "carried errors", but should
  1111. # through repeated runs eventually solve indentation for
  1112. # multi-line expressions less than PEP8_PASSES_MAX lines long.
  1113. if depth:
  1114. for open_row in range(row - 1, -1, -1):
  1115. if parens[open_row]:
  1116. break
  1117. else:
  1118. open_row = 0
  1119. # That's all we get to work with. This code attempts to
  1120. # "reverse" the below logic, and place into the valid indents
  1121. # list
  1122. vi = []
  1123. add_second_chances = False
  1124. if token_type == tokenize.OP and text in ']})':
  1125. # this line starts with a closing bracket, so it needs to
  1126. # be closed at the same indent as the opening one.
  1127. if indent[depth]:
  1128. # hanging indent
  1129. vi.append(indent[depth])
  1130. else:
  1131. # visual indent
  1132. vi.append(indent_level + rel_indent[open_row])
  1133. elif depth and indent[depth]:
  1134. # visual indent was previously confirmed.
  1135. vi.append(indent[depth])
  1136. add_second_chances = True
  1137. elif depth and True in indent_chances.values():
  1138. # visual indent happened before, so stick to
  1139. # visual indent this time.
  1140. if depth > 1 and indent[depth - 1]:
  1141. vi.append(indent[depth - 1])
  1142. else:
  1143. # stupid fallback
  1144. vi.append(indent_level + 4)
  1145. add_second_chances = True
  1146. elif not depth:
  1147. vi.append(indent_level + 4)
  1148. else:
  1149. # must be in hanging indent
  1150. hang = rel_indent[open_row] + 4
  1151. vi.append(indent_level + hang)
  1152. # about the best we can do without look-ahead
  1153. if indent_next and vi[0] == indent_level + 4 and \
  1154. nrows == row + 1:
  1155. vi[0] += 4
  1156. if add_second_chances:
  1157. # visual indenters like to line things up.
  1158. min_indent = vi[0]
  1159. for col, what in indent_chances.items():
  1160. if col > min_indent and (
  1161. what is True or
  1162. (what == str and token_type == tokenize.STRING) or
  1163. (what == text and token_type == tokenize.OP)
  1164. ):
  1165. vi.append(col)
  1166. vi = sorted(vi)
  1167. valid_indents[row] = vi
  1168. # ...returning to original continuation_line_identation func...
  1169. visual_indent = indent_chances.get(start[1])
  1170. last_indent = start
  1171. rel_indent[row] = start[1] - indent_level
  1172. hang = rel_indent[row] - rel_indent[open_row]
  1173. if token_type == tokenize.OP and text in ']})':
  1174. if indent[depth]:
  1175. if start[1] != indent[depth]:
  1176. pass # E124
  1177. elif hang:
  1178. pass # E123
  1179. elif visual_indent is True:
  1180. if not indent[depth]:
  1181. indent[depth] = start[1]
  1182. elif visual_indent in (text, str):
  1183. pass
  1184. elif indent[depth] and start[1] < indent[depth]:
  1185. pass # E128
  1186. elif hang == 4 or (indent_next and rel_indent[row] == 8):
  1187. pass
  1188. else:
  1189. if hang <= 0:
  1190. pass # E122
  1191. elif indent[depth]:
  1192. pass # E127
  1193. elif hang % 4:
  1194. pass # E121
  1195. else:
  1196. pass # E126
  1197. # line altered: comments shouldn't define a visual indent
  1198. if parens[row] and not indent[depth] and token_type not in (
  1199. tokenize.NL, tokenize.COMMENT
  1200. ):
  1201. indent[depth] = start[1]
  1202. indent_chances[start[1]] = True
  1203. elif token_type == tokenize.STRING or text in (
  1204. 'u', 'ur', 'b', 'br'
  1205. ):
  1206. indent_chances[start[1]] = str
  1207. if token_type == tokenize.OP:
  1208. if text in '([{':
  1209. depth += 1
  1210. indent.append(0)
  1211. parens[row] += 1
  1212. elif text in ')]}' and depth > 0:
  1213. prev_indent = indent.pop() or last_indent[1]
  1214. for d in range(depth):
  1215. if indent[d] > prev_indent:
  1216. indent[d] = 0
  1217. for ind in list(indent_chances):
  1218. if ind >= prev_indent:
  1219. del indent_chances[ind]
  1220. depth -= 1
  1221. if depth and indent[depth]: # modified
  1222. indent_chances[indent[depth]] = True
  1223. for idx in range(row, -1, -1):
  1224. if parens[idx]:
  1225. parens[idx] -= 1
  1226. break
  1227. assert len(indent) == depth + 1
  1228. if start[1] not in indent_chances:
  1229. indent_chances[start[1]] = text
  1230. last_token_multiline = (start[0] != end[0])
  1231. if indent_next and rel_indent[-1] == 4:
  1232. pass # E125
  1233. return valid_indents
  1234. def _leading_space_count(line):
  1235. """Return number of leading spaces in line."""
  1236. i = 0
  1237. while i < len(line) and line[i] == ' ':
  1238. i += 1
  1239. return i
  1240. def refactor_with_2to3(source_text, fixer_name):
  1241. """Use lib2to3 to refactor the source.
  1242. Return the refactored source code.
  1243. """
  1244. from lib2to3 import refactor
  1245. fixers = ['lib2to3.fixes.fix_' + fixer_name]
  1246. tool = refactor.RefactoringTool(
  1247. fixer_names=fixers,
  1248. explicit=fixers)
  1249. try:
  1250. return unicode(tool.refactor_string(
  1251. source_text.decode('utf-8'), name=''))
  1252. except NameError:
  1253. return str(tool.refactor_string(source_text, name=''))
  1254. def break_multi_line(source_text, newline, indent_word):
  1255. """Break first line of multi-line code.
  1256. Return None if a break is not possible.
  1257. """
  1258. # Handle special case only.
  1259. if ('(' in source_text and source_text.rstrip().endswith(',')):
  1260. index = 1 + source_text.find('(')
  1261. if index >= MAX_LINE_WIDTH:
  1262. return None
  1263. # Make sure we are not in a string.
  1264. for quote in ['"', "'"]:
  1265. if quote in source_text:
  1266. if source_text.find(quote) < index:
  1267. return None
  1268. # Make sure we are not in a comment.
  1269. if '#' in source_text:
  1270. if source_text.find('#') < index:
  1271. return None
  1272. assert index < len(source_text)
  1273. return (
  1274. source_text[:index].rstrip() + newline +
  1275. _get_indentation(source_text) + indent_word +
  1276. source_text[index:].lstrip())
  1277. else:
  1278. return None
  1279. def check_syntax(code):
  1280. """Return True if syntax is okay."""
  1281. try:
  1282. return compile(code, '<string>', 'exec')
  1283. except (SyntaxError, TypeError, UnicodeDecodeError):
  1284. return False
  1285. def filter_results(source, results):
  1286. """Filter out spurious reports from pep8.
  1287. Currently we filter out errors about indentation in multiline strings.
  1288. """
  1289. string_line_numbers = multiline_string_lines(source)
  1290. for r in results:
  1291. if r['line'] in string_line_numbers:
  1292. if r['id'].lower().startswith('e1'):
  1293. continue
  1294. elif r['id'].lower() in ['e501', 'w191']:
  1295. continue
  1296. # Filter out incorrect E101 reports when there are no tabs.
  1297. # pep8 will complain about this even if the tab indentation found
  1298. # elsewhere is in a multi-line string.
  1299. if r['id'].lower() == 'e101' and '\t' not in source[r['line'] - 1]:
  1300. continue
  1301. yield r
  1302. def multiline_string_lines(source):
  1303. """Return line numbers that are within multiline strings.
  1304. The line numbers are indexed at 1.
  1305. Docstrings are ignored.
  1306. """
  1307. sio = StringIO(source)
  1308. line_numbers = set()
  1309. previous_token_type = ''
  1310. try:
  1311. for t in tokenize.generate_tokens(sio.readline):
  1312. token_type = t[0]
  1313. token_string = t[1]
  1314. start_row = t[2][0]
  1315. end_row = t[3][0]
  1316. if (token_type == tokenize.STRING and
  1317. starts_with_triple(token_string) and
  1318. previous_token_type != tokenize.INDENT):
  1319. # We increment by one since we want the contents of the
  1320. # string.
  1321. line_numbers |= set(range(1 + start_row, 1 + end_row))
  1322. previous_token_type = token_type
  1323. except (IndentationError, tokenize.TokenError):
  1324. pass
  1325. return line_numbers
  1326. def starts_with_triple(string):
  1327. """Return True if the string starts with triple single/double quotes."""
  1328. return (string.strip().startswith('"""') or
  1329. string.strip().startswith("'''"))
  1330. def fix_file(filename, opts, output=sys.stdout):
  1331. tmp_source = read_from_filename(filename)
  1332. # Add missing newline (important for diff)
  1333. if tmp_source:
  1334. tmp_newline = find_newline(tmp_source)
  1335. if tmp_source == tmp_source.rstrip(tmp_newline):
  1336. tmp_source += tmp_newline
  1337. fix = FixPEP8(filename, opts, contents=tmp_source)
  1338. fixed_source = fix.fix()
  1339. original_source = copy.copy(fix.original_source)
  1340. tmp_filename = filename
  1341. if not pep8 or opts.in_place:
  1342. encoding = detect_encoding(filename)
  1343. for _ in range(opts.pep8_passes):
  1344. if fixed_source == tmp_source:
  1345. break
  1346. tmp_source = copy.copy(fixed_source)
  1347. if not pep8:
  1348. tmp_filename = tempfile.mkstemp()[1]
  1349. fp = open_with_encoding(tmp_filename, encoding=encoding, mode='w')
  1350. fp.write(fixed_source)
  1351. fp.close()
  1352. fix = FixPEP8(tmp_filename, opts, contents=tmp_source)
  1353. fixed_source = fix.fix()
  1354. if not pep8:
  1355. os.remove(tmp_filename)
  1356. del tmp_filename
  1357. del tmp_source
  1358. if opts.diff:
  1359. new = StringIO(''.join(fix.source))
  1360. new = new.readlines()
  1361. output.write(_get_difftext(original_source, new, filename))
  1362. elif opts.in_place:
  1363. fp = open_with_encoding(filename, encoding=encoding,
  1364. mode='w')
  1365. fp.write(fixed_source)
  1366. fp.close()
  1367. else:
  1368. output.write(fixed_source)
  1369. def parse_args(args):
  1370. """Parse command-line options."""
  1371. parser = OptionParser(usage='Usage: autopep8 [options] '
  1372. '[filename [filename ...]]',
  1373. version='autopep8: %s' % __version__,
  1374. description=__doc__,
  1375. prog='autopep8')
  1376. parser.add_option('-v', '--verbose', action='count', dest='verbose',
  1377. default=0,
  1378. help='print verbose messages; '
  1379. 'multiple -v result in more verbose messages')
  1380. parser.add_option('-d', '--diff', action='store_true', dest='diff',
  1381. help='print the diff for the fixed source')
  1382. parser.add_option('-i', '--in-place', action='store_true',
  1383. help='make changes to files in place')
  1384. parser.add_option('-r', '--recursive', action='store_true',
  1385. help='run recursively; must be used with --in-place or '
  1386. '--diff')
  1387. parser.add_option('-p', '--pep8-passes',
  1388. default=100, type='int',
  1389. help='maximum number of additional pep8 passes'
  1390. ' (default: %default)')
  1391. parser.add_option('--list-fixes', action='store_true',
  1392. help='list codes for fixes; '
  1393. 'used by --ignore and --select')
  1394. parser.add_option('--ignore', default='',
  1395. help='do not fix these errors/warnings (e.g. E4,W)')
  1396. parser.add_option('--select', default='',
  1397. help='fix only these errors/warnings (e.g. E4,W)')
  1398. opts, args = parser.parse_args(args)
  1399. if not len(args) and not opts.list_fixes:
  1400. parser.error('incorrect number of arguments')
  1401. if len(args) > 1 and not (opts.in_place or opts.diff):
  1402. parser.error('autopep8 only takes one filename as argument '
  1403. 'unless the "--in-place" or "--diff" options are '
  1404. 'used')
  1405. if opts.recursive and not (opts.in_place or opts.diff):
  1406. parser.error('--recursive must be used with --in-place or --diff')
  1407. if opts.in_place and opts.diff:
  1408. parser.error('--in-place and --diff are mutually exclusive')
  1409. return opts, args
  1410. def supported_fixes():
  1411. """Yield pep8 error codes that autopep8 fixes.
  1412. Each item we yield is a tuple of the code followed by its description.
  1413. """
  1414. instance = FixPEP8(filename=None, options=None, contents='')
  1415. for attribute in dir(instance):
  1416. code = re.match('fix_([ew][0-9][0-9][0-9])', attribute)
  1417. if code:
  1418. yield (code.group(1).upper(),
  1419. re.sub(r'\s+', ' ',
  1420. getattr(instance, attribute).__doc__))
  1421. def main():
  1422. """Tool main."""
  1423. opts, args = parse_args(sys.argv[1:])
  1424. if opts.list_fixes:
  1425. for code, description in supported_fixes():
  1426. print('{code} - {description}'.format(
  1427. code=code, description=description))
  1428. return 0
  1429. if opts.in_place or opts.diff:
  1430. filenames = list(set(args))
  1431. else:
  1432. assert len(args) == 1
  1433. assert not opts.recursive
  1434. filenames = args[:1]
  1435. if sys.version_info[0] >= 3:
  1436. output = sys.stdout
  1437. else:
  1438. output = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)
  1439. while filenames:
  1440. name = filenames.pop(0)
  1441. if opts.recursive and os.path.isdir(name):
  1442. for root, directories, children in os.walk(name):
  1443. filenames += [os.path.join(root, f) for f in children
  1444. if f.endswith('.py') and
  1445. not f.startswith('.')]
  1446. for d in directories:
  1447. if d.startswith('.'):
  1448. directories.remove(d)
  1449. else:
  1450. if opts.verbose:
  1451. print('[file:%s]' % name, file=sys.stderr)
  1452. try:
  1453. fix_file(name, opts, output)
  1454. except IOError as error:
  1455. print(str(error), file=sys.stderr)
  1456. if __name__ == '__main__':
  1457. try:
  1458. sys.exit(main())
  1459. except KeyboardInterrupt:
  1460. sys.exit(1)