Robot Framework Integrated Development Environment (RIDE)
markupwriters.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 .markuputils import attribute_escape, html_escape, xml_escape
17 from .robottypes import is_string
18 from .robotio import file_writer
19 
20 
21 class _MarkupWriter():
22 
23 
29  def __init__(self, output, write_empty=True):
30  if is_string(output):
31  output = file_writer(output)
32  self.outputoutput = output
33  self._write_empty_write_empty = write_empty
34  self._preamble_preamble()
35 
36  def _preamble(self):
37  pass
38 
39  def start(self, name, attrs=None, newline=True):
40  attrs = self._format_attrs(attrs)
41  self._start(name, attrs, newline)
42 
43  def _start(self, name, attrs, newline):
44  self._write_write('<%s %s>' % (name, attrs) if attrs else '<%s>' % name,
45  newline)
46 
47  def _format_attrs(self, attrs):
48  if not attrs:
49  return ''
50  attrs = [(k, attribute_escape(attrs[k] or ''))
51  for k in self._order_attrs_order_attrs(attrs)]
52  write_empty = self._write_empty_write_empty
53  return ' '.join('%s="%s"' % a for a in attrs if write_empty or a[1])
54 
55  def _order_attrs(self, attrs):
56  return attrs
57 
58  def content(self, content=None, escape=True, newline=False):
59  if content:
60  self._write_write(self._escape_escape(content) if escape else content, newline)
61 
62  def _escape(self, content):
63  raise NotImplementedError
64 
65  def end(self, name, newline=True):
66  self._write_write('</%s>' % name, newline)
67 
68  def element(self, name, content=None, attrs=None, escape=True,
69  newline=True, replace_newlines=False):
70  attrs = self._format_attrs_format_attrs(attrs)
71  if self._write_empty_write_empty or content or attrs:
72  self._start_start(name, attrs, newline=False)
73  self.contentcontent(content, escape, replace_newlines)
74  self.endend(name, newline)
75 
76 
77  def close(self):
78  self.outputoutput.close()
79 
80  def _write(self, text, newline=False):
81  self.outputoutput.write(text)
82  if newline:
83  self.outputoutput.write('\n')
84 
85 
87 
88  def _order_attrs(self, attrs):
89  return sorted(attrs) # eases testing
90 
91  def _escape(self, content):
92  return html_escape(content)
93 
94 
96 
97  def _preamble(self):
98  self._write_write('<?xml version="1.0" encoding="UTF-8"?>', newline=True)
99 
100  def _escape(self, text):
101  return xml_escape(text)
102 
103 
104 
106 
107  __init__ = start = content = element = end = close = \
108  lambda *args, **kwargs: None
Null implementation of the _MarkupWriter interface.
def close(self)
Closes the underlying output file.
def __init__(self, output, write_empty=True)
:param output: Either an opened, file like object, or a path to the desired output file.
def content(self, content=None, escape=True, newline=False)
def start(self, name, attrs=None, newline=True)
def element(self, name, content=None, attrs=None, escape=True, newline=True, replace_newlines=False)
def write(msg, level='INFO', html=False)
Writes the message to the log file using the given level.
Definition: logger.py:86
def html_escape(text, linkify=True)
Definition: markuputils.py:40
def file_writer(path=None, encoding='UTF-8', newline=None)
Definition: robotio.py:21