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