mi.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #
  2. # Metrix++, Copyright 2009-2013, Metrix++ Project
  3. # Link: http://metrixplusplus.sourceforge.net
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. # Metrix++ is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, version 3 of the License.
  10. #
  11. # Metrix++ is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Metrix++. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import mpp.api
  20. class Plugin(mpp.api.Plugin,
  21. mpp.api.IConfigurable,
  22. mpp.api.Child,
  23. mpp.api.MetricPluginMixin):
  24. def declare_configuration(self, parser):
  25. self.parser = parser
  26. parser.add_option("--std.code.maintindex.simple", "--scmis",
  27. action="store_true", default=False,
  28. help="Enables collection of simple maintainability index metric."
  29. " It uses std.code.line:code, std.code.complexity:cyclomatic"
  30. " metrics to rank level of maintainability."
  31. " Lower value of this metric indicates better maintainability."
  32. " [default: %default]")
  33. def configure(self, options):
  34. self.is_active_simple = options.__dict__['std.code.maintindex.simple']
  35. if self.is_active_simple == True:
  36. required_opts = ['std.code.complexity.cyclomatic', 'std.code.lines.code']
  37. for each in required_opts:
  38. if options.__dict__[each] == False:
  39. self.parser.error('option --std.code.maintindex.simple: requires --{0} option'.
  40. format(each))
  41. def initialize(self):
  42. self.declare_metric(self.is_active_simple,
  43. self.Field('simple', int),
  44. {
  45. 'std.code.complexity':(None, self.RankedComplexityCounter),
  46. 'std.code.lines':(None, self.RankedLinesCounter),
  47. },
  48. # set none, because this plugin is not interested in parsing the code
  49. marker_type_mask=mpp.api.Marker.T.NONE)
  50. super(Plugin, self).initialize(fields=self.get_fields())
  51. if self.is_active() == True:
  52. self.subscribe_by_parents_name('std.code.complexity')
  53. self.subscribe_by_parents_name('std.code.lines')
  54. class RankedComplexityCounter(mpp.api.MetricPluginMixin.RankedCounter):
  55. rank_source = ('std.code.complexity', 'cyclomatic')
  56. rank_ranges = [(None, 7), (8, 11), (12, 19), (20, 49), (50, None)]
  57. class RankedLinesCounter(mpp.api.MetricPluginMixin.RankedCounter):
  58. rank_source = ('std.code.lines', 'code')
  59. rank_ranges = [(None, 124), (125, 249), (250, 499), (500, 999), (1000, None)]