magic.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. import re
  9. class Plugin(mpp.api.Plugin,
  10. mpp.api.IConfigurable,
  11. mpp.api.Child,
  12. mpp.api.MetricPluginMixin):
  13. def declare_configuration(self, parser):
  14. parser.add_option("--myext.magic.numbers", "--mmn",
  15. action="store_true", default=False,
  16. help="Enables collection of magic numbers metric [default: %default]")
  17. def configure(self, options):
  18. self.is_active_numbers = options.__dict__['myext.magic.numbers']
  19. def initialize(self):
  20. # improve pattern to find declarations of constants
  21. pattern_to_search = re.compile(
  22. r'''((const(\s+[_a-zA-Z][_a-zA-Z0-9]*)+\s*[=]\s*)[-+]?[0-9]+\b)|(\b[0-9]+\b)''')
  23. self.declare_metric(self.is_active_numbers,
  24. self.Field('numbers', int),
  25. # give a pair of pattern + custom counter logic class
  26. (pattern_to_search, self.NumbersCounter),
  27. marker_type_mask=mpp.api.Marker.T.CODE,
  28. region_type_mask=mpp.api.Region.T.ANY)
  29. super(Plugin, self).initialize(fields=self.get_fields())
  30. if self.is_active() == True:
  31. self.subscribe_by_parents_interface(mpp.api.ICode)
  32. # implement custom counter behavior:
  33. # increments counter by 1 only if single number spotted,
  34. # but not declaration of a constant
  35. class NumbersCounter(mpp.api.MetricPluginMixin.IterIncrementCounter):
  36. def increment(self, match):
  37. if match.group(0).startswith('const'):
  38. return 0
  39. return 1