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

Unified 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 side-by-side diff with in-line comments
Download patch
« frog/pad/frogpad.dart ('K') | « frog/pad/frogpad.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: frog/pad/frogpad.py
diff --git a/frog/pad/frogpad.py b/frog/pad/frogpad.py
new file mode 100755
index 0000000000000000000000000000000000000000..c03b4b3d1f51a1f2e74bbc6e92812273df5969e3
--- /dev/null
+++ b/frog/pad/frogpad.py
@@ -0,0 +1,212 @@
+#!/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.
+
+# Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+# for details. All rights reserved. Use of this source code is governed by a
+# BSD-style license that can be found in the LICENSE file.
+#
+
+"""
+Generates an html file (frogpad.html) that can be used to execute the frog
+compiler in a web browser or DumpRenderTree,
+
+The generated frogpad.html will contain:
+
+ 1. all the dart files that compose a dart program
+ 2. all the dart files of dart:core and other standard dart libraries
+ 3. frogpad.dart (compiled to javascript)
+
+The contents of each dart file is placed in a separate <script> tag.
+
+When the html page is loaded by a browser, the frog compiler will be invoked
+and the user's dart program will be compiled to javascript. The generated
+javascript will be placed in the <pre> element with id "output".
+
+If using DumpRenderTree, the output javascript can be obtained by dumping
+the page as text and looking for the contents of the output textarea.
+"""
+
+import cgi
+import optparse
+import os.path
+import re
+import sys
+
+# Template for the html page we're going to generate.
+HTML = """<html>
+<head>
+ <style type="text/css">
+ textarea {
+ width: 100%;
+ height: 200px;
+ }
+ .label {
+ margin-top: 5px;
+ }
+ </style>
+ {{script_tags}}
+ <script type="text/javascript">
+ if (window.layoutTestController) {
+ layoutTestController.dumpAsText();
+ }
+ </script>
+</head>
+<body>
+ <h1>Frogpad</h1>
+ <div class="label">Input:</div>
+ <textarea id="input"></textarea>
+ <div class="label">Messages:</div>
+ <textarea id="warnings"></textarea>
+ <div class="label">Output:</div>
+ <pre id="output"></pre>
+ <script type="text/javascript" src="frogpad.dart.js" ></script>
+</body>
+</html>
+"""
+
+# We use "application/inert" here to make the browser ignore the
+# these script tags. (frogpad.dart will fish out the contents as needed.)
+#
+SCRIPT_TAG = """<script type="application/inert" id="{{id}}">
+{{contents}}
+</script>
+"""
+
+# Regex that finds #import, #source and #native directives in .dart files.
+# match.group(1) = "import", "source" or "native"
+# match.group(2) = url of file being imported
+DIRECTIVE_RE = re.compile(r"^#(import|source|native)\([\"']([^\"']*)[\"']")
+
+# id of script tag that holds name of the top dart file to be compiled,
+# (This file name passed will be passed to the frog compiler by frogpad.dart.)
+MAIN_ID = "main_id"
+
+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
+ "core": "lib/corelib.dart",
+ "coreimpl": "lib/corelib_impl.dart",
+ "html": "../client/html/release/html.dart",
+ "htmlimpl": "../client/html/release/htmlimpl.dart",
+ "dom": "../client/dom/frog/dom_frog.dart",
+ "json": "lib/json_frog.dart"
+}
+
+class Pad(object):
+ """
+ Accumulates all source files that are needed to compile a dart program,
+ and places them in <script> tags on an html page.
+ """
+
+ def __init__(self, frog_dir, main_file):
+ # directory of frog compiler source code
+ self.frog_dir = frog_dir
+
+ # which .dart file to compile
+ self.main_file = main_file
+
+ # map from file name to File object (contains entries for all corelib
+ # and all other dart files needed to compile main_file)
+ self.name_to_file = {}
+
+ # map from script tag id to File object
+ self.id_to_file = {}
+
+ self.LoadBuiltins()
+ self.LoadFile(self.main_file)
+
+ 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.
+ tags = []
+ for f in self.id_to_file.values():
+ tags.append(self.CreateTag(f.id, f.contents))
+ tags.append(self.CreateTag(MAIN_ID, self.main_file))
+ html = HTML.replace("{{script_tags}}", "".join(tags))
+ return html
+
+ @staticmethod
+ def CreateTag(id, contents):
+ s = SCRIPT_TAG
+ s = s.replace("{{id}}", id)
+ s = s.replace("{{contents}}", contents)
+ return s
+
+ def Builtin(self, name):
+ path = DART_LIBRARIES[name]
+ if not path:
+ raise Exception("unrecognized 'dart:%s'", name)
+ return os.path.join(self.frog_dir, path)
+
+ 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.
+ for name in DART_LIBRARIES:
+ self.LoadFile(self.Builtin(name))
+
+ def LoadFile(self, name):
+ name = os.path.abspath(name)
+ if name in self.name_to_file:
+ print "already loaded %s, skipping" % name
+ return
+ f = File(self, name)
+ self.name_to_file[f.name] = f
+ if f.id in self.id_to_file:
+ raise Exception("ambiguous id '%s'" % f.id)
+ self.id_to_file[f.id] = f
+ f.Directives()
+
+class File(object):
+ def __init__(self, pad, name):
+ self.pad = pad
+ self.name = name
+ self.id = self.MakeId()
+ if not os.path.exists(name):
+ raise Exception("cannot find file '%s'" % name)
+ with open(self.name, "r") as f:
+ self.contents = f.read()
+ print "creating File '%s' (%d lines)" % (self.name, len(self.contents))
+
+ def MakeId(self):
+ """
+ Generates an id (based on the file name) for the <script> tag that will
+ hold the contents of this file.
+ """
+ (dirname, name) = os.path.split(self.name)
+ dirname = os.path.basename(dirname)
+ name = name.replace(".", "_")
+ return dirname + "_" + name
+
+ def Directives(self):
+ """Load files referenced by #source, #import and #native directives."""
+ lines = self.contents.split("\n")
+ self.line_number = 0
+ for line in lines:
+ self.line_number += 1
+ self.Directive(line)
+
+ def Directive(self, line):
+ match = DIRECTIVE_RE.match(line)
+ if not match:
+ return
+ url = match.group(2)
+ if url.startswith("dart:"):
+ path = self.pad.Builtin(url[len("dart:"):])
+ else:
+ path = os.path.join(os.path.dirname(self.name), url)
+ self.pad.LoadFile(path)
+
+def main(argv):
+ parser = optparse.OptionParser()
+ parser.add_option("-o", "--out", dest="out_file")
+
+ (options, args) = parser.parse_args(argv)
+ main_file = os.path.abspath(args[1])
+
+ script_dir = os.path.abspath(os.path.dirname(argv[0]))
+ frog_dir = os.path.abspath(os.path.join(script_dir, os.pardir))
+
+ pad = Pad(frog_dir, main_file)
+ html = pad.GenerateHtml()
+
+ filename = "frogpad.html"
+ with open(filename, "w") as output:
+ output.write(html)
+ 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
+
+if __name__ == "__main__":
+ sys.exit(main(sys.argv))
+
« 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