magic.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. # reuse by inheriting standard metric facilities
  13. mpp.api.MetricPluginMixin):
  14. def declare_configuration(self, parser):
  15. parser.add_option("--myext.magic.numbers", "--mmn",
  16. action="store_true", default=False,
  17. help="Enables collection of magic numbers metric [default: %default]")
  18. def configure(self, options):
  19. self.is_active_numbers = options.__dict__['myext.magic.numbers']
  20. def initialize(self):
  21. # declare metric rules
  22. self.declare_metric(
  23. self.is_active_numbers, # to count if active in callback
  24. self.Field('numbers', int), # field name and type in the database
  25. re.compile(r'''\b[0-9]+\b'''), # pattern to search
  26. marker_type_mask=mpp.api.Marker.T.CODE, # search in code
  27. region_type_mask=mpp.api.Region.T.ANY) # search in all types of regions
  28. # use superclass facilities to initialize everything from declared fields
  29. super(Plugin, self).initialize(fields=self.get_fields())
  30. # subscribe to all code parsers if at least one metric is active
  31. if self.is_active() == True:
  32. self.subscribe_by_parents_interface(mpp.api.ICode)