/usr/share/pyshared/autopep8.py is in python-autopep8 0.9.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 | #!/usr/bin/env python
#
# Copyright (C) 2010-2011 Hideo Hattori
# Copyright (C) 2011-2013 Hideo Hattori, Steven Myint
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""Automatically formats Python code to conform to the PEP 8 style guide.
Fixes that only need be done once can be added by adding a function of the form
"fix_<code>(source)" to this module. They should return the fixed source code.
These fixes are picked up by apply_global_fixes().
Fixes that depend on pep8 should be added as methods to FixPEP8. See the class
documentation for more information.
"""
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import codecs
import copy
import fnmatch
import inspect
import os
import re
import signal
import sys
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
import token
import tokenize
from optparse import OptionParser
import difflib
import pep8
try:
unicode
except NameError:
unicode = str
__version__ = '0.9.1'
CR = '\r'
LF = '\n'
CRLF = '\r\n'
PYTHON_SHEBANG_REGEX = re.compile(r'^#!.*\bpython[23]?\b')
# For generating line shortening candidates.
SHORTEN_OPERATOR_GROUPS = frozenset([
frozenset([',']),
frozenset(['%']),
frozenset([',', '(', '[', '{']),
frozenset([',', '(', '[', '{', '%', '+', '-', '*', '/', '//']),
])
DEFAULT_IGNORE = 'E24,W6'
def open_with_encoding(filename, encoding=None, mode='r'):
"""Return opened file with a specific encoding."""
if not encoding:
encoding = detect_encoding(filename)
import io
return io.open(filename, mode=mode, encoding=encoding,
newline='') # Preserve line endings
def detect_encoding(filename):
"""Return file encoding."""
try:
with open(filename, 'rb') as input_file:
from lib2to3.pgen2 import tokenize as lib2to3_tokenize
encoding = lib2to3_tokenize.detect_encoding(input_file.readline)[0]
# Check for correctness of encoding
with open_with_encoding(filename, encoding) as test_file:
test_file.read()
return encoding
except (LookupError, SyntaxError, UnicodeDecodeError):
return 'latin-1'
def read_from_filename(filename, readlines=False):
"""Return contents of file."""
with open_with_encoding(filename) as input_file:
return input_file.readlines() if readlines else input_file.read()
def extended_blank_lines(logical_line,
blank_lines,
indent_level,
previous_logical):
"""Check for missing blank lines after class declaration."""
if (previous_logical.startswith('class ')):
if (logical_line.startswith(('def ', 'class ', '@')) or
pep8.DOCSTRING_REGEX.match(logical_line)):
if indent_level and not blank_lines:
yield (0, 'E301 expected 1 blank line, found 0')
elif previous_logical.startswith('def '):
if blank_lines and pep8.DOCSTRING_REGEX.match(logical_line):
yield (0, 'E303 too many blank lines ({0})'.format(blank_lines))
elif pep8.DOCSTRING_REGEX.match(previous_logical):
if (
indent_level and
not blank_lines and
logical_line.startswith(('def ')) and
'(self' in logical_line
):
yield (0, 'E301 expected 1 blank line, found 0')
pep8.register_check(extended_blank_lines)
class FixPEP8(object):
"""Fix invalid code.
Fixer methods are prefixed "fix_". The _fix_source() method looks for these
automatically.
The fixer method can take either one or two arguments (in addition to
self). The first argument is "result", which is the error information from
pep8. The second argument, "logical", is required only for logical-line
fixes.
The fixer method can return the list of modified lines or None. An empty
list would mean that no changes were made. None would mean that only the
line reported in the pep8 error was modified. Note that the modified line
numbers that are returned are indexed at 1. This typically would correspond
with the line number reported in the pep8 error information.
[fixed method list]
- e111
- e121,e122,e123,e124,e125,e126,e127,e128,e129
- e201,e202,e203
- e211
- e221,e222,e223,e224,e225
- e231
- e251
- e261,e262
- e271,e272,e273,e274
- e301,e302,e303
- e401
- e502
- e701,e702
- e711
- w291,w293
- w391
"""
def __init__(self, filename, options, contents=None):
self.filename = filename
if contents is None:
self.source = read_from_filename(filename, readlines=True)
else:
sio = StringIO(contents)
self.source = sio.readlines()
self.newline = find_newline(self.source)
self.options = options
self.indent_word = _get_indentword(''.join(self.source))
# method definition
self.fix_e111 = self.fix_e101
self.fix_e128 = self.fix_e127
self.fix_e129 = self.fix_e125
self.fix_e202 = self.fix_e201
self.fix_e203 = self.fix_e201
self.fix_e211 = self.fix_e201
self.fix_e221 = self.fix_e271
self.fix_e222 = self.fix_e271
self.fix_e223 = self.fix_e271
self.fix_e226 = self.fix_e225
self.fix_e227 = self.fix_e225
self.fix_e228 = self.fix_e225
self.fix_e241 = self.fix_e271
self.fix_e242 = self.fix_e224
self.fix_e261 = self.fix_e262
self.fix_e272 = self.fix_e271
self.fix_e273 = self.fix_e271
self.fix_e274 = self.fix_e271
self.fix_e703 = self.fix_e702
self.fix_w191 = self.fix_e101
def _fix_source(self, results):
completed_lines = set()
for result in sorted(results, key=_priority_key):
if result['line'] in completed_lines:
continue
fixed_methodname = 'fix_%s' % result['id'].lower()
if hasattr(self, fixed_methodname):
fix = getattr(self, fixed_methodname)
is_logical_fix = len(inspect.getargspec(fix).args) > 2
if is_logical_fix:
# Do not run logical fix if any lines have been modified.
if completed_lines:
continue
logical = self._get_logical(result)
if not logical:
continue
modified_lines = fix(result, logical)
else:
modified_lines = fix(result)
if modified_lines:
completed_lines.update(modified_lines)
elif modified_lines == []: # Empty list means no fix
if self.options.verbose >= 2:
print(
'---> Not fixing {f} on line {l}'.format(
f=result['id'], l=result['line']),
file=sys.stderr)
else: # We assume one-line fix when None
completed_lines.add(result['line'])
else:
if self.options.verbose >= 3:
print("---> '%s' is not defined." % fixed_methodname,
file=sys.stderr)
info = result['info'].strip()
print('---> %s:%s:%s:%s' % (self.filename,
result['line'],
result['column'],
info),
file=sys.stderr)
def fix(self):
"""Return a version of the source code with PEP 8 violations fixed."""
pep8_options = {
'ignore': self.options.ignore,
'select': self.options.select,
'max_line_length': self.options.max_line_length,
}
results = _execute_pep8(pep8_options, self.source)
if self.options.verbose:
progress = {}
for r in results:
if r['id'] not in progress:
progress[r['id']] = set()
progress[r['id']].add(r['line'])
print('---> {n} issue(s) to fix {progress}'.format(
n=len(results), progress=progress), file=sys.stderr)
self._fix_source(filter_results(source=''.join(self.source),
results=results,
aggressive=self.options.aggressive))
return ''.join(self.source)
def fix_e101(self, _):
"""Reindent all lines."""
reindenter = Reindenter(self.source, self.newline)
modified_line_numbers = reindenter.run()
if modified_line_numbers:
self.source = reindenter.fixed_lines()
return modified_line_numbers
else:
return []
def _find_logical(self):
# make a variable which is the index of all the starts of lines
logical_start = []
logical_end = []
last_newline = True
sio = StringIO(''.join(self.source))
parens = 0
for t in tokenize.generate_tokens(sio.readline):
if t[0] in [tokenize.COMMENT, tokenize.DEDENT,
tokenize.INDENT, tokenize.NL,
tokenize.ENDMARKER]:
continue
if not parens and t[0] in [
tokenize.NEWLINE, tokenize.SEMI
]:
last_newline = True
logical_end.append((t[3][0] - 1, t[2][1]))
continue
if last_newline and not parens:
logical_start.append((t[2][0] - 1, t[2][1]))
last_newline = False
if t[0] == tokenize.OP:
if t[1] in '([{':
parens += 1
elif t[1] in '}])':
parens -= 1
return (logical_start, logical_end)
def _get_logical(self, result):
"""Return the logical line corresponding to the result.
Assumes input is already E702-clean.
"""
try:
(logical_start, logical_end) = self._find_logical()
except (SyntaxError, tokenize.TokenError):
return None
row = result['line'] - 1
col = result['column'] - 1
ls = None
le = None
for i in range(0, len(logical_start), 1):
x = logical_end[i]
if x[0] > row or (x[0] == row and x[1] > col):
le = x
ls = logical_start[i]
break
if ls is None:
return None
original = self.source[ls[0]:le[0] + 1]
return ls, le, original
def _fix_reindent(self, result, logical):
"""Fix a badly indented line.
This is done by adding or removing from its initial indent only.
"""
assert logical
ls, _, original = logical
rewrapper = Wrapper(original)
valid_indents = rewrapper.pep8_expected()
if not rewrapper.rel_indent:
return [] # pragma: no cover
if result['line'] > ls[0]:
# got a valid continuation line number from pep8
row = result['line'] - ls[0] - 1
# always pick the first option for this
valid = valid_indents[row]
got = rewrapper.rel_indent[row]
else:
return [] # pragma: no cover
line = ls[0] + row
# always pick the expected indent, for now.
indent_to = valid[0]
if got != indent_to:
orig_line = self.source[line]
new_line = ' ' * (indent_to) + orig_line.lstrip()
if new_line == orig_line:
return []
else:
self.source[line] = new_line
return [line + 1] # Line indexed at 1
else:
return [] # pragma: no cover
def fix_e121(self, result, logical):
"""Fix indentation to be a multiple of four."""
# Fix by adjusting initial indent level.
return self._fix_reindent(result, logical)
def fix_e122(self, result, logical):
"""Add absent indentation for hanging indentation."""
# Fix by adding an initial indent.
return self._fix_reindent(result, logical)
def fix_e123(self, result, logical):
"""Align closing bracket to match opening bracket."""
# Fix by deleting whitespace to the correct level.
assert logical
logical_lines = logical[2]
line_index = result['line'] - 1
original_line = self.source[line_index]
fixed_line = (_get_indentation(logical_lines[0]) +
original_line.lstrip())
if fixed_line == original_line:
# Fall back to slower method.
return self._fix_reindent(result, logical)
else:
self.source[line_index] = fixed_line
def fix_e124(self, result, logical):
"""Align closing bracket to match visual indentation."""
# Fix by inserting whitespace before the closing bracket.
return self._fix_reindent(result, logical)
def fix_e125(self, result, logical):
"""Indent to distinguish line from next logical line."""
# Fix by indenting the line in error to the next stop.
modified_lines = self._fix_reindent(result, logical)
if modified_lines:
return modified_lines
else:
# Fallback
line_index = result['line'] - 1
original_line = self.source[line_index]
self.source[line_index] = self.indent_word + original_line
def fix_e126(self, result, logical):
"""Fix over-indented hanging indentation."""
# fix by deleting whitespace to the left
assert logical
logical_lines = logical[2]
line_index = result['line'] - 1
original = self.source[line_index]
fixed = (_get_indentation(logical_lines[0]) +
self.indent_word + original.lstrip())
if fixed == original:
# Fall back to slower method.
return self._fix_reindent(result, logical) # pragma: no cover
else:
self.source[line_index] = fixed
def fix_e127(self, result, logical):
"""Fix visual indentation."""
# Fix by inserting/deleting whitespace to the correct level.
modified_lines = self._align_visual_indent(result, logical)
if modified_lines != []:
return modified_lines
else:
# Fall back to slower method.
return self._fix_reindent(result, logical)
def _align_visual_indent(self, result, logical):
"""Correct visual indent.
This includes over (E127) and under (E128) indented lines.
"""
assert logical
logical_lines = logical[2]
line_index = result['line'] - 1
original = self.source[line_index]
fixed = original
if logical_lines[0].rstrip().endswith('\\'):
fixed = (_get_indentation(logical_lines[0]) +
self.indent_word + original.lstrip())
else:
start_index = None
for symbol in '([{':
if symbol in logical_lines[0]:
found_index = logical_lines[0].find(symbol)
if start_index is None:
start_index = found_index
else:
start_index = min(start_index, found_index)
if start_index is not None:
fixed = start_index * ' ' + original.lstrip()
if fixed == original:
return []
else:
self.source[line_index] = fixed
def fix_e201(self, result):
"""Remove extraneous whitespace."""
line_index = result['line'] - 1
target = self.source[line_index]
offset = result['column'] - 1
# When multiline strings are involved, pep8 reports the error as
# being at the start of the multiline string, which doesn't work
# for us.
if ('"""' in target or
"'''" in target or
target.rstrip().endswith('\\')):
return []
fixed = fix_whitespace(target,
offset=offset,
replacement='')
if fixed == target:
return []
else:
self.source[line_index] = fixed
def fix_e224(self, result):
"""Remove extraneous whitespace around operator."""
target = self.source[result['line'] - 1]
offset = result['column'] - 1
fixed = target[:offset] + target[offset:].replace('\t', ' ')
self.source[result['line'] - 1] = fixed
def fix_e225(self, result):
"""Fix missing whitespace around operator."""
target = self.source[result['line'] - 1]
offset = result['column'] - 1
fixed = target[:offset] + ' ' + target[offset:]
# Only proceed if non-whitespace characters match.
# And make sure we don't break the indentation.
if (fixed.replace(' ', '') == target.replace(' ', '') and
_get_indentation(fixed) == _get_indentation(target)):
self.source[result['line'] - 1] = fixed
else:
return []
def fix_e231(self, result):
"""Add missing whitespace."""
# Optimize for comma case. This will fix all commas in the full source
# code in one pass.
if ',' in result['info']:
original = ''.join(self.source)
new = refactor(original, ['ws_comma'])
if original.strip() != new.strip():
self.source = [new]
return range(1, 1 + len(original))
line_index = result['line'] - 1
target = self.source[line_index]
offset = result['column']
fixed = target[:offset] + ' ' + target[offset:]
self.source[line_index] = fixed
def fix_e251(self, result):
"""Remove whitespace around parameter '=' sign."""
line_index = result['line'] - 1
target = self.source[line_index]
# This is necessary since pep8 sometimes reports columns that goes
# past the end of the physical line. This happens in cases like,
# foo(bar\n=None)
c = min(result['column'] - 1,
len(target) - 1)
if target[c].strip():
fixed = target
else:
fixed = target[:c].rstrip() + target[c:].lstrip()
# There could be an escaped newline
#
# def foo(a=\
# 1)
if (fixed.endswith('=\\\n') or
fixed.endswith('=\\\r\n') or
fixed.endswith('=\\\r')):
self.source[line_index] = fixed.rstrip('\n\r \t\\')
self.source[line_index + 1] = self.source[line_index + 1].lstrip()
return [line_index + 1, line_index + 2] # Line indexed at 1
self.source[result['line'] - 1] = fixed
def fix_e262(self, result):
"""Fix spacing after comment hash."""
target = self.source[result['line'] - 1]
offset = result['column']
code = target[:offset].rstrip(' \t#')
comment = target[offset:].lstrip(' \t#')
fixed = code + (' # ' + comment if comment.strip()
else self.newline)
self.source[result['line'] - 1] = fixed
def fix_e271(self, result):
"""Fix extraneous whitespace around keywords."""
line_index = result['line'] - 1
target = self.source[line_index]
offset = result['column'] - 1
# When multiline strings are involved, pep8 reports the error as
# being at the start of the multiline string, which doesn't work
# for us.
if ('"""' in target or
"'''" in target or
target.rstrip().endswith('\\')):
return []
fixed = fix_whitespace(target,
offset=offset,
replacement=' ')
if fixed == target:
return []
else:
self.source[line_index] = fixed
def fix_e301(self, result):
"""Add missing blank line."""
cr = self.newline
self.source[result['line'] - 1] = cr + self.source[result['line'] - 1]
def fix_e302(self, result):
"""Add missing 2 blank lines."""
add_linenum = 2 - int(result['info'].split()[-1])
cr = self.newline * add_linenum
self.source[result['line'] - 1] = cr + self.source[result['line'] - 1]
def fix_e303(self, result):
"""Remove extra blank lines."""
delete_linenum = int(result['info'].split('(')[1].split(')')[0]) - 2
delete_linenum = max(1, delete_linenum)
# We need to count because pep8 reports an offset line number if there
# are comments.
cnt = 0
line = result['line'] - 2
modified_lines = []
while cnt < delete_linenum and line >= 0:
if not self.source[line].strip():
self.source[line] = ''
modified_lines.append(1 + line) # Line indexed at 1
cnt += 1
line -= 1
return modified_lines
def fix_e304(self, result):
"""Remove blank line following function decorator."""
line = result['line'] - 2
if not self.source[line].strip():
self.source[line] = ''
def fix_e401(self, result):
"""Put imports on separate lines."""
line_index = result['line'] - 1
target = self.source[line_index]
offset = result['column'] - 1
if not target.lstrip().startswith('import'):
return []
# pep8 (1.3.1) reports false positive if there is an import statement
# followed by a semicolon and some unrelated statement with commas in
# it.
if ';' in target:
return []
indentation = re.split(pattern=r'\bimport\b',
string=target, maxsplit=1)[0]
fixed = (target[:offset].rstrip('\t ,') + self.newline +
indentation + 'import ' + target[offset:].lstrip('\t ,'))
self.source[line_index] = fixed
def fix_e501(self, result):
"""Try to make lines fit within --max-line-length characters."""
line_index = result['line'] - 1
target = self.source[line_index]
if target.lstrip().startswith('#'):
# Shorten comment if it is the last comment line.
try:
if self.source[line_index + 1].lstrip().startswith('#'):
return []
except IndexError:
pass
# Wrap commented lines.
fixed = shorten_comment(
line=target,
newline=self.newline,
max_line_length=self.options.max_line_length)
if fixed == self.source[line_index]:
return []
else:
self.source[line_index] = fixed
return
indent = _get_indentation(target)
source = target[len(indent):]
assert source.lstrip() == source
sio = StringIO(source)
# Check for multiline string.
try:
tokens = list(tokenize.generate_tokens(sio.readline))
except (SyntaxError, tokenize.TokenError):
multiline_candidate = break_multiline(
target, newline=self.newline,
indent_word=self.indent_word)
if multiline_candidate:
self.source[line_index] = multiline_candidate
return
else:
return []
candidates = shorten_line(
tokens, source, indent,
self.indent_word, newline=self.newline,
aggressive=self.options.aggressive)
candidates = list(sorted(
set(candidates),
key=lambda x: line_shortening_rank(x,
self.newline,
self.indent_word)))
if self.options.verbose >= 4:
print(('-' * 79 + '\n').join([''] + candidates + ['']),
file=codecs.getwriter('utf-8')(sys.stderr.buffer
if hasattr(sys.stderr,
'buffer')
else sys.stderr))
for _candidate in candidates:
assert _candidate is not None
if (get_longest_length(_candidate, self.newline) >=
get_longest_length(target, self.newline)):
continue
self.source[line_index] = _candidate
return
return []
def fix_e502(self, result):
"""Remove extraneous escape of newline."""
line_index = result['line'] - 1
target = self.source[line_index]
self.source[line_index] = target.rstrip('\n\r \t\\') + self.newline
def fix_e701(self, result):
"""Put colon-separated compound statement on separate lines."""
line_index = result['line'] - 1
target = self.source[line_index]
c = result['column']
fixed_source = (target[:c] + self.newline +
_get_indentation(target) + self.indent_word +
target[c:].lstrip('\n\r \t\\'))
self.source[result['line'] - 1] = fixed_source
def fix_e702(self, result, logical):
"""Put semicolon-separated compound statement on separate lines."""
logical_lines = logical[2]
line_index = result['line'] - 1
target = self.source[line_index]
if target.rstrip().endswith('\\'):
# Normalize '1; \\\n2' into '1; 2'.
self.source[line_index] = target.rstrip('\n \r\t\\')
self.source[line_index + 1] = self.source[line_index + 1].lstrip()
return [line_index + 1, line_index + 2]
if target.rstrip().endswith(';'):
self.source[line_index] = target.rstrip('\n \r\t;') + self.newline
return
offset = result['column'] - 1
first = target[:offset].rstrip(';').rstrip()
second = (_get_indentation(logical_lines[0]) +
target[offset:].lstrip(';').lstrip())
self.source[line_index] = first + self.newline + second
def fix_e711(self, result):
"""Fix comparison with None."""
line_index = result['line'] - 1
target = self.source[line_index]
offset = result['column'] - 1
right_offset = offset + 2
if right_offset >= len(target):
return []
left = target[:offset].rstrip()
center = target[offset:right_offset]
right = target[right_offset:].lstrip()
if not right.startswith('None'):
return []
if center.strip() == '==':
new_center = 'is'
elif center.strip() == '!=':
new_center = 'is not'
else:
return []
self.source[line_index] = ' '.join([left, new_center, right])
def fix_e712(self, result):
"""Fix comparison with boolean."""
line_index = result['line'] - 1
target = self.source[line_index]
offset = result['column'] - 1
right_offset = offset + 2
if right_offset >= len(target):
return []
left = target[:offset].rstrip()
center = target[offset:right_offset]
right = target[right_offset:].lstrip()
# Handle simple cases only.
new_right = None
if center.strip() == '==':
if re.match(r'\bTrue\b', right):
new_right = re.sub(r'\bTrue\b *', '', right, count=1)
elif center.strip() == '!=':
if re.match(r'\bFalse\b', right):
new_right = re.sub(r'\bFalse\b *', '', right, count=1)
if new_right is None:
return []
if new_right[0].isalnum():
new_right = ' ' + new_right
self.source[line_index] = left + new_right
def fix_w291(self, result):
"""Remove trailing whitespace."""
fixed_line = self.source[result['line'] - 1].rstrip()
self.source[result['line'] - 1] = '%s%s' % (fixed_line, self.newline)
def fix_w293(self, result):
"""Remove trailing whitespace on blank line."""
assert not self.source[result['line'] - 1].strip()
self.source[result['line'] - 1] = self.newline
def fix_w391(self, _):
"""Remove trailing blank lines."""
blank_count = 0
for line in reversed(self.source):
line = line.rstrip()
if line:
break
else:
blank_count += 1
original_length = len(self.source)
self.source = self.source[:original_length - blank_count]
return range(1, 1 + original_length)
def fix_e26(source):
"""Format block comments."""
if '#' not in source:
# Optimization.
return source
string_line_numbers = multiline_string_lines(source,
include_docstrings=True)
fixed_lines = []
sio = StringIO(source)
for (line_number, line) in enumerate(sio.readlines(), start=1):
if (line.lstrip().startswith('#') and
line_number not in string_line_numbers):
indentation = _get_indentation(line)
line = line.lstrip()
# Normalize beginning if not a shebang.
if len(line) > 1:
# Leave multiple spaces like '# ' alone.
if line.count('#') > 1 or line[1].isalnum():
line = '# ' + line.lstrip('# \t')
fixed_lines.append(indentation + line)
else:
fixed_lines.append(line)
return ''.join(fixed_lines)
def refactor(source, fixer_names, ignore=None):
"""Return refactored code using lib2to3.
Skip if ignore string is produced in the refactored code.
"""
from lib2to3 import pgen2
try:
new_text = refactor_with_2to3(source,
fixer_names=fixer_names)
except (pgen2.parse.ParseError,
SyntaxError,
UnicodeDecodeError,
UnicodeEncodeError):
return source
if ignore:
if ignore in new_text and ignore not in source:
return source
return new_text
def fix_w602(source):
"""Fix deprecated form of raising exception."""
return refactor(source, ['raise'],
ignore='with_traceback')
def fix_w6(source):
"""Fix various deprecated code (via lib2to3)."""
return refactor(source,
['apply',
'except',
'exec',
'execfile',
'exitfunc',
'has_key',
'idioms',
'import',
'methodattrs', # Python >= 2.6
'ne',
'numliterals',
'operator',
'paren',
'reduce',
'renames',
'repr',
'standarderror',
'sys_exc',
'throw',
'tuple_params',
'types',
'xreadlines'])
def find_newline(source):
"""Return type of newline used in source."""
cr, lf, crlf = 0, 0, 0
for s in source:
if s.endswith(CRLF):
crlf += 1
elif s.endswith(CR):
cr += 1
elif s.endswith(LF):
lf += 1
_max = max(lf, cr, crlf)
if _max == lf:
return LF
elif _max == crlf:
return CRLF
else:
return CR
def _get_indentword(source):
"""Return indentation type."""
sio = StringIO(source)
indent_word = ' ' # Default in case source has no indentation
try:
for t in tokenize.generate_tokens(sio.readline):
if t[0] == token.INDENT:
indent_word = t[1]
break
except (SyntaxError, tokenize.TokenError):
pass
return indent_word
def _get_indentation(line):
"""Return leading whitespace."""
if line.strip():
non_whitespace_index = len(line) - len(line.lstrip())
return line[:non_whitespace_index]
else:
return ''
def get_diff_text(old, new, filename):
"""Return text of unified diff between old and new."""
newline = '\n'
diff = difflib.unified_diff(
old, new,
'original/' + filename,
'fixed/' + filename,
lineterm=newline)
text = ''
for line in diff:
text += line
# Work around missing newline (http://bugs.python.org/issue2142).
if not line.endswith(newline):
text += newline + r'\ No newline at end of file' + newline
return text
def _priority_key(pep8_result):
"""Key for sorting PEP8 results.
Global fixes should be done first. This is important for things
like indentation.
"""
priority = [
# Global fixes.
'e101', 'e111', 'w191',
# Fix multiline colon-based before semicolon based.
'e701',
# Break multiline statements early.
'e702',
# Things that make lines longer.
'e225', 'e231',
# Remove extraneous whitespace before breaking lines.
'e201',
# Before breaking lines.
'e121', 'e122', 'e123', 'e124', 'e125', 'e126', 'e127', 'e128', 'e129',
]
key = pep8_result['id'].lower()
if key in priority:
return priority.index(key)
else:
# Lowest priority
return len(priority)
def shorten_line(tokens, source, indentation, indent_word, newline,
aggressive=False):
"""Separate line at OPERATOR.
Multiple candidates will be yielded.
"""
for candidate in _shorten_line(tokens=tokens,
source=source,
indentation=indentation,
indent_word=indent_word,
newline=newline,
aggressive=aggressive):
yield candidate
if aggressive:
for key_token_strings in SHORTEN_OPERATOR_GROUPS:
shortened = _shorten_line_at_tokens(
tokens=tokens,
source=source,
indentation=indentation,
indent_word=indent_word,
newline=newline,
key_token_strings=key_token_strings,
aggressive=aggressive)
if shortened is not None and shortened != source:
yield shortened
def _shorten_line(tokens, source, indentation, indent_word, newline,
aggressive=False):
"""Separate line at OPERATOR.
Multiple candidates will be yielded.
"""
for tkn in tokens:
# Don't break on '=' after keyword as this violates PEP 8.
if token.OP == tkn[0] and tkn[1] != '=':
assert tkn[0] != token.INDENT
offset = tkn[2][1] + 1
first = source[:offset]
second_indent = indentation
if first.rstrip().endswith('('):
second_indent += indent_word
elif '(' in first:
second_indent += ' ' * (1 + first.find('('))
else:
second_indent += indent_word
second = (second_indent + source[offset:].lstrip())
if not second.strip():
continue
# Do not begin a line with a comma
if second.lstrip().startswith(','):
continue
# Do end a line with a dot
if first.rstrip().endswith('.'):
continue
if tkn[1] in '+-*/':
fixed = first + ' \\' + newline + second
else:
fixed = first + newline + second
# Only fix if syntax is okay.
if check_syntax(normalize_multiline(fixed, newline=newline)
if aggressive else fixed):
yield indentation + fixed
def _shorten_line_at_tokens(tokens, source, indentation, indent_word, newline,
key_token_strings, aggressive):
"""Separate line by breaking at tokens in key_token_strings.
This will always break the line at the first parenthesis.
"""
offsets = []
first_paren = True
for tkn in tokens:
token_type = tkn[0]
token_string = tkn[1]
next_offset = tkn[2][1] + 1
assert token_type != token.INDENT
if token_string in key_token_strings or (first_paren and
token_string == '('):
# Don't split right before newline.
if next_offset < len(source) - 1:
offsets.append(next_offset)
if token_string == '(':
first_paren = False
current_indent = None
fixed = None
for line in split_at_offsets(source, offsets):
if fixed:
fixed += newline + current_indent + line
for symbol in '([{':
if line.endswith(symbol):
current_indent += indent_word
else:
# First line.
fixed = line
assert not current_indent
current_indent = indent_word
assert fixed is not None
if check_syntax(normalize_multiline(fixed, newline=newline)
if aggressive > 1 else fixed):
return indentation + fixed
else:
return None
def normalize_multiline(line, newline):
"""Remove multiline-related code that will cause syntax error.
This is for purposes of checking syntax.
"""
for quote in '\'"':
dict_pattern = r'^{q}[^{q}]*{q} *: *'.format(q=quote)
if re.match(dict_pattern, line):
if not line.strip().endswith('}'):
line += '}'
return '{' + line
if line.startswith('def ') and line.rstrip().endswith(':'):
# Do not allow ':' to be alone. That is invalid.
split_line = [item.strip() for item in line.split(newline)]
if ':' not in split_line and 'def' not in split_line:
return line[len('def'):].strip().rstrip(':')
return line
def fix_whitespace(line, offset, replacement):
"""Replace whitespace at offset and return fixed line."""
# Replace escaped newlines too
left = line[:offset].rstrip('\n\r \t\\')
right = line[offset:].lstrip('\n\r \t\\')
if right.startswith('#'):
return line
else:
return left + replacement + right
def _execute_pep8(pep8_options, source):
"""Execute pep8 via python method calls."""
class QuietReport(pep8.BaseReport):
"""Version of checker that does not print."""
def __init__(self, options):
super(QuietReport, self).__init__(options)
self.__full_error_results = []
def error(self, line_number, offset, text, _):
"""Collect errors."""
code = super(QuietReport, self).error(line_number, offset, text, _)
if code:
self.__full_error_results.append(
{'id': code,
'line': line_number,
'column': offset + 1,
'info': text})
def full_error_results(self):
"""Return error results in detail.
Results are in the form of a list of dictionaries. Each dictionary
contains 'id', 'line', 'column', and 'info'.
"""
return self.__full_error_results
checker = pep8.Checker('', lines=source,
reporter=QuietReport, **pep8_options)
checker.check_all()
return checker.report.full_error_results()
class Reindenter(object):
"""Reindents badly-indented code to uniformly use four-space indentation.
Released to the public domain, by Tim Peters, 03 October 2000.
"""
def __init__(self, input_text, newline):
self.newline = newline
# Raw file lines.
self.raw = input_text
self.after = None
self.string_content_line_numbers = multiline_string_lines(
''.join(self.raw))
# File lines, rstripped & tab-expanded. Dummy at start is so
# that we can use tokenize's 1-based line numbering easily.
# Note that a line is all-blank iff it is a newline.
self.lines = []
for line_number, line in enumerate(self.raw, start=1):
# Do not modify if inside a multiline string.
if line_number in self.string_content_line_numbers:
self.lines.append(line)
else:
# Only expand leading tabs.
self.lines.append(_get_indentation(line).expandtabs() +
line.strip() + newline)
self.lines.insert(0, None)
self.index = 1 # index into self.lines of next line
def run(self):
"""Fix indentation and return modified line numbers.
Line numbers are indexed at 1.
"""
try:
stats = reindent_stats(tokenize.generate_tokens(self.getline))
except (SyntaxError, tokenize.TokenError):
return set()
# Remove trailing empty lines.
lines = self.lines
while lines and lines[-1] == self.newline:
lines.pop()
# Sentinel.
stats.append((len(lines), 0))
# Map count of leading spaces to # we want.
have2want = {}
# Program after transformation.
after = self.after = []
# Copy over initial empty lines -- there's nothing to do until
# we see a line with *something* on it.
i = stats[0][0]
after.extend(lines[1:i])
for i in range(len(stats) - 1):
thisstmt, thislevel = stats[i]
nextstmt = stats[i + 1][0]
have = _leading_space_count(lines[thisstmt])
want = thislevel * 4
if want < 0:
# A comment line.
if have:
# An indented comment line. If we saw the same
# indentation before, reuse what it most recently
# mapped to.
want = have2want.get(have, -1)
if want < 0:
# Then it probably belongs to the next real stmt.
for j in range(i + 1, len(stats) - 1):
jline, jlevel = stats[j]
if jlevel >= 0:
if have == _leading_space_count(lines[jline]):
want = jlevel * 4
break
if want < 0: # Maybe it's a hanging
# comment like this one,
# in which case we should shift it like its base
# line got shifted.
for j in range(i - 1, -1, -1):
jline, jlevel = stats[j]
if jlevel >= 0:
want = (have + _leading_space_count(
after[jline - 1]) -
_leading_space_count(lines[jline]))
break
if want < 0:
# Still no luck -- leave it alone.
want = have
else:
want = 0
assert want >= 0
have2want[have] = want
diff = want - have
if diff == 0 or have == 0:
after.extend(lines[thisstmt:nextstmt])
else:
for line_number, line in enumerate(lines[thisstmt:nextstmt],
start=thisstmt):
if line_number in self.string_content_line_numbers:
after.append(line)
elif diff > 0:
if line == self.newline:
after.append(line)
else:
after.append(' ' * diff + line)
else:
remove = min(_leading_space_count(line), -diff)
after.append(line[remove:])
if self.raw == self.after:
return set()
else:
return (set(range(1, 1 + len(self.raw))) -
self.string_content_line_numbers)
def fixed_lines(self):
return self.after
def getline(self):
"""Line-getter for tokenize."""
if self.index >= len(self.lines):
line = ''
else:
line = self.lines[self.index]
self.index += 1
return line
def reindent_stats(tokens):
"""Return list of (lineno, indentlevel) pairs.
One for each stmt and comment line. indentlevel is -1 for comment lines, as
a signal that tokenize doesn't know what to do about them; indeed, they're
our headache!
"""
find_stmt = 1 # next token begins a fresh stmt?
level = 0 # current indent level
stats = []
for t in tokens:
token_type = t[0]
sline = t[2][0]
line = t[4]
if token_type == tokenize.NEWLINE:
# A program statement, or ENDMARKER, will eventually follow,
# after some (possibly empty) run of tokens of the form
# (NL | COMMENT)* (INDENT | DEDENT+)?
find_stmt = 1
elif token_type == tokenize.INDENT:
find_stmt = 1
level += 1
elif token_type == tokenize.DEDENT:
find_stmt = 1
level -= 1
elif token_type == tokenize.COMMENT:
if find_stmt:
stats.append((sline, -1))
# but we're still looking for a new stmt, so leave
# find_stmt alone
elif token_type == tokenize.NL:
pass
elif find_stmt:
# This is the first "real token" following a NEWLINE, so it
# must be the first token of the next program statement, or an
# ENDMARKER.
find_stmt = 0
if line: # not endmarker
stats.append((sline, level))
return stats
class Wrapper(object):
"""Class for functions relating to continuation lines and line folding.
Each instance operates on a single logical line.
"""
SKIP_TOKENS = frozenset([
tokenize.COMMENT, tokenize.NL, tokenize.INDENT,
tokenize.DEDENT, tokenize.NEWLINE, tokenize.ENDMARKER
])
def __init__(self, physical_lines):
self.lines = physical_lines
self.tokens = []
self.rel_indent = None
sio = StringIO(''.join(physical_lines))
for t in tokenize.generate_tokens(sio.readline):
if not len(self.tokens) and t[0] in self.SKIP_TOKENS:
continue
if t[0] != tokenize.ENDMARKER:
self.tokens.append(t)
self.logical_line = self.build_tokens_logical(self.tokens)
def build_tokens_logical(self, tokens):
"""Build a logical line from a list of tokens.
Return the logical line and a list of (offset, token) tuples. Does
not mute strings like the version in pep8.py.
"""
# from pep8.py with minor modifications
logical = []
previous = None
for t in tokens:
token_type, text = t[0:2]
if token_type in self.SKIP_TOKENS:
continue
if previous:
end_line, end = previous[3]
start_line, start = t[2]
if end_line != start_line: # different row
prev_text = self.lines[end_line - 1][end - 1]
if prev_text == ',' or (prev_text not in '{[('
and text not in '}])'):
logical.append(' ')
elif end != start: # different column
fill = self.lines[end_line - 1][end:start]
logical.append(fill)
logical.append(text)
previous = t
logical_line = ''.join(logical)
assert logical_line.lstrip() == logical_line
assert logical_line.rstrip() == logical_line
return logical_line
def pep8_expected(self):
"""Replicate logic in pep8.py, to know what level to indent things to.
Return a list of lists; each list represents valid indent levels for
the line in question, relative from the initial indent. However, the
first entry is the indent level which was expected.
"""
# What follows is an adjusted version of
# pep8.py:continuation_line_indentation. All of the comments have been
# stripped and the 'yield' statements replaced with 'pass'.
if not self.tokens:
return # pragma: no cover
first_row = self.tokens[0][2][0]
nrows = 1 + self.tokens[-1][2][0] - first_row
# here are the return values
valid_indents = [list()] * nrows
indent_level = self.tokens[0][2][1]
valid_indents[0].append(indent_level)
if nrows == 1:
# bug, really.
return valid_indents # pragma: no cover
indent_next = self.logical_line.endswith(':')
row = depth = 0
parens = [0] * nrows
self.rel_indent = rel_indent = [0] * nrows
indent = [indent_level]
indent_chances = {}
last_indent = (0, 0)
last_token_multiline = None
for token_type, text, start, end, _ in self.tokens:
newline = row < start[0] - first_row
if newline:
row = start[0] - first_row
newline = (not last_token_multiline and
token_type not in (tokenize.NL, tokenize.NEWLINE))
if newline:
# This is where the differences start. Instead of looking at
# the line and determining whether the observed indent matches
# our expectations, we decide which type of indentation is in
# use at the given indent level, and return the offset. This
# algorithm is susceptible to "carried errors", but should
# through repeated runs eventually solve indentation for
# multiline expressions less than PEP8_PASSES_MAX lines long.
if depth:
for open_row in range(row - 1, -1, -1):
if parens[open_row]:
break
else:
open_row = 0
# That's all we get to work with. This code attempts to
# "reverse" the below logic, and place into the valid indents
# list
vi = []
add_second_chances = False
if token_type == tokenize.OP and text in ']})':
# this line starts with a closing bracket, so it needs to
# be closed at the same indent as the opening one.
if indent[depth]:
# hanging indent
vi.append(indent[depth])
else:
# visual indent
vi.append(indent_level + rel_indent[open_row])
elif depth and indent[depth]:
# visual indent was previously confirmed.
vi.append(indent[depth])
add_second_chances = True
elif depth and True in indent_chances.values():
# visual indent happened before, so stick to
# visual indent this time.
if depth > 1 and indent[depth - 1]:
vi.append(indent[depth - 1])
else:
# stupid fallback
vi.append(indent_level + 4)
add_second_chances = True
elif not depth:
vi.append(indent_level + 4)
else:
# must be in hanging indent
hang = rel_indent[open_row] + 4
vi.append(indent_level + hang)
# about the best we can do without look-ahead
if (indent_next and vi[0] == indent_level + 4 and
nrows == row + 1):
vi[0] += 4
if add_second_chances:
# visual indenters like to line things up.
min_indent = vi[0]
for col, what in indent_chances.items():
if col > min_indent and (
what is True or
(what == str and token_type == tokenize.STRING) or
(what == text and token_type == tokenize.OP)
):
vi.append(col)
vi = sorted(vi)
valid_indents[row] = vi
# Returning to original continuation_line_indentation() from
# pep8.
visual_indent = indent_chances.get(start[1])
last_indent = start
rel_indent[row] = start[1] - indent_level
hang = rel_indent[row] - rel_indent[open_row]
if token_type == tokenize.OP and text in ']})':
pass
elif visual_indent is True:
if not indent[depth]:
indent[depth] = start[1]
# line altered: comments shouldn't define a visual indent
if parens[row] and not indent[depth] and token_type not in (
tokenize.NL, tokenize.COMMENT
):
indent[depth] = start[1]
indent_chances[start[1]] = True
elif token_type == tokenize.STRING or text in (
'u', 'ur', 'b', 'br'
):
indent_chances[start[1]] = str
if token_type == tokenize.OP:
if text in '([{':
depth += 1
indent.append(0)
parens[row] += 1
elif text in ')]}' and depth > 0:
prev_indent = indent.pop() or last_indent[1]
for d in range(depth):
if indent[d] > prev_indent:
indent[d] = 0
for ind in list(indent_chances):
if ind >= prev_indent:
del indent_chances[ind]
depth -= 1
if depth and indent[depth]: # modified
indent_chances[indent[depth]] = True
for idx in range(row, -1, -1):
if parens[idx]:
parens[idx] -= 1
break
assert len(indent) == depth + 1
if start[1] not in indent_chances:
indent_chances[start[1]] = text
last_token_multiline = (start[0] != end[0])
return valid_indents
def _leading_space_count(line):
"""Return number of leading spaces in line."""
i = 0
while i < len(line) and line[i] == ' ':
i += 1
return i
def refactor_with_2to3(source_text, fixer_names):
"""Use lib2to3 to refactor the source.
Return the refactored source code.
"""
from lib2to3.refactor import RefactoringTool
fixers = ['lib2to3.fixes.fix_' + name for name in fixer_names]
tool = RefactoringTool(fixer_names=fixers, explicit=fixers)
return unicode(tool.refactor_string(source_text, name=''))
def break_multiline(source_text, newline, indent_word):
"""Break first line of multiline code.
Return None if a break is not possible.
"""
indentation = _get_indentation(source_text)
# Handle special case only.
for symbol in '([{':
# Only valid if symbol is not on a line by itself.
if (symbol in source_text and not source_text.strip() == symbol):
if not source_text.rstrip()[-1] == ',':
continue
index = 1 + source_text.find(symbol)
if index <= len(indent_word) + len(indentation):
continue
if is_probably_inside_string_or_comment(source_text, index - 1):
continue
return (
source_text[:index].rstrip() + newline +
indentation + indent_word +
source_text[index:].lstrip())
return None
def is_probably_inside_string_or_comment(line, index):
"""Return True if index may be inside a string or comment."""
# Make sure we are not in a string.
for quote in ['"', "'"]:
if quote in line:
if line.find(quote) <= index:
return True
# Make sure we are not in a comment.
if '#' in line:
if line.find('#') <= index:
return True
return False
def check_syntax(code):
"""Return True if syntax is okay."""
try:
return compile(code, '<string>', 'exec')
except (SyntaxError, TypeError, UnicodeDecodeError):
return False
def filter_results(source, results, aggressive=False):
"""Filter out spurious reports from pep8.
If aggressive is True, we allow possibly unsafe fixes (E711, E712).
"""
non_docstring_string_line_numbers = multiline_string_lines(
source, include_docstrings=False)
all_string_line_numbers = multiline_string_lines(
source, include_docstrings=True)
split_source = [None] + source.splitlines()
for r in results:
issue_id = r['id'].lower()
if r['line'] in non_docstring_string_line_numbers:
if issue_id.startswith('e1'):
continue
elif issue_id in ['e501', 'w191']:
continue
if r['line'] in all_string_line_numbers:
if issue_id in ['e501']:
continue
# Filter out incorrect E101 reports when there are no tabs.
# pep8 will complain about this even if the tab indentation found
# elsewhere is in a multiline string.
if issue_id == 'e101' and '\t' not in split_source[r['line']]:
continue
if issue_id in ['e711', 'e712'] and not aggressive:
continue
yield r
def multiline_string_lines(source, include_docstrings=False):
"""Return line numbers that are within multiline strings.
The line numbers are indexed at 1.
Docstrings are ignored.
"""
sio = StringIO(source)
line_numbers = set()
previous_token_type = ''
try:
for t in tokenize.generate_tokens(sio.readline):
token_type = t[0]
start_row = t[2][0]
end_row = t[3][0]
start_row = t[2][0]
end_row = t[3][0]
if (token_type == tokenize.STRING and start_row != end_row):
if (include_docstrings or
previous_token_type != tokenize.INDENT):
# We increment by one since we want the contents of the
# string.
line_numbers |= set(range(1 + start_row, 1 + end_row))
previous_token_type = token_type
except (SyntaxError, tokenize.TokenError):
pass
return line_numbers
def shorten_comment(line, newline, max_line_length):
"""Return trimmed or split long comment line."""
assert len(line) > max_line_length
line = line.rstrip()
# PEP 8 recommends 72 characters for comment text.
indentation = _get_indentation(line) + '# '
max_line_length = min(max_line_length,
len(indentation) + 72)
MIN_CHARACTER_REPEAT = 5
if (len(line) - len(line.rstrip(line[-1])) >= MIN_CHARACTER_REPEAT and
not line[-1].isalnum()):
# Trim comments that end with things like ---------
return line[:max_line_length] + newline
elif re.match(r'\s*#+\s*\w+', line):
import textwrap
split_lines = textwrap.wrap(line.lstrip(' \t#'),
initial_indent=indentation,
subsequent_indent=indentation,
width=max_line_length,
break_long_words=False,
break_on_hyphens=False)
return newline.join(split_lines) + newline
else:
return line + newline
def normalize_line_endings(lines):
"""Return fixed line endings.
All lines will be modified to use the most common line ending.
"""
newline = find_newline(lines)
return [line.rstrip('\n\r') + newline for line in lines]
def mutual_startswith(a, b):
return b.startswith(a) or a.startswith(b)
def code_match(code, select, ignore):
if ignore:
for ignored_code in [c.strip() for c in ignore]:
if mutual_startswith(code.lower(), ignored_code.lower()):
return False
if select:
for selected_code in [c.strip() for c in select]:
if mutual_startswith(code.lower(), selected_code.lower()):
return True
return False
return True
def fix_string(source, options=None):
"""Return fixed source code."""
if not options:
options = parse_args([''])[0]
sio = StringIO(source)
return fix_lines(sio.readlines(), options=options)
def fix_lines(source_lines, options, filename=''):
"""Return fixed source code."""
tmp_source = ''.join(normalize_line_endings(source_lines))
# Keep a history to break out of cycles.
previous_hashes = set([hash(tmp_source)])
# Apply global fixes only once (for efficiency).
fixed_source = apply_global_fixes(tmp_source, options)
passes = 0
while True:
if options.pep8_passes >= 0 and passes > options.pep8_passes:
break
passes += 1
tmp_source = copy.copy(fixed_source)
fix = FixPEP8(filename, options, contents=tmp_source)
fixed_source = fix.fix()
if hash(fixed_source) in previous_hashes:
break
else:
previous_hashes.add(hash(fixed_source))
return fixed_source
def fix_file(filename, options=None, output=None):
if not options:
options = parse_args([filename])[0]
original_source = read_from_filename(filename, readlines=True)
fixed_source = original_source
if options.in_place or output:
encoding = detect_encoding(filename)
if output:
output = codecs.getwriter(encoding)(output.buffer
if hasattr(output, 'buffer')
else output)
output = LineEndingWrapper(output)
fixed_source = fix_lines(fixed_source, options, filename=filename)
if options.diff:
new = StringIO(fixed_source)
new = new.readlines()
diff = get_diff_text(original_source, new, filename)
if output:
output.write(diff)
output.flush()
else:
return diff
elif options.in_place:
fp = open_with_encoding(filename, encoding=encoding,
mode='w')
fp.write(fixed_source)
fp.close()
else:
if output:
output.write(fixed_source)
output.flush()
else:
return fixed_source
def global_fixes():
"""Yield multiple (code, function) tuples."""
for function in globals().values():
if inspect.isfunction(function):
arguments = inspect.getargspec(function)[0]
if arguments != ['source']:
continue
code = extract_code_from_function(function)
if code:
yield (code, function)
def apply_global_fixes(source, options):
"""Run global fixes on source code.
Thsese are fixes that only need be done once (unlike those in FixPEP8,
which are dependent on pep8).
"""
for (code, function) in global_fixes():
if code_match(code, select=options.select, ignore=options.ignore):
if options.verbose:
print('---> Applying global fix for {0}'.format(code.upper()),
file=sys.stderr)
source = function(source)
return source
def extract_code_from_function(function):
"""Return code handled by function."""
if not function.__name__.startswith('fix_'):
return None
code = re.sub('^fix_', '', function.__name__)
if not code:
return None
try:
int(code[1:])
except ValueError:
return None
return code
def parse_args(args):
"""Parse command-line options."""
parser = OptionParser(usage='Usage: autopep8 [options] '
'[filename [filename ...]]'
'\nUse filename \'-\' for stdin.',
version='%prog {0}'.format(__version__),
description=__doc__.split('\n')[0],
prog='autopep8')
parser.add_option('-v', '--verbose', action='count', dest='verbose',
default=0,
help='print verbose messages; '
'multiple -v result in more verbose messages')
parser.add_option('-d', '--diff', action='store_true', dest='diff',
help='print the diff for the fixed source')
parser.add_option('-i', '--in-place', action='store_true',
help='make changes to files in place')
parser.add_option('-r', '--recursive', action='store_true',
help='run recursively; must be used with --in-place or '
'--diff')
parser.add_option('-j', '--jobs', type=int, metavar='n', default=1,
help='number of parallel jobs; '
'match CPU count if value is less than 1')
parser.add_option('-p', '--pep8-passes', metavar='n',
default=-1, type=int,
help='maximum number of additional pep8 passes '
'(default: infinite)')
parser.add_option('-a', '--aggressive', action='count', default=0,
help='enable non-whitespace changes; '
'multiple -a result in more aggressive changes')
parser.add_option('--exclude', metavar='globs',
help='exclude files/directories that match these '
'comma-separated globs')
parser.add_option('--list-fixes', action='store_true',
help='list codes for fixes; '
'used by --ignore and --select')
parser.add_option('--ignore', metavar='errors', default='',
help='do not fix these errors/warnings '
'(default: {0})'.format(DEFAULT_IGNORE))
parser.add_option('--select', metavar='errors', default='',
help='fix only these errors/warnings (e.g. E4,W)')
parser.add_option('--max-line-length', metavar='n', default=79, type=int,
help='set maximum allowed line length '
'(default: %default)')
options, args = parser.parse_args(args)
if not len(args) and not options.list_fixes:
parser.error('incorrect number of arguments')
if '-' in args:
if len(args) > 1:
parser.error('cannot mix stdin and regular files')
if options.diff:
parser.error('--diff cannot be used with standard input')
if options.in_place:
parser.error('--in-place cannot be used with standard input')
if options.recursive:
parser.error('--recursive cannot be used with standard input')
if len(args) > 1 and not (options.in_place or options.diff):
parser.error('autopep8 only takes one filename as argument '
'unless the "--in-place" or "--diff" options are '
'used')
if options.recursive and not (options.in_place or options.diff):
parser.error('--recursive must be used with --in-place or --diff')
if options.exclude and not options.recursive:
parser.error('--exclude is only relevant when used with --recursive')
if options.in_place and options.diff:
parser.error('--in-place and --diff are mutually exclusive')
if options.max_line_length <= 0:
parser.error('--max-line-length must be greater than 0')
if options.select:
options.select = options.select.split(',')
if options.ignore:
options.ignore = options.ignore.split(',')
elif not options.select:
if options.aggressive:
# Enable everything by default if aggressive.
options.select = ['E', 'W']
else:
options.ignore = DEFAULT_IGNORE.split(',')
if options.exclude:
options.exclude = options.exclude.split(',')
else:
options.exclude = []
if options.jobs < 1:
# Do not import multiprocessing globally in case it is not supported
# on the platform.
import multiprocessing
options.jobs = multiprocessing.cpu_count()
if options.jobs > 1 and not options.in_place:
parser.error('parallel jobs requires --in-place')
return options, args
def supported_fixes():
"""Yield pep8 error codes that autopep8 fixes.
Each item we yield is a tuple of the code followed by its description.
"""
instance = FixPEP8(filename=None, options=None, contents='')
for attribute in dir(instance):
code = re.match('fix_([ew][0-9][0-9][0-9])', attribute)
if code:
yield (code.group(1).upper(),
re.sub(r'\s+', ' ',
getattr(instance, attribute).__doc__))
for (code, function) in sorted(global_fixes()):
yield (code.upper() + (4 - len(code)) * ' ',
re.sub(r'\s+', ' ', function.__doc__))
def line_shortening_rank(candidate, newline, indent_word):
"""Return rank of candidate.
This is for sorting candidates.
"""
rank = 0
if candidate.strip():
lines = candidate.split(newline)
offset = 0
if lines[0].rstrip()[-1] not in '([{':
for symbol in '([{':
offset = max(offset, 1 + lines[0].find(symbol))
max_length = max([offset + len(x.strip()) for x in lines])
rank += max_length
rank += len(lines)
bad_staring_symbol = {
'(': ')',
'[': ']',
'{': '}'}.get(lines[0][-1], None)
if len(lines) > 1:
if (bad_staring_symbol and
lines[1].lstrip().startswith(bad_staring_symbol)):
rank += 20
else:
rank -= 10
if lines[0].endswith('(['):
rank += 10
for current_line in lines:
for bad_start in ['.', '%', '+', '-', '/']:
if current_line.startswith(bad_start):
rank += 100
for ending in '([{':
# Avoid lonely opening. They result in longer lines.
if (current_line.endswith(ending) and
len(current_line.strip()) <= len(indent_word)):
rank += 100
if current_line.endswith('%'):
rank -= 20
# Try to break list comprehensions at the "for".
if current_line.lstrip().startswith('for'):
rank -= 50
rank += 10 * count_unbalanced_brackets(current_line)
else:
rank = 100000
return max(0, rank)
def count_unbalanced_brackets(line):
"""Return number of unmatched open/close brackets."""
count = 0
for opening, closing in ['()', '[]', '{}']:
count += abs(line.count(opening) - line.count(closing))
return count
def split_at_offsets(line, offsets):
"""Split line at offsets.
Return list of strings.
"""
result = []
previous_offset = 0
current_offset = 0
for current_offset in sorted(offsets):
if current_offset < len(line) and previous_offset != current_offset:
result.append(line[previous_offset:current_offset])
previous_offset = current_offset
result.append(line[current_offset:])
return result
def get_longest_length(text, newline):
"""Return length of longest line."""
return max([len(line) for line in text.split(newline)])
class LineEndingWrapper(object):
r"""Replace line endings to work with sys.stdout.
It seems that sys.stdout expects only '\n' as the line ending, no matter
the platform. Otherwise, we get repeated line endings.
"""
def __init__(self, output):
self.__output = output
def write(self, s):
self.__output.write(s.replace('\r\n', '\n').replace('\r', '\n'))
def flush(self):
self.__output.flush()
def match_file(filename, exclude):
"""Return True if file is okay for modifying/recursing."""
if os.path.basename(filename).startswith('.'):
return False
for pattern in exclude:
if fnmatch.fnmatch(filename, pattern):
return False
if not is_python_file(filename):
return False
return True
def find_files(filenames, recursive, exclude):
"""Yield filenames."""
while filenames:
name = filenames.pop(0)
if recursive and os.path.isdir(name):
for root, directories, children in os.walk(name):
filenames += [os.path.join(root, f) for f in children
if match_file(f, exclude)]
directories[:] = [d for d in directories
if not d.startswith('.')]
else:
yield name
def _fix_file(parameters):
"""Helper function for optionally running fix_file() in parallel."""
if parameters[1].verbose:
print('[file:{0}]'.format(parameters[0]), file=sys.stderr)
try:
fix_file(*parameters)
except IOError as error:
print(str(error), file=sys.stderr)
def fix_multiple_files(filenames, options, output=None):
"""Fix list of files.
Optionally fix files recursively.
"""
filenames = find_files(filenames, options.recursive, options.exclude)
if options.jobs > 1:
import multiprocessing
pool = multiprocessing.Pool(options.jobs)
pool.map(_fix_file,
[(name, options) for name in filenames])
else:
for name in filenames:
_fix_file((name, options, output))
def is_python_file(filename):
"""Return True if filename is Python file."""
if filename.endswith('.py'):
return True
try:
with open_with_encoding(filename) as f:
first_line = f.readlines(1)[0]
except (IOError, IndexError):
return False
if len(first_line) > 200:
# This is probably not even a text file.
return False
if not PYTHON_SHEBANG_REGEX.match(first_line):
return False
return True
def main():
"""Tool main."""
try:
# Exit on broken pipe.
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
except AttributeError: # pragma: no cover
# SIGPIPE is not available on Windows.
pass
try:
options, args = parse_args(sys.argv[1:])
if options.list_fixes:
for code, description in supported_fixes():
print('{code} - {description}'.format(
code=code, description=description))
return 0
if args == ['-']:
assert not options.in_place
# LineEndingWrapper is unnecessary here due to the symmetry between
# standard in and standard out.
sys.stdout.write(fix_string(sys.stdin.read(),
options))
else:
if options.in_place or options.diff:
filenames = list(set(args))
else:
assert len(args) == 1
assert not options.recursive
filenames = args[:1]
fix_multiple_files(filenames, options, sys.stdout)
except KeyboardInterrupt:
return 1 # pragma: no cover
if __name__ == '__main__':
sys.exit(main())
|