Plugins/CheckerPlugins/Pep8/Pep8Fixer.py

changeset 2960
9453efa25fd5
parent 2937
de26bc76d6ee
child 2962
d6c9d1ca2da4
equal deleted inserted replaced
2959:86ad8854361b 2960:9453efa25fd5
249 return res 249 return res
250 250
251 def finalize(self): 251 def finalize(self):
252 """ 252 """
253 Public method to apply all deferred fixes. 253 Public method to apply all deferred fixes.
254
255 @return dictionary containing the fix results
254 """ 256 """
255 results = {} 257 results = {}
256 258
257 # step 1: do fixes operating on logical lines first 259 # step 1: do fixes operating on logical lines first
258 for id_, code, line, pos in self.__stackLogical: 260 for id_, code, line, pos in self.__stackLogical:
430 432
431 This is done by adding or removing from its initial indent only. 433 This is done by adding or removing from its initial indent only.
432 434
433 @param line line number of the issue (integer) 435 @param line line number of the issue (integer)
434 @param pos position inside line (integer) 436 @param pos position inside line (integer)
437 @param logical logical line structure
435 @return flag indicating a change was done (boolean) 438 @return flag indicating a change was done (boolean)
436 """ 439 """
437 assert logical 440 assert logical
438 ls, _, original = logical 441 ls, _, original = logical
439 442
482 else: 485 else:
483 return left + replacement + right 486 return left + replacement + right
484 487
485 def __fixD111(self, code, line, pos): 488 def __fixD111(self, code, line, pos):
486 """ 489 """
487 Private method to fix docstring enclosed in wrong quotes (D111). 490 Private method to fix docstring enclosed in wrong quotes.
491
492 Codes: D111
488 493
489 @param code code of the issue (string) 494 @param code code of the issue (string)
490 @param line line number of the issue (integer) 495 @param line line number of the issue (integer)
491 @param pos position inside line (integer) 496 @param pos position inside line (integer)
492 @return value indicating an applied/deferred fix (-1, 0, 1), 497 @return value indicating an applied/deferred fix (-1, 0, 1),
509 "Triple single quotes converted to triple double quotes."), 514 "Triple single quotes converted to triple double quotes."),
510 0) 515 0)
511 516
512 def __fixD112(self, code, line, pos): 517 def __fixD112(self, code, line, pos):
513 """ 518 """
514 Private method to fix docstring 'r' or 'u' in leading quotes 519 Private method to fix docstring 'r' or 'u' in leading quotes.
515 (D112, D113). 520
521 Codes: D112, D113
516 522
517 @param code code of the issue (string) 523 @param code code of the issue (string)
518 @param line line number of the issue (integer) 524 @param line line number of the issue (integer)
519 @param pos position inside line (integer) 525 @param pos position inside line (integer)
520 @return value indicating an applied/deferred fix (-1, 0, 1), 526 @return value indicating an applied/deferred fix (-1, 0, 1),
538 .format(insertChar), 544 .format(insertChar),
539 0) 545 0)
540 546
541 def __fixD121(self, code, line, pos, apply=False): 547 def __fixD121(self, code, line, pos, apply=False):
542 """ 548 """
543 Private method to fix a single line docstring on multiple lines (D121). 549 Private method to fix a single line docstring on multiple lines.
550
551 Codes: D121
544 552
545 @param code code of the issue (string) 553 @param code code of the issue (string)
546 @param line line number of the issue (integer) 554 @param line line number of the issue (integer)
547 @param pos position inside line (integer) 555 @param pos position inside line (integer)
548 @keyparam apply flag indicating, that the fix should be applied 556 @keyparam apply flag indicating, that the fix should be applied
578 return (-1, "", id) 586 return (-1, "", id)
579 587
580 def __fixD131(self, code, line, pos): 588 def __fixD131(self, code, line, pos):
581 """ 589 """
582 Private method to fix a docstring summary not ending with a 590 Private method to fix a docstring summary not ending with a
583 period (D131). 591 period.
592
593 Codes: D131
584 594
585 @param code code of the issue (string) 595 @param code code of the issue (string)
586 @param line line number of the issue (integer) 596 @param line line number of the issue (integer)
587 @param pos position inside line (integer) 597 @param pos position inside line (integer)
588 @return value indicating an applied/deferred fix (-1, 0, 1), 598 @return value indicating an applied/deferred fix (-1, 0, 1),
611 return (0, "", 0) 621 return (0, "", 0)
612 622
613 def __fixD141(self, code, line, pos, apply=False): 623 def __fixD141(self, code, line, pos, apply=False):
614 """ 624 """
615 Private method to fix a function/method docstring preceded by a 625 Private method to fix a function/method docstring preceded by a
616 blank line (D141). 626 blank line.
627
628 Codes: D141
617 629
618 @param code code of the issue (string) 630 @param code code of the issue (string)
619 @param line line number of the issue (integer) 631 @param line line number of the issue (integer)
620 @param pos position inside line (integer) 632 @param pos position inside line (integer)
621 @keyparam apply flag indicating, that the fix should be applied 633 @keyparam apply flag indicating, that the fix should be applied
638 return (-1, "", id) 650 return (-1, "", id)
639 651
640 def __fixD142(self, code, line, pos, apply=False): 652 def __fixD142(self, code, line, pos, apply=False):
641 """ 653 """
642 Private method to fix a class docstring not preceded by a 654 Private method to fix a class docstring not preceded by a
643 blank line (D142). 655 blank line.
656
657 Codes: D142
644 658
645 @param code code of the issue (string) 659 @param code code of the issue (string)
646 @param line line number of the issue (integer) 660 @param line line number of the issue (integer)
647 @param pos position inside line (integer) 661 @param pos position inside line (integer)
648 @keyparam apply flag indicating, that the fix should be applied 662 @keyparam apply flag indicating, that the fix should be applied
664 return (-1, "", id) 678 return (-1, "", id)
665 679
666 def __fixD143(self, code, line, pos, apply=False): 680 def __fixD143(self, code, line, pos, apply=False):
667 """ 681 """
668 Private method to fix a class docstring not followed by a 682 Private method to fix a class docstring not followed by a
669 blank line (D143). 683 blank line.
684
685 Codes: D143
670 686
671 @param code code of the issue (string) 687 @param code code of the issue (string)
672 @param line line number of the issue (integer) 688 @param line line number of the issue (integer)
673 @param pos position inside line (integer) 689 @param pos position inside line (integer)
674 @keyparam apply flag indicating, that the fix should be applied 690 @keyparam apply flag indicating, that the fix should be applied
690 return (-1, "", id) 706 return (-1, "", id)
691 707
692 def __fixD144(self, code, line, pos, apply=False): 708 def __fixD144(self, code, line, pos, apply=False):
693 """ 709 """
694 Private method to fix a docstring summary not followed by a 710 Private method to fix a docstring summary not followed by a
695 blank line (D144). 711 blank line.
712
713 Codes: D144
696 714
697 @param code code of the issue (string) 715 @param code code of the issue (string)
698 @param line line number of the issue (integer) 716 @param line line number of the issue (integer)
699 @param pos position inside line (integer) 717 @param pos position inside line (integer)
700 @keyparam apply flag indicating, that the fix should be applied 718 @keyparam apply flag indicating, that the fix should be applied
720 return (-1, "", id) 738 return (-1, "", id)
721 739
722 def __fixD145(self, code, line, pos, apply=False): 740 def __fixD145(self, code, line, pos, apply=False):
723 """ 741 """
724 Private method to fix the last paragraph of a multi-line docstring 742 Private method to fix the last paragraph of a multi-line docstring
725 not followed by a blank line (D143). 743 not followed by a blank line.
744
745 Codes: D143
726 746
727 @param code code of the issue (string) 747 @param code code of the issue (string)
728 @param line line number of the issue (integer) 748 @param line line number of the issue (integer)
729 @param pos position inside line (integer) 749 @param pos position inside line (integer)
730 @keyparam apply flag indicating, that the fix should be applied 750 @keyparam apply flag indicating, that the fix should be applied
747 return (-1, "", id) 767 return (-1, "", id)
748 768
749 def __fixD221(self, code, line, pos, apply=False): 769 def __fixD221(self, code, line, pos, apply=False):
750 """ 770 """
751 Private method to fix leading and trailing quotes of docstring 771 Private method to fix leading and trailing quotes of docstring
752 not on separate lines (D221, D222). 772 not on separate lines.
773
774 Codes: D221, D222
753 775
754 @param code code of the issue (string) 776 @param code code of the issue (string)
755 @param line line number of the issue (integer) 777 @param line line number of the issue (integer)
756 @param pos position inside line (integer) 778 @param pos position inside line (integer)
757 @keyparam apply flag indicating, that the fix should be applied 779 @keyparam apply flag indicating, that the fix should be applied
787 return (-1, "", id) 809 return (-1, "", id)
788 810
789 def __fixD242(self, code, line, pos, apply=False): 811 def __fixD242(self, code, line, pos, apply=False):
790 """ 812 """
791 Private method to fix a class or function/method docstring preceded 813 Private method to fix a class or function/method docstring preceded
792 by a blank line (D242, D244). 814 by a blank line.
815
816 Codes: D242, D244
793 817
794 @param code code of the issue (string) 818 @param code code of the issue (string)
795 @param line line number of the issue (integer) 819 @param line line number of the issue (integer)
796 @param pos position inside line (integer) 820 @param pos position inside line (integer)
797 @keyparam apply flag indicating, that the fix should be applied 821 @keyparam apply flag indicating, that the fix should be applied
815 return (-1, "", id) 839 return (-1, "", id)
816 840
817 def __fixD243(self, code, line, pos, apply=False): 841 def __fixD243(self, code, line, pos, apply=False):
818 """ 842 """
819 Private method to fix a class or function/method docstring followed 843 Private method to fix a class or function/method docstring followed
820 by a blank line (D243, D245). 844 by a blank line.
845
846 Codes: D243, D245
821 847
822 @param code code of the issue (string) 848 @param code code of the issue (string)
823 @param line line number of the issue (integer) 849 @param line line number of the issue (integer)
824 @param pos position inside line (integer) 850 @param pos position inside line (integer)
825 @keyparam apply flag indicating, that the fix should be applied 851 @keyparam apply flag indicating, that the fix should be applied
843 return (-1, "", id) 869 return (-1, "", id)
844 870
845 def __fixD247(self, code, line, pos, apply=False): 871 def __fixD247(self, code, line, pos, apply=False):
846 """ 872 """
847 Private method to fix a last paragraph of a docstring followed 873 Private method to fix a last paragraph of a docstring followed
848 by a blank line (D247). 874 by a blank line.
875
876 Codes: D247
849 877
850 @param code code of the issue (string) 878 @param code code of the issue (string)
851 @param line line number of the issue (integer) 879 @param line line number of the issue (integer)
852 @param pos position inside line (integer) 880 @param pos position inside line (integer)
853 @keyparam apply flag indicating, that the fix should be applied 881 @keyparam apply flag indicating, that the fix should be applied
868 self.__stack.append((id, code, line, pos)) 896 self.__stack.append((id, code, line, pos))
869 return (-1, "", id) 897 return (-1, "", id)
870 898
871 def __fixE101(self, code, line, pos): 899 def __fixE101(self, code, line, pos):
872 """ 900 """
873 Private method to fix obsolete tab usage and indentation errors 901 Private method to fix obsolete tab usage and indentation errors.
874 (E101, E111, W191). 902
903 Codes: E101, E111, W191
875 904
876 @param code code of the issue (string) 905 @param code code of the issue (string)
877 @param line line number of the issue (integer) 906 @param line line number of the issue (integer)
878 @param pos position inside line (integer) 907 @param pos position inside line (integer)
879 @return value indicating an applied/deferred fix (-1, 0, 1), 908 @return value indicating an applied/deferred fix (-1, 0, 1),
896 return (0, "", 0) 925 return (0, "", 0)
897 926
898 def __fixE121(self, code, line, pos, apply=False): 927 def __fixE121(self, code, line, pos, apply=False):
899 """ 928 """
900 Private method to fix the indentation of continuation lines and 929 Private method to fix the indentation of continuation lines and
901 closing brackets (E121, E124). 930 closing brackets.
931
932 Codes: E121, E124
902 933
903 @param code code of the issue (string) 934 @param code code of the issue (string)
904 @param line line number of the issue (integer) 935 @param line line number of the issue (integer)
905 @param pos position inside line (integer) 936 @param pos position inside line (integer)
906 @keyparam apply flag indicating, that the fix should be applied 937 @keyparam apply flag indicating, that the fix should be applied
928 self.__stackLogical.append((id, code, line, pos)) 959 self.__stackLogical.append((id, code, line, pos))
929 return (-1, "", id) 960 return (-1, "", id)
930 961
931 def __fixE122(self, code, line, pos, apply=False): 962 def __fixE122(self, code, line, pos, apply=False):
932 """ 963 """
933 Private method to fix a missing indentation of continuation lines 964 Private method to fix a missing indentation of continuation lines.
934 (E122). 965
966 Codes: E122
935 967
936 @param code code of the issue (string) 968 @param code code of the issue (string)
937 @param line line number of the issue (integer) 969 @param line line number of the issue (integer)
938 @param pos position inside line (integer) 970 @param pos position inside line (integer)
939 @keyparam apply flag indicating, that the fix should be applied 971 @keyparam apply flag indicating, that the fix should be applied
965 self.__stackLogical.append((id, code, line, pos)) 997 self.__stackLogical.append((id, code, line, pos))
966 return (-1, "", id) 998 return (-1, "", id)
967 999
968 def __fixE123(self, code, line, pos, apply=False): 1000 def __fixE123(self, code, line, pos, apply=False):
969 """ 1001 """
970 Private method to fix the indentation of a closing bracket lines 1002 Private method to fix the indentation of a closing bracket lines.
971 (E123). 1003
1004 Codes: E123
972 1005
973 @param code code of the issue (string) 1006 @param code code of the issue (string)
974 @param line line number of the issue (integer) 1007 @param line line number of the issue (integer)
975 @param pos position inside line (integer) 1008 @param pos position inside line (integer)
976 @keyparam apply flag indicating, that the fix should be applied 1009 @keyparam apply flag indicating, that the fix should be applied
1004 return (-1, "", id) 1037 return (-1, "", id)
1005 1038
1006 def __fixE125(self, code, line, pos, apply=False): 1039 def __fixE125(self, code, line, pos, apply=False):
1007 """ 1040 """
1008 Private method to fix the indentation of continuation lines not 1041 Private method to fix the indentation of continuation lines not
1009 distinguishable from next logical line (E125). 1042 distinguishable from next logical line.
1043
1044 Codes: E125
1010 1045
1011 @param code code of the issue (string) 1046 @param code code of the issue (string)
1012 @param line line number of the issue (integer) 1047 @param line line number of the issue (integer)
1013 @param pos position inside line (integer) 1048 @param pos position inside line (integer)
1014 @keyparam apply flag indicating, that the fix should be applied 1049 @keyparam apply flag indicating, that the fix should be applied
1035 return (-1, "", id) 1070 return (-1, "", id)
1036 1071
1037 def __fixE126(self, code, line, pos, apply=False): 1072 def __fixE126(self, code, line, pos, apply=False):
1038 """ 1073 """
1039 Private method to fix over-indented/under-indented hanging 1074 Private method to fix over-indented/under-indented hanging
1040 indentation (E126, E133). 1075 indentation.
1076
1077 Codes: E126, E133
1041 1078
1042 @param code code of the issue (string) 1079 @param code code of the issue (string)
1043 @param line line number of the issue (integer) 1080 @param line line number of the issue (integer)
1044 @param pos position inside line (integer) 1081 @param pos position inside line (integer)
1045 @keyparam apply flag indicating, that the fix should be applied 1082 @keyparam apply flag indicating, that the fix should be applied
1073 self.__stackLogical.append((id, code, line, pos)) 1110 self.__stackLogical.append((id, code, line, pos))
1074 return (-1, "", id) 1111 return (-1, "", id)
1075 1112
1076 def __fixE127(self, code, line, pos, apply=False): 1113 def __fixE127(self, code, line, pos, apply=False):
1077 """ 1114 """
1078 Private method to fix over/under indented lines (E127, E128). 1115 Private method to fix over/under indented lines.
1116
1117 Codes: E127, E128
1079 1118
1080 @param code code of the issue (string) 1119 @param code code of the issue (string)
1081 @param line line number of the issue (integer) 1120 @param line line number of the issue (integer)
1082 @param pos position inside line (integer) 1121 @param pos position inside line (integer)
1083 @keyparam apply flag indicating, that the fix should be applied 1122 @keyparam apply flag indicating, that the fix should be applied
1125 self.__stackLogical.append((id, code, line, pos)) 1164 self.__stackLogical.append((id, code, line, pos))
1126 return (-1, "", id) 1165 return (-1, "", id)
1127 1166
1128 def __fixE201(self, code, line, pos): 1167 def __fixE201(self, code, line, pos):
1129 """ 1168 """
1130 Private method to fix extraneous whitespace (E201, E202, 1169 Private method to fix extraneous whitespace.
1131 E203, E211). 1170
1171 Codes: E201, E202, E203, E211
1132 1172
1133 @param code code of the issue (string) 1173 @param code code of the issue (string)
1134 @param line line number of the issue (integer) 1174 @param line line number of the issue (integer)
1135 @param pos position inside line (integer) 1175 @param pos position inside line (integer)
1136 @return value indicating an applied/deferred fix (-1, 0, 1), 1176 @return value indicating an applied/deferred fix (-1, 0, 1),
1151 return (1, self.trUtf8("Extraneous whitespace removed."), 0) 1191 return (1, self.trUtf8("Extraneous whitespace removed."), 0)
1152 1192
1153 def __fixE221(self, code, line, pos): 1193 def __fixE221(self, code, line, pos):
1154 """ 1194 """
1155 Private method to fix extraneous whitespace around operator or 1195 Private method to fix extraneous whitespace around operator or
1156 keyword (E221, E222, E223, E224, E225, E226, E227, E228, E241, 1196 keyword.
1157 E242, E271, E272, E273, E274). 1197
1198 Codes: E221, E222, E223, E224, E225, E226, E227, E228, E241,
1199 E242, E271, E272, E273, E274).
1158 1200
1159 @param code code of the issue (string) 1201 @param code code of the issue (string)
1160 @param line line number of the issue (integer) 1202 @param line line number of the issue (integer)
1161 @param pos position inside line (integer) 1203 @param pos position inside line (integer)
1162 @return value indicating an applied/deferred fix (-1, 0, 1), 1204 @return value indicating an applied/deferred fix (-1, 0, 1),
1180 return (1, self.trUtf8("Extraneous whitespace removed."), 0) 1222 return (1, self.trUtf8("Extraneous whitespace removed."), 0)
1181 1223
1182 def __fixE231(self, code, line, pos): 1224 def __fixE231(self, code, line, pos):
1183 """ 1225 """
1184 Private method to fix missing whitespace after ',;:'. 1226 Private method to fix missing whitespace after ',;:'.
1227
1228 Codes: E231
1185 1229
1186 @param code code of the issue (string) 1230 @param code code of the issue (string)
1187 @param line line number of the issue (integer) 1231 @param line line number of the issue (integer)
1188 @param pos position inside line (integer) 1232 @param pos position inside line (integer)
1189 @return value indicating an applied/deferred fix (-1, 0, 1), 1233 @return value indicating an applied/deferred fix (-1, 0, 1),
1197 return (1, self.trUtf8("Missing whitespace added."), 0) 1241 return (1, self.trUtf8("Missing whitespace added."), 0)
1198 1242
1199 def __fixE251(self, code, line, pos): 1243 def __fixE251(self, code, line, pos):
1200 """ 1244 """
1201 Private method to fix extraneous whitespace around keyword and 1245 Private method to fix extraneous whitespace around keyword and
1202 default parameter equals (E251). 1246 default parameter equals.
1247
1248 Codes: E251
1203 1249
1204 @param code code of the issue (string) 1250 @param code code of the issue (string)
1205 @param line line number of the issue (integer) 1251 @param line line number of the issue (integer)
1206 @param pos position inside line (integer) 1252 @param pos position inside line (integer)
1207 @return value indicating an applied/deferred fix (-1, 0, 1), 1253 @return value indicating an applied/deferred fix (-1, 0, 1),
1231 self.__source[line] = newText 1277 self.__source[line] = newText
1232 return (1, self.trUtf8("Extraneous whitespace removed."), 0) 1278 return (1, self.trUtf8("Extraneous whitespace removed."), 0)
1233 1279
1234 def __fixE261(self, code, line, pos): 1280 def __fixE261(self, code, line, pos):
1235 """ 1281 """
1236 Private method to fix whitespace before or after inline comment 1282 Private method to fix whitespace before or after inline comment.
1237 (E261, E262). 1283
1284 Codes: E261, E262
1238 1285
1239 @param code code of the issue (string) 1286 @param code code of the issue (string)
1240 @param line line number of the issue (integer) 1287 @param line line number of the issue (integer)
1241 @param pos position inside line (integer) 1288 @param pos position inside line (integer)
1242 @return value indicating an applied/deferred fix (-1, 0, 1), 1289 @return value indicating an applied/deferred fix (-1, 0, 1),
1251 self.__source[line] = newText 1298 self.__source[line] = newText
1252 return (1, self.trUtf8("Whitespace around comment sign corrected."), 0) 1299 return (1, self.trUtf8("Whitespace around comment sign corrected."), 0)
1253 1300
1254 def __fixE301(self, code, line, pos, apply=False): 1301 def __fixE301(self, code, line, pos, apply=False):
1255 """ 1302 """
1256 Private method to fix the need for one blank line (E301). 1303 Private method to fix the need for one blank line.
1304
1305 Codes: E301
1257 1306
1258 @param code code of the issue (string) 1307 @param code code of the issue (string)
1259 @param line line number of the issue (integer) 1308 @param line line number of the issue (integer)
1260 @param pos position inside line (integer) 1309 @param pos position inside line (integer)
1261 @keyparam apply flag indicating, that the fix should be applied 1310 @keyparam apply flag indicating, that the fix should be applied
1272 self.__stack.append((id, code, line, pos)) 1321 self.__stack.append((id, code, line, pos))
1273 return (-1, "", id) 1322 return (-1, "", id)
1274 1323
1275 def __fixE302(self, code, line, pos, apply=False): 1324 def __fixE302(self, code, line, pos, apply=False):
1276 """ 1325 """
1277 Private method to fix the need for two blank lines (E302). 1326 Private method to fix the need for two blank lines.
1327
1328 Codes: E302
1278 1329
1279 @param code code of the issue (string) 1330 @param code code of the issue (string)
1280 @param line line number of the issue (integer) 1331 @param line line number of the issue (integer)
1281 @param pos position inside line (integer) 1332 @param pos position inside line (integer)
1282 @keyparam apply flag indicating, that the fix should be applied 1333 @keyparam apply flag indicating, that the fix should be applied
1330 self.__stack.append((id, code, line, pos)) 1381 self.__stack.append((id, code, line, pos))
1331 return (-1, "", id) 1382 return (-1, "", id)
1332 1383
1333 def __fixE303(self, code, line, pos, apply=False): 1384 def __fixE303(self, code, line, pos, apply=False):
1334 """ 1385 """
1335 Private method to fix superfluous blank lines (E303). 1386 Private method to fix superfluous blank lines.
1387
1388 Codes: E303
1336 1389
1337 @param code code of the issue (string) 1390 @param code code of the issue (string)
1338 @param line line number of the issue (integer) 1391 @param line line number of the issue (integer)
1339 @param pos position inside line (integer) 1392 @param pos position inside line (integer)
1340 @keyparam apply flag indicating, that the fix should be applied 1393 @keyparam apply flag indicating, that the fix should be applied
1358 return (-1, "", id) 1411 return (-1, "", id)
1359 1412
1360 def __fixE304(self, code, line, pos, apply=False): 1413 def __fixE304(self, code, line, pos, apply=False):
1361 """ 1414 """
1362 Private method to fix superfluous blank lines after a function 1415 Private method to fix superfluous blank lines after a function
1363 decorator (E304). 1416 decorator.
1417
1418 Codes: E304
1364 1419
1365 @param code code of the issue (string) 1420 @param code code of the issue (string)
1366 @param line line number of the issue (integer) 1421 @param line line number of the issue (integer)
1367 @param pos position inside line (integer) 1422 @param pos position inside line (integer)
1368 @keyparam apply flag indicating, that the fix should be applied 1423 @keyparam apply flag indicating, that the fix should be applied
1387 self.__stack.append((id, code, line, pos)) 1442 self.__stack.append((id, code, line, pos))
1388 return (-1, "", id) 1443 return (-1, "", id)
1389 1444
1390 def __fixE401(self, code, line, pos, apply=False): 1445 def __fixE401(self, code, line, pos, apply=False):
1391 """ 1446 """
1392 Private method to fix multiple imports on one line (E401). 1447 Private method to fix multiple imports on one line.
1448
1449 Codes: E401
1393 1450
1394 @param code code of the issue (string) 1451 @param code code of the issue (string)
1395 @param line line number of the issue (integer) 1452 @param line line number of the issue (integer)
1396 @param pos position inside line (integer) 1453 @param pos position inside line (integer)
1397 @keyparam apply flag indicating, that the fix should be applied 1454 @keyparam apply flag indicating, that the fix should be applied
1421 self.__stack.append((id, code, line, pos)) 1478 self.__stack.append((id, code, line, pos))
1422 return (-1, "", id) 1479 return (-1, "", id)
1423 1480
1424 def __fixE501(self, code, line, pos, apply=False): 1481 def __fixE501(self, code, line, pos, apply=False):
1425 """ 1482 """
1426 Private method to fix the long lines by breaking them (E501). 1483 Private method to fix the long lines by breaking them.
1484
1485 Codes: E501
1427 1486
1428 @param code code of the issue (string) 1487 @param code code of the issue (string)
1429 @param line line number of the issue (integer) 1488 @param line line number of the issue (integer)
1430 @param pos position inside line (integer) 1489 @param pos position inside line (integer)
1431 @keyparam apply flag indicating, that the fix should be applied 1490 @keyparam apply flag indicating, that the fix should be applied
1465 self.__stack.append((id, code, line, pos)) 1524 self.__stack.append((id, code, line, pos))
1466 return (-1, "", id) 1525 return (-1, "", id)
1467 1526
1468 def __fixE502(self, code, line, pos): 1527 def __fixE502(self, code, line, pos):
1469 """ 1528 """
1470 Private method to fix redundant backslash within brackets (E502). 1529 Private method to fix redundant backslash within brackets.
1530
1531 Codes: E502
1471 1532
1472 @param code code of the issue (string) 1533 @param code code of the issue (string)
1473 @param line line number of the issue (integer) 1534 @param line line number of the issue (integer)
1474 @param pos position inside line (integer) 1535 @param pos position inside line (integer)
1475 @return value indicating an applied/deferred fix (-1, 0, 1), 1536 @return value indicating an applied/deferred fix (-1, 0, 1),
1480 self.__source[line - 1].rstrip("\n\r \t\\") + self.__getEol() 1541 self.__source[line - 1].rstrip("\n\r \t\\") + self.__getEol()
1481 return (1, self.trUtf8("Redundant backslash in brackets removed."), 0) 1542 return (1, self.trUtf8("Redundant backslash in brackets removed."), 0)
1482 1543
1483 def __fixE701(self, code, line, pos, apply=False): 1544 def __fixE701(self, code, line, pos, apply=False):
1484 """ 1545 """
1485 Private method to fix colon-separated compound statements (E701). 1546 Private method to fix colon-separated compound statements.
1547
1548 Codes: E701
1486 1549
1487 @param code code of the issue (string) 1550 @param code code of the issue (string)
1488 @param line line number of the issue (integer) 1551 @param line line number of the issue (integer)
1489 @param pos position inside line (integer) 1552 @param pos position inside line (integer)
1490 @keyparam apply flag indicating, that the fix should be applied 1553 @keyparam apply flag indicating, that the fix should be applied
1508 self.__stack.append((id, code, line, pos)) 1571 self.__stack.append((id, code, line, pos))
1509 return (-1, "", id) 1572 return (-1, "", id)
1510 1573
1511 def __fixE702(self, code, line, pos, apply=False): 1574 def __fixE702(self, code, line, pos, apply=False):
1512 """ 1575 """
1513 Private method to fix semicolon-separated compound statements 1576 Private method to fix semicolon-separated compound statements.
1514 (E702, E703). 1577
1578 Codes: E702, E703
1515 1579
1516 @param code code of the issue (string) 1580 @param code code of the issue (string)
1517 @param line line number of the issue (integer) 1581 @param line line number of the issue (integer)
1518 @param pos position inside line (integer) 1582 @param pos position inside line (integer)
1519 @keyparam apply flag indicating, that the fix should be applied 1583 @keyparam apply flag indicating, that the fix should be applied
1542 self.__stack.append((id, code, line, pos)) 1606 self.__stack.append((id, code, line, pos))
1543 return (-1, "", id) 1607 return (-1, "", id)
1544 1608
1545 def __fixE711(self, code, line, pos): 1609 def __fixE711(self, code, line, pos):
1546 """ 1610 """
1547 Private method to fix comparison with None (E711, E712). 1611 Private method to fix comparison with None.
1612
1613 Codes: E711, E712
1548 1614
1549 @param code code of the issue (string) 1615 @param code code of the issue (string)
1550 @param line line number of the issue (integer) 1616 @param line line number of the issue (integer)
1551 @param pos position inside line (integer) 1617 @param pos position inside line (integer)
1552 @return value indicating an applied/deferred fix (-1, 0, 1), 1618 @return value indicating an applied/deferred fix (-1, 0, 1),
1578 return (1, self.trUtf8("Comparison to None/True/False corrected."), 0) 1644 return (1, self.trUtf8("Comparison to None/True/False corrected."), 0)
1579 1645
1580 def __fixN804(self, code, line, pos, apply=False): 1646 def __fixN804(self, code, line, pos, apply=False):
1581 """ 1647 """
1582 Private method to fix a wrong first argument of normal and 1648 Private method to fix a wrong first argument of normal and
1583 class methods (N804, N805). 1649 class methods.
1650
1651 Codes: N804, N805
1584 1652
1585 @param code code of the issue (string) 1653 @param code code of the issue (string)
1586 @param line line number of the issue (integer) 1654 @param line line number of the issue (integer)
1587 @param pos position inside line (integer) 1655 @param pos position inside line (integer)
1588 @keyparam apply flag indicating, that the fix should be applied 1656 @keyparam apply flag indicating, that the fix should be applied
1618 self.__stack.append((id, code, line, pos)) 1686 self.__stack.append((id, code, line, pos))
1619 return (-1, "", id) 1687 return (-1, "", id)
1620 1688
1621 def __fixN806(self, code, line, pos, apply=False): 1689 def __fixN806(self, code, line, pos, apply=False):
1622 """ 1690 """
1623 Private method to fix a wrong first argument of static methods 1691 Private method to fix a wrong first argument of static methods.
1624 (N806). 1692
1693 Codes: N806
1625 1694
1626 @param code code of the issue (string) 1695 @param code code of the issue (string)
1627 @param line line number of the issue (integer) 1696 @param line line number of the issue (integer)
1628 @param pos position inside line (integer) 1697 @param pos position inside line (integer)
1629 @keyparam apply flag indicating, that the fix should be applied 1698 @keyparam apply flag indicating, that the fix should be applied
1677 self.__stack.append((id, code, line, pos)) 1746 self.__stack.append((id, code, line, pos))
1678 return (-1, "", id) 1747 return (-1, "", id)
1679 1748
1680 def __fixW291(self, code, line, pos): 1749 def __fixW291(self, code, line, pos):
1681 """ 1750 """
1682 Private method to fix trailing whitespace (W291, W293). 1751 Private method to fix trailing whitespace.
1752
1753 Codes: W291, W293
1683 1754
1684 @param code code of the issue (string) 1755 @param code code of the issue (string)
1685 @param line line number of the issue (integer) 1756 @param line line number of the issue (integer)
1686 @param pos position inside line (integer) 1757 @param pos position inside line (integer)
1687 @return value indicating an applied/deferred fix (-1, 0, 1), 1758 @return value indicating an applied/deferred fix (-1, 0, 1),
1692 self.__source[line - 1]) 1763 self.__source[line - 1])
1693 return (1, self.trUtf8("Whitespace stripped from end of line."), 0) 1764 return (1, self.trUtf8("Whitespace stripped from end of line."), 0)
1694 1765
1695 def __fixW292(self, code, line, pos): 1766 def __fixW292(self, code, line, pos):
1696 """ 1767 """
1697 Private method to fix a missing newline at the end of file (W292). 1768 Private method to fix a missing newline at the end of file.
1769
1770 Codes: W292
1698 1771
1699 @param code code of the issue (string) 1772 @param code code of the issue (string)
1700 @param line line number of the issue (integer) 1773 @param line line number of the issue (integer)
1701 @param pos position inside line (integer) 1774 @param pos position inside line (integer)
1702 @return value indicating an applied/deferred fix (-1, 0, 1), 1775 @return value indicating an applied/deferred fix (-1, 0, 1),
1706 self.__source[line - 1] += self.__getEol() 1779 self.__source[line - 1] += self.__getEol()
1707 return (1, self.trUtf8("newline added to end of file."), 0) 1780 return (1, self.trUtf8("newline added to end of file."), 0)
1708 1781
1709 def __fixW391(self, code, line, pos): 1782 def __fixW391(self, code, line, pos):
1710 """ 1783 """
1711 Private method to fix trailing blank lines (W391). 1784 Private method to fix trailing blank lines.
1785
1786 Codes: W391
1712 1787
1713 @param code code of the issue (string) 1788 @param code code of the issue (string)
1714 @param line line number of the issue (integer) 1789 @param line line number of the issue (integer)
1715 @param pos position inside line (integer) 1790 @param pos position inside line (integer)
1716 @return value indicating an applied/deferred fix (-1, 0, 1), 1791 @return value indicating an applied/deferred fix (-1, 0, 1),
1727 return (1, self.trUtf8( 1802 return (1, self.trUtf8(
1728 "Superfluous trailing blank lines removed from end of file."), 0) 1803 "Superfluous trailing blank lines removed from end of file."), 0)
1729 1804
1730 def __fixW603(self, code, line, pos): 1805 def __fixW603(self, code, line, pos):
1731 """ 1806 """
1732 Private method to fix the not equal notation (W603). 1807 Private method to fix the not equal notation.
1808
1809 Codes: W603
1733 1810
1734 @param code code of the issue (string) 1811 @param code code of the issue (string)
1735 @param line line number of the issue (integer) 1812 @param line line number of the issue (integer)
1736 @param pos position inside line (integer) 1813 @param pos position inside line (integer)
1737 @return value indicating an applied/deferred fix (-1, 0, 1), 1814 @return value indicating an applied/deferred fix (-1, 0, 1),
1775 self.stats = [] 1852 self.stats = []
1776 1853
1777 def run(self): 1854 def run(self):
1778 """ 1855 """
1779 Public method to run the re-indenter. 1856 Public method to run the re-indenter.
1857
1858 @return flag indicating that a change was done (boolean)
1780 """ 1859 """
1781 try: 1860 try:
1782 stats = self.__genStats(tokenize.generate_tokens(self.getline)) 1861 stats = self.__genStats(tokenize.generate_tokens(self.getline))
1783 except (SyntaxError, tokenize.TokenError): 1862 except (SyntaxError, tokenize.TokenError):
1784 return False 1863 return False
1877 def __genStats(self, tokens): 1956 def __genStats(self, tokens):
1878 """ 1957 """
1879 Private method to generate the re-indent statistics. 1958 Private method to generate the re-indent statistics.
1880 1959
1881 @param tokens tokens generator (tokenize._tokenize) 1960 @param tokens tokens generator (tokenize._tokenize)
1961 @return reference to the generated statistics
1882 """ 1962 """
1883 find_stmt = True # next token begins a fresh stmt? 1963 find_stmt = True # next token begins a fresh stmt?
1884 level = 0 # current indent level 1964 level = 0 # current indent level
1885 stats = [] 1965 stats = []
1886 1966
1940 """ 2020 """
1941 Class used by fixers dealing with indentation. 2021 Class used by fixers dealing with indentation.
1942 2022
1943 Each instance operates on a single logical line. 2023 Each instance operates on a single logical line.
1944 """ 2024 """
1945
1946 SKIP_TOKENS = frozenset([ 2025 SKIP_TOKENS = frozenset([
1947 tokenize.COMMENT, tokenize.NL, tokenize.INDENT, 2026 tokenize.COMMENT, tokenize.NL, tokenize.INDENT,
1948 tokenize.DEDENT, tokenize.NEWLINE, tokenize.ENDMARKER 2027 tokenize.DEDENT, tokenize.NEWLINE, tokenize.ENDMARKER
1949 ]) 2028 ])
1950 2029

eric ide

mercurial