magic.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. mpp.api.MetricPluginMixin):
  25. def declare_configuration(self, parser):
  26. parser.add_option("--myext.magic.numbers", "--mmn",
  27. action="store_true", default=False,
  28. help="Enables collection of magic numbers metric [default: %default]")
  29. # Add new option
  30. parser.add_option("--myext.magic.numbers.simplier", "--mmns",
  31. action="store_true", default=False,
  32. help="Is set, 0, -1 and 1 numbers are not counted [default: %default]")
  33. def configure(self, options):
  34. self.is_active_numbers = options.__dict__['myext.magic.numbers']
  35. # remember the option here
  36. self.is_active_numbers_simplier = options.__dict__['myext.magic.numbers.simplier']
  37. def initialize(self):
  38. pattern_to_search_java = re.compile(
  39. r'''((const(\s+[_$a-zA-Z][_$a-zA-Z0-9]*)+\s*[=]\s*)[-+]?[0-9]+\b)|(\b[0-9]+\b)''')
  40. pattern_to_search_cpp_cs = re.compile(
  41. r'''((const(\s+[_a-zA-Z][_a-zA-Z0-9]*)+\s*[=]\s*)[-+]?[0-9]+\b)|(\b[0-9]+\b)''')
  42. pattern_to_search = re.compile(
  43. r'''\b[0-9]+\b''')
  44. self.declare_metric(self.is_active_numbers,
  45. self.Field('numbers', int,
  46. non_zero=True),
  47. {
  48. 'std.code.java': (pattern_to_search_java, self.NumbersCounter),
  49. 'std.code.cpp': (pattern_to_search_cpp_cs, self.NumbersCounter),
  50. 'std.code.cs': (pattern_to_search_cpp_cs, self.NumbersCounter),
  51. '*': pattern_to_search
  52. },
  53. marker_type_mask=mpp.api.Marker.T.CODE,
  54. region_type_mask=mpp.api.Region.T.ANY)
  55. super(Plugin, self).initialize(fields=self.get_fields(),
  56. # remember option settings in data file properties
  57. # in order to detect changes in settings on iterative re-run
  58. properties=[self.Property('number.simplier', self.is_active_numbers_simplier)])
  59. if self.is_active() == True:
  60. self.subscribe_by_parents_interface(mpp.api.ICode)
  61. class NumbersCounter(mpp.api.MetricPluginMixin.IterIncrementCounter):
  62. def increment(self, match):
  63. if (match.group(0).startswith('const') or
  64. (self.plugin.is_active_numbers_simplier == True and
  65. match.group(0) in ['0', '1', '-1', '+1'])):
  66. return 0
  67. return 1