summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/mozlog/mozlog/formatters/html/xmlgen.py
blob: 933958b9f1b23d1e4b91a0cb1555ffc34b0dad40 (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"""
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

This file is originally from: https://bitbucket.org/hpk42/py, specifically:
https://bitbucket.org/hpk42/py/src/980c8d526463958ee7cae678a7e4e9b054f36b94/py/_xmlgen.py?at=default
by holger krekel, holger at merlinux eu. 2009
"""

import re
import sys

if sys.version_info >= (3, 0):

    def u(s):
        return s

    def unicode(x):
        if hasattr(x, "__unicode__"):
            return x.__unicode__()
        return str(x)


else:

    def u(s):
        return unicode(s)

    # pylint: disable=W1612
    unicode = unicode


class NamespaceMetaclass(type):
    def __getattr__(self, name):
        if name[:1] == "_":
            raise AttributeError(name)
        if self == Namespace:
            raise ValueError("Namespace class is abstract")
        tagspec = self.__tagspec__
        if tagspec is not None and name not in tagspec:
            raise AttributeError(name)
        classattr = {}
        if self.__stickyname__:
            classattr["xmlname"] = name
        cls = type(name, (self.__tagclass__,), classattr)
        setattr(self, name, cls)
        return cls


class Tag(list):
    class Attr(object):
        def __init__(self, **kwargs):
            self.__dict__.update(kwargs)

    def __init__(self, *args, **kwargs):
        super(Tag, self).__init__(args)
        self.attr = self.Attr(**kwargs)

    def __unicode__(self):
        return self.unicode(indent=0)

    __str__ = __unicode__

    def unicode(self, indent=2):
        l = []
        SimpleUnicodeVisitor(l.append, indent).visit(self)
        return u("").join(l)

    def __repr__(self):
        name = self.__class__.__name__
        return "<%r tag object %d>" % (name, id(self))


Namespace = NamespaceMetaclass(
    "Namespace",
    (object,),
    {
        "__tagspec__": None,
        "__tagclass__": Tag,
        "__stickyname__": False,
    },
)


class HtmlTag(Tag):
    def unicode(self, indent=2):
        l = []
        HtmlVisitor(l.append, indent, shortempty=False).visit(self)
        return u("").join(l)


# exported plain html namespace


class html(Namespace):
    __tagclass__ = HtmlTag
    __stickyname__ = True
    __tagspec__ = dict(
        [
            (x, 1)
            for x in (
                "a,abbr,acronym,address,applet,area,b,bdo,big,blink,"
                "blockquote,body,br,button,caption,center,cite,code,col,"
                "colgroup,comment,dd,del,dfn,dir,div,dl,dt,em,embed,"
                "fieldset,font,form,frameset,h1,h2,h3,h4,h5,h6,head,html,"
                "i,iframe,img,input,ins,kbd,label,legend,li,link,listing,"
                "map,marquee,menu,meta,multicol,nobr,noembed,noframes,"
                "noscript,object,ol,optgroup,option,p,pre,q,s,script,"
                "select,small,span,strike,strong,style,sub,sup,table,"
                "tbody,td,textarea,tfoot,th,thead,title,tr,tt,u,ul,xmp,"
                "base,basefont,frame,hr,isindex,param,samp,var"
            ).split(",")
            if x
        ]
    )

    class Style(object):
        def __init__(self, **kw):
            for x, y in kw.items():
                x = x.replace("_", "-")
                setattr(self, x, y)


class raw(object):
    """just a box that can contain a unicode string that will be
    included directly in the output"""

    def __init__(self, uniobj):
        self.uniobj = uniobj


class SimpleUnicodeVisitor(object):
    """recursive visitor to write unicode."""

    def __init__(self, write, indent=0, curindent=0, shortempty=True):
        self.write = write
        self.cache = {}
        self.visited = {}  # for detection of recursion
        self.indent = indent
        self.curindent = curindent
        self.parents = []
        self.shortempty = shortempty  # short empty tags or not

    def visit(self, node):
        """dispatcher on node's class/bases name."""
        cls = node.__class__
        try:
            visitmethod = self.cache[cls]
        except KeyError:
            for subclass in cls.__mro__:
                visitmethod = getattr(self, subclass.__name__, None)
                if visitmethod is not None:
                    break
            else:
                visitmethod = self.__object
            self.cache[cls] = visitmethod
        visitmethod(node)

    # the default fallback handler is marked private
    # to avoid clashes with the tag name object
    def __object(self, obj):
        # self.write(obj)
        self.write(escape(unicode(obj)))

    def raw(self, obj):
        self.write(obj.uniobj)

    def list(self, obj):
        assert id(obj) not in self.visited
        self.visited[id(obj)] = 1
        for elem in obj:
            self.visit(elem)

    def Tag(self, tag):
        assert id(tag) not in self.visited
        try:
            tag.parent = self.parents[-1]
        except IndexError:
            tag.parent = None
        self.visited[id(tag)] = 1
        tagname = getattr(tag, "xmlname", tag.__class__.__name__)
        if self.curindent and not self._isinline(tagname):
            self.write("\n" + u(" ") * self.curindent)
        if tag:
            self.curindent += self.indent
            self.write(u("<%s%s>") % (tagname, self.attributes(tag)))
            self.parents.append(tag)
            for x in tag:
                self.visit(x)
            self.parents.pop()
            self.write(u("</%s>") % tagname)
            self.curindent -= self.indent
        else:
            nameattr = tagname + self.attributes(tag)
            if self._issingleton(tagname):
                self.write(u("<%s/>") % (nameattr,))
            else:
                self.write(u("<%s></%s>") % (nameattr, tagname))

    def attributes(self, tag):
        # serialize attributes
        attrlist = dir(tag.attr)
        attrlist.sort()
        l = []
        for name in attrlist:
            res = self.repr_attribute(tag.attr, name)
            if res is not None:
                l.append(res)
        l.extend(self.getstyle(tag))
        return u("").join(l)

    def repr_attribute(self, attrs, name):
        if name[:2] != "__":
            value = getattr(attrs, name)
            if name.endswith("_"):
                name = name[:-1]
            if isinstance(value, raw):
                insert = value.uniobj
            else:
                insert = escape(unicode(value))
            return ' %s="%s"' % (name, insert)

    def getstyle(self, tag):
        """return attribute list suitable for styling."""
        try:
            styledict = tag.style.__dict__
        except AttributeError:
            return []
        else:
            stylelist = [x + ": " + y for x, y in styledict.items()]
            return [u(' style="%s"') % u("; ").join(stylelist)]

    def _issingleton(self, tagname):
        """can (and will) be overridden in subclasses"""
        return self.shortempty

    def _isinline(self, tagname):
        """can (and will) be overridden in subclasses"""
        return False


class HtmlVisitor(SimpleUnicodeVisitor):

    single = dict(
        [
            (x, 1)
            for x in ("br,img,area,param,col,hr,meta,link,base," "input,frame").split(
                ","
            )
        ]
    )
    inline = dict(
        [
            (x, 1)
            for x in (
                "a abbr acronym b basefont bdo big br cite code dfn em font "
                "i img input kbd label q s samp select small span strike "
                "strong sub sup textarea tt u var".split(" ")
            )
        ]
    )

    def repr_attribute(self, attrs, name):
        if name == "class_":
            value = getattr(attrs, name)
            if value is None:
                return
        return super(HtmlVisitor, self).repr_attribute(attrs, name)

    def _issingleton(self, tagname):
        return tagname in self.single

    def _isinline(self, tagname):
        return tagname in self.inline


class _escape:
    def __init__(self):
        self.escape = {
            u('"'): u("&quot;"),
            u("<"): u("&lt;"),
            u(">"): u("&gt;"),
            u("&"): u("&amp;"),
            u("'"): u("&apos;"),
        }
        self.charef_rex = re.compile(u("|").join(self.escape.keys()))

    def _replacer(self, match):
        return self.escape[match.group(0)]

    def __call__(self, ustring):
        """xml-escape the given unicode string."""
        ustring = unicode(ustring)
        return self.charef_rex.sub(self._replacer, ustring)


escape = _escape()