todo.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. self.parser = parser
  15. parser.add_option("--std.code.todo.comments", "--sctc",
  16. action="store_true", default=False,
  17. help="Enables collection of number of todo/fixme/etc markers in comments [default: %default]")
  18. parser.add_option("--std.code.todo.strings", "--scts",
  19. action="store_true", default=False,
  20. help="Enables collection of number of todo/fixme/etc markers in strings [default: %default]")
  21. parser.add_option("--std.code.todo.tags", "--sctt", type=str,
  22. default="TODO,ToDo,FIXME,FixMe,TBD,HACK,XXX",
  23. help="A list of typical todo markers to search, separated by comma [default: %default]")
  24. def configure(self, options):
  25. self.is_active_comments = options.__dict__['std.code.todo.comments']
  26. self.is_active_strings = options.__dict__['std.code.todo.strings']
  27. self.tags_list = options.__dict__['std.code.todo.tags'].split(',')
  28. self.tags_list.sort()
  29. for tag in self.tags_list:
  30. if re.match(r'''^[A-Za-z0-9_]+$''', tag) == None:
  31. self.parser.error('option --std.code.todo.tags: tag {0} includes not allowed symbols'.
  32. format(tag))
  33. self.pattern_to_search = re.compile(
  34. r'\b({0})\b'.
  35. format('|'.join(self.tags_list)))
  36. def initialize(self):
  37. self.declare_metric(self.is_active_comments,
  38. self.Field('comments', int, non_zero=True),
  39. self.pattern_to_search,
  40. marker_type_mask=mpp.api.Marker.T.COMMENT,
  41. region_type_mask=mpp.api.Region.T.ANY)
  42. self.declare_metric(self.is_active_strings,
  43. self.Field('strings', int, non_zero=True),
  44. self.pattern_to_search,
  45. marker_type_mask=mpp.api.Marker.T.STRING,
  46. region_type_mask=mpp.api.Region.T.ANY)
  47. super(Plugin, self).initialize(fields=self.get_fields(),
  48. properties=[self.Property('tags', ','.join(self.tags_list))])
  49. if self.is_active() == True:
  50. self.subscribe_by_parents_interface(mpp.api.ICode)