length.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 mpp.api
  20. class Plugin(mpp.api.Plugin, mpp.api.Child, mpp.api.IConfigurable):
  21. def declare_configuration(self, parser):
  22. parser.add_option("--std.code.length.total", "--sclent", 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.total']
  26. def initialize(self):
  27. fields = []
  28. if self.is_active == True:
  29. fields.append(self.Field('total', int))
  30. mpp.api.Plugin.initialize(self, fields=fields)
  31. if len(fields) != 0:
  32. self.subscribe_by_parents_interface(mpp.api.ICode)
  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(), 'total', size)