Robot Framework
jswriter.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.htmldata import JsonWriter
17 
18 
20 
23  _output_attr = 'window.output'
24 
27  _settings_attr = 'window.settings'
28 
31  _suite_key = 'suite'
32 
35  _strings_key = 'strings'
36 
37  def __init__(self, output,
38  start_block='<script type="text/javascript">\n',
39  end_block='</script>\n',
40  split_threshold=9500):
41  writer = JsonWriter(output, separator=end_block+start_block)
42  self._write_write = writer.write
43  self._write_json_write_json = writer.write_json
44  self._start_block_start_block = start_block
45  self._end_block_end_block = end_block
46  self._split_threshold_split_threshold = split_threshold
47 
48  def write(self, result, settings):
49  self._start_output_block_start_output_block()
50  self._write_suite_write_suite(result.suite)
51  self._write_strings_write_strings(result.strings)
52  self._write_data_write_data(result.data)
53  self._write_settings_and_end_output_block_write_settings_and_end_output_block(settings)
54 
56  self._write_write(self._start_block_start_block, postfix='', separator=False)
57  self._write_write('%s = {}' % self._output_attr_output_attr)
58 
59  def _write_suite(self, suite):
60  writer = SuiteWriter(self._write_json_write_json, self._split_threshold_split_threshold)
61  writer.write(suite, self._output_var_output_var(self._suite_key_suite_key))
62 
63  def _write_strings(self, strings):
64  variable = self._output_var_output_var(self._strings_key_strings_key)
65  self._write_write('%s = []' % variable)
66  prefix = '%s = %s.concat(' % (variable, variable)
67  postfix = ');\n'
68  threshold = self._split_threshold_split_threshold
69  for index in range(0, len(strings), threshold):
70  self._write_json_write_json(prefix, strings[index:index+threshold], postfix)
71 
72  def _write_data(self, data):
73  for key in data:
74  self._write_json_write_json('%s = ' % self._output_var_output_var(key), data[key])
75 
77  self._write_json_write_json('%s = ' % self._settings_attr_settings_attr, settings,
78  separator=False)
79  self._write_write(self._end_block_end_block, postfix='', separator=False)
80 
81  def _output_var(self, key):
82  return '%s["%s"]' % (self._output_attr_output_attr, key)
83 
84 
86 
87  def __init__(self, write_json, split_threshold):
88  self._write_json_write_json = write_json
89  self._split_threshold_split_threshold = split_threshold
90 
91  def write(self, suite, variable):
92  mapping = {}
93  self._write_parts_over_threshold_write_parts_over_threshold(suite, mapping)
94  self._write_json_write_json('%s = ' % variable, suite, mapping=mapping)
95 
96  def _write_parts_over_threshold(self, data, mapping):
97  if not isinstance(data, tuple):
98  return 1
99  not_written = 1 + sum(self._write_parts_over_threshold_write_parts_over_threshold(item, mapping)
100  for item in data)
101  if not_written > self._split_threshold_split_threshold:
102  self._write_part_write_part(data, mapping)
103  return 1
104  return not_written
105 
106  def _write_part(self, data, mapping):
107  part_name = 'window.sPart%d' % len(mapping)
108  self._write_json_write_json('%s = ' % part_name, data, mapping=mapping)
109  mapping[data] = part_name
110 
111 
113 
114  def __init__(self, output):
115  self._writer_writer = JsonWriter(output)
116 
117  def write(self, keywords, strings, index, notify):
118  self._writer_writer.write_json('window.keywords%d = ' % index, keywords)
119  self._writer_writer.write_json('window.strings%d = ' % index, strings)
120  self._writer_writer.write('window.fileLoading.notify("%s")' % notify)
def write(self, result, settings)
Definition: jswriter.py:48
def __init__(self, output, start_block='< script type="text/javascript">\n', end_block='</script >\n', split_threshold=9500)
Definition: jswriter.py:40
def _write_settings_and_end_output_block(self, settings)
Definition: jswriter.py:76
def write(self, keywords, strings, index, notify)
Definition: jswriter.py:117
def _write_part(self, data, mapping)
Definition: jswriter.py:106
def __init__(self, write_json, split_threshold)
Definition: jswriter.py:87
def write(self, suite, variable)
Definition: jswriter.py:91
def _write_parts_over_threshold(self, data, mapping)
Definition: jswriter.py:96