OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 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 argparse |
| 7 import sys |
| 8 import re |
| 9 |
| 10 IMPORT_TEMPLATE = '<link rel="import" href="/%s.html" as="%s" />' |
| 11 PREAMBLE_TEMPLATE = '<script>' |
| 12 POSTAMBLE_TEMPLATE = ' this.exports = exports;\n</script>' |
| 13 |
| 14 class Import(object): |
| 15 def __init__(self, path, name): |
| 16 self.path = path |
| 17 self.name = name |
| 18 |
| 19 class Module(object): |
| 20 def __init__(self): |
| 21 self.name = "" |
| 22 self.imports = [] |
| 23 self.body = "" |
| 24 |
| 25 def Serialize(module): |
| 26 lines = [] |
| 27 for i in module.imports: |
| 28 lines.append(IMPORT_TEMPLATE % (i.path, i.name)) |
| 29 lines.append(PREAMBLE_TEMPLATE) |
| 30 lines.append(module.body) |
| 31 lines.append(POSTAMBLE_TEMPLATE) |
| 32 return "\n".join(lines) |
| 33 |
| 34 name_regex = re.compile(r'define\("([^"]+)"') |
| 35 import_regex = re.compile(r' +"([^"]+)",') |
| 36 begin_body_regexp = re.compile(r', function\(([^)]*)\)') |
| 37 end_body_regexp = re.compile(r'return exports') |
| 38 |
| 39 def AddImportNames(module, unparsed_names): |
| 40 names = [n.strip() for n in unparsed_names.split(',')] |
| 41 for i in range(len(module.imports)): |
| 42 module.imports[i].name = names[i] |
| 43 |
| 44 def RewritePathNames(path): |
| 45 return path.replace("mojo/public/js/bindings", "mojo/public/html") |
| 46 |
| 47 def Parse(amd_module): |
| 48 module = Module() |
| 49 body_lines = [] |
| 50 state = "name" |
| 51 for line in amd_module.splitlines(): |
| 52 if state == "name": |
| 53 m = name_regex.search(line) |
| 54 if m: |
| 55 module.name = m.group(1) |
| 56 m = begin_body_regexp.search(line) |
| 57 if m: |
| 58 AddImportNames(module, m.group(1)) |
| 59 state = "body" |
| 60 else: |
| 61 state = "imports" |
| 62 continue |
| 63 if state == "imports": |
| 64 m = import_regex.search(line) |
| 65 if m: |
| 66 module.imports.append(Import(RewritePathNames(m.group(1)), None)) |
| 67 continue |
| 68 m = begin_body_regexp.search(line) |
| 69 if m: |
| 70 AddImportNames(module, m.group(1)) |
| 71 state = "body" |
| 72 continue |
| 73 raise Exception, "Unknown import declaration" |
| 74 if state == "body": |
| 75 if end_body_regexp.search(line): |
| 76 module.body = "\n".join(body_lines) |
| 77 return module |
| 78 body_lines.append(line) |
| 79 continue |
| 80 raise Exception, "Unknown parser state" |
| 81 raise Exception, "End of file reached with finding a module" |
| 82 |
| 83 def main(): |
| 84 parser = argparse.ArgumentParser() |
| 85 parser.add_argument("--input") |
| 86 parser.add_argument("--output") |
| 87 args = parser.parse_args() |
| 88 |
| 89 module = None |
| 90 with open(args.input, "r") as input_file: |
| 91 module = Parse(input_file.read()) |
| 92 |
| 93 with open(args.output, "w+") as output_file: |
| 94 output_file.write(Serialize(module)) |
| 95 |
| 96 return 0 |
| 97 |
| 98 if __name__ == "__main__": |
| 99 sys.exit(main()) |
OLD | NEW |