Robot Framework
jsbuildingcontext.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 contextlib import contextmanager
17 from os.path import exists, dirname
18 
19 from robot.output.loggerhelper import LEVELS
20 from robot.utils import (attribute_escape, get_link_path, html_escape, is_string,
21  safe_str, timestamp_to_secs)
22 
23 from .expandkeywordmatcher import ExpandKeywordMatcher
24 from .stringcache import StringCache
25 
26 
28 
29  def __init__(self, log_path=None, split_log=False, expand_keywords=None,
30  prune_input=False):
31  # log_path can be a custom object in unit tests
32  self._log_dir_log_dir = dirname(log_path) if is_string(log_path) else None
33  self._split_log_split_log = split_log
34  self._prune_input_prune_input = prune_input
35  self._strings_strings = self._top_level_strings_top_level_strings = StringCache()
36  self.basemillisbasemillis = None
37  self.split_resultssplit_results = []
38  self.min_levelmin_level = 'NONE'
39  self._msg_links_msg_links = {}
40  self._expand_matcher_expand_matcher = ExpandKeywordMatcher(expand_keywords) \
41  if expand_keywords else None
42 
43  def string(self, string, escape=True, attr=False):
44  if escape and string:
45  if not is_string(string):
46  string = safe_str(string)
47  string = (html_escape if not attr else attribute_escape)(string)
48  return self._strings_strings.add(string)
49 
50  def html(self, string):
51  return self._strings_strings.add(string, html=True)
52 
53  def relative_source(self, source):
54  rel_source = get_link_path(source, self._log_dir_log_dir) \
55  if self._log_dir_log_dir and source and exists(source) else ''
56  return self.stringstring(rel_source)
57 
58  def timestamp(self, time):
59  if not time:
60  return None
61  millis = int(timestamp_to_secs(time) * 1000)
62  if self.basemillisbasemillis is None:
63  self.basemillisbasemillis = millis
64  return millis - self.basemillisbasemillis
65 
66  def message_level(self, level):
67  if LEVELS[level] < LEVELS[self.min_levelmin_level]:
68  self.min_levelmin_level = level
69 
70  def create_link_target(self, msg):
71  id = self._top_level_strings_top_level_strings.add(msg.parent.id)
72  self._msg_links_msg_links[self._link_key_link_key(msg)] = id
73 
74  def check_expansion(self, kw):
75  if self._expand_matcher_expand_matcher is not None:
76  self._expand_matcher_expand_matcher.match(kw)
77 
78  @property
79  expand_keywords = property
80 
81  def expand_keywords(self):
82  return self._expand_matcher_expand_matcher.matched_ids if self._expand_matcher_expand_matcher else None
83 
84  def link(self, msg):
85  return self._msg_links_msg_links.get(self._link_key_link_key(msg))
86 
87  def _link_key(self, msg):
88  return (msg.message, msg.level, msg.timestamp)
89 
90  @property
91  strings = property
92 
93  def strings(self):
94  return self._strings_strings.dump()
95 
96  def start_splitting_if_needed(self, split=False):
97  if self._split_log_split_log and split:
98  self._strings_strings = StringCache()
99  return True
100  return False
101 
102  def end_splitting(self, model):
103  self.split_resultssplit_results.append((model, self.stringsstringsstrings))
104  self._strings_strings = self._top_level_strings_top_level_strings
105  return len(self.split_resultssplit_results)
106 
107  @contextmanager
108  def prune_input(self, *items):
109  yield
110  if self._prune_input_prune_input:
111  for item in items:
112  item.clear()
def __init__(self, log_path=None, split_log=False, expand_keywords=None, prune_input=False)
def string(self, string, escape=True, attr=False)
def get_link_path(target, base)
Returns a relative path to target from base.
Definition: robotpath.py:78
def timestamp_to_secs(timestamp, seps=None)
Definition: robottime.py:339
def safe_str(item)
Definition: unic.py:21