magic.py 2.1 KB

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