def var_table(self, var): # simple data types if isinstance(var, basestring) or isinstance(var, float) or isinstance(var, int) or isinstance(var, long): return '<table class="vars"><tr><td class="value">%r' "</td></tr></table>" % _escape(repr(var)) # dicts if isinstance(var, dict) or hasattr(var, "items"): items = var.items() items.sort() # empty dict if not items: return '<table class="vars"><tr><th>no data given' "</th></tr></table>" result = ['<table class="vars"><tr><th>Name' "</th><th>Value</th></tr>"] for key, value in items: try: val = _escape(_pprint.pformat(value)) except (SystemExit, KeyboardInterrupt): raise except: val = "?" result.append( '<tr><td class="name">%s</td><td class="value">%s' "</td></tr>" % (_escape(repr(key)), val) ) result.append("</table>") return "\n".join(result) # lists if isinstance(var, list): # empty list if not var: return '<table class="vars"><tr><th>no data given' "</th></tr></table>" result = ['<table class="vars">'] for line in var: try: val = _escape(_pprint.pformat(line)) except (SystemExit, KeyboardInterrupt): raise except: val = "?" result.append('<tr><td class="value">%s</td></tr>' % (val)) result.append("</table>") return "\n".join(result) # unknown things try: value = _escape(repr(var)) except (SystemExit, KeyboardInterrupt): raise except: value = "?" return '<table class="vars"><tr><th>%s</th></tr></table>' % value
def characters(self, content, **kwargs): if content and re.search(r'[\x00-\x08\x0B-\x0C\x0E-\x1F]', content): # Fail loudly when content has control chars (unsupported in XML 1.0) # See http://www.w3.org/International/questions/qa-controls raise UnserializableContentError( 'Control characters are not supported in XML 1.0') # XMLGenerator.characters(self, content) # Python 3 # https://github.com/python/cpython/blob/3.6/Lib/xml/sax/saxutils.py#L209 try: if content: self._finish_pending_start_element() if not isinstance(content, str): content = str(content, self._encoding) # Python 2 # https://github.com/python/cpython/blob/2.7/Lib/xml/sax/saxutils.py#L185 except AttributeError: if not isinstance(content, unicode): content = unicode(content, self._encoding) # Custom kwarg if kwargs['escape']: content = _escape(content) # Custom kwarg if kwargs['cdata']: content = '<![CDATA[%s]]>' % content self._write(content)
def _write_attribute(self, name: Name, value: str, parent_namespace: Optional[str]): self._target.write(" ") self._write_name( name if name.namespace != parent_namespace else Name(name.name)) self._target.write('="') self._target.write(_escape(value)) self._target.write('"')
def save(self, buf): buf.write('<text x="{0.x}" y="{0.y}" '.format(self.pt)) buf.write('style="') for key, value in self.style.items(): buf.write('{}:{};'.format(key, value)) buf.write('"') if self.rotate_angle: buf.write(' transform="rotate({0} {1.x},{1.y})"' .format(self.rotate_angle, self.rotate_center)) buf.write('>\n') buf.write(_escape(self.text)) buf.write('\n</text>\n')
def request_information(self): result = [ "<h2>Request Environment</h2>", '<p class="text">The following list contains all environment', "variables. Click on a header to expand the list.</p>", ] if not hasattr(self.c, "frames"): del result[0] for key, info in self.c.req_vars: result.append("<dl><dt>%s</dt><dd>%s</dd></dl>" % (_escape(key), self.var_table(info))) return "\n".join(result)
def header(self): data = [ '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ' '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">', '<html xmlns="http://www.w3.org/1999/xhtml"><head>', "<title>Python Traceback</title>", '<script type="text/javascript">%s</script>' % JAVASCRIPT, '<style type="text/css">%s</style>' % STYLESHEET, "</head><body>", '<div id="wsgi-traceback">', ] if hasattr(self.c, "exception_type"): title = _escape(self.c.exception_type) exc = _escape(self.c.exception_value) data += ["<h1>%s</h1>" % title, '<p class="errormsg">%s</p>' % exc] if hasattr(self.c, "last_frame"): data += [ '<p class="errorline">%s in %s, line %s</p>' % (self.c.last_frame["filename"], self.c.last_frame["function"], self.c.last_frame["lineno"]) ] return "\n".join(data)
def dump(self, node: XmlNode, write_xmlns: bool): self._target.write("<") self._write_name(node.name) if write_xmlns: self._write_namespaces() for attribute, value in node.attributes.items(): self._write_attribute(attribute, value, node.name.namespace) if node.children: self._target.write(">") for child in node.children: if isinstance(child, XmlNode): self.dump(child, False) else: self._target.write(_escape(child)) self._target.write("</") self._write_name(node.name) self._target.write(">") else: self._target.write("/>")
def escape(*args): return tuple(_escape(unicode(x)) for x in args)
def escape(value, entities=None): _ent = escape_entities if entities is not None: _ent = _ent.copy() _ent.update(entities) return _escape(value, entities)
def escape(text): escaped = _escape(text) if len(escaped) - len(text) > 12 and "]]>" not in text: return "<![CDATA[" + text + "]]>" return escaped
def escape(s): return _escape(_illegal_xml_chars_RE.sub(" ", s))