length.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #
  2. # Metrix++, Copyright 2009-2019, Metrix++ Project
  3. # Link: https://github.com/metrixplusplus/metrixplusplus
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. import mpp.api
  8. class Plugin(mpp.api.Plugin, mpp.api.Child, mpp.api.IConfigurable):
  9. def declare_configuration(self, parser):
  10. parser.add_option("--std.code.length.total", "--sclent", action="store_true", default=False,
  11. help="Enables collection of size metric (in number of symbols per region) [default: %default]")
  12. def configure(self, options):
  13. self.is_active = options.__dict__['std.code.length.total']
  14. def initialize(self):
  15. fields = []
  16. if self.is_active == True:
  17. fields.append(self.Field('total', int))
  18. mpp.api.Plugin.initialize(self, fields=fields)
  19. if len(fields) != 0:
  20. self.subscribe_by_parents_interface(mpp.api.ICode)
  21. def callback(self, parent, data, is_updated):
  22. is_updated = is_updated or self.is_updated
  23. if is_updated == True:
  24. for region in data.iterate_regions():
  25. size = 0
  26. start_pos = region.get_offset_begin()
  27. for sub_id in region.iterate_subregion_ids():
  28. # exclude sub regions, like enclosed classes
  29. size += data.get_region(sub_id).get_offset_begin() - start_pos
  30. start_pos = data.get_region(sub_id).get_offset_end()
  31. size += region.get_offset_end() - start_pos
  32. region.set_data(self.get_name(), 'total', size)