lines.py 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.blank", "--sclb", action="store_true", default=False,
  36. help="Enables collection of blank lines metric - "
  37. "number of blank lines, i.e. lines without code or comments "
  38. "[default: %default]")
  39. parser.add_option("--std.code.lines.total", "--sclt", action="store_true", default=False,
  40. help="Enables collection of lines of comments metric - "
  41. "number of non-empty lines of comments "
  42. "[default: %default]")
  43. def configure(self, options):
  44. self.is_active_code = options.__dict__['std.code.lines.code']
  45. self.is_active_preprocessor = options.__dict__['std.code.lines.preprocessor']
  46. self.is_active_comments = options.__dict__['std.code.lines.comments']
  47. self.is_active_blank = options.__dict__['std.code.lines.blank']
  48. self.is_active_total = options.__dict__['std.code.lines.total']
  49. def initialize(self):
  50. fields = []
  51. if self.is_active_code == True:
  52. fields.append(self.Field('code', int))
  53. if self.is_active_preprocessor == True:
  54. fields.append(self.Field('preprocessor', int))
  55. if self.is_active_comments == True:
  56. fields.append(self.Field('comments', int))
  57. if self.is_active_blank == True:
  58. fields.append(self.Field('blank', int))
  59. if self.is_active_total == True:
  60. fields.append(self.Field('total', int))
  61. core.api.Plugin.initialize(self, fields=fields)
  62. if len(fields) != 0:
  63. core.api.subscribe_by_parents_interface(core.api.ICode, self, 'callback')
  64. pattern_line = re.compile(r'''[^\s].*''')
  65. def callback(self, parent, data, is_updated):
  66. is_updated = is_updated or self.is_updated
  67. if is_updated == True:
  68. if self.is_active_code == True:
  69. text = data.get_content(exclude = data.get_marker_types().ALL_EXCEPT_CODE)
  70. for region in data.iterate_regions():
  71. count = 0
  72. start_pos = region.get_offset_begin()
  73. for sub_id in region.iterate_subregion_ids():
  74. count += len(self.pattern_line.findall(text, start_pos, data.get_region(sub_id).get_offset_begin()))
  75. start_pos = data.get_region(sub_id).get_offset_end()
  76. count += len(self.pattern_line.findall(text, start_pos, region.get_offset_end()))
  77. region.set_data(self.get_name(), 'code', count)