src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleChecker.py

branch
eric7
changeset 11160
070b01a1a4c1
parent 11159
2abfc48a72db
equal deleted inserted replaced
11159:2abfc48a72db 11160:070b01a1a4c1
52 return codeStyleBatchCheck 52 return codeStyleBatchCheck
53 53
54 54
55 class CodeStyleCheckerReport(pycodestyle.BaseReport): 55 class CodeStyleCheckerReport(pycodestyle.BaseReport):
56 """ 56 """
57 Class implementing a special report to be used with our dialog. 57 Class implementing a special 'pycodestyle' report to be used with our checker.
58 """ 58 """
59 59
60 def __init__(self, options): 60 def __init__(self, options):
61 """ 61 """
62 Constructor 62 Constructor
85 @type list 85 @type list
86 @return error code 86 @return error code
87 @rtype str 87 @rtype str
88 """ 88 """
89 code = text.split(None, 1)[0] 89 code = text.split(None, 1)[0]
90 if self._ignore_code(code):
91 return None
92
90 errorCode = code[0] + "-" + code[1:] 93 errorCode = code[0] + "-" + code[1:]
91 if self._ignore_code(errorCode):
92 return None
93 94
94 if errorCode in self.counters: 95 if errorCode in self.counters:
95 self.counters[errorCode] += 1 96 self.counters[errorCode] += 1
96 else: 97 else:
97 self.counters[errorCode] = 1 98 self.counters[errorCode] = 1
447 else None 448 else None
448 ) 449 )
449 450
450 if not errors: 451 if not errors:
451 if includeMessages: 452 if includeMessages:
452 select = [s.strip() for s in includeMessages.split(",") if s.strip()] 453 selected = [s.strip() for s in includeMessages.split(",") if s.strip()]
453 else: 454 else:
454 select = [] 455 selected = []
455 if excludeMessages: 456 if excludeMessages:
456 ignore = [i.strip() for i in excludeMessages.split(",") if i.strip()] 457 ignored = [i.strip() for i in excludeMessages.split(",") if i.strip()]
457 else: 458 else:
458 ignore = [] 459 ignored = []
459 460
460 syntaxError, syntaxStats, tree = __checkSyntax(filename, source) 461 syntaxError, syntaxStats, tree = __checkSyntax(filename, source)
461 462
462 # perform the checks only, if syntax is ok and AST tree was generated 463 # perform the checks only, if syntax is ok and AST tree was generated
463 if tree: 464 if tree:
466 for includeMessage in includeMessages.split(","): 467 for includeMessage in includeMessages.split(","):
467 category = includeMessage.strip().split("-", 1)[0] 468 category = includeMessage.strip().split("-", 1)[0]
468 enabledCategories.add(category) 469 enabledCategories.add(category)
469 470
470 # check coding style 471 # check coding style
471 pycodestyle.BLANK_LINES_CONFIG = { 472 if "E" in enabledCategories or "W" in enabledCategories:
472 # Top level class and function. 473 pycodestyle.BLANK_LINES_CONFIG = {
473 "top_level": blankLines[0], 474 # Top level class and function.
474 # Methods and nested class and function. 475 "top_level": blankLines[0],
475 "method": blankLines[1], 476 # Methods and nested class and function.
476 } 477 "method": blankLines[1],
477 styleGuide = pycodestyle.StyleGuide( 478 }
478 reporter=CodeStyleCheckerReport, 479 styleGuide = pycodestyle.StyleGuide(
479 repeat=repeatMessages, 480 reporter=CodeStyleCheckerReport,
480 select=[], 481 repeat=repeatMessages,
481 ignore=[x for x in ignore if x.startswith(("E", "W"))], 482 select=[
482 max_line_length=maxLineLength, 483 x.replace("-", "", 1) # change to pycodestyle error codes
483 max_doc_length=maxDocLineLength, 484 for x in selected
484 hang_closing=hangClosing, 485 if x.startswith(("E-", "W-"))
485 ) 486 ],
486 report = styleGuide.options.report 487 ignore=[
487 styleGuide.input_file(filename, lines=source) 488 x.replace("-", "", 1) # change to pycodestyle error codes
488 stats.update(report.counters) 489 for x in ignored
489 errors += report.errors 490 if x.startswith(("E-", "W-"))
491 ],
492 max_line_length=maxLineLength,
493 max_doc_length=maxDocLineLength,
494 hang_closing=hangClosing,
495 )
496 report = styleGuide.options.report
497 styleGuide.input_file(filename, lines=source)
498 stats.update(report.counters)
499 errors += report.errors
490 500
491 # check documentation style 501 # check documentation style
492 if DocStyleChecker.Category in enabledCategories: 502 if DocStyleChecker.Category in enabledCategories:
493 docStyleChecker = DocStyleChecker( 503 docStyleChecker = DocStyleChecker(
494 source, 504 source,
495 filename, 505 filename,
496 select, 506 selected,
497 ignore, 507 ignored,
498 [], 508 [],
499 repeatMessages, 509 repeatMessages,
500 maxLineLength=maxDocLineLength, 510 maxLineLength=maxDocLineLength,
501 docType=docType, 511 docType=docType,
502 ) 512 )
508 if MiscellaneousChecker.Category in enabledCategories: 518 if MiscellaneousChecker.Category in enabledCategories:
509 miscellaneousChecker = MiscellaneousChecker( 519 miscellaneousChecker = MiscellaneousChecker(
510 source, 520 source,
511 filename, 521 filename,
512 tree, 522 tree,
513 select, 523 selected,
514 ignore, 524 ignored,
515 [], 525 [],
516 repeatMessages, 526 repeatMessages,
517 miscellaneousArgs, 527 miscellaneousArgs,
518 ) 528 )
519 miscellaneousChecker.run() 529 miscellaneousChecker.run()
521 errors += miscellaneousChecker.errors 531 errors += miscellaneousChecker.errors
522 532
523 # check code complexity 533 # check code complexity
524 if ComplexityChecker.Category in enabledCategories: 534 if ComplexityChecker.Category in enabledCategories:
525 complexityChecker = ComplexityChecker( 535 complexityChecker = ComplexityChecker(
526 source, filename, tree, select, ignore, codeComplexityArgs 536 source, filename, tree, selected, ignored, codeComplexityArgs
527 ) 537 )
528 complexityChecker.run() 538 complexityChecker.run()
529 stats.update(complexityChecker.counters) 539 stats.update(complexityChecker.counters)
530 errors += complexityChecker.errors 540 errors += complexityChecker.errors
531 541
533 if AnnotationsChecker.Category in enabledCategories: 543 if AnnotationsChecker.Category in enabledCategories:
534 annotationsChecker = AnnotationsChecker( 544 annotationsChecker = AnnotationsChecker(
535 source, 545 source,
536 filename, 546 filename,
537 tree, 547 tree,
538 select, 548 selected,
539 ignore, 549 ignored,
540 [], 550 [],
541 repeatMessages, 551 repeatMessages,
542 annotationArgs, 552 annotationArgs,
543 ) 553 )
544 annotationsChecker.run() 554 annotationsChecker.run()
549 if SecurityChecker.Category in enabledCategories: 559 if SecurityChecker.Category in enabledCategories:
550 securityChecker = SecurityChecker( 560 securityChecker = SecurityChecker(
551 source, 561 source,
552 filename, 562 filename,
553 tree, 563 tree,
554 select, 564 selected,
555 ignore, 565 ignored,
556 [], 566 [],
557 repeatMessages, 567 repeatMessages,
558 securityArgs, 568 securityArgs,
559 ) 569 )
560 securityChecker.run() 570 securityChecker.run()
562 errors += securityChecker.errors 572 errors += securityChecker.errors
563 573
564 # check for pathlib usage 574 # check for pathlib usage
565 if PathlibChecker.Category in enabledCategories: 575 if PathlibChecker.Category in enabledCategories:
566 pathlibChecker = PathlibChecker( 576 pathlibChecker = PathlibChecker(
567 source, filename, tree, select, ignore, [], repeatMessages 577 source, filename, tree, selected, ignored, [], repeatMessages
568 ) 578 )
569 pathlibChecker.run() 579 pathlibChecker.run()
570 stats.update(pathlibChecker.counters) 580 stats.update(pathlibChecker.counters)
571 errors += pathlibChecker.errors 581 errors += pathlibChecker.errors
572 582
573 # check for code simplifications 583 # check for code simplifications
574 if SimplifyChecker.Category in enabledCategories: 584 if SimplifyChecker.Category in enabledCategories:
575 simplifyChecker = SimplifyChecker( 585 simplifyChecker = SimplifyChecker(
576 source, filename, tree, select, ignore, [], repeatMessages 586 source, filename, tree, selected, ignored, [], repeatMessages
577 ) 587 )
578 simplifyChecker.run() 588 simplifyChecker.run()
579 stats.update(simplifyChecker.counters) 589 stats.update(simplifyChecker.counters)
580 errors += simplifyChecker.errors 590 errors += simplifyChecker.errors
581 591
583 if ImportsChecker.Category in enabledCategories: 593 if ImportsChecker.Category in enabledCategories:
584 importsChecker = ImportsChecker( 594 importsChecker = ImportsChecker(
585 source, 595 source,
586 filename, 596 filename,
587 tree, 597 tree,
588 select, 598 selected,
589 ignore, 599 ignored,
590 [], 600 [],
591 repeatMessages, 601 repeatMessages,
592 importsArgs, 602 importsArgs,
593 ) 603 )
594 importsChecker.run() 604 importsChecker.run()
599 if NamingStyleChecker.Category in enabledCategories: 609 if NamingStyleChecker.Category in enabledCategories:
600 namingStyleChecker = NamingStyleChecker( 610 namingStyleChecker = NamingStyleChecker(
601 source, 611 source,
602 filename, 612 filename,
603 tree, 613 tree,
604 select, 614 selected,
605 ignore, 615 ignored,
606 [], 616 [],
607 repeatMessages, 617 repeatMessages,
608 {}, # no arguments yet 618 {}, # no arguments yet
609 ) 619 )
610 namingStyleChecker.run() 620 namingStyleChecker.run()
615 if NameOrderChecker.Category in enabledCategories: 625 if NameOrderChecker.Category in enabledCategories:
616 nameOrderChecker = NameOrderChecker( 626 nameOrderChecker = NameOrderChecker(
617 source, 627 source,
618 filename, 628 filename,
619 tree, 629 tree,
620 select, 630 selected,
621 ignore, 631 ignored,
622 [], 632 [],
623 repeatMessages, 633 repeatMessages,
624 nameOrderArgs, 634 nameOrderArgs,
625 ) 635 )
626 nameOrderChecker.run() 636 nameOrderChecker.run()
631 if UnusedChecker.Category in enabledCategories: 641 if UnusedChecker.Category in enabledCategories:
632 unusedChecker = UnusedChecker( 642 unusedChecker = UnusedChecker(
633 source, 643 source,
634 filename, 644 filename,
635 tree, 645 tree,
636 select, 646 selected,
637 ignore, 647 ignored,
638 [], 648 [],
639 repeatMessages, 649 repeatMessages,
640 unusedArgs, 650 unusedArgs,
641 ) 651 )
642 unusedChecker.run() 652 unusedChecker.run()
647 if AsyncChecker.Category in enabledCategories: 657 if AsyncChecker.Category in enabledCategories:
648 asyncChecker = AsyncChecker( 658 asyncChecker = AsyncChecker(
649 source, 659 source,
650 filename, 660 filename,
651 tree, 661 tree,
652 select, 662 selected,
653 ignore, 663 ignored,
654 [], 664 [],
655 repeatMessages, 665 repeatMessages,
656 {}, # no arguments yet 666 {}, # no arguments yet
657 ) 667 )
658 asyncChecker.run() 668 asyncChecker.run()
663 if LoggingChecker.Category in enabledCategories: 673 if LoggingChecker.Category in enabledCategories:
664 loggingChecker = LoggingChecker( 674 loggingChecker = LoggingChecker(
665 source, 675 source,
666 filename, 676 filename,
667 tree, 677 tree,
668 select, 678 selected,
669 ignore, 679 ignored,
670 [], 680 [],
671 repeatMessages, 681 repeatMessages,
672 {}, # no arguments yet 682 {}, # no arguments yet
673 ) 683 )
674 loggingChecker.run() 684 loggingChecker.run()
679 if PydanticChecker.Category in enabledCategories: 689 if PydanticChecker.Category in enabledCategories:
680 pydanticChecker = PydanticChecker( 690 pydanticChecker = PydanticChecker(
681 source, 691 source,
682 filename, 692 filename,
683 tree, 693 tree,
684 select, 694 selected,
685 ignore, 695 ignored,
686 [], 696 [],
687 repeatMessages, 697 repeatMessages,
688 {}, # no arguments yet 698 {}, # no arguments yet
689 ) 699 )
690 pydanticChecker.run() 700 pydanticChecker.run()

eric ide

mercurial