Robot Framework
stringcache.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 robot.utils import compress_text, html_format
17 
18 
19 class StringIndex(int):
20  pass
21 
22 
24 
27  _compress_threshold = 80
28 
31  _use_compressed_threshold = 1.1
32 
35  _zero_index = StringIndex(0)
36 
37  def __init__(self):
38  self._cache_cache = {('', False): self._zero_index_zero_index}
39 
40  def add(self, text, html=False):
41  if not text:
42  return self._zero_index_zero_index
43  key = (text, html)
44  if key not in self._cache_cache:
45  self._cache_cache[key] = StringIndex(len(self._cache_cache))
46  return self._cache_cache[key]
47 
48  def dump(self):
49  return tuple(self._encode_encode(text, html) for text, html in self._cache_cache)
50 
51  def _encode(self, text, html=False):
52  if html:
53  text = html_format(text)
54  if len(text) > self._compress_threshold_compress_threshold:
55  compressed = compress_text(text)
56  if len(compressed) * self._use_compressed_threshold_use_compressed_threshold < len(text):
57  return compressed
58  # Strings starting with '*' are raw, others are compressed.
59  return '*' + text
def add(self, text, html=False)
Definition: stringcache.py:40
def _encode(self, text, html=False)
Definition: stringcache.py:51
def compress_text(text)
Definition: compress.py:20