member.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. import re
  21. class Plugin(mpp.api.Plugin,
  22. mpp.api.IConfigurable,
  23. mpp.api.Child,
  24. mpp.api.MetricPluginMixin):
  25. def declare_configuration(self, parser):
  26. parser.add_option("--std.code.member.fields", "--scmf",
  27. action="store_true", default=False,
  28. help="Enables collection of number of data members / fields "
  29. "per classes, structs and interfaces [default: %default]")
  30. parser.add_option("--std.code.member.globals", "--scmg",
  31. action="store_true", default=False,
  32. help="Enables collection of number of global variables / fields "
  33. "per global regions and namespaces [default: %default]")
  34. parser.add_option("--std.code.member.classes", "--scmc",
  35. action="store_true", default=False,
  36. help="Enables collection of number of classes defined "
  37. "per any region [default: %default]")
  38. parser.add_option("--std.code.member.structs", "--scms",
  39. action="store_true", default=False,
  40. help="Enables collection of number of structs defined "
  41. "per any region [default: %default]")
  42. parser.add_option("--std.code.member.interfaces", "--scmi",
  43. action="store_true", default=False,
  44. help="Enables collection of number of interfaces defined "
  45. "per any region [default: %default]")
  46. parser.add_option("--std.code.member.types", "--scmt",
  47. action="store_true", default=False,
  48. help="Enables collection of number of types (classes, structs "
  49. "or interface) defined per any region [default: %default]")
  50. parser.add_option("--std.code.member.methods", "--scmm",
  51. action="store_true", default=False,
  52. help="Enables collection of number of methods (functions) defined "
  53. "per any region [default: %default]")
  54. parser.add_option("--std.code.member.namespaces", "--scmnss",
  55. action="store_true", default=False,
  56. help="Enables collection of number of namespaces defined "
  57. "globally and enclosed (sub-namespaces) [default: %default]")
  58. def configure(self, options):
  59. self.is_active_fields = options.__dict__['std.code.member.fields']
  60. self.is_active_globals = options.__dict__['std.code.member.globals']
  61. self.is_active_classes = options.__dict__['std.code.member.classes']
  62. self.is_active_structs = options.__dict__['std.code.member.structs']
  63. self.is_active_interfaces = options.__dict__['std.code.member.interfaces']
  64. self.is_active_types = options.__dict__['std.code.member.types']
  65. self.is_active_methods = options.__dict__['std.code.member.methods']
  66. self.is_active_namespaces = options.__dict__['std.code.member.namespaces']
  67. def initialize(self):
  68. # counts fields and properties with default getter/setter
  69. pattern_to_search_cs = re.compile(
  70. r'''([_a-zA-Z][_a-zA-Z0-9]*\s+[_a-zA-Z][_a-zA-Z0-9])\s*([=;]|'''
  71. r'''[{]\s*(public\s+|private\s+|protected\s+|internal\s+)?(get|set)\s*[;]\s*[a-z \t\r\n]*[}])''')
  72. pattern_to_search_cpp = re.compile(
  73. r'''([_a-zA-Z][_a-zA-Z0-9]*\s+[_a-zA-Z][_a-zA-Z0-9])\s*[=;]''')
  74. pattern_to_search_java = re.compile(
  75. r'''([_$a-zA-Z][_$a-zA-Z0-9]*\s+[_$a-zA-Z][_$a-zA-Z0-9])\s*[=;]''')
  76. self.declare_metric(self.is_active_fields,
  77. self.Field('fields', int, non_zero=True),
  78. {
  79. 'std.code.java': pattern_to_search_java,
  80. 'std.code.cpp': pattern_to_search_cpp,
  81. 'std.code.cs': pattern_to_search_cs,
  82. },
  83. marker_type_mask=mpp.api.Marker.T.CODE,
  84. region_type_mask=mpp.api.Region.T.CLASS |
  85. mpp.api.Region.T.STRUCT | mpp.api.Region.T.INTERFACE)
  86. self.declare_metric(self.is_active_globals,
  87. self.Field('globals', int, non_zero=True),
  88. {
  89. 'std.code.java': pattern_to_search_java,
  90. 'std.code.cpp': pattern_to_search_cpp,
  91. 'std.code.cs': pattern_to_search_cs,
  92. },
  93. marker_type_mask=mpp.api.Marker.T.CODE,
  94. region_type_mask=mpp.api.Region.T.GLOBAL |
  95. mpp.api.Region.T.NAMESPACE)
  96. self.declare_metric(self.is_active_classes,
  97. self.Field('classes', int, non_zero=True),
  98. (None, self.ClassesCounter),
  99. exclude_subregions=False,
  100. merge_markers=True)
  101. self.declare_metric(self.is_active_structs,
  102. self.Field('structs', int, non_zero=True),
  103. (None, self.StructCounter),
  104. exclude_subregions=False,
  105. merge_markers=True)
  106. self.declare_metric(self.is_active_interfaces,
  107. self.Field('interfaces', int, non_zero=True),
  108. (None, self.InterfaceCounter),
  109. exclude_subregions=False,
  110. merge_markers=True)
  111. self.declare_metric(self.is_active_types,
  112. self.Field('types', int, non_zero=True),
  113. (None, self.TypeCounter),
  114. exclude_subregions=False,
  115. merge_markers=True)
  116. self.declare_metric(self.is_active_methods,
  117. self.Field('methods', int, non_zero=True),
  118. (None, self.MethodCounter),
  119. exclude_subregions=False,
  120. merge_markers=True)
  121. self.declare_metric(self.is_active_namespaces,
  122. self.Field('namespaces', int, non_zero=True),
  123. (None, self.NamespaceCounter),
  124. exclude_subregions=False,
  125. merge_markers=True)
  126. super(Plugin, self).initialize(fields=self.get_fields())
  127. if self.is_active() == True:
  128. self.subscribe_by_parents_interface(mpp.api.ICode)
  129. class ClassesCounter(mpp.api.MetricPluginMixin.PlainCounter):
  130. def count(self, marker, pattern_to_search):
  131. self.result = sum(1 for unused in self.data.iterate_regions(
  132. filter_group=mpp.api.Region.T.CLASS, region_id=self.region.get_id()))
  133. class StructCounter(mpp.api.MetricPluginMixin.PlainCounter):
  134. def count(self, marker, pattern_to_search):
  135. self.result = sum(1 for unused in self.data.iterate_regions(
  136. filter_group=mpp.api.Region.T.STRUCT, region_id=self.region.get_id()))
  137. class InterfaceCounter(mpp.api.MetricPluginMixin.PlainCounter):
  138. def count(self, marker, pattern_to_search):
  139. self.result = sum(1 for unused in self.data.iterate_regions(
  140. filter_group=mpp.api.Region.T.INTERFACE, region_id=self.region.get_id()))
  141. class TypeCounter(mpp.api.MetricPluginMixin.PlainCounter):
  142. def count(self, marker, pattern_to_search):
  143. self.result = sum(1 for unused in self.data.iterate_regions(
  144. filter_group=mpp.api.Region.T.CLASS | mpp.api.Region.T.STRUCT |
  145. mpp.api.Region.T.INTERFACE, region_id=self.region.get_id()))
  146. class MethodCounter(mpp.api.MetricPluginMixin.PlainCounter):
  147. def count(self, marker, pattern_to_search):
  148. self.result = sum(1 for unused in self.data.iterate_regions(
  149. filter_group=mpp.api.Region.T.FUNCTION, region_id=self.region.get_id()))
  150. class NamespaceCounter(mpp.api.MetricPluginMixin.PlainCounter):
  151. def count(self, marker, pattern_to_search):
  152. self.result = sum(1 for unused in self.data.iterate_regions(
  153. filter_group=mpp.api.Region.T.NAMESPACE, region_id=self.region.get_id()))