lines.py 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. class Plugin(core.api.Plugin, core.api.Child, core.api.IConfigurable):
  21. def declare_configuration(self, parser):
  22. parser.add_option("--std.code.lines.code", "--sclc", action="store_true", default=False,
  23. help="Enables collection of lines of code metric - "
  24. "number of non-empty lines of code, excluding comments "
  25. "[default: %default]")
  26. parser.add_option("--std.code.lines.preprocessor", "--sclp", action="store_true", default=False,
  27. help="Enables collection of lines of preprocessor code metric - "
  28. "number of non-empty lines of preprocessor code "
  29. "[default: %default]")
  30. parser.add_option("--std.code.lines.comments", "--sclcom", action="store_true", default=False,
  31. help="Enables collection of lines of comments metric - "
  32. "number of non-empty lines of comments "
  33. "[default: %default]")
  34. parser.add_option("--std.code.lines.blank", "--sclb", action="store_true", default=False,
  35. help="Enables collection of blank lines metric - "
  36. "number of blank lines, i.e. lines without code or comments "
  37. "[default: %default]")
  38. parser.add_option("--std.code.lines.total", "--sclt", action="store_true", default=False,
  39. help="Enables collection of lines of comments metric - "
  40. "number of non-empty lines of comments "
  41. "[default: %default]")
  42. def configure(self, options):
  43. self.is_active_code = options.__dict__['std.code.lines.code']
  44. self.is_active_preprocessor = options.__dict__['std.code.lines.preprocessor']
  45. self.is_active_comments = options.__dict__['std.code.lines.comments']
  46. self.is_active_blank = options.__dict__['std.code.lines.blank']
  47. self.is_active_total = options.__dict__['std.code.lines.total']
  48. def initialize(self):
  49. fields = []
  50. if self.is_active_code == True:
  51. fields.append(self.Field('code', int))
  52. if self.is_active_preprocessor == True:
  53. fields.append(self.Field('preprocessor', int))
  54. if self.is_active_comments == True:
  55. fields.append(self.Field('comments', int))
  56. if self.is_active_blank == True:
  57. fields.append(self.Field('blank', int))
  58. if self.is_active_total == True:
  59. fields.append(self.Field('total', int))
  60. core.api.Plugin.initialize(self, fields=fields)
  61. if len(fields) != 0:
  62. core.api.subscribe_by_parents_interface(core.api.ICode, self, 'callback')
  63. def callback(self, parent, data, is_updated):
  64. is_updated = is_updated or self.is_updated
  65. if is_updated == True:
  66. for region in data.iterate_regions():
  67. size = 0
  68. start_pos = region.get_offset_begin()
  69. for sub_id in region.iterate_subregion_ids():
  70. # exclude sub regions, like enclosed classes
  71. size += data.get_region(sub_id).get_offset_begin() - start_pos
  72. start_pos = data.get_region(sub_id).get_offset_end()
  73. size += region.get_offset_end() - start_pos
  74. region.set_data(self.get_name(), 'size', size)