lines.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 core.api
  20. import re
  21. class Plugin(core.api.Plugin, core.api.SimpleMetricMixin, core.api.Child, core.api.IConfigurable):
  22. def declare_configuration(self, parser):
  23. parser.add_option("--std.code.lines.code", "--sclc", action="store_true", default=False,
  24. help="Enables collection of lines of code metric - "
  25. "number of non-empty lines of code, excluding comments "
  26. "[default: %default]")
  27. parser.add_option("--std.code.lines.preprocessor", "--sclp", action="store_true", default=False,
  28. help="Enables collection of lines of preprocessor code metric - "
  29. "number of non-empty lines of preprocessor code "
  30. "[default: %default]")
  31. parser.add_option("--std.code.lines.comments", "--sclcom", action="store_true", default=False,
  32. help="Enables collection of lines of comments metric - "
  33. "number of non-empty lines of comments "
  34. "[default: %default]")
  35. parser.add_option("--std.code.lines.total", "--sclt", action="store_true", default=False,
  36. help="Enables collection of lines of comments metric - "
  37. "number of non-empty lines of comments "
  38. "[default: %default]")
  39. def configure(self, options):
  40. self.is_active_code = options.__dict__['std.code.lines.code']
  41. self.is_active_preprocessor = options.__dict__['std.code.lines.preprocessor']
  42. self.is_active_comments = options.__dict__['std.code.lines.comments']
  43. self.is_active_total = options.__dict__['std.code.lines.total']
  44. pattern_line = re.compile(r'''[^\s].*''')
  45. def initialize(self):
  46. self.declare_metric(self.is_active_code,
  47. self.Field('code', int),
  48. self.pattern_line,
  49. core.api.Marker.T.CODE)
  50. self.declare_metric(self.is_active_preprocessor,
  51. self.Field('preprocessor', int),
  52. self.pattern_line,
  53. core.api.Marker.T.PREPROCESSOR)
  54. self.declare_metric(self.is_active_comments,
  55. self.Field('comments', int),
  56. self.pattern_line,
  57. core.api.Marker.T.COMMENT)
  58. self.declare_metric(self.is_active_total,
  59. self.Field('total', int),
  60. self.pattern_line,
  61. core.api.Marker.T.ANY,
  62. merge_markers=True)
  63. super(Plugin, self).initialize(fields=self.get_fields())
  64. if self.is_active() == True:
  65. self.subscribe_by_parents_interface(core.api.ICode, 'callback')
  66. def callback(self, parent, data, is_updated):
  67. is_updated = is_updated or self.is_updated
  68. if is_updated == True:
  69. self.count_if_active('code', data)
  70. self.count_if_active('preprocessor', data)
  71. self.count_if_active('comments', data)
  72. self.count_if_active('total', data)