lines.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.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. def initialize(self):
  45. fields = []
  46. if self.is_active_code == True:
  47. fields.append(self.Field('code', int))
  48. if self.is_active_preprocessor == True:
  49. fields.append(self.Field('preprocessor', int))
  50. if self.is_active_comments == True:
  51. fields.append(self.Field('comments', int))
  52. if self.is_active_total == True:
  53. fields.append(self.Field('total', int))
  54. core.api.Plugin.initialize(self, fields=fields)
  55. if len(fields) != 0:
  56. core.api.subscribe_by_parents_interface(core.api.ICode, self, 'callback')
  57. pattern_line = re.compile(r'''[^\s].*''')
  58. def callback(self, parent, data, is_updated):
  59. is_updated = is_updated or self.is_updated
  60. if is_updated == True:
  61. if self.is_active_code == True:
  62. self.count_in_code(data,
  63. data.get_marker_types().ALL_EXCEPT_CODE,
  64. 'code')
  65. if self.is_active_preprocessor == True:
  66. self.count_in_markers(data,
  67. data.get_marker_types().PREPROCESSOR,
  68. 'preprocessor')
  69. if self.is_active_comments == True:
  70. self.count_in_markers(data,
  71. data.get_marker_types().COMMENT,
  72. 'comments')
  73. if self.is_active_total == True:
  74. self.count_in_code(data,
  75. data.get_marker_types().NONE,
  76. 'total')
  77. def count_in_code(self, data, exclude, field_name):
  78. text = data.get_content(exclude=exclude)
  79. for region in data.iterate_regions():
  80. count = 0
  81. start_pos = region.get_offset_begin()
  82. for sub_id in region.iterate_subregion_ids():
  83. count += len(self.pattern_line.findall(text, start_pos, data.get_region(sub_id).get_offset_begin()))
  84. start_pos = data.get_region(sub_id).get_offset_end()
  85. count += len(self.pattern_line.findall(text, start_pos, region.get_offset_end()))
  86. region.set_data(self.get_name(), 'code', count)
  87. def count_in_markers(self, data, marker_type, field_name):
  88. text = data.get_content()
  89. for region in data.iterate_regions():
  90. count = 0
  91. for marker in data.iterate_markers(
  92. filter_group = marker_type,
  93. region_id = region.get_id(),
  94. exclude_children = True):
  95. count += len(self.pattern_line.findall(text, marker.get_offset_begin(), marker.get_offset_end()))
  96. region.set_data(self.get_name(), field_name, count)