PluginMetricsRadon.py

branch
eric7
changeset 83
d3490ea9facc
parent 80
a4f2000c6687
child 86
326e08294b3d
equal deleted inserted replaced
82:042dcb26e7b4 83:d3490ea9facc
8 """ 8 """
9 9
10 import contextlib 10 import contextlib
11 import os 11 import os
12 12
13 from PyQt5.QtCore import pyqtSignal, QObject, QTranslator 13 from PyQt6.QtCore import pyqtSignal, QObject, QTranslator
14 from PyQt5.QtWidgets import QAction 14 from PyQt6.QtGui import QAction
15 15
16 from E5Gui.E5Application import e5App 16 from EricGui.EricAction import EricAction
17 from E5Gui.E5Action import E5Action 17
18 from E5Gui import E5MessageBox 18 from EricWidgets.EricApplication import ericApp
19 from EricWidgets import EricMessageBox
19 20
20 from Project.ProjectBrowserModel import ProjectBrowserFileItem 21 from Project.ProjectBrowserModel import ProjectBrowserFileItem
21 22
22 import Preferences 23 import Preferences
23 from Utilities import determinePythonVersion 24 from Utilities import determinePythonVersion
25 # Start-Of-Header 26 # Start-Of-Header
26 name = "Radon Metrics Plugin" 27 name = "Radon Metrics Plugin"
27 author = "Detlev Offenbach <detlev@die-offenbachs.de>" 28 author = "Detlev Offenbach <detlev@die-offenbachs.de>"
28 autoactivate = True 29 autoactivate = True
29 deactivateable = True 30 deactivateable = True
30 version = "4.0.0" 31 version = "1.0.0"
31 className = "RadonMetricsPlugin" 32 className = "RadonMetricsPlugin"
32 packageName = "RadonMetrics" 33 packageName = "RadonMetrics"
33 shortDescription = "Code metrics plugin using radon package" 34 shortDescription = "Code metrics plugin using radon package"
34 longDescription = ( 35 longDescription = (
35 """This plug-in implements dialogs to show various code metrics. These""" 36 """This plug-in implements dialogs to show various code metrics. These"""
67 def __init__(self, ui): 68 def __init__(self, ui):
68 """ 69 """
69 Constructor 70 Constructor
70 71
71 @param ui reference to the user interface object 72 @param ui reference to the user interface object
72 @type UI.UserInterface 73 @type UserInterface
73 """ 74 """
74 super().__init__(ui) 75 super().__init__(ui)
75 self.__ui = ui 76 self.__ui = ui
76 self.__initialize() 77 self.__initialize()
77 78
78 self.backgroundService = e5App().getObject("BackgroundService") 79 self.backgroundService = ericApp().getObject("BackgroundService")
79 80
80 path = os.path.join(os.path.dirname(__file__), packageName) 81 path = os.path.join(os.path.dirname(__file__), packageName)
81 82
82 # raw code metrics calculation 83 # raw code metrics calculation
83 self.backgroundService.serviceConnect( 84 self.backgroundService.serviceConnect(
382 """ 383 """
383 global error 384 global error
384 error = "" # clear previous error 385 error = "" # clear previous error
385 386
386 # Project menu actions 387 # Project menu actions
387 menu = e5App().getObject("Project").getMenu("Show") 388 menu = ericApp().getObject("Project").getMenu("Show")
388 if menu: 389 if menu:
389 if not menu.isEmpty(): 390 if not menu.isEmpty():
390 act = menu.addSeparator() 391 act = menu.addSeparator()
391 self.__projectSeparatorActs.append(act) 392 self.__projectSeparatorActs.append(act)
392 393
397 act.setFont(font) 398 act.setFont(font)
398 act.triggered.connect(self.__showRadonVersion) 399 act.triggered.connect(self.__showRadonVersion)
399 menu.addAction(act) 400 menu.addAction(act)
400 self.__projectMetricsActs.append(act) 401 self.__projectMetricsActs.append(act)
401 402
402 act = E5Action( 403 act = EricAction(
403 self.tr('Code Metrics'), 404 self.tr('Code Metrics'),
404 self.tr('Code &Metrics...'), 0, 0, 405 self.tr('Code &Metrics...'), 0, 0,
405 self, 'project_show_radon_raw') 406 self, 'project_show_radon_raw')
406 act.setStatusTip( 407 act.setStatusTip(
407 self.tr('Show raw code metrics.')) 408 self.tr('Show raw code metrics.'))
414 )) 415 ))
415 act.triggered.connect(self.__projectRawMetrics) 416 act.triggered.connect(self.__projectRawMetrics)
416 menu.addAction(act) 417 menu.addAction(act)
417 self.__projectMetricsActs.append(act) 418 self.__projectMetricsActs.append(act)
418 419
419 act = E5Action( 420 act = EricAction(
420 self.tr('Maintainability Index'), 421 self.tr('Maintainability Index'),
421 self.tr('Maintainability &Index...'), 0, 0, 422 self.tr('Maintainability &Index...'), 0, 0,
422 self, 'project_show_radon_mi') 423 self, 'project_show_radon_mi')
423 act.setStatusTip( 424 act.setStatusTip(
424 self.tr('Show the maintainability index for Python files.')) 425 self.tr('Show the maintainability index for Python files.'))
429 )) 430 ))
430 act.triggered.connect(self.__projectMaintainabilityIndex) 431 act.triggered.connect(self.__projectMaintainabilityIndex)
431 menu.addAction(act) 432 menu.addAction(act)
432 self.__projectMetricsActs.append(act) 433 self.__projectMetricsActs.append(act)
433 434
434 act = E5Action( 435 act = EricAction(
435 self.tr('Cyclomatic Complexity'), 436 self.tr('Cyclomatic Complexity'),
436 self.tr('Cyclomatic &Complexity...'), 0, 0, 437 self.tr('Cyclomatic &Complexity...'), 0, 0,
437 self, 'project_show_radon_cc') 438 self, 'project_show_radon_cc')
438 act.setStatusTip( 439 act.setStatusTip(
439 self.tr('Show the cyclomatic complexity for Python files.')) 440 self.tr('Show the cyclomatic complexity for Python files.'))
447 self.__projectMetricsActs.append(act) 448 self.__projectMetricsActs.append(act)
448 449
449 act = menu.addSeparator() 450 act = menu.addSeparator()
450 self.__projectSeparatorActs.append(act) 451 self.__projectSeparatorActs.append(act)
451 452
452 e5App().getObject("Project").addE5Actions( 453 ericApp().getObject("Project").addEricActions(
453 self.__projectMetricsActs[1:]) 454 self.__projectMetricsActs[1:])
454 455
455 # Editor menu actions 456 # Editor menu actions (one separator each above and below)
456 act = QAction(self) 457 act = QAction(self)
457 act.setSeparator(True) 458 act.setSeparator(True)
458 self.__editorSeparatorActs.append(act) 459 self.__editorSeparatorActs.append(act)
459 act = QAction(self) 460 act = QAction(self)
460 act.setSeparator(True) 461 act.setSeparator(True)
466 font.setBold(True) 467 font.setBold(True)
467 act.setFont(font) 468 act.setFont(font)
468 act.triggered.connect(self.__showRadonVersion) 469 act.triggered.connect(self.__showRadonVersion)
469 self.__editorMetricsActs.append(act) 470 self.__editorMetricsActs.append(act)
470 471
471 act = E5Action( 472 act = EricAction(
472 self.tr('Code Metrics'), 473 self.tr('Code Metrics'),
473 self.tr('Code &Metrics...'), 0, 0, 474 self.tr('Code &Metrics...'), 0, 0,
474 self, "") 475 self, "")
475 act.setStatusTip( 476 act.setStatusTip(
476 self.tr('Show raw code metrics.')) 477 self.tr('Show raw code metrics.'))
482 """ multi-line strings and blank lines.</p>""" 483 """ multi-line strings and blank lines.</p>"""
483 )) 484 ))
484 act.triggered.connect(self.__editorRawMetrics) 485 act.triggered.connect(self.__editorRawMetrics)
485 self.__editorMetricsActs.append(act) 486 self.__editorMetricsActs.append(act)
486 487
487 act = E5Action( 488 act = EricAction(
488 self.tr('Maintainability Index'), 489 self.tr('Maintainability Index'),
489 self.tr('Maintainability &Index...'), 0, 0, 490 self.tr('Maintainability &Index...'), 0, 0,
490 self, "") 491 self, "")
491 act.setStatusTip( 492 act.setStatusTip(
492 self.tr('Show the maintainability index for Python files.')) 493 self.tr('Show the maintainability index for Python files.'))
496 """ files and shows it together with a ranking.</p>""" 497 """ files and shows it together with a ranking.</p>"""
497 )) 498 ))
498 act.triggered.connect(self.__editorMaintainabilityIndex) 499 act.triggered.connect(self.__editorMaintainabilityIndex)
499 self.__editorMetricsActs.append(act) 500 self.__editorMetricsActs.append(act)
500 501
501 act = E5Action( 502 act = EricAction(
502 self.tr('Cyclomatic Complexity'), 503 self.tr('Cyclomatic Complexity'),
503 self.tr('Cyclomatic &Complexity...'), 0, 0, 504 self.tr('Cyclomatic &Complexity...'), 0, 0,
504 self, '') 505 self, '')
505 act.setStatusTip( 506 act.setStatusTip(
506 self.tr('Show the cyclomatic complexity for Python files.')) 507 self.tr('Show the cyclomatic complexity for Python files.'))
510 """ files and shows it together with a ranking.</p>""" 511 """ files and shows it together with a ranking.</p>"""
511 )) 512 ))
512 act.triggered.connect(self.__editorCyclomaticComplexity) 513 act.triggered.connect(self.__editorCyclomaticComplexity)
513 self.__editorMetricsActs.append(act) 514 self.__editorMetricsActs.append(act)
514 515
515 e5App().getObject("Project").showMenu.connect(self.__projectShowMenu) 516 ericApp().getObject("Project").showMenu.connect(self.__projectShowMenu)
516 e5App().getObject("Project").projectClosed.connect( 517 ericApp().getObject("Project").projectClosed.connect(
517 self.__projectClosed) 518 self.__projectClosed)
518 e5App().getObject("ProjectBrowser").getProjectBrowser( 519 ericApp().getObject("ProjectBrowser").getProjectBrowser(
519 "sources").showMenu.connect(self.__projectBrowserShowMenu) 520 "sources").showMenu.connect(self.__projectBrowserShowMenu)
520 e5App().getObject("ViewManager").editorOpenedEd.connect( 521 ericApp().getObject("ViewManager").editorOpenedEd.connect(
521 self.__editorOpened) 522 self.__editorOpened)
522 e5App().getObject("ViewManager").editorClosedEd.connect( 523 ericApp().getObject("ViewManager").editorClosedEd.connect(
523 self.__editorClosed) 524 self.__editorClosed)
524 525
525 for editor in e5App().getObject("ViewManager").getOpenEditors(): 526 for editor in ericApp().getObject("ViewManager").getOpenEditors():
526 self.__editorOpened(editor) 527 self.__editorOpened(editor)
527 528
528 return None, True 529 return None, True
529 530
530 def deactivate(self): 531 def deactivate(self):
531 """ 532 """
532 Public method to deactivate this plug-in. 533 Public method to deactivate this plug-in.
533 """ 534 """
534 e5App().getObject("Project").showMenu.disconnect( 535 ericApp().getObject("Project").showMenu.disconnect(
535 self.__projectShowMenu) 536 self.__projectShowMenu)
536 e5App().getObject("Project").projectClosed.disconnect( 537 ericApp().getObject("Project").projectClosed.disconnect(
537 self.__projectClosed) 538 self.__projectClosed)
538 e5App().getObject("ProjectBrowser").getProjectBrowser( 539 ericApp().getObject("ProjectBrowser").getProjectBrowser(
539 "sources").showMenu.disconnect(self.__projectBrowserShowMenu) 540 "sources").showMenu.disconnect(self.__projectBrowserShowMenu)
540 e5App().getObject("ViewManager").editorOpenedEd.disconnect( 541 ericApp().getObject("ViewManager").editorOpenedEd.disconnect(
541 self.__editorOpened) 542 self.__editorOpened)
542 e5App().getObject("ViewManager").editorClosedEd.disconnect( 543 ericApp().getObject("ViewManager").editorClosedEd.disconnect(
543 self.__editorClosed) 544 self.__editorClosed)
544 545
545 menu = e5App().getObject("Project").getMenu("Show") 546 menu = ericApp().getObject("Project").getMenu("Show")
546 if menu: 547 if menu:
547 for sep in self.__projectSeparatorActs: 548 for sep in self.__projectSeparatorActs:
548 menu.removeAction(sep) 549 menu.removeAction(sep)
549 for act in self.__projectMetricsActs: 550 for act in self.__projectMetricsActs:
550 menu.removeAction(act) 551 menu.removeAction(act)
551 e5App().getObject("Project").removeE5Actions( 552 ericApp().getObject("Project").removeE5Actions(
552 self.__projectMetricsActs[1:]) 553 self.__projectMetricsActs[1:])
553 554
554 if self.__projectBrowserMenu: 555 if self.__projectBrowserMenu:
555 for sep in self.__projectBrowserSeparatorActs: 556 for sep in self.__projectBrowserSeparatorActs:
556 self.__projectBrowserMenu.removeAction(sep) 557 self.__projectBrowserMenu.removeAction(sep)
580 translation = "radon_{0}".format(loc) 581 translation = "radon_{0}".format(loc)
581 translator = QTranslator(None) 582 translator = QTranslator(None)
582 loaded = translator.load(translation, locale_dir) 583 loaded = translator.load(translation, locale_dir)
583 if loaded: 584 if loaded:
584 self.__translator = translator 585 self.__translator = translator
585 e5App().installTranslator(self.__translator) 586 ericApp().installTranslator(self.__translator)
586 else: 587 else:
587 print("Warning: translation file '{0}' could not be" 588 print("Warning: translation file '{0}' could not be"
588 " loaded.".format(translation)) 589 " loaded.".format(translation))
589 print("Using default.") 590 print("Using default.")
590 591
599 @type QMenu 600 @type QMenu
600 """ 601 """
601 if menuName == "Show": 602 if menuName == "Show":
602 for act in self.__projectMetricsActs[1:]: 603 for act in self.__projectMetricsActs[1:]:
603 act.setEnabled( 604 act.setEnabled(
604 e5App().getObject("Project").getProjectLanguage() == 605 ericApp().getObject("Project").getProjectLanguage() ==
605 "Python3") 606 "Python3")
606 607
607 def __projectBrowserShowMenu(self, menuName, menu): 608 def __projectBrowserShowMenu(self, menuName, menu):
608 """ 609 """
609 Private slot called, when the the project browser context menu or a 610 Private slot called, when the the project browser context menu or a
610 submenu is about to be shown. 611 submenu is about to be shown.
611 612
612 @param menuName name of the menu to be shown (string) 613 @param menuName name of the menu to be shown
613 @param menu reference to the menu (QMenu) 614 @type str
615 @param menu reference to the menu
616 @type QMenu
614 """ 617 """
615 if ( 618 if (
616 menuName == "Show" and 619 menuName == "Show" and
617 e5App().getObject("Project").getProjectLanguage() == "Python3" and 620 ericApp().getObject("Project").getProjectLanguage() ==
621 "Python3" and
618 self.__projectBrowserMenu is None 622 self.__projectBrowserMenu is None
619 ): 623 ):
620 self.__projectBrowserMenu = menu 624 self.__projectBrowserMenu = menu
621 625
622 act = menu.addSeparator() 626 act = menu.addSeparator()
629 act.setFont(font) 633 act.setFont(font)
630 act.triggered.connect(self.__showRadonVersion) 634 act.triggered.connect(self.__showRadonVersion)
631 menu.addAction(act) 635 menu.addAction(act)
632 self.__projectBrowserMetricsActs.append(act) 636 self.__projectBrowserMetricsActs.append(act)
633 637
634 act = E5Action( 638 act = EricAction(
635 self.tr('Code Metrics'), 639 self.tr('Code Metrics'),
636 self.tr('Code &Metrics...'), 0, 0, 640 self.tr('Code &Metrics...'), 0, 0,
637 self, '') 641 self, '')
638 act.setStatusTip(self.tr( 642 act.setStatusTip(self.tr(
639 'Show raw code metrics.')) 643 'Show raw code metrics.'))
646 )) 650 ))
647 act.triggered.connect(self.__projectBrowserRawMetrics) 651 act.triggered.connect(self.__projectBrowserRawMetrics)
648 menu.addAction(act) 652 menu.addAction(act)
649 self.__projectBrowserMetricsActs.append(act) 653 self.__projectBrowserMetricsActs.append(act)
650 654
651 act = E5Action( 655 act = EricAction(
652 self.tr('Maintainability Index'), 656 self.tr('Maintainability Index'),
653 self.tr('Maintainability &Index...'), 0, 0, 657 self.tr('Maintainability &Index...'), 0, 0,
654 self, '') 658 self, '')
655 act.setStatusTip(self.tr( 659 act.setStatusTip(self.tr(
656 'Show the maintainability index for Python files.')) 660 'Show the maintainability index for Python files.'))
663 act.triggered.connect( 667 act.triggered.connect(
664 self.__projectBrowserMaintainabilityIndex) 668 self.__projectBrowserMaintainabilityIndex)
665 menu.addAction(act) 669 menu.addAction(act)
666 self.__projectBrowserMetricsActs.append(act) 670 self.__projectBrowserMetricsActs.append(act)
667 671
668 act = E5Action( 672 act = EricAction(
669 self.tr('Cyclomatic Complexity'), 673 self.tr('Cyclomatic Complexity'),
670 self.tr('Cyclomatic &Complexity...'), 0, 0, 674 self.tr('Cyclomatic &Complexity...'), 0, 0,
671 self, '') 675 self, '')
672 act.setStatusTip(self.tr( 676 act.setStatusTip(self.tr(
673 'Show the cyclomatic complexity for Python files.')) 677 'Show the cyclomatic complexity for Python files.'))
688 def __editorOpened(self, editor): 692 def __editorOpened(self, editor):
689 """ 693 """
690 Private slot called, when a new editor was opened. 694 Private slot called, when a new editor was opened.
691 695
692 @param editor reference to the new editor 696 @param editor reference to the new editor
693 @type QScintilla.Editor 697 @type Editor
694 """ 698 """
695 menu = editor.getMenu("Show") 699 menu = editor.getMenu("Show")
696 if menu is not None: 700 if menu is not None:
697 menu.addAction(self.__editorSeparatorActs[0]) 701 menu.addAction(self.__editorSeparatorActs[0])
698 menu.addActions(self.__editorMetricsActs) 702 menu.addActions(self.__editorMetricsActs)
703 707
704 def __editorClosed(self, editor): 708 def __editorClosed(self, editor):
705 """ 709 """
706 Private slot called, when an editor was closed. 710 Private slot called, when an editor was closed.
707 711
708 @param editor reference to the editor (QScintilla.Editor) 712 @param editor reference to the editor
713 @type Editor
709 """ 714 """
710 with contextlib.suppress(ValueError): 715 with contextlib.suppress(ValueError):
711 self.__editors.remove(editor) 716 self.__editors.remove(editor)
712 717
713 def __editorRenamed(self, editor): 718 def __editorRenamed(self, editor):
714 """ 719 """
715 Private slot called, when an editor was renamed. 720 Private slot called, when an editor was renamed.
716 721
717 @param editor reference to the renamed editor 722 @param editor reference to the renamed editor
718 @type QScintilla.Editor 723 @type Editor
719 """ 724 """
720 menu = editor.getMenu("Show") 725 menu = editor.getMenu("Show")
721 if menu is not None: 726 if menu is not None:
722 menu.addAction(self.__editorSeparatorActs[0]) 727 menu.addAction(self.__editorSeparatorActs[0])
723 menu.addActions(self.__editorMetricsActs) 728 menu.addActions(self.__editorMetricsActs)
726 def __editorShowMenu(self, menuName, menu, editor): 731 def __editorShowMenu(self, menuName, menu, editor):
727 """ 732 """
728 Private slot called, when the the editor context menu or a submenu is 733 Private slot called, when the the editor context menu or a submenu is
729 about to be shown. 734 about to be shown.
730 735
731 @param menuName name of the menu to be shown (string) 736 @param menuName name of the menu to be shown
732 @param menu reference to the menu (QMenu) 737 @type str
738 @param menu reference to the menu
739 @type QMenu
733 @param editor reference to the editor 740 @param editor reference to the editor
741 @type Editor
734 """ 742 """
735 if menuName == "Show": 743 if menuName == "Show":
736 enable = editor.isPyFile() 744 enable = editor.isPyFile()
737 for act in self.__editorMetricsActs: 745 for act in self.__editorMetricsActs:
738 act.setEnabled(enable) 746 act.setEnabled(enable)
743 751
744 def __projectRawMetrics(self): 752 def __projectRawMetrics(self):
745 """ 753 """
746 Private slot used to calculate raw code metrics for the project. 754 Private slot used to calculate raw code metrics for the project.
747 """ 755 """
748 project = e5App().getObject("Project") 756 project = ericApp().getObject("Project")
749 project.saveAllScripts() 757 project.saveAllScripts()
750 ppath = project.getProjectPath() 758 ppath = project.getProjectPath()
751 files = [os.path.join(ppath, file) 759 files = [
752 for file in project.getSources() 760 os.path.join(ppath, file)
753 if file.endswith( 761 for file in project.getSources()
754 tuple(Preferences.getPython("Python3Extensions")) + 762 if file.endswith(tuple(Preferences.getPython("Python3Extensions")))
755 tuple(Preferences.getPython("PythonExtensions")))] 763 ]
756 764
757 if self.__projectRawMetricsDialog is None: 765 if self.__projectRawMetricsDialog is None:
758 from RadonMetrics.RawMetricsDialog import RawMetricsDialog 766 from RadonMetrics.RawMetricsDialog import RawMetricsDialog
759 self.__projectRawMetricsDialog = RawMetricsDialog(self) 767 self.__projectRawMetricsDialog = RawMetricsDialog(self)
760 self.__projectRawMetricsDialog.show() 768 self.__projectRawMetricsDialog.show()
763 def __projectBrowserRawMetrics(self): 771 def __projectBrowserRawMetrics(self):
764 """ 772 """
765 Private method to handle the code metrics context menu action of the 773 Private method to handle the code metrics context menu action of the
766 project sources browser. 774 project sources browser.
767 """ 775 """
768 browser = e5App().getObject("ProjectBrowser").getProjectBrowser( 776 browser = (
777 ericApp().getObject("ProjectBrowser").getProjectBrowser("sources")
778 )
779 if browser.getSelectedItemsCount([ProjectBrowserFileItem]) > 1:
780 fn = []
781 for itm in browser.getSelectedItems([ProjectBrowserFileItem]):
782 fn.append(itm.fileName())
783 else:
784 itm = browser.model().item(browser.currentIndex())
785 try:
786 fn = itm.fileName()
787 except AttributeError:
788 fn = itm.dirName()
789
790 if self.__projectBrowserRawMetricsDialog is None:
791 from RadonMetrics.RawMetricsDialog import RawMetricsDialog
792 self.__projectBrowserRawMetricsDialog = RawMetricsDialog(self)
793 self.__projectBrowserRawMetricsDialog.show()
794 self.__projectBrowserRawMetricsDialog.start(fn)
795
796 def __editorRawMetrics(self):
797 """
798 Private slot to handle the raw code metrics action of the editor show
799 menu.
800 """
801 editor = ericApp().getObject("ViewManager").activeWindow()
802 if (
803 editor is not None and
804 editor.checkDirty() and
805 editor.getFileName() is not None
806 ):
807 if self.__editorRawMetricsDialog is None:
808 from RadonMetrics.RawMetricsDialog import RawMetricsDialog
809 self.__editorRawMetricsDialog = RawMetricsDialog(self)
810 self.__editorRawMetricsDialog.show()
811 self.__editorRawMetricsDialog.start(editor.getFileName())
812
813 ##################################################################
814 ## Maintainability index calculations
815 ##################################################################
816
817 def __projectMaintainabilityIndex(self):
818 """
819 Private slot used to calculate the maintainability indexes for the
820 project.
821 """
822 project = ericApp().getObject("Project")
823 project.saveAllScripts()
824 ppath = project.getProjectPath()
825 files = [
826 os.path.join(ppath, file)
827 for file in project.getSources()
828 if file.endswith(tuple(Preferences.getPython("Python3Extensions")))
829 ]
830
831 if self.__projectMIDialog is None:
832 from RadonMetrics.MaintainabilityIndexDialog import (
833 MaintainabilityIndexDialog
834 )
835 self.__projectMIDialog = MaintainabilityIndexDialog(self)
836 self.__projectMIDialog.show()
837 self.__projectMIDialog.prepare(files, project)
838
839 def __projectBrowserMaintainabilityIndex(self):
840 """
841 Private method to handle the maintainability index context menu action
842 of the project sources browser.
843 """
844 browser = ericApp().getObject("ProjectBrowser").getProjectBrowser(
769 "sources") 845 "sources")
770 if browser.getSelectedItemsCount([ProjectBrowserFileItem]) > 1: 846 if browser.getSelectedItemsCount([ProjectBrowserFileItem]) > 1:
771 fn = [] 847 fn = []
772 for itm in browser.getSelectedItems([ProjectBrowserFileItem]): 848 for itm in browser.getSelectedItems([ProjectBrowserFileItem]):
773 fn.append(itm.fileName()) 849 fn.append(itm.fileName())
776 try: 852 try:
777 fn = itm.fileName() 853 fn = itm.fileName()
778 except AttributeError: 854 except AttributeError:
779 fn = itm.dirName() 855 fn = itm.dirName()
780 856
781 if self.__projectBrowserRawMetricsDialog is None:
782 from RadonMetrics.RawMetricsDialog import RawMetricsDialog
783 self.__projectBrowserRawMetricsDialog = RawMetricsDialog(self)
784 self.__projectBrowserRawMetricsDialog.show()
785 self.__projectBrowserRawMetricsDialog.start(fn)
786
787 def __editorRawMetrics(self):
788 """
789 Private slot to handle the raw code metrics action of the editor show
790 menu.
791 """
792 editor = e5App().getObject("ViewManager").activeWindow()
793 if (
794 editor is not None and
795 editor.checkDirty() and
796 editor.getFileName() is not None
797 ):
798 if self.__editorRawMetricsDialog is None:
799 from RadonMetrics.RawMetricsDialog import RawMetricsDialog
800 self.__editorRawMetricsDialog = RawMetricsDialog(self)
801 self.__editorRawMetricsDialog.show()
802 self.__editorRawMetricsDialog.start(editor.getFileName())
803
804 ##################################################################
805 ## Maintainability index calculations
806 ##################################################################
807
808 def __projectMaintainabilityIndex(self):
809 """
810 Private slot used to calculate the maintainability indexes for the
811 project.
812 """
813 project = e5App().getObject("Project")
814 project.saveAllScripts()
815 ppath = project.getProjectPath()
816 files = [os.path.join(ppath, file)
817 for file in project.getSources()
818 if file.endswith(
819 tuple(Preferences.getPython("Python3Extensions")) +
820 tuple(Preferences.getPython("PythonExtensions")))]
821
822 if self.__projectMIDialog is None:
823 from RadonMetrics.MaintainabilityIndexDialog import (
824 MaintainabilityIndexDialog
825 )
826 self.__projectMIDialog = MaintainabilityIndexDialog(self)
827 self.__projectMIDialog.show()
828 self.__projectMIDialog.prepare(files, project)
829
830 def __projectBrowserMaintainabilityIndex(self):
831 """
832 Private method to handle the maintainability index context menu action
833 of the project sources browser.
834 """
835 browser = e5App().getObject("ProjectBrowser").getProjectBrowser(
836 "sources")
837 if browser.getSelectedItemsCount([ProjectBrowserFileItem]) > 1:
838 fn = []
839 for itm in browser.getSelectedItems([ProjectBrowserFileItem]):
840 fn.append(itm.fileName())
841 else:
842 itm = browser.model().item(browser.currentIndex())
843 try:
844 fn = itm.fileName()
845 except AttributeError:
846 fn = itm.dirName()
847
848 if self.__projectBrowserMIDialog is None: 857 if self.__projectBrowserMIDialog is None:
849 from RadonMetrics.MaintainabilityIndexDialog import ( 858 from RadonMetrics.MaintainabilityIndexDialog import (
850 MaintainabilityIndexDialog 859 MaintainabilityIndexDialog
851 ) 860 )
852 self.__projectBrowserMIDialog = MaintainabilityIndexDialog(self) 861 self.__projectBrowserMIDialog = MaintainabilityIndexDialog(self)
856 def __editorMaintainabilityIndex(self): 865 def __editorMaintainabilityIndex(self):
857 """ 866 """
858 Private slot to handle the maintainability index action of the editor 867 Private slot to handle the maintainability index action of the editor
859 show menu. 868 show menu.
860 """ 869 """
861 editor = e5App().getObject("ViewManager").activeWindow() 870 editor = ericApp().getObject("ViewManager").activeWindow()
862 if ( 871 if (
863 editor is not None and 872 editor is not None and
864 editor.checkDirty() and 873 editor.checkDirty() and
865 editor.getFileName() is not None 874 editor.getFileName() is not None
866 ): 875 ):
879 def __projectCyclomaticComplexity(self): 888 def __projectCyclomaticComplexity(self):
880 """ 889 """
881 Private slot used to calculate the cyclomatic complexity for the 890 Private slot used to calculate the cyclomatic complexity for the
882 project. 891 project.
883 """ 892 """
884 project = e5App().getObject("Project") 893 project = ericApp().getObject("Project")
885 project.saveAllScripts() 894 project.saveAllScripts()
886 ppath = project.getProjectPath() 895 ppath = project.getProjectPath()
887 files = [os.path.join(ppath, file) 896 files = [
888 for file in project.getSources() 897 os.path.join(ppath, file)
889 if file.endswith( 898 for file in project.getSources()
890 tuple(Preferences.getPython("Python3Extensions")) + 899 if file.endswith(tuple(Preferences.getPython("Python3Extensions")))
891 tuple(Preferences.getPython("PythonExtensions")))] 900 ]
892 901
893 if self.__projectCCDialog is None: 902 if self.__projectCCDialog is None:
894 from RadonMetrics.CyclomaticComplexityDialog import ( 903 from RadonMetrics.CyclomaticComplexityDialog import (
895 CyclomaticComplexityDialog 904 CyclomaticComplexityDialog
896 ) 905 )
901 def __projectBrowserCyclomaticComplexity(self): 910 def __projectBrowserCyclomaticComplexity(self):
902 """ 911 """
903 Private method to handle the cyclomatic complexity context menu action 912 Private method to handle the cyclomatic complexity context menu action
904 of the project sources browser. 913 of the project sources browser.
905 """ 914 """
906 browser = e5App().getObject("ProjectBrowser").getProjectBrowser( 915 browser = ericApp().getObject("ProjectBrowser").getProjectBrowser(
907 "sources") 916 "sources")
908 if browser.getSelectedItemsCount([ProjectBrowserFileItem]) > 1: 917 if browser.getSelectedItemsCount([ProjectBrowserFileItem]) > 1:
909 fn = [] 918 fn = []
910 for itm in browser.getSelectedItems([ProjectBrowserFileItem]): 919 for itm in browser.getSelectedItems([ProjectBrowserFileItem]):
911 fn.append(itm.fileName()) 920 fn.append(itm.fileName())
928 def __editorCyclomaticComplexity(self): 937 def __editorCyclomaticComplexity(self):
929 """ 938 """
930 Private slot to handle the cyclomatic complexity action of the editor 939 Private slot to handle the cyclomatic complexity action of the editor
931 show menu. 940 show menu.
932 """ 941 """
933 editor = e5App().getObject("ViewManager").activeWindow() 942 editor = ericApp().getObject("ViewManager").activeWindow()
934 if ( 943 if (
935 editor is not None and 944 editor is not None and
936 editor.checkDirty() and 945 editor.checkDirty() and
937 editor.getFileName() is not None 946 editor.getFileName() is not None
938 ): 947 ):
951 960
952 def __showRadonVersion(self): 961 def __showRadonVersion(self):
953 """ 962 """
954 Private slot to show the version number of the used radon library. 963 Private slot to show the version number of the used radon library.
955 """ 964 """
956 from RadonMetrics.radon import __version__ 965 from radon import __version__
957 E5MessageBox.information( 966 EricMessageBox.information(
958 None, 967 None,
959 self.tr("Radon"), 968 self.tr("Radon"),
960 self.tr( 969 self.tr(
961 """<p><b>Radon Version {0}</b></p>""" 970 """<p><b>Radon Version {0}</b></p>"""
962 """<p>Radon is a Python tool that computes various metrics""" 971 """<p>Radon is a Python tool that computes various metrics"""

eric ide

mercurial