Merge "Change PTL informatin in INFO"
[bottlenecks.git] / testsuites / vstf / vstf_scripts / vstf / common / pyhtml.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10
11 from sys import stdout, modules
12
13 doc_type = '<!DOCTYPE HTML>\n'
14 default_title = "Html Page"
15 charset = '<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />\n'
16
17 html4_tags = {
18     'a',
19     'abbr',
20     'acronym',
21     'address',
22     'area',
23     'b',
24     'base',
25     'bdo',
26     'big',
27     'blockquote',
28     'body',
29     'br',
30     'button',
31     'caption',
32     'cite',
33     'code',
34     'col',
35     'colgroup',
36     'dd',
37     'del',
38     'div',
39     'dfn',
40     'dl',
41     'dt',
42     'em',
43     'fieldset',
44     'form',
45     'frame',
46     'frameset',
47     'h1',
48     'h2',
49     'h3',
50     'h4',
51     'h5',
52     'h6',
53     'head',
54     'hr',
55     'html',
56     'i',
57     'iframe',
58     'img',
59     'input',
60     'ins',
61     'kbd',
62     'label',
63     'legend',
64     'li',
65     'link',
66     'map',
67     'menu',
68     'menuitem',
69     'meta',
70     'noframes',
71     'noscript',
72     'object',
73     'ol',
74     'optgroup',
75     'option',
76     'p',
77     'param',
78     'pre',
79     'q',
80     'samp',
81     'script',
82     'select',
83     'small',
84     'span',
85     'strong',
86     'style',
87     'sub',
88     'sup',
89     'table',
90     'tbody',
91     'td',
92     'textarea',
93     'tfoot',
94     'th',
95     'thead',
96     'title',
97     'tr',
98     'tt',
99     'ul',
100     'var'}
101 disused_tags = {'isindex', 'font', 'dir', 's', 'strike',
102                 'u', 'center', 'basefont', 'applet', 'xmp'}
103 html5_tags = {
104     'article',
105     'aside',
106     'audio',
107     'bdi',
108     'canvas',
109     'command',
110     'datalist',
111     'details',
112     'dialog',
113     'embed',
114     'figcaption',
115     'figure',
116     'footer',
117     'header',
118     'keygen',
119     'mark',
120     'meter',
121     'nav',
122     'output',
123     'progress',
124     'rp',
125     'rt',
126     'ruby',
127     'section',
128     'source',
129     'summary',
130     'details',
131     'time',
132     'track',
133     'video',
134     'wbr'}
135
136 nl = '\n'
137 tags = html4_tags | disused_tags | html5_tags
138
139 __all__ = [x.title() for x in tags] + ['PyHtml', 'space']
140
141 self_close = {'input', 'img', 'link', 'br'}
142
143
144 def space(n):
145     return ' ' * n
146
147
148 class Tag(list):
149     tag_name = ''
150
151     def __init__(self, *args, **kwargs):
152         self.attributes = kwargs
153         if self.tag_name:
154             name = self.tag_name
155             self.is_seq = False
156         else:
157             name = 'sequence'
158             self.is_seq = True
159         self._id = kwargs.get('id', name)
160         for arg in args:
161             self.add_obj(arg)
162
163     def __iadd__(self, obj):
164         if isinstance(obj, Tag) and obj.is_seq:
165             for o in obj:
166                 self.add_obj(o)
167         else:
168             self.add_obj(obj)
169         return self
170
171     def add_obj(self, obj):
172         if not isinstance(obj, Tag):
173             obj = str(obj)
174         _id = self.set_id(obj)
175         setattr(self, _id, obj)
176         self.append(obj)
177
178     def set_id(self, obj):
179         if isinstance(obj, Tag):
180             _id = obj._id
181             obj_lst = filter(lambda t: isinstance(
182                 t, Tag) and t._id.startswith(_id), self)
183         else:
184             _id = 'content'
185             obj_lst = filter(lambda t: not isinstance(t, Tag), self)
186         length = len(obj_lst)
187         if obj_lst:
188             _id = '%s_%03i' % (_id, length)
189         if isinstance(obj, Tag):
190             obj._id = _id
191         return _id
192
193     def __add__(self, obj):
194         if self.tag_name:
195             return Tag(self, obj)
196         self.add_obj(obj)
197         return self
198
199     def __lshift__(self, obj):
200         if isinstance(obj, Tag):
201             self += obj
202             return obj
203         print "unknown obj: %s " % obj
204         return self
205
206     def render(self):
207         result = ''
208         if self.tag_name:
209             result += '<%s%s%s>' % (self.tag_name,
210                                     self._render_attr(),
211                                     self._self_close() * ' /')
212         if not self._self_close():
213             isnl = True
214             for c in self:
215                 if isinstance(c, Tag):
216                     result += isnl * nl
217                     isnl = False
218                     result += c.render()
219                 else:
220                     result += c
221             if self.tag_name:
222                 result += '</%s>' % self.tag_name
223         result += nl
224         return result
225
226     def _render_attr(self):
227         result = ''
228         for key, value in self.attributes.iteritems():
229             if key != 'txt' and key != 'open':
230                 if key == 'cl':
231                     key = 'class'
232                 result += ' %s="%s"' % (key, value)
233         return result
234
235     def _self_close(self):
236         return self.tag_name in self_close
237
238 """
239 def tag_factory(tag):
240     class F(Tag):
241         tag_name = tag
242
243     F.__name__ = tag.title()
244     return F
245
246
247 THIS = modules[__name__]
248
249 for t in tags:
250     setattr(THIS, t.title(), tag_factory(t))
251 """
252 THIS = modules[__name__]
253 for t in tags:
254     obj = type(t.title(), (Tag, ), {'tag_name': t})
255     setattr(THIS, t.title(), obj)
256
257
258 def _render_style(style):
259     result = ''
260     for item in style:
261         result += item
262         result += '\n{\n'
263         values = style[item]
264         for key, value in values.iteritems():
265             result += "    %s: %s;\n" % (key, value)
266         result += '}\n'
267     if result:
268         result = '\n' + result
269     return result
270
271
272 class PyHtml(Tag):
273     tag_name = 'html'
274
275     def __init__(self, title=default_title):
276         self._id = 'html'
277         self += Head()
278         self += Body()
279         self.attributes = dict(xmlns='http://www.w3.org/1999/xhtml', lang='en')
280         self.head += Title(title)
281
282     def __iadd__(self, obj):
283         if isinstance(obj, Head) or isinstance(obj, Body):
284             self.add_obj(obj)
285         elif isinstance(obj, Meta) or isinstance(obj, Link):
286             self.head += obj
287         else:
288             self.body += obj
289             _id = self.set_id(obj)
290             setattr(self, _id, obj)
291         return self
292
293     def add_js(self, *arg):
294         for f in arg:
295             self.head += Script(type='text/javascript', src=f)
296
297     def add_css(self, *arg):
298         for f in arg:
299             self.head += Link(rel='stylesheet', type='text/css', href=f)
300
301     def output(self, name=''):
302         if name:
303             fil = open(name, 'w')
304         else:
305             fil = stdout
306         fil.write(self.as_string())
307         fil.flush()
308         if name:
309             fil.close()
310
311     def as_string(self):
312         return doc_type + self.render()
313
314     def add_style(self, style):
315         self.head += Style(_render_style(style))
316
317     def add_table(self, data):
318         table = self << Table()
319         rows = len(data)
320         cols = len(zip(*data))
321
322         for i in range(rows):
323             tr = table << Tr()
324             tr << Th(data[i][0])
325             for j in range(1, cols):
326                 tr << Td(data[i][j])