123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- import mpp.api
- import re
- class Plugin(mpp.api.Plugin, mpp.api.MetricPluginMixin, mpp.api.Child, mpp.api.IConfigurable):
-
- def declare_configuration(self, parser):
- parser.add_option("--std.code.complexity.cyclomatic", "--sccc", action="store_true", default=False,
- help="Enables collection of cyclomatic complexity metric (McCabe) [default: %default]")
- parser.add_option("--std.code.complexity.maxindent", "--sccmi", action="store_true", default=False,
- help="Enables collection of maximum indent level metric [default: %default]")
-
- def configure(self, options):
- self.is_active_cyclomatic = options.__dict__['std.code.complexity.cyclomatic']
- self.is_active_maxindent = options.__dict__['std.code.complexity.maxindent']
-
-
-
- pattern_cpp = re.compile(r'''([^0-9A-Za-z_]((if)|(case)|(for)|(while)|(catch))[^0-9A-Za-z_])|[&][&]|[|][|]|[?]''')
-
-
- pattern_cs = re.compile(r'''([^0-9A-Za-z_]((if)|(case)|(for)|(foreach)|(while)|(catch))[^0-9A-Za-z_])|[&][&]|[|][|]|[?][?]?''')
-
- pattern_java = re.compile(r'''([^0-9A-Za-z_]((if)|(case)|(for)|(while)|(catch))[^0-9A-Za-z_])|[&][&]|[|][|]|[?]''')
- pattern_indent = re.compile(r'''[}{]''')
- def initialize(self):
- self.declare_metric(self.is_active_cyclomatic,
- self.Field('cyclomatic', int),
- {
- 'std.code.cpp': self.pattern_cpp,
- 'std.code.cs': self.pattern_cs,
- 'std.code.java': self.pattern_java
- },
- marker_type_mask=mpp.api.Marker.T.CODE,
- region_type_mask=mpp.api.Region.T.FUNCTION)
- self.declare_metric(self.is_active_maxindent,
- self.Field('maxindent', int),
- {
- 'std.code.cpp': self.pattern_indent,
- 'std.code.cs': self.pattern_indent,
- 'std.code.java': self.pattern_indent,
- },
- marker_type_mask=mpp.api.Marker.T.CODE,
-
- region_type_mask=mpp.api.Region.T.ANY)
-
- super(Plugin, self).initialize(fields=self.get_fields())
-
- if self.is_active() == True:
- self.subscribe_by_parents_name('std.code.cpp')
- self.subscribe_by_parents_name('std.code.cs')
- self.subscribe_by_parents_name('std.code.java')
- def _maxindent_count_initialize(self, data, alias, region):
- return (0, {'cur_level': 0})
-
- def _maxindent_count(self, data, alias, text, begin, end, m, count, counter_data, region, marker):
- if m.group(0) == '{':
- counter_data['cur_level'] += 1
- if counter_data['cur_level'] > count:
- count = counter_data['cur_level']
- elif m.group(0) == '}':
- counter_data['cur_level'] -= 1
- else:
- assert False
- return count
|