Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: frog/pad/frogpad.py

Issue 9392005: initial frogpad (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: updated comment Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« frog/pad/frogpad.dart ('K') | « frog/pad/frogpad.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/python
Bob Nystrom 2012/02/13 18:10:18 What made you decide to write this in Python inste
mattsh 2012/02/13 20:57:36 See comment below.
2
3 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
4 # for details. All rights reserved. Use of this source code is governed by a
5 # BSD-style license that can be found in the LICENSE file.
6 #
7
8 """
9 Generates an html file (frogpad.html) that can be used to execute the frog
10 compiler in a web browser or DumpRenderTree,
11
12 The generated frogpad.html will contain:
13
14 1. all the dart files that compose a dart program
15 2. all the dart files of dart:core and other standard dart libraries
16 3. frogpad.dart (compiled to javascript)
17
18 The contents of each dart file is placed in a separate <script> tag.
19
20 When the html page is loaded by a browser, the frog compiler will be invoked
21 and the user's dart program will be compiled to javascript. The generated
22 javascript will be placed in the <pre> element with id "output".
23
24 If using DumpRenderTree, the output javascript can be obtained by dumping
25 the page as text and looking for the contents of the output textarea.
26 """
27
28 import cgi
29 import optparse
30 import os.path
31 import re
32 import sys
33
34 # Template for the html page we're going to generate.
35 HTML = """<html>
36 <head>
37 <style type="text/css">
38 textarea {
39 width: 100%;
40 height: 200px;
41 }
42 .label {
43 margin-top: 5px;
44 }
45 </style>
46 {{script_tags}}
47 <script type="text/javascript">
48 if (window.layoutTestController) {
49 layoutTestController.dumpAsText();
50 }
51 </script>
52 </head>
53 <body>
54 <h1>Frogpad</h1>
55 <div class="label">Input:</div>
56 <textarea id="input"></textarea>
57 <div class="label">Messages:</div>
58 <textarea id="warnings"></textarea>
59 <div class="label">Output:</div>
60 <pre id="output"></pre>
61 <script type="text/javascript" src="frogpad.dart.js" ></script>
62 </body>
63 </html>
64 """
65
66 # We use "application/inert" here to make the browser ignore the
67 # these script tags. (frogpad.dart will fish out the contents as needed.)
68 #
69 SCRIPT_TAG = """<script type="application/inert" id="{{id}}">
70 {{contents}}
71 </script>
72 """
73
74 # Regex that finds #import, #source and #native directives in .dart files.
75 # match.group(1) = "import", "source" or "native"
76 # match.group(2) = url of file being imported
77 DIRECTIVE_RE = re.compile(r"^#(import|source|native)\([\"']([^\"']*)[\"']")
78
79 # id of script tag that holds name of the top dart file to be compiled,
80 # (This file name passed will be passed to the frog compiler by frogpad.dart.)
81 MAIN_ID = "main_id"
82
83 DART_LIBRARIES = {
Jennifer Messerly 2012/02/13 19:12:12 This worries me, since it will need to interact wi
mattsh 2012/02/13 20:57:36 I want to keep dependencies to a minimum, so I'd p
84 "core": "lib/corelib.dart",
85 "coreimpl": "lib/corelib_impl.dart",
86 "html": "../client/html/release/html.dart",
87 "htmlimpl": "../client/html/release/htmlimpl.dart",
88 "dom": "../client/dom/frog/dom_frog.dart",
89 "json": "lib/json_frog.dart"
90 }
91
92 class Pad(object):
93 """
94 Accumulates all source files that are needed to compile a dart program,
95 and places them in <script> tags on an html page.
96 """
97
98 def __init__(self, frog_dir, main_file):
99 # directory of frog compiler source code
100 self.frog_dir = frog_dir
101
102 # which .dart file to compile
103 self.main_file = main_file
104
105 # map from file name to File object (contains entries for all corelib
106 # and all other dart files needed to compile main_file)
107 self.name_to_file = {}
108
109 # map from script tag id to File object
110 self.id_to_file = {}
111
112 self.LoadBuiltins()
113 self.LoadFile(self.main_file)
114
115 def GenerateHtml(self):
Emily Fortuna 2012/02/13 18:27:58 Google Python style says to have lower case method
Jennifer Messerly 2012/02/13 19:12:12 +1. Here is citation: http://google-styleguide.go
mattsh 2012/02/13 20:57:36 Done.
116 tags = []
117 for f in self.id_to_file.values():
118 tags.append(self.CreateTag(f.id, f.contents))
119 tags.append(self.CreateTag(MAIN_ID, self.main_file))
120 html = HTML.replace("{{script_tags}}", "".join(tags))
121 return html
122
123 @staticmethod
124 def CreateTag(id, contents):
125 s = SCRIPT_TAG
126 s = s.replace("{{id}}", id)
127 s = s.replace("{{contents}}", contents)
128 return s
129
130 def Builtin(self, name):
131 path = DART_LIBRARIES[name]
132 if not path:
133 raise Exception("unrecognized 'dart:%s'", name)
134 return os.path.join(self.frog_dir, path)
135
136 def LoadBuiltins(self):
Jennifer Messerly 2012/02/13 19:12:12 These could be resolved on demand when dart: is se
mattsh 2012/02/13 20:57:36 Yes, good suggestion. Done.
137 for name in DART_LIBRARIES:
138 self.LoadFile(self.Builtin(name))
139
140 def LoadFile(self, name):
141 name = os.path.abspath(name)
142 if name in self.name_to_file:
143 print "already loaded %s, skipping" % name
144 return
145 f = File(self, name)
146 self.name_to_file[f.name] = f
147 if f.id in self.id_to_file:
148 raise Exception("ambiguous id '%s'" % f.id)
149 self.id_to_file[f.id] = f
150 f.Directives()
151
152 class File(object):
153 def __init__(self, pad, name):
154 self.pad = pad
155 self.name = name
156 self.id = self.MakeId()
157 if not os.path.exists(name):
158 raise Exception("cannot find file '%s'" % name)
159 with open(self.name, "r") as f:
160 self.contents = f.read()
161 print "creating File '%s' (%d lines)" % (self.name, len(self.contents))
162
163 def MakeId(self):
164 """
165 Generates an id (based on the file name) for the <script> tag that will
166 hold the contents of this file.
167 """
168 (dirname, name) = os.path.split(self.name)
169 dirname = os.path.basename(dirname)
170 name = name.replace(".", "_")
171 return dirname + "_" + name
172
173 def Directives(self):
174 """Load files referenced by #source, #import and #native directives."""
175 lines = self.contents.split("\n")
176 self.line_number = 0
177 for line in lines:
178 self.line_number += 1
179 self.Directive(line)
180
181 def Directive(self, line):
182 match = DIRECTIVE_RE.match(line)
183 if not match:
184 return
185 url = match.group(2)
186 if url.startswith("dart:"):
187 path = self.pad.Builtin(url[len("dart:"):])
188 else:
189 path = os.path.join(os.path.dirname(self.name), url)
190 self.pad.LoadFile(path)
191
192 def main(argv):
193 parser = optparse.OptionParser()
194 parser.add_option("-o", "--out", dest="out_file")
195
196 (options, args) = parser.parse_args(argv)
197 main_file = os.path.abspath(args[1])
198
199 script_dir = os.path.abspath(os.path.dirname(argv[0]))
200 frog_dir = os.path.abspath(os.path.join(script_dir, os.pardir))
201
202 pad = Pad(frog_dir, main_file)
203 html = pad.GenerateHtml()
204
205 filename = "frogpad.html"
206 with open(filename, "w") as output:
207 output.write(html)
208 print "generated '%s' (%d bytes)" % (filename, len(html))
Jennifer Messerly 2012/02/13 19:12:12 I'd remove prints, unless we think they'll be usef
mattsh 2012/02/13 20:57:36 Yes, will do after a bit more testing is completed
209
210 if __name__ == "__main__":
211 sys.exit(main(sys.argv))
212
OLDNEW
« frog/pad/frogpad.dart ('K') | « frog/pad/frogpad.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698