length.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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.length:size", "--scls", action="store_true", default=False,
  23. help="Enables collection of size metric (in number of symbols per region) [default: %default]")
  24. def configure(self, options):
  25. self.is_active = options.__dict__['std.code.length:size']
  26. def initialize(self):
  27. if self.is_active == True:
  28. # trigger version property set
  29. core.api.Plugin.initialize(self)
  30. namespace = self.get_plugin_loader().get_database_loader().create_namespace(self.get_name(), support_regions = True)
  31. namespace.add_field('size', int)
  32. core.api.subscribe_by_parents_interface(core.api.ICode, self, 'callback')
  33. def callback(self, parent, data, is_updated):
  34. is_updated = is_updated or self.is_updated
  35. if is_updated == True:
  36. for region in data.iterate_regions():
  37. size = 0
  38. start_pos = region.get_offset_begin()
  39. for sub_id in region.iterate_subregion_ids():
  40. # exclude sub regions, like enclosed classes
  41. size += data.get_region(sub_id).get_offset_begin() - start_pos
  42. start_pos = data.get_region(sub_id).get_offset_end()
  43. size += region.get_offset_end() - start_pos
  44. region.set_data(self.get_name(), 'size', size)