Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / third_party / tvcm / tvcm / generate.py
1 # Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import base64
6 import optparse
7 import sys
8 import os
9 import re
10 import StringIO
11
12 from tvcm import js_utils
13 from tvcm import module as module_module
14 from tvcm import html_generation_controller
15
16
17 srcdir = os.path.abspath(os.path.join(os.path.dirname(__file__),
18                                       "..", "..", "..", "src"))
19
20 html_warning_message = """
21
22
23 <!--
24 WARNING: This file is auto generated.
25
26          Do not edit directly.
27 -->
28 """
29
30 js_warning_message = """
31 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
32 // Use of this source code is governed by a BSD-style license that can be
33 // found in the LICENSE file.
34
35 /* WARNING: This file is auto generated.
36  *
37  * Do not edit directly.
38  */
39 """
40
41 css_warning_message = """
42 /* Copyright (c) 2014 The Chromium Authors. All rights reserved.
43  * Use of this source code is governed by a BSD-style license that can be
44  * found in the LICENSE file. */
45
46 /* WARNING: This file is auto-generated.
47  *
48  * Do not edit directly.
49  */
50 """
51
52 def GenerateJS(load_sequence,
53                use_include_tags_for_scripts=False,
54                dir_for_include_tag_root=None):
55   f = StringIO.StringIO()
56   GenerateJSToFile(f,
57                    load_sequence,
58                    use_include_tags_for_scripts,
59                    dir_for_include_tag_root)
60   return f.getvalue()
61
62 def GenerateJSToFile(f,
63                      load_sequence,
64                      use_include_tags_for_scripts=False,
65                      dir_for_include_tag_root=None):
66   if use_include_tags_for_scripts and dir_for_include_tag_root == None:
67     raise Exception('Must provide dir_for_include_tag_root')
68
69   f.write(js_warning_message)
70   f.write('\n')
71
72   loader = load_sequence[0].loader
73
74   polymer_script = loader.LoadRawScript('polymer.js')
75   f.write(polymer_script.contents)
76
77   f.write('\n')
78   f.write("window._TVCM_IS_COMPILED = true;\n")
79
80   for module in load_sequence:
81     module.AppendJSContentsToFile(f,
82                                   use_include_tags_for_scripts,
83                                   dir_for_include_tag_root)
84
85 class ExtraScript(object):
86   def __init__(self, script_id=None, text_content=None, content_type=None):
87     if script_id != None:
88       assert script_id[0] != '#'
89     self.script_id = script_id
90     self.text_content = text_content
91     self.content_type = content_type
92
93   def WriteToFile(self, output_file):
94     attrs = []
95     if self.script_id:
96       attrs.append('id="%s"' % self.script_id)
97     if self.content_type:
98       attrs.append('content-type="%s"' % self.content_type)
99
100     if len(attrs) > 0:
101       output_file.write('<script %s>\n' % ' '.join(attrs))
102     else:
103       output_file.write('<script>\n')
104     if self.text_content:
105       output_file.write(self.text_content)
106     output_file.write('</script>\n')
107
108
109 def GenerateStandaloneHTMLAsString(*args, **kwargs):
110   f = StringIO.StringIO()
111   GenerateStandaloneHTMLToFile(f, *args, **kwargs)
112   return f.getvalue()
113
114 def GenerateStandaloneHTMLToFile(output_file,
115                                  load_sequence,
116                                  title,
117                                  flattened_js_url=None,
118                                  extra_scripts=None):
119   extra_scripts = extra_scripts or []
120
121   output_file.write("""<!DOCTYPE HTML>
122 <html>
123   <head i18n-values="dir:textdirection;">
124   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
125   <title>%s</title>
126 """ % title)
127
128   loader = load_sequence[0].loader
129
130   written_style_sheets = set()
131
132   class HTMLGenerationController(html_generation_controller.HTMLGenerationController):
133     def __init__(self, module):
134       self.module = module
135     def GetHTMLForStylesheetHRef(self, href):
136       resource = self.module.HRefToResource(
137           href, '<link rel="stylesheet" href="%s">' % href)
138       style_sheet = loader.LoadStyleSheet(resource.name)
139
140       if style_sheet in written_style_sheets:
141         return None
142       written_style_sheets.add(style_sheet)
143
144       return "<style>\n%s\n</style>" % style_sheet.contents_with_inlined_images
145
146   for module in load_sequence:
147     ctl = HTMLGenerationController(module)
148     module.AppendHTMLContentsToFile(output_file, ctl)
149
150   if flattened_js_url:
151     output_file.write('<script src="%s"></script>\n' % flattened_js_url)
152   else:
153     output_file.write('<script>\n')
154     output_file.write(GenerateJS(load_sequence))
155     output_file.write('</script>\n')
156
157   for extra_script in extra_scripts:
158     extra_script.WriteToFile(output_file)
159
160   output_file.write("""</head>
161 <body>
162 """)
163
164   output_file.write("""</body>
165 </html>
166 """)