Source code for robot.model.tagstatistics

#  Copyright 2008-2015 Nokia Networks
#  Copyright 2016-     Robot Framework Foundation
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  See the License for the specific language governing permissions and
#  limitations under the License.

from itertools import chain
import re

from robot.utils import NormalizedDict

from .stats import CombinedTagStat, TagStat
from .tags import TagPatterns

[docs]class TagStatistics: """Container for tag statistics.""" def __init__(self, combined_stats): #: Dictionary, where key is the name of the tag as a string and value #: is an instance of :class:`~robot.model.stats.TagStat`. self.tags = NormalizedDict(ignore='_') #: List of :class:`~robot.model.stats.CombinedTagStat` objects. self.combined = combined_stats
[docs] def visit(self, visitor): visitor.visit_tag_statistics(self)
def __iter__(self): return iter(sorted(chain(self.combined, self.tags.values())))
[docs]class TagStatisticsBuilder: def __init__(self, included=None, excluded=None, combined=None, docs=None, links=None): self._included = TagPatterns(included) self._excluded = TagPatterns(excluded) self._reserved = TagPatterns('robot:*') self._info = TagStatInfo(docs, links) self.stats = TagStatistics(self._info.get_combined_stats(combined))
[docs] def add_test(self, test): self._add_tags_to_statistics(test) self._add_to_combined_statistics(test)
def _add_tags_to_statistics(self, test): for tag in test.tags: if self._is_included(tag) and not self._suppress_reserved(tag): if tag not in self.stats.tags: self.stats.tags[tag] = self._info.get_stat(tag) self.stats.tags[tag].add_test(test) def _is_included(self, tag): if self._included and tag not in self._included: return False return tag not in self._excluded def _suppress_reserved(self, tag): return tag in self._reserved and tag not in self._included def _add_to_combined_statistics(self, test): for stat in self.stats.combined: if stat.match(test.tags): stat.add_test(test)
[docs]class TagStatInfo: def __init__(self, docs=None, links=None): self._docs = [TagStatDoc(*doc) for doc in docs or []] self._links = [TagStatLink(*link) for link in links or []]
[docs] def get_stat(self, tag): return TagStat(tag, self.get_doc(tag), self.get_links(tag))
[docs] def get_combined_stats(self, combined=None): return [self._get_combined_stat(*comb) for comb in combined or []]
def _get_combined_stat(self, pattern, name=None): name = name or pattern return CombinedTagStat(pattern, name, self.get_doc(name), self.get_links(name))
[docs] def get_doc(self, tag): return ' & '.join(doc.text for doc in self._docs if doc.match(tag))
[docs]class TagStatDoc: def __init__(self, pattern, doc): self._matcher = TagPatterns(pattern) self.text = doc
[docs] def match(self, tag): return self._matcher.match(tag)