summaryrefslogtreecommitdiffstats
path: root/testsuites/vstf/vstf_scripts/vstf/common/pyhtml.py
blob: b216229012c4b12bd21204f992e4395bea3779f5 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
##############################################################################
# Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################


from sys import stdout, modules

doc_type = '<!DOCTYPE HTML>\n'
default_title = "Html Page"
charset = '<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />\n'

html4_tags = {
    'a',
    'abbr',
    'acronym',
    'address',
    'area',
    'b',
    'base',
    'bdo',
    'big',
    'blockquote',
    'body',
    'br',
    'button',
    'caption',
    'cite',
    'code',
    'col',
    'colgroup',
    'dd',
    'del',
    'div',
    'dfn',
    'dl',
    'dt',
    'em',
    'fieldset',
    'form',
    'frame',
    'frameset',
    'h1',
    'h2',
    'h3',
    'h4',
    'h5',
    'h6',
    'head',
    'hr',
    'html',
    'i',
    'iframe',
    'img',
    'input',
    'ins',
    'kbd',
    'label',
    'legend',
    'li',
    'link',
    'map',
    'menu',
    'menuitem',
    'meta',
    'noframes',
    'noscript',
    'object',
    'ol',
    'optgroup',
    'option',
    'p',
    'param',
    'pre',
    'q',
    'samp',
    'script',
    'select',
    'small',
    'span',
    'strong',
    'style',
    'sub',
    'sup',
    'table',
    'tbody',
    'td',
    'textarea',
    'tfoot',
    'th',
    'thead',
    'title',
    'tr',
    'tt',
    'ul',
    'var'}
disused_tags = {'isindex', 'font', 'dir', 's', 'strike',
                'u', 'center', 'basefont', 'applet', 'xmp'}
html5_tags = {
    'article',
    'aside',
    'audio',
    'bdi',
    'canvas',
    'command',
    'datalist',
    'details',
    'dialog',
    'embed',
    'figcaption',
    'figure',
    'footer',
    'header',
    'keygen',
    'mark',
    'meter',
    'nav',
    'output',
    'progress',
    'rp',
    'rt',
    'ruby',
    'section',
    'source',
    'summary',
    'details',
    'time',
    'track',
    'video',
    'wbr'}

nl = '\n'
tags = html4_tags | disused_tags | html5_tags

__all__ = [x.title() for x in tags] + ['PyHtml', 'space']

self_close = {'input', 'img', 'link', 'br'}


def space(n):
    return ' ' * n


class Tag(list):
    tag_name = ''

    def __init__(self, *args, **kwargs):
        self.attributes = kwargs
        if self.tag_name:
            name = self.tag_name
            self.is_seq = False
        else:
            name = 'sequence'
            self.is_seq = True
        self._id = kwargs.get('id', name)
        for arg in args:
            self.add_obj(arg)

    def __iadd__(self, obj):
        if isinstance(obj, Tag) and obj.is_seq:
            for o in obj:
                self.add_obj(o)
        else:
            self.add_obj(obj)
        return self

    def add_obj(self, obj):
        if not isinstance(obj, Tag):
            obj = str(obj)
        _id = self.set_id(obj)
        setattr(self, _id, obj)
        self.append(obj)

    def set_id(self, obj):
        if isinstance(obj, Tag):
            _id = obj._id
            obj_lst = filter(lambda t: isinstance(
                t, Tag) and t._id.startswith(_id), self)
        else:
            _id = 'content'
            obj_lst = filter(lambda t: not isinstance(t, Tag), self)
        length = len(obj_lst)
        if obj_lst:
            _id = '%s_%03i' % (_id, length)
        if isinstance(obj, Tag):
            obj._id = _id
        return _id

    def __add__(self, obj):
        if self.tag_name:
            return Tag(self, obj)
        self.add_obj(obj)
        return self

    def __lshift__(self, obj):
        if isinstance(obj, Tag):
            self += obj
            return obj
        print "unknown obj: %s " % obj
        return self

    def render(self):
        result = ''
        if self.tag_name:
            result += '<%s%s%s>' % (self.tag_name,
                                    self._render_attr(),
                                    self._self_close() * ' /')
        if not self._self_close():
            isnl = True
            for c in self:
                if isinstance(c, Tag):
                    result += isnl * nl
                    isnl = False
                    result += c.render()
                else:
                    result += c
            if self.tag_name:
                result += '</%s>' % self.tag_name
        result += nl
        return result

    def _render_attr(self):
        result = ''
        for key, value in self.attributes.iteritems():
            if key != 'txt' and key != 'open':
                if key == 'cl':
                    key = 'class'
                result += ' %s="%s"' % (key, value)
        return result

    def _self_close(self):
        return self.tag_name in self_close

"""
def tag_factory(tag):
    class F(Tag):
        tag_name = tag

    F.__name__ = tag.title()
    return F


THIS = modules[__name__]

for t in tags:
    setattr(THIS, t.title(), tag_factory(t))
"""
THIS = modules[__name__]
for t in tags:
    obj = type(t.title(), (Tag, ), {'tag_name': t})
    setattr(THIS, t.title(), obj)


def _render_style(style):
    result = ''
    for item in style:
        result += item
        result += '\n{\n'
        values = style[item]
        for key, value in values.iteritems():
            result += "    %s: %s;\n" % (key, value)
        result += '}\n'
    if result:
        result = '\n' + result
    return result


class PyHtml(Tag):
    tag_name = 'html'

    def __init__(self, title=default_title):
        self._id = 'html'
        self += Head()
        self += Body()
        self.attributes = dict(xmlns='http://www.w3.org/1999/xhtml', lang='en')
        self.head += Title(title)

    def __iadd__(self, obj):
        if isinstance(obj, Head) or isinstance(obj, Body):
            self.add_obj(obj)
        elif isinstance(obj, Meta) or isinstance(obj, Link):
            self.head += obj
        else:
            self.body += obj
            _id = self.set_id(obj)
            setattr(self, _id, obj)
        return self

    def add_js(self, *arg):
        for f in arg:
            self.head += Script(type='text/javascript', src=f)

    def add_css(self, *arg):
        for f in arg:
            self.head += Link(rel='stylesheet', type='text/css', href=f)

    def output(self, name=''):
        if name:
            fil = open(name, 'w')
        else:
            fil = stdout
        fil.write(self.as_string())
        fil.flush()
        if name:
            fil.close()

    def as_string(self):
        return doc_type + self.render()

    def add_style(self, style):
        self.head += Style(_render_style(style))

    def add_table(self, data):
        table = self << Table()
        rows = len(data)
        cols = len(zip(*data))

        for i in range(rows):
            tr = table << Tr()
            tr << Th(data[i][0])
            for j in range(1, cols):
                tr << Td(data[i][j])