eric6/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleFixer.py

changeset 7610
df7025fe26a3
parent 7609
d5aff4fd0ef8
child 7628
f904d0eef264
equal deleted inserted replaced
7609:d5aff4fd0ef8 7610:df7025fe26a3
16 from io import StringIO # __IGNORE_WARNING__ 16 from io import StringIO # __IGNORE_WARNING__
17 import os 17 import os
18 import re 18 import re
19 import tokenize 19 import tokenize
20 20
21 # CodeStyleCheckerDialog tries to import FixableCodeStyleIssues what fail under 21 # CodeStyleCheckerDialog tries to import FixableCodeStyleIssues which fails
22 # Python3. So ignore it. 22 # under Python3. So ignore it.
23 try: 23 try:
24 import pycodestyle 24 import pycodestyle
25 except ImportError: 25 except ImportError:
26 pass 26 pass
27 27
236 236
237 with open(self.__filename, "wb") as fp: 237 with open(self.__filename, "wb") as fp:
238 fp.write(txt) 238 fp.write(txt)
239 except (IOError, UnicodeError) as err: 239 except (IOError, UnicodeError) as err:
240 # Could not save the file! Skipping it. Reason: {0} 240 # Could not save the file! Skipping it. Reason: {0}
241 return ("FIXWRITE_ERROR", (str(err),)) 241 return ("FIXWRITE_ERROR", [str(err)])
242 242
243 return None 243 return None
244 244
245 def __codeMatch(self, code): 245 def __codeMatch(self, code):
246 """ 246 """
270 return True 270 return True
271 return False 271 return False
272 272
273 return True 273 return True
274 274
275 def fixIssue(self, line, pos, message): 275 def fixIssue(self, line, pos, code):
276 """ 276 """
277 Public method to fix the fixable issues. 277 Public method to fix the fixable issues.
278 278
279 @param line line number of issue (integer) 279 @param line line number of the issue
280 @param pos character position of issue (integer) 280 @type int
281 @param message message text (string) 281 @param pos position inside line
282 @return value indicating an applied/deferred fix (-1, 0, 1), 282 @type int
283 a message for the fix (string) and an ID for a deferred 283 @param code code of the issue
284 fix (integer) 284 @type str
285 """ 285 @return value indicating an applied/deferred fix (-1, 0, 1),
286 if isinstance(message, (tuple, list)): 286 a message code for the fix, arguments list for the message
287 code = message[0].strip() 287 and an ID for a deferred fix
288 else: 288 @rtype tuple of (int, str, list, int)
289 code = message.split(None, 1)[0].strip() 289 """
290
291 if ( 290 if (
292 line <= len(self.__source) and 291 line <= len(self.__source) and
293 self.__codeMatch(code) and 292 self.__codeMatch(code) and
294 code in self.__fixes 293 code in self.__fixes
295 ): 294 ):
296 res = self.__fixes[code](code, line, pos) 295 res = self.__fixes[code](code, line, pos)
297 if res[0] == 1: 296 if res[0] == 1:
298 self.__modified = True 297 self.__modified = True
299 self.fixed += 1 298 self.fixed += 1
300 else: 299 else:
301 res = (0, "", 0) 300 res = (0, "", [], 0)
302 301
303 return res 302 return res
304 303
305 def finalize(self): 304 def finalize(self):
306 """ 305 """
310 """ 309 """
311 results = {} 310 results = {}
312 311
313 # step 1: do fixes operating on logical lines first 312 # step 1: do fixes operating on logical lines first
314 for id_, code, line, pos in self.__stackLogical: 313 for id_, code, line, pos in self.__stackLogical:
315 res, msg, _ = self.__fixes[code](code, line, pos, apply=True) 314 res, msg, args, _ = self.__fixes[code](code, line, pos, apply=True)
316 if res == 1: 315 if res == 1:
317 self.__modified = True 316 self.__modified = True
318 self.fixed += 1 317 self.fixed += 1
319 results[id_] = (res, msg) 318 results[id_] = (res, msg, args)
320 319
321 # step 2: do fixes that change the number of lines 320 # step 2: do fixes that change the number of lines
322 for id_, code, line, pos in reversed(self.__stack): 321 for id_, code, line, pos in reversed(self.__stack):
323 res, msg, _ = self.__fixes[code](code, line, pos, apply=True) 322 res, msg, args, _ = self.__fixes[code](code, line, pos, apply=True)
324 if res == 1: 323 if res == 1:
325 self.__modified = True 324 self.__modified = True
326 self.fixed += 1 325 self.fixed += 1
327 results[id_] = (res, msg) 326 results[id_] = (res, msg, args)
328 327
329 return results 328 return results
330 329
331 def __getID(self): 330 def __getID(self):
332 """ 331 """
525 """ 524 """
526 Private method to fix docstring enclosed in wrong quotes. 525 Private method to fix docstring enclosed in wrong quotes.
527 526
528 Codes: D111 527 Codes: D111
529 528
530 @param code code of the issue (string) 529 @param code code of the issue
531 @param line line number of the issue (integer) 530 @type str
532 @param pos position inside line (integer) 531 @param line line number of the issue
533 @return value indicating an applied/deferred fix (-1, 0, 1), 532 @type int
534 a message for the fix (string) and an ID for a deferred 533 @param pos position inside line
535 fix (integer) 534 @type int
535 @return value indicating an applied/deferred fix (-1, 0, 1),
536 a message code for the fix, a list of arguments for the
537 message and an ID for a deferred fix
538 @rtype tuple of (int, str, list or int, int)
536 """ 539 """
537 line = line - 1 540 line = line - 1
538 quotes = re.match(r"""\s*[ru]?('''|'|\")""", 541 quotes = re.match(r"""\s*[ru]?('''|'|\")""",
539 self.__source[line]).group(1) 542 self.__source[line]).group(1)
540 left, right = self.__source[line].split(quotes, 1) 543 left, right = self.__source[line].split(quotes, 1)
545 self.__source[line] = left + '"""' + right 548 self.__source[line] = left + '"""' + right
546 break 549 break
547 line += 1 550 line += 1
548 551
549 # Triple single quotes converted to triple double quotes. 552 # Triple single quotes converted to triple double quotes.
550 return (1, "FIXD111", 0) 553 return (1, "FIXD111", [], 0)
551 554
552 def __fixD112(self, code, line, pos): 555 def __fixD112(self, code, line, pos):
553 """ 556 """
554 Private method to fix docstring 'r' or 'u' in leading quotes. 557 Private method to fix docstring 'r' or 'u' in leading quotes.
555 558
556 Codes: D112, D113 559 Codes: D112, D113
557 560
558 @param code code of the issue (string) 561 @param code code of the issue
559 @param line line number of the issue (integer) 562 @type str
560 @param pos position inside line (integer) 563 @param line line number of the issue
561 @return value indicating an applied/deferred fix (-1, 0, 1), 564 @type int
562 a message for the fix (string) and an ID for a deferred 565 @param pos position inside line
563 fix (integer) 566 @type int
567 @return value indicating an applied/deferred fix (-1, 0, 1),
568 a message code for the fix, a list of arguments for the
569 message and an ID for a deferred fix
570 @rtype tuple of (int, str, list or int, int)
564 """ 571 """
565 line = line - 1 572 line = line - 1
566 if code == "D112": 573 if code == "D112":
567 insertChar = "r" 574 insertChar = "r"
568 elif code == "D113": 575 elif code == "D113":
575 insertChar + 582 insertChar +
576 self.__source[line].lstrip() 583 self.__source[line].lstrip()
577 ) 584 )
578 self.__source[line] = newText 585 self.__source[line] = newText
579 # Introductory quotes corrected to be {0}""" 586 # Introductory quotes corrected to be {0}"""
580 return (1, ('FIXD112', (insertChar,)), 0) 587 return (1, 'FIXD112', [insertChar], 0)
581 588
582 def __fixD121(self, code, line, pos, apply=False): 589 def __fixD121(self, code, line, pos, apply=False):
583 """ 590 """
584 Private method to fix a single line docstring on multiple lines. 591 Private method to fix a single line docstring on multiple lines.
585 592
586 Codes: D121 593 Codes: D121
587 594
588 @param code code of the issue (string) 595 @param code code of the issue
589 @param line line number of the issue (integer) 596 @type str
590 @param pos position inside line (integer) 597 @param line line number of the issue
591 @keyparam apply flag indicating, that the fix should be applied 598 @type int
592 (boolean) 599 @param pos position inside line
593 @return value indicating an applied/deferred fix (-1, 0, 1), 600 @type int
594 a message for the fix (string) and an ID for a deferred 601 @param apply flag indicating, that the fix should be applied
595 fix (integer) 602 @type bool
603 @return value indicating an applied/deferred fix (-1, 0, 1),
604 a message code for the fix, a list of arguments for the
605 message and an ID for a deferred fix
606 @rtype tuple of (int, str, list or int, int)
596 """ 607 """
597 if apply: 608 if apply:
598 line = line - 1 609 line = line - 1
599 if not self.__source[line].lstrip().startswith( 610 if not self.__source[line].lstrip().startswith(
600 ('"""', 'r"""', 'u"""')): 611 ('"""', 'r"""', 'u"""')):
601 # only correctly formatted docstrings will be fixed 612 # only correctly formatted docstrings will be fixed
602 return (0, "", 0) 613 return (0, "", [], 0)
603 614
604 docstring = ( 615 docstring = (
605 self.__source[line].rstrip() + 616 self.__source[line].rstrip() +
606 self.__source[line + 1].strip() 617 self.__source[line + 1].strip()
607 ) 618 )
612 self.__source[line + 2] = "" 623 self.__source[line + 2] = ""
613 624
614 self.__source[line] = docstring 625 self.__source[line] = docstring
615 self.__source[line + 1] = "" 626 self.__source[line + 1] = ""
616 # Single line docstring put on one line. 627 # Single line docstring put on one line.
617 return (1, "FIXD121", 0) 628 return (1, "FIXD121", [], 0)
618 else: 629 else:
619 fixId = self.__getID() 630 fixId = self.__getID()
620 self.__stack.append((fixId, code, line, pos)) 631 self.__stack.append((fixId, code, line, pos))
621 return (-1, "", fixId) 632 return (-1, "", [], fixId)
622 633
623 def __fixD131(self, code, line, pos): 634 def __fixD131(self, code, line, pos):
624 """ 635 """
625 Private method to fix a docstring summary not ending with a 636 Private method to fix a docstring summary not ending with a
626 period. 637 period.
627 638
628 Codes: D131 639 Codes: D131
629 640
630 @param code code of the issue (string) 641 @param code code of the issue
631 @param line line number of the issue (integer) 642 @type str
632 @param pos position inside line (integer) 643 @param line line number of the issue
633 @return value indicating an applied/deferred fix (-1, 0, 1), 644 @type int
634 a message for the fix (string) and an ID for a deferred 645 @param pos position inside line
635 fix (integer) 646 @type int
647 @return value indicating an applied/deferred fix (-1, 0, 1),
648 a message code for the fix, a list of arguments for the
649 message and an ID for a deferred fix
650 @rtype tuple of (int, str, list or int, int)
636 """ 651 """
637 line = line - 1 652 line = line - 1
638 newText = "" 653 newText = ""
639 if ( 654 if (
640 self.__source[line].rstrip().endswith(('"""', "'''")) and 655 self.__source[line].rstrip().endswith(('"""', "'''")) and
658 newText = self.__source[line].rstrip() + "." + self.__eol 673 newText = self.__source[line].rstrip() + "." + self.__eol
659 674
660 if newText: 675 if newText:
661 self.__source[line] = newText 676 self.__source[line] = newText
662 # Period added to summary line. 677 # Period added to summary line.
663 return (1, "FIXD131", 0) 678 return (1, "FIXD131", [], 0)
664 else: 679 else:
665 return (0, "", 0) 680 return (0, "", [], 0)
666 681
667 def __fixD141(self, code, line, pos, apply=False): 682 def __fixD141(self, code, line, pos, apply=False):
668 """ 683 """
669 Private method to fix a function/method docstring preceded by a 684 Private method to fix a function/method docstring preceded by a
670 blank line. 685 blank line.
671 686
672 Codes: D141 687 Codes: D141
673 688
674 @param code code of the issue (string) 689 @param code code of the issue
675 @param line line number of the issue (integer) 690 @type str
676 @param pos position inside line (integer) 691 @param line line number of the issue
677 @keyparam apply flag indicating, that the fix should be applied 692 @type int
678 (boolean) 693 @param pos position inside line
679 @return value indicating an applied/deferred fix (-1, 0, 1), 694 @type int
680 a message for the fix (string) and an ID for a deferred 695 @param apply flag indicating, that the fix should be applied
681 fix (integer) 696 @type bool
697 @return value indicating an applied/deferred fix (-1, 0, 1),
698 a message code for the fix, a list of arguments for the
699 message and an ID for a deferred fix
700 @rtype tuple of (int, str, list or int, int)
682 """ 701 """
683 if apply: 702 if apply:
684 line = line - 1 703 line = line - 1
685 self.__source[line - 1] = "" 704 self.__source[line - 1] = ""
686 # Blank line before function/method docstring removed. 705 # Blank line before function/method docstring removed.
687 return (1, "FIXD141", 0) 706 return (1, "FIXD141", [], 0)
688 else: 707 else:
689 fixId = self.__getID() 708 fixId = self.__getID()
690 self.__stack.append((fixId, code, line, pos)) 709 self.__stack.append((fixId, code, line, pos))
691 return (-1, "", fixId) 710 return (-1, "", [], fixId)
692 711
693 def __fixD142(self, code, line, pos, apply=False): 712 def __fixD142(self, code, line, pos, apply=False):
694 """ 713 """
695 Private method to fix a class docstring not preceded by a 714 Private method to fix a class docstring not preceded by a
696 blank line. 715 blank line.
697 716
698 Codes: D142 717 Codes: D142
699 718
700 @param code code of the issue (string) 719 @param code code of the issue
701 @param line line number of the issue (integer) 720 @type str
702 @param pos position inside line (integer) 721 @param line line number of the issue
703 @keyparam apply flag indicating, that the fix should be applied 722 @type int
704 (boolean) 723 @param pos position inside line
705 @return value indicating an applied/deferred fix (-1, 0, 1), 724 @type int
706 a message for the fix (string) and an ID for a deferred 725 @param apply flag indicating, that the fix should be applied
707 fix (integer) 726 @type bool
727 @return value indicating an applied/deferred fix (-1, 0, 1),
728 a message code for the fix, a list of arguments for the
729 message and an ID for a deferred fix
730 @rtype tuple of (int, str, list or int, int)
708 """ 731 """
709 if apply: 732 if apply:
710 line = line - 1 733 line = line - 1
711 self.__source[line] = self.__eol + self.__source[line] 734 self.__source[line] = self.__eol + self.__source[line]
712 # Blank line inserted before class docstring. 735 # Blank line inserted before class docstring.
713 return (1, "FIXD142", 0) 736 return (1, "FIXD142", [], 0)
714 else: 737 else:
715 fixId = self.__getID() 738 fixId = self.__getID()
716 self.__stack.append((fixId, code, line, pos)) 739 self.__stack.append((fixId, code, line, pos))
717 return (-1, "", fixId) 740 return (-1, "", [], fixId)
718 741
719 def __fixD143(self, code, line, pos, apply=False): 742 def __fixD143(self, code, line, pos, apply=False):
720 """ 743 """
721 Private method to fix a class docstring not followed by a 744 Private method to fix a class docstring not followed by a
722 blank line. 745 blank line.
723 746
724 Codes: D143 747 Codes: D143
725 748
726 @param code code of the issue (string) 749 @param code code of the issue
727 @param line line number of the issue (integer) 750 @type str
728 @param pos position inside line (integer) 751 @param line line number of the issue
729 @keyparam apply flag indicating, that the fix should be applied 752 @type int
730 (boolean) 753 @param pos position inside line
731 @return value indicating an applied/deferred fix (-1, 0, 1), 754 @type int
732 a message for the fix (string) and an ID for a deferred 755 @param apply flag indicating, that the fix should be applied
733 fix (integer) 756 @type bool
757 @return value indicating an applied/deferred fix (-1, 0, 1),
758 a message code for the fix, a list of arguments for the
759 message and an ID for a deferred fix
760 @rtype tuple of (int, str, list or int, int)
734 """ 761 """
735 if apply: 762 if apply:
736 line = line - 1 763 line = line - 1
737 self.__source[line] += self.__eol 764 self.__source[line] += self.__eol
738 # Blank line inserted after class docstring. 765 # Blank line inserted after class docstring.
739 return (1, "FIXD143", 0) 766 return (1, "FIXD143", [], 0)
740 else: 767 else:
741 fixId = self.__getID() 768 fixId = self.__getID()
742 self.__stack.append((fixId, code, line, pos)) 769 self.__stack.append((fixId, code, line, pos))
743 return (-1, "", fixId) 770 return (-1, "", [], fixId)
744 771
745 def __fixD144(self, code, line, pos, apply=False): 772 def __fixD144(self, code, line, pos, apply=False):
746 """ 773 """
747 Private method to fix a docstring summary not followed by a 774 Private method to fix a docstring summary not followed by a
748 blank line. 775 blank line.
749 776
750 Codes: D144 777 Codes: D144
751 778
752 @param code code of the issue (string) 779 @param code code of the issue
753 @param line line number of the issue (integer) 780 @type str
754 @param pos position inside line (integer) 781 @param line line number of the issue
755 @keyparam apply flag indicating, that the fix should be applied 782 @type int
756 (boolean) 783 @param pos position inside line
757 @return value indicating an applied/deferred fix (-1, 0, 1), 784 @type int
758 a message for the fix (string) and an ID for a deferred 785 @param apply flag indicating, that the fix should be applied
759 fix (integer) 786 @type bool
787 @return value indicating an applied/deferred fix (-1, 0, 1),
788 a message code for the fix, a list of arguments for the
789 message and an ID for a deferred fix
790 @rtype tuple of (int, str, list or int, int)
760 """ 791 """
761 if apply: 792 if apply:
762 line = line - 1 793 line = line - 1
763 if not self.__source[line].rstrip().endswith("."): 794 if not self.__source[line].rstrip().endswith("."):
764 # only correct summary lines can be fixed here 795 # only correct summary lines can be fixed here
765 return (0, "", 0) 796 return (0, "", 0)
766 797
767 self.__source[line] += self.__eol 798 self.__source[line] += self.__eol
768 # Blank line inserted after docstring summary. 799 # Blank line inserted after docstring summary.
769 return (1, "FIXD144", 0) 800 return (1, "FIXD144", [], 0)
770 else: 801 else:
771 fixId = self.__getID() 802 fixId = self.__getID()
772 self.__stack.append((fixId, code, line, pos)) 803 self.__stack.append((fixId, code, line, pos))
773 return (-1, "", fixId) 804 return (-1, "", [], fixId)
774 805
775 def __fixD145(self, code, line, pos, apply=False): 806 def __fixD145(self, code, line, pos, apply=False):
776 """ 807 """
777 Private method to fix the last paragraph of a multi-line docstring 808 Private method to fix the last paragraph of a multi-line docstring
778 not followed by a blank line. 809 not followed by a blank line.
779 810
780 Codes: D143 811 Codes: D143
781 812
782 @param code code of the issue (string) 813 @param code code of the issue
783 @param line line number of the issue (integer) 814 @type str
784 @param pos position inside line (integer) 815 @param line line number of the issue
785 @keyparam apply flag indicating, that the fix should be applied 816 @type int
786 (boolean) 817 @param pos position inside line
787 @return value indicating an applied/deferred fix (-1, 0, 1), 818 @type int
788 a message for the fix (string) and an ID for a deferred 819 @param apply flag indicating, that the fix should be applied
789 fix (integer) 820 @type bool
821 @return value indicating an applied/deferred fix (-1, 0, 1),
822 a message code for the fix, a list of arguments for the
823 message and an ID for a deferred fix
824 @rtype tuple of (int, str, list or int, int)
790 """ 825 """
791 if apply: 826 if apply:
792 line = line - 1 827 line = line - 1
793 self.__source[line] = self.__eol + self.__source[line] 828 self.__source[line] = self.__eol + self.__source[line]
794 # Blank line inserted after last paragraph of docstring. 829 # Blank line inserted after last paragraph of docstring.
795 return (1, "FIXD145", 0) 830 return (1, "FIXD145", [], 0)
796 else: 831 else:
797 fixId = self.__getID() 832 fixId = self.__getID()
798 self.__stack.append((fixId, code, line, pos)) 833 self.__stack.append((fixId, code, line, pos))
799 return (-1, "", fixId) 834 return (-1, "", [], fixId)
800 835
801 def __fixD221(self, code, line, pos, apply=False): 836 def __fixD221(self, code, line, pos, apply=False):
802 """ 837 """
803 Private method to fix leading and trailing quotes of docstring 838 Private method to fix leading and trailing quotes of docstring
804 not on separate lines. 839 not on separate lines.
805 840
806 Codes: D221, D222 841 Codes: D221, D222
807 842
808 @param code code of the issue (string) 843 @param code code of the issue
809 @param line line number of the issue (integer) 844 @type str
810 @param pos position inside line (integer) 845 @param line line number of the issue
811 @keyparam apply flag indicating, that the fix should be applied 846 @type int
812 (boolean) 847 @param pos position inside line
813 @return value indicating an applied/deferred fix (-1, 0, 1), 848 @type int
814 a message for the fix (string) and an ID for a deferred 849 @param apply flag indicating, that the fix should be applied
815 fix (integer) 850 @type bool
851 @return value indicating an applied/deferred fix (-1, 0, 1),
852 a message code for the fix, a list of arguments for the
853 message and an ID for a deferred fix
854 @rtype tuple of (int, str, list or int, int)
816 """ 855 """
817 if apply: 856 if apply:
818 line = line - 1 857 line = line - 1
819 indent = self.__getIndent(self.__source[line]) 858 indent = self.__getIndent(self.__source[line])
820 source = self.__source[line].strip() 859 source = self.__source[line].strip()
840 # Leading quotes put on separate line. 879 # Leading quotes put on separate line.
841 msg = "FIXD221" 880 msg = "FIXD221"
842 else: 881 else:
843 # Trailing quotes put on separate line. 882 # Trailing quotes put on separate line.
844 msg = "FIXD222" 883 msg = "FIXD222"
845 return (1, msg, 0) 884 return (1, msg, [], 0)
846 else: 885 else:
847 fixId = self.__getID() 886 fixId = self.__getID()
848 self.__stack.append((fixId, code, line, pos)) 887 self.__stack.append((fixId, code, line, pos))
849 return (-1, "", fixId) 888 return (-1, "", [], fixId)
850 889
851 def __fixD242(self, code, line, pos, apply=False): 890 def __fixD242(self, code, line, pos, apply=False):
852 """ 891 """
853 Private method to fix a class or function/method docstring preceded 892 Private method to fix a class or function/method docstring preceded
854 by a blank line. 893 by a blank line.
855 894
856 Codes: D242, D244 895 Codes: D242, D244
857 896
858 @param code code of the issue (string) 897 @param code code of the issue
859 @param line line number of the issue (integer) 898 @type str
860 @param pos position inside line (integer) 899 @param line line number of the issue
861 @keyparam apply flag indicating, that the fix should be applied 900 @type int
862 (boolean) 901 @param pos position inside line
863 @return value indicating an applied/deferred fix (-1, 0, 1), 902 @type int
864 a message for the fix (string) and an ID for a deferred 903 @param apply flag indicating, that the fix should be applied
865 fix (integer) 904 @type bool
905 @return value indicating an applied/deferred fix (-1, 0, 1),
906 a message code for the fix, a list of arguments for the
907 message and an ID for a deferred fix
908 @rtype tuple of (int, str, list or int, int)
866 """ 909 """
867 if apply: 910 if apply:
868 line = line - 1 911 line = line - 1
869 self.__source[line - 1] = "" 912 self.__source[line - 1] = ""
870 if code == "D242": 913 if code == "D242":
871 # Blank line before class docstring removed. 914 # Blank line before class docstring removed.
872 msg = "FIXD242" 915 msg = "FIXD242"
873 else: 916 else:
874 # Blank line before function/method docstring removed. 917 # Blank line before function/method docstring removed.
875 msg = "FIXD244" 918 msg = "FIXD244"
876 return (1, msg, 0) 919 return (1, msg, [], 0)
877 else: 920 else:
878 fixId = self.__getID() 921 fixId = self.__getID()
879 self.__stack.append((fixId, code, line, pos)) 922 self.__stack.append((fixId, code, line, pos))
880 return (-1, "", fixId) 923 return (-1, "", [], fixId)
881 924
882 def __fixD243(self, code, line, pos, apply=False): 925 def __fixD243(self, code, line, pos, apply=False):
883 """ 926 """
884 Private method to fix a class or function/method docstring followed 927 Private method to fix a class or function/method docstring followed
885 by a blank line. 928 by a blank line.
886 929
887 Codes: D243, D245 930 Codes: D243, D245
888 931
889 @param code code of the issue (string) 932 @param code code of the issue
890 @param line line number of the issue (integer) 933 @type str
891 @param pos position inside line (integer) 934 @param line line number of the issue
892 @keyparam apply flag indicating, that the fix should be applied 935 @type int
893 (boolean) 936 @param pos position inside line
894 @return value indicating an applied/deferred fix (-1, 0, 1), 937 @type int
895 a message for the fix (string) and an ID for a deferred 938 @param apply flag indicating, that the fix should be applied
896 fix (integer) 939 @type bool
940 @return value indicating an applied/deferred fix (-1, 0, 1),
941 a message code for the fix, a list of arguments for the
942 message and an ID for a deferred fix
943 @rtype tuple of (int, str, list or int, int)
897 """ 944 """
898 if apply: 945 if apply:
899 line = line - 1 946 line = line - 1
900 self.__source[line + 1] = "" 947 self.__source[line + 1] = ""
901 if code == "D243": 948 if code == "D243":
902 # Blank line after class docstring removed. 949 # Blank line after class docstring removed.
903 msg = "FIXD243" 950 msg = "FIXD243"
904 else: 951 else:
905 # Blank line after function/method docstring removed. 952 # Blank line after function/method docstring removed.
906 msg = "FIXD245" 953 msg = "FIXD245"
907 return (1, msg, 0) 954 return (1, msg, [], 0)
908 else: 955 else:
909 fixId = self.__getID() 956 fixId = self.__getID()
910 self.__stack.append((fixId, code, line, pos)) 957 self.__stack.append((fixId, code, line, pos))
911 return (-1, "", fixId) 958 return (-1, "", [], fixId)
912 959
913 def __fixD247(self, code, line, pos, apply=False): 960 def __fixD247(self, code, line, pos, apply=False):
914 """ 961 """
915 Private method to fix a last paragraph of a docstring followed 962 Private method to fix a last paragraph of a docstring followed
916 by a blank line. 963 by a blank line.
917 964
918 Codes: D247 965 Codes: D247
919 966
920 @param code code of the issue (string) 967 @param code code of the issue
921 @param line line number of the issue (integer) 968 @type str
922 @param pos position inside line (integer) 969 @param line line number of the issue
923 @keyparam apply flag indicating, that the fix should be applied 970 @type int
924 (boolean) 971 @param pos position inside line
925 @return value indicating an applied/deferred fix (-1, 0, 1), 972 @type int
926 a message for the fix (string) and an ID for a deferred 973 @param apply flag indicating, that the fix should be applied
927 fix (integer) 974 @type bool
975 @return value indicating an applied/deferred fix (-1, 0, 1),
976 a message code for the fix, a list of arguments for the
977 message and an ID for a deferred fix
978 @rtype tuple of (int, str, list or int, int)
928 """ 979 """
929 if apply: 980 if apply:
930 line = line - 1 981 line = line - 1
931 self.__source[line - 1] = "" 982 self.__source[line - 1] = ""
932 # Blank line after last paragraph removed. 983 # Blank line after last paragraph removed.
933 return (1, "FIXD247", 0) 984 return (1, "FIXD247", [], 0)
934 else: 985 else:
935 fixId = self.__getID() 986 fixId = self.__getID()
936 self.__stack.append((fixId, code, line, pos)) 987 self.__stack.append((fixId, code, line, pos))
937 return (-1, "", fixId) 988 return (-1, "", [], fixId)
938 989
939 def __fixE101(self, code, line, pos): 990 def __fixE101(self, code, line, pos):
940 """ 991 """
941 Private method to fix obsolete tab usage and indentation errors. 992 Private method to fix obsolete tab usage and indentation errors.
942 993
943 Codes: E101, E111, W191 994 Codes: E101, E111, W191
944 995
945 @param code code of the issue (string) 996 @param code code of the issue
946 @param line line number of the issue (integer) 997 @type str
947 @param pos position inside line (integer) 998 @param line line number of the issue
948 @return value indicating an applied/deferred fix (-1, 0, 1), 999 @type int
949 a message for the fix (string) and an ID for a deferred 1000 @param pos position inside line
950 fix (integer) 1001 @type int
1002 @return value indicating an applied/deferred fix (-1, 0, 1),
1003 a message code for the fix, a list of arguments for the
1004 message and an ID for a deferred fix
1005 @rtype tuple of (int, str, list or int, int)
951 """ 1006 """
952 if self.__reindenter is None: 1007 if self.__reindenter is None:
953 self.__reindenter = Reindenter(self.__source) 1008 self.__reindenter = Reindenter(self.__source)
954 self.__reindenter.run() 1009 self.__reindenter.run()
955 fixedLine = self.__reindenter.fixedLine(line - 1) 1010 fixedLine = self.__reindenter.fixedLine(line - 1)
959 # Tab converted to 4 spaces. 1014 # Tab converted to 4 spaces.
960 msg = "FIXE101" 1015 msg = "FIXE101"
961 else: 1016 else:
962 # Indentation adjusted to be a multiple of four. 1017 # Indentation adjusted to be a multiple of four.
963 msg = "FIXE111" 1018 msg = "FIXE111"
964 return (1, msg, 0) 1019 return (1, msg, [], 0)
965 else: 1020 else:
966 return (0, "", 0) 1021 return (0, "", [], 0)
967 1022
968 def __fixE121(self, code, line, pos, apply=False): 1023 def __fixE121(self, code, line, pos, apply=False):
969 """ 1024 """
970 Private method to fix the indentation of continuation lines and 1025 Private method to fix the indentation of continuation lines and
971 closing brackets. 1026 closing brackets.
972 1027
973 Codes: E121, E124 1028 Codes: E121, E124
974 1029
975 @param code code of the issue (string) 1030 @param code code of the issue
976 @param line line number of the issue (integer) 1031 @type str
977 @param pos position inside line (integer) 1032 @param line line number of the issue
978 @keyparam apply flag indicating, that the fix should be applied 1033 @type int
979 (boolean) 1034 @param pos position inside line
980 @return value indicating an applied/deferred fix (-1, 0, 1), 1035 @type int
981 a message for the fix (string) and an ID for a deferred 1036 @param apply flag indicating, that the fix should be applied
982 fix (integer) 1037 @type bool
1038 @return value indicating an applied/deferred fix (-1, 0, 1),
1039 a message code for the fix, a list of arguments for the
1040 message and an ID for a deferred fix
1041 @rtype tuple of (int, str, list or int, int)
983 """ 1042 """
984 if apply: 1043 if apply:
985 logical = self.__getLogical(line, pos) 1044 logical = self.__getLogical(line, pos)
986 if logical: 1045 if logical:
987 # Fix by adjusting initial indent level. 1046 # Fix by adjusting initial indent level.
991 # Indentation of continuation line corrected. 1050 # Indentation of continuation line corrected.
992 msg = "FIXE121" 1051 msg = "FIXE121"
993 elif code == "E124": 1052 elif code == "E124":
994 # Indentation of closing bracket corrected. 1053 # Indentation of closing bracket corrected.
995 msg = "FIXE124" 1054 msg = "FIXE124"
996 return (1, msg, 0) 1055 return (1, msg, [], 0)
997 return (0, "", 0) 1056 return (0, "", [], 0)
998 else: 1057 else:
999 fixId = self.__getID() 1058 fixId = self.__getID()
1000 self.__stackLogical.append((fixId, code, line, pos)) 1059 self.__stackLogical.append((fixId, code, line, pos))
1001 return (-1, "", fixId) 1060 return (-1, "", [], fixId)
1002 1061
1003 def __fixE122(self, code, line, pos, apply=False): 1062 def __fixE122(self, code, line, pos, apply=False):
1004 """ 1063 """
1005 Private method to fix a missing indentation of continuation lines. 1064 Private method to fix a missing indentation of continuation lines.
1006 1065
1007 Codes: E122 1066 Codes: E122
1008 1067
1009 @param code code of the issue (string) 1068 @param code code of the issue
1010 @param line line number of the issue (integer) 1069 @type str
1011 @param pos position inside line (integer) 1070 @param line line number of the issue
1012 @keyparam apply flag indicating, that the fix should be applied 1071 @type int
1013 (boolean) 1072 @param pos position inside line
1014 @return value indicating an applied/deferred fix (-1, 0, 1), 1073 @type int
1015 a message for the fix (string) and an ID for a deferred 1074 @param apply flag indicating, that the fix should be applied
1016 fix (integer) 1075 @type bool
1076 @return value indicating an applied/deferred fix (-1, 0, 1),
1077 a message code for the fix, a list of arguments for the
1078 message and an ID for a deferred fix
1079 @rtype tuple of (int, str, list or int, int)
1017 """ 1080 """
1018 if apply: 1081 if apply:
1019 logical = self.__getLogical(line, pos) 1082 logical = self.__getLogical(line, pos)
1020 if logical: 1083 if logical:
1021 # Fix by adding an initial indent. 1084 # Fix by adding an initial indent.
1028 self.__source[line] = ( 1091 self.__source[line] = (
1029 indentation + 1092 indentation +
1030 self.__indentWord + text.lstrip() 1093 self.__indentWord + text.lstrip()
1031 ) 1094 )
1032 # Missing indentation of continuation line corrected. 1095 # Missing indentation of continuation line corrected.
1033 return (1, "FIXE122", 0) 1096 return (1, "FIXE122", [], 0)
1034 return (0, "", 0) 1097 return (0, "", [], 0)
1035 else: 1098 else:
1036 fixId = self.__getID() 1099 fixId = self.__getID()
1037 self.__stackLogical.append((fixId, code, line, pos)) 1100 self.__stackLogical.append((fixId, code, line, pos))
1038 return (-1, "", fixId) 1101 return (-1, "", [], fixId)
1039 1102
1040 def __fixE123(self, code, line, pos, apply=False): 1103 def __fixE123(self, code, line, pos, apply=False):
1041 """ 1104 """
1042 Private method to fix the indentation of a closing bracket lines. 1105 Private method to fix the indentation of a closing bracket lines.
1043 1106
1044 Codes: E123 1107 Codes: E123
1045 1108
1046 @param code code of the issue (string) 1109 @param code code of the issue
1047 @param line line number of the issue (integer) 1110 @type str
1048 @param pos position inside line (integer) 1111 @param line line number of the issue
1049 @keyparam apply flag indicating, that the fix should be applied 1112 @type int
1050 (boolean) 1113 @param pos position inside line
1051 @return value indicating an applied/deferred fix (-1, 0, 1), 1114 @type int
1052 a message for the fix (string) and an ID for a deferred 1115 @param apply flag indicating, that the fix should be applied
1053 fix (integer) 1116 @type bool
1117 @return value indicating an applied/deferred fix (-1, 0, 1),
1118 a message code for the fix, a list of arguments for the
1119 message and an ID for a deferred fix
1120 @rtype tuple of (int, str, list or int, int)
1054 """ 1121 """
1055 if apply: 1122 if apply:
1056 logical = self.__getLogical(line, pos) 1123 logical = self.__getLogical(line, pos)
1057 if logical: 1124 if logical:
1058 # Fix by deleting whitespace to the correct level. 1125 # Fix by deleting whitespace to the correct level.
1066 else: 1133 else:
1067 self.__source[row] = newText 1134 self.__source[row] = newText
1068 changed = True 1135 changed = True
1069 if changed: 1136 if changed:
1070 # Closing bracket aligned to opening bracket. 1137 # Closing bracket aligned to opening bracket.
1071 return (1, "FIXE123", 0) 1138 return (1, "FIXE123", [], 0)
1072 return (0, "", 0) 1139 return (0, "", [], 0)
1073 else: 1140 else:
1074 fixId = self.__getID() 1141 fixId = self.__getID()
1075 self.__stackLogical.append((fixId, code, line, pos)) 1142 self.__stackLogical.append((fixId, code, line, pos))
1076 return (-1, "", fixId) 1143 return (-1, "", [], fixId)
1077 1144
1078 def __fixE125(self, code, line, pos, apply=False): 1145 def __fixE125(self, code, line, pos, apply=False):
1079 """ 1146 """
1080 Private method to fix the indentation of continuation lines not 1147 Private method to fix the indentation of continuation lines not
1081 distinguishable from next logical line. 1148 distinguishable from next logical line.
1082 1149
1083 Codes: E125 1150 Codes: E125
1084 1151
1085 @param code code of the issue (string) 1152 @param code code of the issue
1086 @param line line number of the issue (integer) 1153 @type str
1087 @param pos position inside line (integer) 1154 @param line line number of the issue
1088 @keyparam apply flag indicating, that the fix should be applied 1155 @type int
1089 (boolean) 1156 @param pos position inside line
1090 @return value indicating an applied/deferred fix (-1, 0, 1), 1157 @type int
1091 a message for the fix (string) and an ID for a deferred 1158 @param apply flag indicating, that the fix should be applied
1092 fix (integer) 1159 @type bool
1160 @return value indicating an applied/deferred fix (-1, 0, 1),
1161 a message code for the fix, a list of arguments for the
1162 message and an ID for a deferred fix
1163 @rtype tuple of (int, str, list or int, int)
1093 """ 1164 """
1094 if apply: 1165 if apply:
1095 logical = self.__getLogical(line, pos) 1166 logical = self.__getLogical(line, pos)
1096 if logical: 1167 if logical:
1097 # Fix by adjusting initial indent level. 1168 # Fix by adjusting initial indent level.
1102 self.__source[row] = ( 1173 self.__source[row] = (
1103 self.__getIndent(text) + 1174 self.__getIndent(text) +
1104 self.__indentWord + text.lstrip() 1175 self.__indentWord + text.lstrip()
1105 ) 1176 )
1106 # Indentation level changed. 1177 # Indentation level changed.
1107 return (1, "FIXE125", 0) 1178 return (1, "FIXE125", [], 0)
1108 return (0, "", 0) 1179 return (0, "", [], 0)
1109 else: 1180 else:
1110 fixId = self.__getID() 1181 fixId = self.__getID()
1111 self.__stackLogical.append((fixId, code, line, pos)) 1182 self.__stackLogical.append((fixId, code, line, pos))
1112 return (-1, "", fixId) 1183 return (-1, "", [], fixId)
1113 1184
1114 def __fixE126(self, code, line, pos, apply=False): 1185 def __fixE126(self, code, line, pos, apply=False):
1115 """ 1186 """
1116 Private method to fix over-indented/under-indented hanging 1187 Private method to fix over-indented/under-indented hanging
1117 indentation. 1188 indentation.
1118 1189
1119 Codes: E126, E133 1190 Codes: E126, E133
1120 1191
1121 @param code code of the issue (string) 1192 @param code code of the issue
1122 @param line line number of the issue (integer) 1193 @type str
1123 @param pos position inside line (integer) 1194 @param line line number of the issue
1124 @keyparam apply flag indicating, that the fix should be applied 1195 @type int
1125 (boolean) 1196 @param pos position inside line
1126 @return value indicating an applied/deferred fix (-1, 0, 1), 1197 @type int
1127 a message for the fix (string) and an ID for a deferred 1198 @param apply flag indicating, that the fix should be applied
1128 fix (integer) 1199 @type bool
1200 @return value indicating an applied/deferred fix (-1, 0, 1),
1201 a message code for the fix, a list of arguments for the
1202 message and an ID for a deferred fix
1203 @rtype tuple of (int, str, list or int, int)
1129 """ 1204 """
1130 if apply: 1205 if apply:
1131 logical = self.__getLogical(line, pos) 1206 logical = self.__getLogical(line, pos)
1132 if logical: 1207 if logical:
1133 # Fix by deleting whitespace to the left. 1208 # Fix by deleting whitespace to the left.
1144 else: 1219 else:
1145 self.__source[row] = newText 1220 self.__source[row] = newText
1146 changed = True 1221 changed = True
1147 if changed: 1222 if changed:
1148 # Indentation level of hanging indentation changed. 1223 # Indentation level of hanging indentation changed.
1149 return (1, "FIXE126", 0) 1224 return (1, "FIXE126", [], 0)
1150 return (0, "", 0) 1225 return (0, "", [], 0)
1151 else: 1226 else:
1152 fixId = self.__getID() 1227 fixId = self.__getID()
1153 self.__stackLogical.append((fixId, code, line, pos)) 1228 self.__stackLogical.append((fixId, code, line, pos))
1154 return (-1, "", fixId) 1229 return (-1, "", [], fixId)
1155 1230
1156 def __fixE127(self, code, line, pos, apply=False): 1231 def __fixE127(self, code, line, pos, apply=False):
1157 """ 1232 """
1158 Private method to fix over/under indented lines. 1233 Private method to fix over/under indented lines.
1159 1234
1160 Codes: E127, E128 1235 Codes: E127, E128
1161 1236
1162 @param code code of the issue (string) 1237 @param code code of the issue
1163 @param line line number of the issue (integer) 1238 @type str
1164 @param pos position inside line (integer) 1239 @param line line number of the issue
1165 @keyparam apply flag indicating, that the fix should be applied 1240 @type int
1166 (boolean) 1241 @param pos position inside line
1167 @return value indicating an applied/deferred fix (-1, 0, 1), 1242 @type int
1168 a message for the fix (string) and an ID for a deferred 1243 @param apply flag indicating, that the fix should be applied
1169 fix (integer) 1244 @type bool
1245 @return value indicating an applied/deferred fix (-1, 0, 1),
1246 a message code for the fix, a list of arguments for the
1247 message and an ID for a deferred fix
1248 @rtype tuple of (int, str, list or int, int)
1170 """ 1249 """
1171 if apply: 1250 if apply:
1172 logical = self.__getLogical(line, pos) 1251 logical = self.__getLogical(line, pos)
1173 if logical: 1252 if logical:
1174 # Fix by inserting/deleting whitespace to the correct level. 1253 # Fix by inserting/deleting whitespace to the correct level.
1202 else: 1281 else:
1203 self.__source[row] = newText 1282 self.__source[row] = newText
1204 changed = True 1283 changed = True
1205 if changed: 1284 if changed:
1206 # Visual indentation corrected. 1285 # Visual indentation corrected.
1207 return (1, "FIXE127", 0) 1286 return (1, "FIXE127", [], 0)
1208 return (0, "", 0) 1287 return (0, "", [], 0)
1209 else: 1288 else:
1210 fixId = self.__getID() 1289 fixId = self.__getID()
1211 self.__stackLogical.append((fixId, code, line, pos)) 1290 self.__stackLogical.append((fixId, code, line, pos))
1212 return (-1, "", fixId) 1291 return (-1, "", [], fixId)
1213 1292
1214 def __fixE201(self, code, line, pos): 1293 def __fixE201(self, code, line, pos):
1215 """ 1294 """
1216 Private method to fix extraneous whitespace. 1295 Private method to fix extraneous whitespace.
1217 1296
1218 Codes: E201, E202, E203, E211 1297 Codes: E201, E202, E203, E211
1219 1298
1220 @param code code of the issue (string) 1299 @param code code of the issue
1221 @param line line number of the issue (integer) 1300 @type str
1222 @param pos position inside line (integer) 1301 @param line line number of the issue
1223 @return value indicating an applied/deferred fix (-1, 0, 1), 1302 @type int
1224 a message for the fix (string) and an ID for a deferred 1303 @param pos position inside line
1225 fix (integer) 1304 @type int
1305 @return value indicating an applied/deferred fix (-1, 0, 1),
1306 a message code for the fix, a list of arguments for the
1307 message and an ID for a deferred fix
1308 @rtype tuple of (int, str, list or int, int)
1226 """ 1309 """
1227 line = line - 1 1310 line = line - 1
1228 text = self.__source[line] 1311 text = self.__source[line]
1229 1312
1230 if '"""' in text or "'''" in text or text.rstrip().endswith('\\'): 1313 if '"""' in text or "'''" in text or text.rstrip().endswith('\\'):
1231 return (0, "", 0) 1314 return (0, "", [], 0)
1232 1315
1233 newText = self.__fixWhitespace(text, pos, '') 1316 newText = self.__fixWhitespace(text, pos, '')
1234 if newText == text: 1317 if newText == text:
1235 return (0, "", 0) 1318 return (0, "", [], 0)
1236 1319
1237 self.__source[line] = newText 1320 self.__source[line] = newText
1238 # Extraneous whitespace removed. 1321 # Extraneous whitespace removed.
1239 return (1, "FIXE201", 0) 1322 return (1, "FIXE201", [], 0)
1240 1323
1241 def __fixE221(self, code, line, pos): 1324 def __fixE221(self, code, line, pos):
1242 """ 1325 """
1243 Private method to fix extraneous whitespace around operator or 1326 Private method to fix extraneous whitespace around operator or
1244 keyword. 1327 keyword.
1245 1328
1246 Codes: E221, E222, E223, E224, E241, E242, E271, E272, E273, E274 1329 Codes: E221, E222, E223, E224, E241, E242, E271, E272, E273, E274
1247 1330
1248 @param code code of the issue (string) 1331 @param code code of the issue
1249 @param line line number of the issue (integer) 1332 @type str
1250 @param pos position inside line (integer) 1333 @param line line number of the issue
1251 @return value indicating an applied/deferred fix (-1, 0, 1), 1334 @type int
1252 a message for the fix (string) and an ID for a deferred 1335 @param pos position inside line
1253 fix (integer) 1336 @type int
1337 @return value indicating an applied/deferred fix (-1, 0, 1),
1338 a message code for the fix, a list of arguments for the
1339 message and an ID for a deferred fix
1340 @rtype tuple of (int, str, list or int, int)
1254 """ 1341 """
1255 line = line - 1 1342 line = line - 1
1256 text = self.__source[line] 1343 text = self.__source[line]
1257 1344
1258 if '"""' in text or "'''" in text or text.rstrip().endswith('\\'): 1345 if '"""' in text or "'''" in text or text.rstrip().endswith('\\'):
1259 return (0, "", 0) 1346 return (0, "", [], 0)
1260 1347
1261 newText = self.__fixWhitespace(text, pos, ' ') 1348 newText = self.__fixWhitespace(text, pos, ' ')
1262 if newText == text: 1349 if newText == text:
1263 return (0, "", 0) 1350 return (0, "", [], 0)
1264 1351
1265 self.__source[line] = newText 1352 self.__source[line] = newText
1266 return (1, "FIXE221", 0) 1353 return (1, "FIXE221", [], 0)
1267 1354
1268 def __fixE225(self, code, line, pos): 1355 def __fixE225(self, code, line, pos):
1269 """ 1356 """
1270 Private method to fix extraneous whitespaces around operator. 1357 Private method to fix extraneous whitespaces around operator.
1271 1358
1272 Codes: E225, E226, E227, E228 1359 Codes: E225, E226, E227, E228
1273 1360
1274 @param code code of the issue (string) 1361 @param code code of the issue
1275 @param line line number of the issue (integer) 1362 @type str
1276 @param pos position inside line (integer) 1363 @param line line number of the issue
1277 @return value indicating an applied/deferred fix (-1, 0, 1), 1364 @type int
1278 a message for the fix (string) and an ID for a deferred 1365 @param pos position inside line
1279 fix (integer) 1366 @type int
1367 @return value indicating an applied/deferred fix (-1, 0, 1),
1368 a message code for the fix, a list of arguments for the
1369 message and an ID for a deferred fix
1370 @rtype tuple of (int, str, list or int, int)
1280 """ 1371 """
1281 line = line - 1 1372 line = line - 1
1282 text = self.__source[line] 1373 text = self.__source[line]
1283 1374
1284 if '"""' in text or "'''" in text or text.rstrip().endswith('\\'): 1375 if '"""' in text or "'''" in text or text.rstrip().endswith('\\'):
1285 return (0, "", 0) 1376 return (0, "", [], 0)
1286 1377
1287 newText = text 1378 newText = text
1288 # determine length of operator 1379 # determine length of operator
1289 tokens = '<>*/=^&|%!+-' 1380 tokens = '<>*/=^&|%!+-'
1290 pos2 = pos 1381 pos2 = pos
1298 break 1389 break
1299 if pos2 < len(text) and text[pos2] not in ' \t': 1390 if pos2 < len(text) and text[pos2] not in ' \t':
1300 newText = self.__fixWhitespace(newText, pos2, ' ') 1391 newText = self.__fixWhitespace(newText, pos2, ' ')
1301 newText = self.__fixWhitespace(newText, pos, ' ') 1392 newText = self.__fixWhitespace(newText, pos, ' ')
1302 if newText == text: 1393 if newText == text:
1303 return (0, "", 0) 1394 return (0, "", [], 0)
1304 1395
1305 self.__source[line] = newText 1396 self.__source[line] = newText
1306 # Missing whitespaces added. 1397 # Missing whitespaces added.
1307 return (1, "FIXE225", 0) 1398 return (1, "FIXE225", [], 0)
1308 1399
1309 def __fixE231(self, code, line, pos): 1400 def __fixE231(self, code, line, pos):
1310 """ 1401 """
1311 Private method to fix missing whitespace after ',;:'. 1402 Private method to fix missing whitespace after ',;:'.
1312 1403
1313 Codes: E231 1404 Codes: E231
1314 1405
1315 @param code code of the issue (string) 1406 @param code code of the issue
1316 @param line line number of the issue (integer) 1407 @type str
1317 @param pos position inside line (integer) 1408 @param line line number of the issue
1318 @return value indicating an applied/deferred fix (-1, 0, 1), 1409 @type int
1319 a message for the fix (string) and an ID for a deferred 1410 @param pos position inside line
1320 fix (integer) 1411 @type int
1412 @return value indicating an applied/deferred fix (-1, 0, 1),
1413 a message code for the fix, a list of arguments for the
1414 message and an ID for a deferred fix
1415 @rtype tuple of (int, str, list or int, int)
1321 """ 1416 """
1322 line = line - 1 1417 line = line - 1
1323 pos = pos + 1 1418 pos = pos + 1
1324 self.__source[line] = ( 1419 self.__source[line] = (
1325 self.__source[line][:pos] + 1420 self.__source[line][:pos] +
1326 " " + 1421 " " +
1327 self.__source[line][pos:] 1422 self.__source[line][pos:]
1328 ) 1423 )
1329 # Missing whitespace added. 1424 # Missing whitespace added.
1330 return (1, "FIXE231", 0) 1425 return (1, "FIXE231", [], 0)
1331 1426
1332 def __fixE251(self, code, line, pos): 1427 def __fixE251(self, code, line, pos):
1333 """ 1428 """
1334 Private method to fix extraneous whitespace around keyword and 1429 Private method to fix extraneous whitespace around keyword and
1335 default parameter equals. 1430 default parameter equals.
1336 1431
1337 Codes: E251 1432 Codes: E251
1338 1433
1339 @param code code of the issue (string) 1434 @param code code of the issue
1340 @param line line number of the issue (integer) 1435 @type str
1341 @param pos position inside line (integer) 1436 @param line line number of the issue
1342 @return value indicating an applied/deferred fix (-1, 0, 1), 1437 @type int
1343 a message for the fix (string) and an ID for a deferred 1438 @param pos position inside line
1344 fix (integer) 1439 @type int
1440 @return value indicating an applied/deferred fix (-1, 0, 1),
1441 a message code for the fix, a list of arguments for the
1442 message and an ID for a deferred fix
1443 @rtype tuple of (int, str, list or int, int)
1345 """ 1444 """
1346 line = line - 1 1445 line = line - 1
1347 text = self.__source[line] 1446 text = self.__source[line]
1348 1447
1349 # This is necessary since pycodestyle sometimes reports columns that 1448 # This is necessary since pycodestyle sometimes reports columns that
1360 self.__source[line] = newText.rstrip("\n\r \t\\") 1459 self.__source[line] = newText.rstrip("\n\r \t\\")
1361 self.__source[line + 1] = self.__source[line + 1].lstrip() 1460 self.__source[line + 1] = self.__source[line + 1].lstrip()
1362 else: 1461 else:
1363 self.__source[line] = newText 1462 self.__source[line] = newText
1364 # Extraneous whitespace removed. 1463 # Extraneous whitespace removed.
1365 return (1, "FIXE251", 0) 1464 return (1, "FIXE251", [], 0)
1366 1465
1367 def __fixE261(self, code, line, pos): 1466 def __fixE261(self, code, line, pos):
1368 """ 1467 """
1369 Private method to fix whitespace before or after inline comment. 1468 Private method to fix whitespace before or after inline comment.
1370 1469
1371 Codes: E261, E262 1470 Codes: E261, E262
1372 1471
1373 @param code code of the issue (string) 1472 @param code code of the issue
1374 @param line line number of the issue (integer) 1473 @type str
1375 @param pos position inside line (integer) 1474 @param line line number of the issue
1376 @return value indicating an applied/deferred fix (-1, 0, 1), 1475 @type int
1377 a message for the fix (string) and an ID for a deferred 1476 @param pos position inside line
1378 fix (integer) 1477 @type int
1478 @return value indicating an applied/deferred fix (-1, 0, 1),
1479 a message code for the fix, a list of arguments for the
1480 message and an ID for a deferred fix
1481 @rtype tuple of (int, str, list or int, int)
1379 """ 1482 """
1380 line = line - 1 1483 line = line - 1
1381 text = self.__source[line] 1484 text = self.__source[line]
1382 left = text[:pos].rstrip(' \t#') 1485 left = text[:pos].rstrip(' \t#')
1383 right = text[pos:].lstrip(' \t#') 1486 right = text[pos:].lstrip(' \t#')
1384 newText = left + (" # " + right if right.strip() else right) 1487 newText = left + (" # " + right if right.strip() else right)
1385 self.__source[line] = newText 1488 self.__source[line] = newText
1386 # Whitespace around comment sign corrected. 1489 # Whitespace around comment sign corrected.
1387 return (1, "FIXE261", 0) 1490 return (1, "FIXE261", [], 0)
1388 1491
1389 def __fixBlankLinesBefore(self, code, line, pos, apply=False): 1492 def __fixBlankLinesBefore(self, code, line, pos, apply=False):
1390 """ 1493 """
1391 Private method to fix the need for blank lines before class, function 1494 Private method to fix the need for blank lines before class, function
1392 and method definitions. 1495 and method definitions.
1393 1496
1394 Codes: E301, E302, E303, E305, E306, E307, E308 1497 Codes: E301, E302, E303, E305, E306, E307, E308
1395 1498
1396 @param code code of the issue (string) 1499 @param code code of the issue
1397 @param line line number of the issue (integer) 1500 @type str
1398 @param pos position inside line (integer) 1501 @param line line number of the issue
1399 @keyparam apply flag indicating, that the fix should be applied 1502 @type int
1400 (boolean) 1503 @param pos position inside line
1401 @return value indicating an applied/deferred fix (-1, 0, 1), 1504 @type int
1402 a message for the fix (string) and an ID for a deferred 1505 @param apply flag indicating, that the fix should be applied
1403 fix (integer) 1506 @type bool
1507 @return value indicating an applied/deferred fix (-1, 0, 1),
1508 a message code for the fix, a list of arguments for the
1509 message and an ID for a deferred fix
1510 @rtype tuple of (int, str, list or int, int)
1404 """ 1511 """
1405 if apply: 1512 if apply:
1406 if code in ["E301", "E306", "E307"]: 1513 if code in ["E301", "E306", "E307"]:
1407 blankLinesBefore = self.__blankLines["method"] 1514 blankLinesBefore = self.__blankLines["method"]
1408 elif code == "E308": 1515 elif code == "E308":
1426 # insert blank lines (one or two) 1533 # insert blank lines (one or two)
1427 while delta < 0: 1534 while delta < 0:
1428 self.__source.insert(line, self.__eol) 1535 self.__source.insert(line, self.__eol)
1429 delta += 1 1536 delta += 1
1430 # %n blank line(s) inserted. 1537 # %n blank line(s) inserted.
1431 return (1, ("FIXE302+", blankLinesBefore - blanks), 0) 1538 return (1, "FIXE302+", blankLinesBefore - blanks, 0)
1432 elif delta > 0: 1539 elif delta > 0:
1433 # delete superfluous blank lines 1540 # delete superfluous blank lines
1434 while delta > 0: 1541 while delta > 0:
1435 del self.__source[line - 1] 1542 del self.__source[line - 1]
1436 line -= 1 1543 line -= 1
1437 delta -= 1 1544 delta -= 1
1438 # %n superfluous line(s) removed. 1545 # %n superfluous line(s) removed.
1439 return (1, ("FIXE302-", blanks - blankLinesBefore), 0) 1546 return (1, "FIXE302-", blanks - blankLinesBefore, 0)
1440 else: 1547 else:
1441 return (0, "", 0) 1548 return (0, "", [], 0)
1442 else: 1549 else:
1443 fixId = self.__getID() 1550 fixId = self.__getID()
1444 self.__stack.append((fixId, code, line, pos)) 1551 self.__stack.append((fixId, code, line, pos))
1445 return (-1, "", fixId) 1552 return (-1, "", [], fixId)
1446 1553
1447 def __fixE304(self, code, line, pos, apply=False): 1554 def __fixE304(self, code, line, pos, apply=False):
1448 """ 1555 """
1449 Private method to fix superfluous blank lines after a function 1556 Private method to fix superfluous blank lines after a function
1450 decorator. 1557 decorator.
1451 1558
1452 Codes: E304 1559 Codes: E304
1453 1560
1454 @param code code of the issue (string) 1561 @param code code of the issue
1455 @param line line number of the issue (integer) 1562 @type str
1456 @param pos position inside line (integer) 1563 @param line line number of the issue
1457 @keyparam apply flag indicating, that the fix should be applied 1564 @type int
1458 (boolean) 1565 @param pos position inside line
1459 @return value indicating an applied/deferred fix (-1, 0, 1), 1566 @type int
1460 a message for the fix (string) and an ID for a deferred 1567 @param apply flag indicating, that the fix should be applied
1461 fix (integer) 1568 @type bool
1569 @return value indicating an applied/deferred fix (-1, 0, 1),
1570 a message code for the fix, a list of arguments for the
1571 message and an ID for a deferred fix
1572 @rtype tuple of (int, str, list or int, int)
1462 """ 1573 """
1463 if apply: 1574 if apply:
1464 index = line - 2 1575 index = line - 2
1465 while index: 1576 while index:
1466 if self.__source[index].strip() == "": 1577 if self.__source[index].strip() == "":
1467 del self.__source[index] 1578 del self.__source[index]
1468 index -= 1 1579 index -= 1
1469 else: 1580 else:
1470 break 1581 break
1471 # Superfluous blank lines after function decorator removed. 1582 # Superfluous blank lines after function decorator removed.
1472 return (1, "FIXE304", 0) 1583 return (1, "FIXE304", [], 0)
1473 else: 1584 else:
1474 fixId = self.__getID() 1585 fixId = self.__getID()
1475 self.__stack.append((fixId, code, line, pos)) 1586 self.__stack.append((fixId, code, line, pos))
1476 return (-1, "", fixId) 1587 return (-1, "", [], fixId)
1477 1588
1478 def __fixE401(self, code, line, pos, apply=False): 1589 def __fixE401(self, code, line, pos, apply=False):
1479 """ 1590 """
1480 Private method to fix multiple imports on one line. 1591 Private method to fix multiple imports on one line.
1481 1592
1482 Codes: E401 1593 Codes: E401
1483 1594
1484 @param code code of the issue (string) 1595 @param code code of the issue
1485 @param line line number of the issue (integer) 1596 @type str
1486 @param pos position inside line (integer) 1597 @param line line number of the issue
1487 @keyparam apply flag indicating, that the fix should be applied 1598 @type int
1488 (boolean) 1599 @param pos position inside line
1489 @return value indicating an applied/deferred fix (-1, 0, 1), 1600 @type int
1490 a message for the fix (string) and an ID for a deferred 1601 @param apply flag indicating, that the fix should be applied
1491 fix (integer) 1602 @type bool
1603 @return value indicating an applied/deferred fix (-1, 0, 1),
1604 a message code for the fix, a list of arguments for the
1605 message and an ID for a deferred fix
1606 @rtype tuple of (int, str, list or int, int)
1492 """ 1607 """
1493 if apply: 1608 if apply:
1494 line = line - 1 1609 line = line - 1
1495 text = self.__source[line] 1610 text = self.__source[line]
1496 if not text.lstrip().startswith("import"): 1611 if not text.lstrip().startswith("import"):
1497 return (0, "", 0) 1612 return (0, "", [], 0)
1498 1613
1499 # pycodestyle (1.3.1) reports false positive if there is an import 1614 # pycodestyle (1.3.1) reports false positive if there is an import
1500 # statement followed by a semicolon and some unrelated 1615 # statement followed by a semicolon and some unrelated
1501 # statement with commas in it. 1616 # statement with commas in it.
1502 if ';' in text: 1617 if ';' in text:
1503 return (0, "", 0) 1618 return (0, "", [], 0)
1504 1619
1505 newText = ( 1620 newText = (
1506 text[:pos].rstrip("\t ,") + 1621 text[:pos].rstrip("\t ,") +
1507 self.__eol + 1622 self.__eol +
1508 self.__getIndent(text) + 1623 self.__getIndent(text) +
1509 "import " + 1624 "import " +
1510 text[pos:].lstrip("\t ,") 1625 text[pos:].lstrip("\t ,")
1511 ) 1626 )
1512 self.__source[line] = newText 1627 self.__source[line] = newText
1513 # Imports were put on separate lines. 1628 # Imports were put on separate lines.
1514 return (1, "FIXE401", 0) 1629 return (1, "FIXE401", [], 0)
1515 else: 1630 else:
1516 fixId = self.__getID() 1631 fixId = self.__getID()
1517 self.__stack.append((fixId, code, line, pos)) 1632 self.__stack.append((fixId, code, line, pos))
1518 return (-1, "", fixId) 1633 return (-1, "", [], fixId)
1519 1634
1520 def __fixE501(self, code, line, pos, apply=False): 1635 def __fixE501(self, code, line, pos, apply=False):
1521 """ 1636 """
1522 Private method to fix the long lines by breaking them. 1637 Private method to fix the long lines by breaking them.
1523 1638
1524 Codes: E501 1639 Codes: E501
1525 1640
1526 @param code code of the issue (string) 1641 @param code code of the issue
1527 @param line line number of the issue (integer) 1642 @type str
1528 @param pos position inside line (integer) 1643 @param line line number of the issue
1529 @keyparam apply flag indicating, that the fix should be applied 1644 @type int
1530 (boolean) 1645 @param pos position inside line
1531 @return value indicating an applied/deferred fix (-1, 0, 1), 1646 @type int
1532 a message for the fix (string) and an ID for a deferred 1647 @param apply flag indicating, that the fix should be applied
1533 fix (integer) 1648 @type bool
1649 @return value indicating an applied/deferred fix (-1, 0, 1),
1650 a message code for the fix, a list of arguments for the
1651 message and an ID for a deferred fix
1652 @rtype tuple of (int, str, list or int, int)
1534 """ 1653 """
1535 if apply: 1654 if apply:
1536 multilineStringLines, docStringLines = ( 1655 multilineStringLines, docStringLines = (
1537 self.__multilineStringLines() 1656 self.__multilineStringLines()
1538 ) 1657 )
1558 if newNextText and newNextText != nextText: 1677 if newNextText and newNextText != nextText:
1559 if newNextText == " ": 1678 if newNextText == " ":
1560 newNextText = "" 1679 newNextText = ""
1561 self.__source[line + 1] = newNextText 1680 self.__source[line + 1] = newNextText
1562 # Long lines have been shortened. 1681 # Long lines have been shortened.
1563 return (1, "FIXE501", 0) 1682 return (1, "FIXE501", [], 0)
1564 else: 1683 else:
1565 return (0, "", 0) 1684 return (0, "", [], 0)
1566 else: 1685 else:
1567 fixId = self.__getID() 1686 fixId = self.__getID()
1568 self.__stack.append((fixId, code, line, pos)) 1687 self.__stack.append((fixId, code, line, pos))
1569 return (-1, "", fixId) 1688 return (-1, "", [], fixId)
1570 1689
1571 def __fixE502(self, code, line, pos): 1690 def __fixE502(self, code, line, pos):
1572 """ 1691 """
1573 Private method to fix redundant backslash within brackets. 1692 Private method to fix redundant backslash within brackets.
1574 1693
1575 Codes: E502 1694 Codes: E502
1576 1695
1577 @param code code of the issue (string) 1696 @param code code of the issue
1578 @param line line number of the issue (integer) 1697 @type str
1579 @param pos position inside line (integer) 1698 @param line line number of the issue
1580 @return value indicating an applied/deferred fix (-1, 0, 1), 1699 @type int
1581 a message for the fix (string) and an ID for a deferred 1700 @param pos position inside line
1582 fix (integer) 1701 @type int
1702 @return value indicating an applied/deferred fix (-1, 0, 1),
1703 a message code for the fix, a list of arguments for the
1704 message and an ID for a deferred fix
1705 @rtype tuple of (int, str, list or int, int)
1583 """ 1706 """
1584 self.__source[line - 1] = ( 1707 self.__source[line - 1] = (
1585 self.__source[line - 1].rstrip("\n\r \t\\") + 1708 self.__source[line - 1].rstrip("\n\r \t\\") +
1586 self.__eol 1709 self.__eol
1587 ) 1710 )
1588 # Redundant backslash in brackets removed. 1711 # Redundant backslash in brackets removed.
1589 return (1, "FIXE502", 0) 1712 return (1, "FIXE502", [], 0)
1590 1713
1591 def __fixE701(self, code, line, pos, apply=False): 1714 def __fixE701(self, code, line, pos, apply=False):
1592 """ 1715 """
1593 Private method to fix colon-separated compound statements. 1716 Private method to fix colon-separated compound statements.
1594 1717
1595 Codes: E701 1718 Codes: E701
1596 1719
1597 @param code code of the issue (string) 1720 @param code code of the issue
1598 @param line line number of the issue (integer) 1721 @type str
1599 @param pos position inside line (integer) 1722 @param line line number of the issue
1600 @keyparam apply flag indicating, that the fix should be applied 1723 @type int
1601 (boolean) 1724 @param pos position inside line
1602 @return value indicating an applied/deferred fix (-1, 0, 1), 1725 @type int
1603 a message for the fix (string) and an ID for a deferred 1726 @param apply flag indicating, that the fix should be applied
1604 fix (integer) 1727 @type bool
1728 @return value indicating an applied/deferred fix (-1, 0, 1),
1729 a message code for the fix, a list of arguments for the
1730 message and an ID for a deferred fix
1731 @rtype tuple of (int, str, list or int, int)
1605 """ 1732 """
1606 if apply: 1733 if apply:
1607 line = line - 1 1734 line = line - 1
1608 text = self.__source[line] 1735 text = self.__source[line]
1609 pos = pos + 1 1736 pos = pos + 1
1616 text[pos:].lstrip("\n\r \t\\") + 1743 text[pos:].lstrip("\n\r \t\\") +
1617 self.__eol 1744 self.__eol
1618 ) 1745 )
1619 self.__source[line] = newText 1746 self.__source[line] = newText
1620 # Compound statement corrected. 1747 # Compound statement corrected.
1621 return (1, "FIXE701", 0) 1748 return (1, "FIXE701", [], 0)
1622 else: 1749 else:
1623 fixId = self.__getID() 1750 fixId = self.__getID()
1624 self.__stack.append((fixId, code, line, pos)) 1751 self.__stack.append((fixId, code, line, pos))
1625 return (-1, "", fixId) 1752 return (-1, "", [], fixId)
1626 1753
1627 def __fixE702(self, code, line, pos, apply=False): 1754 def __fixE702(self, code, line, pos, apply=False):
1628 """ 1755 """
1629 Private method to fix semicolon-separated compound statements. 1756 Private method to fix semicolon-separated compound statements.
1630 1757
1631 Codes: E702, E703 1758 Codes: E702, E703
1632 1759
1633 @param code code of the issue (string) 1760 @param code code of the issue
1634 @param line line number of the issue (integer) 1761 @type str
1635 @param pos position inside line (integer) 1762 @param line line number of the issue
1636 @keyparam apply flag indicating, that the fix should be applied 1763 @type int
1637 (boolean) 1764 @param pos position inside line
1638 @return value indicating an applied/deferred fix (-1, 0, 1), 1765 @type int
1639 a message for the fix (string) and an ID for a deferred 1766 @param apply flag indicating, that the fix should be applied
1640 fix (integer) 1767 @type bool
1768 @return value indicating an applied/deferred fix (-1, 0, 1),
1769 a message code for the fix, a list of arguments for the
1770 message and an ID for a deferred fix
1771 @rtype tuple of (int, str, list or int, int)
1641 """ 1772 """
1642 if apply: 1773 if apply:
1643 line = line - 1 1774 line = line - 1
1644 text = self.__source[line] 1775 text = self.__source[line]
1645 1776
1652 else: 1783 else:
1653 first = text[:pos].rstrip("\n\r \t;") + self.__eol 1784 first = text[:pos].rstrip("\n\r \t;") + self.__eol
1654 second = text[pos:].lstrip("\n\r \t;") 1785 second = text[pos:].lstrip("\n\r \t;")
1655 self.__source[line] = first + self.__getIndent(text) + second 1786 self.__source[line] = first + self.__getIndent(text) + second
1656 # Compound statement corrected. 1787 # Compound statement corrected.
1657 return (1, "FIXE702", 0) 1788 return (1, "FIXE702", [], 0)
1658 else: 1789 else:
1659 fixId = self.__getID() 1790 fixId = self.__getID()
1660 self.__stack.append((fixId, code, line, pos)) 1791 self.__stack.append((fixId, code, line, pos))
1661 return (-1, "", fixId) 1792 return (-1, "", [], fixId)
1662 1793
1663 def __fixE711(self, code, line, pos): 1794 def __fixE711(self, code, line, pos):
1664 """ 1795 """
1665 Private method to fix comparison with None. 1796 Private method to fix comparison with None.
1666 1797
1667 Codes: E711, E712 1798 Codes: E711, E712
1668 1799
1669 @param code code of the issue (string) 1800 @param code code of the issue
1670 @param line line number of the issue (integer) 1801 @type str
1671 @param pos position inside line (integer) 1802 @param line line number of the issue
1672 @return value indicating an applied/deferred fix (-1, 0, 1), 1803 @type int
1673 a message for the fix (string) and an ID for a deferred 1804 @param pos position inside line
1674 fix (integer) 1805 @type int
1806 @return value indicating an applied/deferred fix (-1, 0, 1),
1807 a message code for the fix, a list of arguments for the
1808 message and an ID for a deferred fix
1809 @rtype tuple of (int, str, list or int, int)
1675 """ 1810 """
1676 line = line - 1 1811 line = line - 1
1677 text = self.__source[line] 1812 text = self.__source[line]
1678 1813
1679 rightPos = pos + 2 1814 rightPos = pos + 2
1683 left = text[:pos].rstrip() 1818 left = text[:pos].rstrip()
1684 center = text[pos:rightPos] 1819 center = text[pos:rightPos]
1685 right = text[rightPos:].lstrip() 1820 right = text[rightPos:].lstrip()
1686 1821
1687 if not right.startswith(("None", "True", "False")): 1822 if not right.startswith(("None", "True", "False")):
1688 return (0, "", 0) 1823 return (0, "", [], 0)
1689 1824
1690 if center.strip() == "==": 1825 if center.strip() == "==":
1691 center = "is" 1826 center = "is"
1692 elif center.strip() == "!=": 1827 elif center.strip() == "!=":
1693 center = "is not" 1828 center = "is not"
1694 else: 1829 else:
1695 return (0, "", 0) 1830 return (0, "", [], 0)
1696 1831
1697 self.__source[line] = " ".join([left, center, right]) 1832 self.__source[line] = " ".join([left, center, right])
1698 # Comparison to None/True/False corrected. 1833 # Comparison to None/True/False corrected.
1699 return (1, "FIXE711", 0) 1834 return (1, "FIXE711", [], 0)
1700 1835
1701 def __fixN804(self, code, line, pos, apply=False): 1836 def __fixN804(self, code, line, pos, apply=False):
1702 """ 1837 """
1703 Private method to fix a wrong first argument of normal and 1838 Private method to fix a wrong first argument of normal and
1704 class methods. 1839 class methods.
1705 1840
1706 Codes: N804, N805 1841 Codes: N804, N805
1707 1842
1708 @param code code of the issue (string) 1843 @param code code of the issue
1709 @param line line number of the issue (integer) 1844 @type str
1710 @param pos position inside line (integer) 1845 @param line line number of the issue
1711 @keyparam apply flag indicating, that the fix should be applied 1846 @type int
1712 (boolean) 1847 @param pos position inside line
1713 @return value indicating an applied/deferred fix (-1, 0, 1), 1848 @type int
1714 a message for the fix (string) and an ID for a deferred 1849 @param apply flag indicating, that the fix should be applied
1715 fix (integer) 1850 @type bool
1851 @return value indicating an applied/deferred fix (-1, 0, 1),
1852 a message code for the fix, a list of arguments for the
1853 message and an ID for a deferred fix
1854 @rtype tuple of (int, str, list or int, int)
1716 """ 1855 """
1717 if apply: 1856 if apply:
1718 line = line - 1 1857 line = line - 1
1719 text = self.__source[line] 1858 text = self.__source[line]
1720 if code == "N804": 1859 if code == "N804":
1740 else: 1879 else:
1741 center = arg + ", " 1880 center = arg + ", "
1742 newText = left + center + right 1881 newText = left + center + right
1743 self.__source[line] = newText 1882 self.__source[line] = newText
1744 # '{0}' argument added. 1883 # '{0}' argument added.
1745 return (1, ("FIXN804", (arg,)), 0) 1884 return (1, "FIXN804", [arg], 0)
1746 else: 1885 else:
1747 fixId = self.__getID() 1886 fixId = self.__getID()
1748 self.__stack.append((fixId, code, line, pos)) 1887 self.__stack.append((fixId, code, line, pos))
1749 return (-1, "", fixId) 1888 return (-1, "", [], fixId)
1750 1889
1751 def __fixN806(self, code, line, pos, apply=False): 1890 def __fixN806(self, code, line, pos, apply=False):
1752 """ 1891 """
1753 Private method to fix a wrong first argument of static methods. 1892 Private method to fix a wrong first argument of static methods.
1754 1893
1755 Codes: N806 1894 Codes: N806
1756 1895
1757 @param code code of the issue (string) 1896 @param code code of the issue
1758 @param line line number of the issue (integer) 1897 @type str
1759 @param pos position inside line (integer) 1898 @param line line number of the issue
1760 @keyparam apply flag indicating, that the fix should be applied 1899 @type int
1761 (boolean) 1900 @param pos position inside line
1762 @return value indicating an applied/deferred fix (-1, 0, 1), 1901 @type int
1763 a message for the fix (string) and an ID for a deferred 1902 @param apply flag indicating, that the fix should be applied
1764 fix (integer) 1903 @type bool
1904 @return value indicating an applied/deferred fix (-1, 0, 1),
1905 a message code for the fix, a list of arguments for the
1906 message and an ID for a deferred fix
1907 @rtype tuple of (int, str, list or int, int)
1765 """ 1908 """
1766 if apply: 1909 if apply:
1767 line = line - 1 1910 line = line - 1
1768 text = self.__source[line] 1911 text = self.__source[line]
1769 index = text.find("(") + 1 1912 index = text.find("(") + 1
1802 self.__source[line] = "" 1945 self.__source[line] = ""
1803 else: 1946 else:
1804 self.__source[line] = indent + right 1947 self.__source[line] = indent + right
1805 1948
1806 # '{0}' argument removed. 1949 # '{0}' argument removed.
1807 return (1, ("FIXN806", arg), 0) 1950 return (1, "FIXN806", [arg], 0)
1808 else: 1951 else:
1809 fixId = self.__getID() 1952 fixId = self.__getID()
1810 self.__stack.append((fixId, code, line, pos)) 1953 self.__stack.append((fixId, code, line, pos))
1811 return (-1, "", fixId) 1954 return (-1, "", [], fixId)
1812 1955
1813 def __fixW291(self, code, line, pos): 1956 def __fixW291(self, code, line, pos):
1814 """ 1957 """
1815 Private method to fix trailing whitespace. 1958 Private method to fix trailing whitespace.
1816 1959
1817 Codes: W291, W293 1960 Codes: W291, W293
1818 1961
1819 @param code code of the issue (string) 1962 @param code code of the issue
1820 @param line line number of the issue (integer) 1963 @type str
1821 @param pos position inside line (integer) 1964 @param line line number of the issue
1822 @return value indicating an applied/deferred fix (-1, 0, 1), 1965 @type int
1823 a message for the fix (string) and an ID for a deferred 1966 @param pos position inside line
1824 fix (integer) 1967 @type int
1968 @return value indicating an applied/deferred fix (-1, 0, 1),
1969 a message code for the fix, a list of arguments for the
1970 message and an ID for a deferred fix
1971 @rtype tuple of (int, str, list or int, int)
1825 """ 1972 """
1826 self.__source[line - 1] = re.sub(r'[\t ]+(\r?)$', r"\1", 1973 self.__source[line - 1] = re.sub(r'[\t ]+(\r?)$', r"\1",
1827 self.__source[line - 1]) 1974 self.__source[line - 1])
1828 # Whitespace stripped from end of line. 1975 # Whitespace stripped from end of line.
1829 return (1, "FIXW291", 0) 1976 return (1, "FIXW291", [], 0)
1830 1977
1831 def __fixW292(self, code, line, pos): 1978 def __fixW292(self, code, line, pos):
1832 """ 1979 """
1833 Private method to fix a missing newline at the end of file. 1980 Private method to fix a missing newline at the end of file.
1834 1981
1835 Codes: W292 1982 Codes: W292
1836 1983
1837 @param code code of the issue (string) 1984 @param code code of the issue
1838 @param line line number of the issue (integer) 1985 @type str
1839 @param pos position inside line (integer) 1986 @param line line number of the issue
1840 @return value indicating an applied/deferred fix (-1, 0, 1), 1987 @type int
1841 a message for the fix (string) and an ID for a deferred 1988 @param pos position inside line
1842 fix (integer) 1989 @type int
1990 @return value indicating an applied/deferred fix (-1, 0, 1),
1991 a message code for the fix, a list of arguments for the
1992 message and an ID for a deferred fix
1993 @rtype tuple of (int, str, list or int, int)
1843 """ 1994 """
1844 self.__source[line - 1] += self.__eol 1995 self.__source[line - 1] += self.__eol
1845 # newline added to end of file. 1996 # newline added to end of file.
1846 return (1, "FIXW292", 0) 1997 return (1, "FIXW292", [], 0)
1847 1998
1848 def __fixW391(self, code, line, pos): 1999 def __fixW391(self, code, line, pos):
1849 """ 2000 """
1850 Private method to fix trailing blank lines. 2001 Private method to fix trailing blank lines.
1851 2002
1852 Codes: W391 2003 Codes: W391
1853 2004
1854 @param code code of the issue (string) 2005 @param code code of the issue
1855 @param line line number of the issue (integer) 2006 @type str
1856 @param pos position inside line (integer) 2007 @param line line number of the issue
1857 @return value indicating an applied/deferred fix (-1, 0, 1), 2008 @type int
1858 a message for the fix (string) and an ID for a deferred 2009 @param pos position inside line
1859 fix (integer) 2010 @type int
2011 @return value indicating an applied/deferred fix (-1, 0, 1),
2012 a message code for the fix, a list of arguments for the
2013 message and an ID for a deferred fix
2014 @rtype tuple of (int, str, list or int, int)
1860 """ 2015 """
1861 index = line - 1 2016 index = line - 1
1862 while index: 2017 while index:
1863 if self.__source[index].strip() == "": 2018 if self.__source[index].strip() == "":
1864 del self.__source[index] 2019 del self.__source[index]
1865 index -= 1 2020 index -= 1
1866 else: 2021 else:
1867 break 2022 break
1868 # Superfluous trailing blank lines removed from end of file. 2023 # Superfluous trailing blank lines removed from end of file.
1869 return (1, "FIXW391", 0) 2024 return (1, "FIXW391", [], 0)
1870 2025
1871 def __fixW603(self, code, line, pos): 2026 def __fixW603(self, code, line, pos):
1872 """ 2027 """
1873 Private method to fix the not equal notation. 2028 Private method to fix the not equal notation.
1874 2029
1875 Codes: W603 2030 Codes: W603
1876 2031
1877 @param code code of the issue (string) 2032 @param code code of the issue
1878 @param line line number of the issue (integer) 2033 @type str
1879 @param pos position inside line (integer) 2034 @param line line number of the issue
1880 @return value indicating an applied/deferred fix (-1, 0, 1), 2035 @type int
1881 a message for the fix (string) and an ID for a deferred 2036 @param pos position inside line
1882 fix (integer) 2037 @type int
2038 @return value indicating an applied/deferred fix (-1, 0, 1),
2039 a message code for the fix, a list of arguments for the
2040 message and an ID for a deferred fix
2041 @rtype tuple of (int, str, list or int, int)
1883 """ 2042 """
1884 self.__source[line - 1] = self.__source[line - 1].replace("<>", "!=") 2043 self.__source[line - 1] = self.__source[line - 1].replace("<>", "!=")
1885 # '<>' replaced by '!='. 2044 # '<>' replaced by '!='.
1886 return (1, "FIXW603", 0) 2045 return (1, "FIXW603", [], 0)
1887 2046
1888 2047
1889 class Reindenter(object): 2048 class Reindenter(object):
1890 """ 2049 """
1891 Class to reindent badly-indented code to uniformly use four-space 2050 Class to reindent badly-indented code to uniformly use four-space

eric ide

mercurial