Show More
@@ -66,19 +66,18 b' class engine(object):' | |||||
66 | self._defaults = defaults |
|
66 | self._defaults = defaults | |
67 | self._cache = {} |
|
67 | self._cache = {} | |
68 |
|
68 | |||
|
69 | def process(self, t, mapping): | |||
|
70 | '''Perform expansion. t is name of map element to expand. | |||
|
71 | mapping contains added elements for use during expansion. Is a | |||
|
72 | generator.''' | |||
|
73 | return _flatten(self._process(self._load(t), mapping)) | |||
|
74 | ||||
69 | def _load(self, t): |
|
75 | def _load(self, t): | |
70 | '''load, parse, and cache a template''' |
|
76 | '''load, parse, and cache a template''' | |
71 | if t not in self._cache: |
|
77 | if t not in self._cache: | |
72 | self._cache[t] = self._parse(self._loader(t)) |
|
78 | self._cache[t] = self._parse(self._loader(t)) | |
73 | return self._cache[t] |
|
79 | return self._cache[t] | |
74 |
|
80 | |||
75 | def process(self, t, mapping): |
|
|||
76 | '''Perform expansion. t is name of map element to expand. |
|
|||
77 | mapping contains added elements for use during expansion. Is a |
|
|||
78 | generator.''' |
|
|||
79 |
|
||||
80 | return _flatten(self._process(self._load(t), mapping)) |
|
|||
81 |
|
||||
82 | def _get(self, mapping, key): |
|
81 | def _get(self, mapping, key): | |
83 | v = mapping.get(key) |
|
82 | v = mapping.get(key) | |
84 | if v is None: |
|
83 | if v is None: | |
@@ -87,9 +86,6 b' class engine(object):' | |||||
87 | v = v(**mapping) |
|
86 | v = v(**mapping) | |
88 | return v |
|
87 | return v | |
89 |
|
88 | |||
90 | def _raw(self, mapping, x): |
|
|||
91 | return x |
|
|||
92 |
|
||||
93 | def _filter(self, mapping, parts): |
|
89 | def _filter(self, mapping, parts): | |
94 | filters, val = parts |
|
90 | filters, val = parts | |
95 | x = self._get(mapping, val) |
|
91 | x = self._get(mapping, val) | |
@@ -116,27 +112,26 b' class engine(object):' | |||||
116 |
|
112 | |||
117 | def _parse(self, tmpl): |
|
113 | def _parse(self, tmpl): | |
118 | '''preparse a template''' |
|
114 | '''preparse a template''' | |
119 |
|
||||
120 | parsed = [] |
|
115 | parsed = [] | |
121 | pos, stop = 0, len(tmpl) |
|
116 | pos, stop = 0, len(tmpl) | |
122 | while pos < stop: |
|
117 | while pos < stop: | |
123 | n = tmpl.find('{', pos) |
|
118 | n = tmpl.find('{', pos) | |
124 | if n < 0: |
|
119 | if n < 0: | |
125 |
parsed.append(( |
|
120 | parsed.append((None, tmpl[pos:stop])) | |
126 | break |
|
121 | break | |
127 | if n > 0 and tmpl[n - 1] == '\\': |
|
122 | if n > 0 and tmpl[n - 1] == '\\': | |
128 | # escaped |
|
123 | # escaped | |
129 |
parsed.append(( |
|
124 | parsed.append((None, tmpl[pos:n + 1])) | |
130 | pos = n + 1 |
|
125 | pos = n + 1 | |
131 | continue |
|
126 | continue | |
132 | if n > pos: |
|
127 | if n > pos: | |
133 |
parsed.append(( |
|
128 | parsed.append((None, tmpl[pos:n])) | |
134 |
|
129 | |||
135 | pos = n |
|
130 | pos = n | |
136 | n = tmpl.find('}', pos) |
|
131 | n = tmpl.find('}', pos) | |
137 | if n < 0: |
|
132 | if n < 0: | |
138 | # no closing |
|
133 | # no closing | |
139 |
parsed.append(( |
|
134 | parsed.append((None, tmpl[pos:stop])) | |
140 | break |
|
135 | break | |
141 |
|
136 | |||
142 | expr = tmpl[pos + 1:n] |
|
137 | expr = tmpl[pos + 1:n] | |
@@ -161,7 +156,10 b' class engine(object):' | |||||
161 | def _process(self, parsed, mapping): |
|
156 | def _process(self, parsed, mapping): | |
162 | '''Render a template. Returns a generator.''' |
|
157 | '''Render a template. Returns a generator.''' | |
163 | for f, e in parsed: |
|
158 | for f, e in parsed: | |
164 |
|
|
159 | if f: | |
|
160 | yield f(mapping, e) | |||
|
161 | else: | |||
|
162 | yield e | |||
165 |
|
163 | |||
166 | engines = {'default': engine} |
|
164 | engines = {'default': engine} | |
167 |
|
165 |
General Comments 0
You need to be logged in to leave comments.
Login now