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

Side by Side Diff: lib/html/scripts/templateloader.py

Issue 11187063: Generate contains() method for list mixin only if not already defined. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 2 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
« no previous file with comments | « lib/html/scripts/systemnative.py ('k') | lib/html/src/_Collections.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 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 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. 4 # BSD-style license that can be found in the LICENSE file.
5 5
6 # Template loader and preprocessor. 6 # Template loader and preprocessor.
7 # 7 #
8 # Preprocessor language: 8 # Preprocessor language:
9 # 9 #
10 # //$ Comment line removed by preprocessor 10 # //$ Comment line removed by preprocessor
(...skipping 15 matching lines...) Expand all
26 root - a string, the directory under which the templates are stored. 26 root - a string, the directory under which the templates are stored.
27 subpaths - a list of strings, subpaths of root in search order. 27 subpaths - a list of strings, subpaths of root in search order.
28 conditions - a dictionay from strings to booleans. Any conditional 28 conditions - a dictionay from strings to booleans. Any conditional
29 expression must be a key in the map. 29 expression must be a key in the map.
30 """ 30 """
31 self._root = root 31 self._root = root
32 self._subpaths = subpaths 32 self._subpaths = subpaths
33 self._conditions = conditions 33 self._conditions = conditions
34 self._cache = {} 34 self._cache = {}
35 35
36 def TryLoad(self, name): 36 def TryLoad(self, name, more_conditions={}):
37 """Returns content of template file as a string, or None of not found.""" 37 """Returns content of template file as a string, or None of not found."""
38 if name in self._cache: 38 conditions = dict(self._conditions, **more_conditions)
39 return self._cache[name] 39 cache_key = (name, tuple(sorted(conditions.items())))
40 if cache_key in self._cache:
41 return self._cache[cache_key]
40 42
41 for subpath in self._subpaths: 43 for subpath in self._subpaths:
42 template_file = os.path.join(self._root, subpath, name) 44 template_file = os.path.join(self._root, subpath, name)
43 if os.path.exists(template_file): 45 if os.path.exists(template_file):
44 template = ''.join(open(template_file).readlines()) 46 template = ''.join(open(template_file).readlines())
45 template = self._Preprocess(template, template_file) 47 template = self._Preprocess(template, template_file, conditions)
46 self._cache[name] = template 48 self._cache[cache_key] = template
47 return template 49 return template
48 50
49 return None 51 return None
50 52
51 def Load(self, name): 53 def Load(self, name, more_conditions={}):
52 """Returns contents of template file as a string, or raises an exception.""" 54 """Returns contents of template file as a string, or raises an exception."""
53 template = self.TryLoad(name) 55 template = self.TryLoad(name, more_conditions)
54 if template is not None: # Can be empty string 56 if template is not None: # Can be empty string
55 return template 57 return template
56 raise Exception("Could not find template '%s' on %s / %s" % ( 58 raise Exception("Could not find template '%s' on %s / %s" % (
57 name, self._root, self._subpaths)) 59 name, self._root, self._subpaths))
58 60
59 def _Preprocess(self, template, filename): 61 def _Preprocess(self, template, filename, conditions):
60 def error(lineno, message): 62 def error(lineno, message):
61 raise Exception('%s:%s: %s' % (filename, lineno, message)) 63 raise Exception('%s:%s: %s' % (filename, lineno, message))
62 64
63 lines = template.splitlines(True) 65 lines = template.splitlines(True)
64 out = [] 66 out = []
65 67
66 condition_stack = [] 68 condition_stack = []
67 active = True 69 active = True
68 seen_else = False 70 seen_else = False
69 71
70 for (lineno, full_line) in enumerate(lines): 72 for (lineno, full_line) in enumerate(lines):
71 line = full_line.strip() 73 line = full_line.strip()
72 74
73 if line.startswith('$'): 75 if line.startswith('$'):
74 words = line.split() 76 words = line.split()
75 directive = words[0] 77 directive = words[0]
76 78
77 if directive == '$if': 79 if directive == '$if':
78 if len(words) != 2: 80 if len(words) != 2:
79 error(lineno, '$if does not have single variable') 81 error(lineno, '$if does not have single variable')
80 variable = words[1] 82 variable = words[1]
81 if variable in self._conditions: 83 if variable in conditions:
82 condition_stack.append((active, seen_else)) 84 condition_stack.append((active, seen_else))
83 active = self._conditions[variable] 85 active = conditions[variable]
84 seen_else = False 86 seen_else = False
85 else: 87 else:
86 error(lineno, "Unknown $if variable '%s'" % variable) 88 error(lineno, "Unknown $if variable '%s'" % variable)
87 89
88 elif directive == '$else': 90 elif directive == '$else':
89 if not condition_stack: 91 if not condition_stack:
90 error(lineno, '$else without $if') 92 error(lineno, '$else without $if')
91 if seen_else: 93 if seen_else:
92 raise error(lineno, 'Double $else') 94 raise error(lineno, 'Double $else')
93 seen_else = True 95 seen_else = True
(...skipping 13 matching lines...) Expand all
107 109
108 else: 110 else:
109 if active: 111 if active:
110 out.append(full_line) 112 out.append(full_line)
111 continue 113 continue
112 114
113 if condition_stack: 115 if condition_stack:
114 error(len(lines), 'Unterminated $if') 116 error(len(lines), 'Unterminated $if')
115 117
116 return ''.join(out) 118 return ''.join(out)
OLDNEW
« no previous file with comments | « lib/html/scripts/systemnative.py ('k') | lib/html/src/_Collections.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698