Robot Framework
tagstatistics.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from itertools import chain
17 import re
18 
19 from robot.utils import NormalizedDict
20 
21 from .stats import CombinedTagStat, TagStat
22 from .tags import TagPatterns
23 
24 
25 
27 
28  def __init__(self, combined_stats):
29  #: Dictionary, where key is the name of the tag as a string and value
30  #: is an instance of :class:`~robot.model.stats.TagStat`.
31  self.tagstags = NormalizedDict(ignore='_')
32  #: List of :class:`~robot.model.stats.CombinedTagStat` objects.
33  self.combinedcombined = combined_stats
34 
35  def visit(self, visitor):
36  visitor.visit_tag_statistics(self)
37 
38  def __iter__(self):
39  return iter(sorted(chain(self.combinedcombined, self.tagstags.values())))
40 
41 
43 
44  def __init__(self, included=None, excluded=None, combined=None, docs=None,
45  links=None):
46  self._included_included = TagPatterns(included)
47  self._excluded_excluded = TagPatterns(excluded)
48  self._reserved_reserved = TagPatterns('robot:*')
49  self._info_info = TagStatInfo(docs, links)
50  self.statsstats = TagStatistics(self._info_info.get_combined_stats(combined))
51 
52  def add_test(self, test):
53  self._add_tags_to_statistics_add_tags_to_statistics(test)
54  self._add_to_combined_statistics_add_to_combined_statistics(test)
55 
56  def _add_tags_to_statistics(self, test):
57  for tag in test.tags:
58  if self._is_included_is_included(tag) and not self._suppress_reserved_suppress_reserved(tag):
59  if tag not in self.statsstats.tags:
60  self.statsstats.tags[tag] = self._info_info.get_stat(tag)
61  self.statsstats.tags[tag].add_test(test)
62 
63  def _is_included(self, tag):
64  if self._included_included and tag not in self._included_included:
65  return False
66  return tag not in self._excluded_excluded
67 
68  def _suppress_reserved(self, tag):
69  return tag in self._reserved_reserved and tag not in self._included_included
70 
71  def _add_to_combined_statistics(self, test):
72  for stat in self.statsstats.combined:
73  if stat.match(test.tags):
74  stat.add_test(test)
75 
76 
78 
79  def __init__(self, docs=None, links=None):
80  self._docs_docs = [TagStatDoc(*doc) for doc in docs or []]
81  self._links_links = [TagStatLink(*link) for link in links or []]
82 
83  def get_stat(self, tag):
84  return TagStat(tag, self.get_docget_doc(tag), self.get_linksget_links(tag))
85 
86  def get_combined_stats(self, combined=None):
87  return [self._get_combined_stat_get_combined_stat(*comb) for comb in combined or []]
88 
89  def _get_combined_stat(self, pattern, name=None):
90  name = name or pattern
91  return CombinedTagStat(pattern, name, self.get_docget_doc(name),
92  self.get_linksget_links(name))
93 
94  def get_doc(self, tag):
95  return ' & '.join(doc.text for doc in self._docs_docs if doc.match(tag))
96 
97  def get_links(self, tag):
98  return [link.get_link(tag) for link in self._links_links if link.match(tag)]
99 
100 
102 
103  def __init__(self, pattern, doc):
104  self._matcher_matcher = TagPatterns(pattern)
105  self.texttext = doc
106 
107  def match(self, tag):
108  return self._matcher_matcher.match(tag)
109 
110 
112 
115  _match_pattern_tokenizer = re.compile(r'(\*|\?+)')
116 
117  def __init__(self, pattern, link, title):
118  self._regexp_regexp = self._get_match_regexp_get_match_regexp(pattern)
119  self._link_link = link
120  self._title_title = title.replace('_', ' ')
121 
122  def match(self, tag):
123  return self._regexp_regexp.match(tag) is not None
124 
125  def get_link(self, tag):
126  match = self._regexp_regexp.match(tag)
127  if not match:
128  return None
129  link, title = self._replace_groups_replace_groups(self._link_link, self._title_title, match)
130  return link, title
131 
132  def _replace_groups(self, link, title, match):
133  for index, group in enumerate(match.groups()):
134  placefolder = '%%%d' % (index+1)
135  link = link.replace(placefolder, group)
136  title = title.replace(placefolder, group)
137  return link, title
138 
139  def _get_match_regexp(self, pattern):
140  pattern = '^%s$' % ''.join(self._yield_match_pattern_yield_match_pattern(pattern))
141  return re.compile(pattern, re.IGNORECASE)
142 
143  def _yield_match_pattern(self, pattern):
144  for token in self._match_pattern_tokenizer_match_pattern_tokenizer.split(pattern):
145  if token.startswith('?'):
146  yield '(%s)' % ('.'*len(token))
147  elif token == '*':
148  yield '(.*)'
149  else:
150  yield re.escape(token)
Stores statistic values for a single tag.
Definition: stats.py:134
def __init__(self, pattern, doc)
def get_combined_stats(self, combined=None)
def __init__(self, docs=None, links=None)
def _get_combined_stat(self, pattern, name=None)
def __init__(self, included=None, excluded=None, combined=None, docs=None, links=None)
Container for tag statistics.
def __init__(self, combined_stats)