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