mi.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #
  2. # Metrix++, Copyright 2009-2019, Metrix++ Project
  3. # Link: https://github.com/metrixplusplus/metrixplusplus
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. import mpp.api
  8. class Plugin(mpp.api.Plugin,
  9. mpp.api.IConfigurable,
  10. mpp.api.Child,
  11. mpp.api.MetricPluginMixin):
  12. def declare_configuration(self, parser):
  13. self.parser = parser
  14. parser.add_option("--std.code.maintindex.simple", "--scmis",
  15. action="store_true", default=False,
  16. help="Enables collection of simple maintainability index metric."
  17. " It uses std.code.line:code, std.code.complexity:cyclomatic"
  18. " metrics to rank level of maintainability."
  19. " Lower value of this metric indicates better maintainability."
  20. " [default: %default]")
  21. def configure(self, options):
  22. self.is_active_simple = options.__dict__['std.code.maintindex.simple']
  23. if self.is_active_simple == True:
  24. required_opts = ['std.code.complexity.cyclomatic', 'std.code.lines.code']
  25. for each in required_opts:
  26. if options.__dict__[each] == False:
  27. self.parser.error('option --std.code.maintindex.simple: requires --{0} option'.
  28. format(each))
  29. def initialize(self):
  30. self.declare_metric(self.is_active_simple,
  31. self.Field('simple', int),
  32. {
  33. 'std.code.complexity':(None, self.RankedComplexityCounter),
  34. 'std.code.lines':(None, self.RankedLinesCounter),
  35. },
  36. # set none, because this plugin is not interested in parsing the code
  37. marker_type_mask=mpp.api.Marker.T.NONE)
  38. super(Plugin, self).initialize(fields=self.get_fields())
  39. if self.is_active() == True:
  40. self.subscribe_by_parents_name('std.code.complexity')
  41. self.subscribe_by_parents_name('std.code.lines')
  42. class RankedComplexityCounter(mpp.api.MetricPluginMixin.RankedCounter):
  43. rank_source = ('std.code.complexity', 'cyclomatic')
  44. rank_ranges = [(None, 7), (8, 11), (12, 19), (20, 49), (50, None)]
  45. class RankedLinesCounter(mpp.api.MetricPluginMixin.RankedCounter):
  46. rank_source = ('std.code.lines', 'code')
  47. rank_ranges = [(None, 124), (125, 249), (250, 499), (500, 999), (1000, None)]