/usr/lib/python3/dist-packages/dominate/util.py is in python3-dominate 2.1.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | '''
Utility classes for creating dynamic html documents
'''
__license__ = '''
This file is part of Dominate.
Dominate is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Dominate is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with Dominate. If not, see
<http://www.gnu.org/licenses/>.
'''
import re
from .dom_tag import dom_tag
try:
basestring = basestring
except NameError:
basestring = str
unichr = chr
def include(f):
'''
includes the contents of a file on disk.
takes a filename
'''
fl = file(f, 'rb')
data = fl.read()
fl.close()
return data
def system(cmd, data='', mode='t'):
'''
pipes the output of a program
'''
import os
fin, fout = os.popen4(cmd, mode)
fin.write(data)
fin.close()
return fout.read()
def escape(data, quote=True): # stoled from std lib cgi
'''
Escapes special characters into their html entities
Replace special characters "&", "<" and ">" to HTML-safe sequences.
If the optional flag quote is true, the quotation mark character (")
is also translated.
This is used to escape content that appears in the body of an HTML cocument
'''
data = data.replace("&", "&") # Must be done first!
data = data.replace("<", "<")
data = data.replace(">", ">")
if quote:
data = data.replace('"', """)
return data
_unescape = {
'quot': 34,
'amp': 38,
'lt': 60,
'gt': 62,
'nbsp': 32,
# more here
# http://www.w3.org/TR/html4/sgml/entities.html
'yuml': 255,
}
def unescape(data):
'''
unescapes html entities. the opposite of escape.
'''
cc = re.compile('&(?:(?:#(\d+))|([^;]+));')
result = []
m = cc.search(data)
while m:
result.append(data[0:m.start()])
d = m.group(1)
if d:
d = int(d)
result.append(unichr(d))
else:
d = _unescape.get(m.group(2), ord('?'))
result.append(unichr(d))
data = data[m.end():]
m = cc.search(data)
result.append(data)
return ''.join(result)
_reserved = ";/?:@&=+$, "
_replace_map = dict((c, '%%%2X' % ord(c)) for c in _reserved)
def url_escape(data):
return ''.join(_replace_map.get(c, c) for c in data)
def url_unescape(data):
return re.sub('%([0-9a-fA-F]{2})',
lambda m: chr(int(m.group(1), 16)), data)
class lazy(dom_tag):
'''
delays function execution until rendered
'''
def __init__(self, func, *args, **kwargs):
super(lazy, self).__init__()
self.func = func
self.args = args
self.kwargs = kwargs
def render(self, indent=1, inline=False):
return self.func(*self.args, **self.kwargs)
# TODO rename this to raw?
class text(dom_tag):
'''
Just a string. useful for inside context managers
Note: this will not escape HTML, it is a raw passthrough
'''
def __init__(self, _text, escape=True):
super(text, self).__init__()
if escape:
from . import util
self.text = util.escape(_text)
else:
self.text = _text
def _render(self, rendered, indent, inline):
rendered.append(self.text)
return rendered
def raw(s):
'''
Inserts a raw string into the DOM. Unsafe.
'''
return text(s, escape=False)
|