Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / third_party / tvcm / tvcm / generate.py
1 #!/usr/bin/env python
2 # Copyright (c) 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 import base64
7 import optparse
8 import parse_deps
9 import sys
10 import os
11 import re
12
13 srcdir = os.path.abspath(os.path.join(os.path.dirname(__file__),
14                                       "..", "..", "..", "src"))
15
16 html_warning_message = """
17
18
19 <!--
20 WARNING: This file is auto generated.
21
22          Do not edit directly.
23 -->
24 """
25
26 js_warning_message = """
27 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
28 // Use of this source code is governed by a BSD-style license that can be
29 // found in the LICENSE file.
30
31 /* WARNING: This file is auto generated.
32  *
33  * Do not edit directly.
34  */
35 """
36
37 css_warning_message = """
38 /* Copyright (c) 2014 The Chromium Authors. All rights reserved.
39  * Use of this source code is governed by a BSD-style license that can be
40  * found in the LICENSE file. */
41
42 /* WARNING: This file is auto-generated.
43  *
44  * Do not edit directly.
45  */
46 """
47
48 def generate_css(load_sequence):
49   style_sheet_chunks = [css_warning_message, '\n']
50   for module in load_sequence:
51     for style_sheet in module.style_sheets:
52       style_sheet_chunks.append(style_sheet.contents_with_inlined_images)
53       style_sheet_chunks.append('\n')
54   return ''.join(style_sheet_chunks)
55
56 def generate_js(load_sequence, include_html_templates=True):
57   js_chunks = [js_warning_message, '\n']
58   js_chunks.append("window.FLATTENED = {};\n")
59   js_chunks.append("window.FLATTENED_RAW_SCRIPTS = {};\n")
60
61   for module in load_sequence:
62     for dependent_raw_scripts in module.dependent_raw_scripts:
63       js_chunks.append("window.FLATTENED_RAW_SCRIPTS['%s'] = true;\n" %
64         dependent_raw_script.resource.unix_style_relative_path)
65     js_chunks.append( "window.FLATTENED['%s'] = true;\n" % module.name)
66
67   if include_html_templates:
68     html_encoded = base64.b64encode(
69         generate_html_for_combined_templates(load_sequence))
70     js_chunks.append("var templateData_ = window.atob('" +
71                      html_encoded + "');\n");
72     js_chunks.append("var templateElem_ = document.createElement('div');\n");
73     js_chunks.append("templateElem_.innerHTML = templateData_;\n");
74     js_chunks.append("while (templateElem_.hasChildNodes()) {\n");
75     js_chunks.append("  document.head.appendChild(" +
76                      "templateElem_.removeChild(templateElem_.firstChild));\n");
77     js_chunks.append("}\n\n");
78
79   for module in load_sequence:
80     for dependent_raw_script in module.dependent_raw_scripts:
81       js_chunks.append(dependent_raw_script.contents)
82       js_chunks.append('\n')
83     js_chunks.append(module.contents)
84     js_chunks.append("\n")
85
86   return ''.join(js_chunks)
87
88 def generate_deps_js(load_sequence, project):
89   chunks = [js_warning_message, '\n']
90   loader = load_sequence[0].loader
91   for module in loader.loaded_modules.values():
92     chunks.append("tvcm.setResourceFileName('%s','%s');\n" % (
93         module.name, module.resource.relative_path))
94
95   for module in load_sequence:
96     for dependent_module in module.dependent_modules:
97       chunks.append("tvcm.addModuleDependency('%s','%s');\n" % (
98           module.name, dependent_module.name));
99
100     for dependent_raw_script in module.dependent_raw_scripts:
101       relative_path = dependent_raw_script.resource.relative_path
102       chunks.append(
103           "tvcm.addModuleRawScriptDependency('%s','%s');\n" % (
104            module.name, relative_path));
105
106     for style_sheet in module.style_sheets:
107       chunks.append("tvcm.addModuleStylesheet('%s','%s');\n" % (
108           module.name, style_sheet.name));
109   return "".join(chunks)
110
111 def generate_html_for_combined_templates(load_sequence):
112   chunks = []
113   for module in load_sequence:
114     for html_template in module.html_templates:
115       chunks.append(html_template.contents)
116   return "\n".join(chunks)
117
118 class ExtraScript(object):
119   def __init__(self, script_id, text_content, content_type=None):
120     if script_id != None:
121       assert script_id[0] != '#'
122     assert isinstance(text_content, basestring)
123     self.script_id = script_id
124     self.text_content = text_content
125     self.content_type = content_type
126
127 def generate_standalone_html_file(load_sequence,
128                                   title,
129                                   flattened_js_url=None,
130                                   extra_scripts=None):
131   extra_scripts = extra_scripts or []
132
133   head_html_chunks = []
134   head_html_chunks.append("<style>")
135   head_html_chunks.append(generate_css(load_sequence))
136   head_html_chunks.append("</style>")
137   head_html_chunks.append(generate_html_for_combined_templates(load_sequence))
138   if flattened_js_url:
139     head_html_chunks.append('<script src="%s"></script>' % flattened_js_url)
140   else:
141     head_html_chunks.append('<script>')
142     head_html_chunks.append(generate_js(load_sequence,
143                                         include_html_templates=False))
144     head_html_chunks.append('</script>')
145
146   for extra_script in extra_scripts:
147     attrs = []
148     if extra_script.script_id:
149       attrs.append('id="%s"' % extra_script.script_id)
150     if extra_script.content_type:
151       attrs.append('content-type="%s"' % extra_script.content_type)
152     if len(attrs) > 0:
153       head_html_chunks.append('<script %s>' % ' '.join(attrs))
154     else:
155       head_html_chunks.append('<script>')
156     head_html_chunks.append(extra_script.text_content)
157     head_html_chunks.append('</script>')
158
159   return """
160 <!DOCTYPE HTML>
161 <html>
162   %s
163   <head i18n-values="dir:textdirection;">
164   <title>%s</title>
165   %s
166 </head>
167 <body>
168 </body>
169 </html>
170 """ % (html_warning_message,
171        title,
172        '\n'.join(head_html_chunks))