| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/python | |
| 2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
| 3 # for details. All rights reserved. Use of this source code is governed by a | |
| 4 # BSD-style license that can be found in the LICENSE file. | |
| 5 | |
| 6 import logging.config | |
| 7 import os | |
| 8 import sys | |
| 9 import re | |
| 10 | |
| 11 _logger = logging.getLogger('snippet_manager') | |
| 12 | |
| 13 # Regular expression to find method signatures in dart snippet files. | |
| 14 # (find unindented lines that end with a {) | |
| 15 METHOD_SIGNATURE_RE = re.compile(r'(\S[^{]*){') | |
| 16 | |
| 17 class SnippetManager(object): | |
| 18 """The SnippetManager loads all the files in the snippets directory | |
| 19 and searches for method signatures. These will be inserted | |
| 20 into the Dart interfaces that dartdomgenerator.py produces. | |
| 21 """ | |
| 22 def __init__(self, root_dir): | |
| 23 self._root_dir = root_dir | |
| 24 | |
| 25 # map from interface name to snippet text | |
| 26 self.snippet_map = {} | |
| 27 self._load() | |
| 28 | |
| 29 def _load(self): | |
| 30 res = [] | |
| 31 def visitor(arg, dirname, names): | |
| 32 for name in list(names): | |
| 33 if name == ".svn": | |
| 34 names.remove(name); | |
| 35 continue; | |
| 36 path = os.path.join(dirname, name) | |
| 37 if os.path.isdir(path): | |
| 38 continue | |
| 39 self._load_file(path) | |
| 40 os.path.walk(self._root_dir, visitor, None) | |
| 41 | |
| 42 def _load_file(self, path): | |
| 43 match = re.compile(r'.*/(.*?)(Impl)?.dart.snippet').match(path) | |
| 44 if match is None: | |
| 45 raise RuntimeError('bad snippet filename "%s"' % (path)) | |
| 46 interface_name = match.group(1) | |
| 47 is_impl = match.group(2) | |
| 48 _logger.info("processing snippet file %s" % path) | |
| 49 f = open(path, 'r') | |
| 50 | |
| 51 if not self.snippet_map.has_key(interface_name): | |
| 52 self.snippet_map[interface_name] = '' | |
| 53 if is_impl: | |
| 54 method_signatures = [] | |
| 55 for line in f.readlines(): | |
| 56 match = METHOD_SIGNATURE_RE.match(line) | |
| 57 if match: | |
| 58 method_signatures.append(match.group(1).strip() + ";") | |
| 59 if len(method_signatures) > 0: | |
| 60 self.snippet_map[interface_name] += "\n".join(method_signatures) | |
| 61 else: | |
| 62 self.snippet_map[interface_name] += f.read() | |
| 63 | |
| 64 def main(): | |
| 65 """Used for debugging to dump all the snippets. | |
| 66 """ | |
| 67 current_dir = os.path.dirname(__file__) | |
| 68 logging.config.fileConfig(os.path.join(current_dir, "logging.conf")) | |
| 69 snippet_dir = os.path.join(current_dir, '..', 'snippets') | |
| 70 snippet_manager = SnippetManager(snippet_dir) | |
| 71 for interface_name, snippet in snippet_manager.snippet_map.items(): | |
| 72 print '---' | |
| 73 print '%s:' % interface_name | |
| 74 print snippet | |
| 75 | |
| 76 if __name__ == "__main__": | |
| 77 sys.exit(main()) | |
| OLD | NEW |