Show More
@@ -17,12 +17,14 b' def _flatten(thing):' | |||||
17 | if isinstance(thing, str): |
|
17 | if isinstance(thing, str): | |
18 | yield thing |
|
18 | yield thing | |
19 | elif not hasattr(thing, '__iter__'): |
|
19 | elif not hasattr(thing, '__iter__'): | |
|
20 | if i is not None: | |||
20 | yield str(thing) |
|
21 | yield str(thing) | |
21 | elif thing is not None: |
|
22 | else: | |
22 | for i in thing: |
|
23 | for i in thing: | |
23 | if isinstance(i, str): |
|
24 | if isinstance(i, str): | |
24 | yield i |
|
25 | yield i | |
25 | elif not hasattr(i, '__iter__'): |
|
26 | elif not hasattr(i, '__iter__'): | |
|
27 | if i is not None: | |||
26 | yield str(i) |
|
28 | yield str(i) | |
27 | elif i is not None: |
|
29 | elif i is not None: | |
28 | for j in _flatten(i): |
|
30 | for j in _flatten(i): | |
@@ -64,36 +66,40 b' class engine(object):' | |||||
64 | self._defaults = defaults |
|
66 | self._defaults = defaults | |
65 | self._cache = {} |
|
67 | self._cache = {} | |
66 |
|
68 | |||
67 |
def |
|
69 | def _load(self, t): | |
68 | '''Perform expansion. t is name of map element to expand. mapping contains |
|
70 | '''load, parse, and cache a template''' | |
69 | added elements for use during expansion. Is a generator.''' |
|
|||
70 | if t not in self._cache: |
|
71 | if t not in self._cache: | |
71 | self._cache[t] = self._parse(self._loader(t)) |
|
72 | self._cache[t] = self._parse(self._loader(t)) | |
72 |
return |
|
73 | return self._cache[t] | |
73 |
|
74 | |||
74 |
def |
|
75 | def process(self, t, mapping): | |
75 | '''preparse a template''' |
|
76 | '''Perform expansion. t is name of map element to expand. | |
|
77 | mapping contains added elements for use during expansion. Is a | |||
|
78 | generator.''' | |||
76 |
|
79 | |||
77 | defget = self._defaults.get |
|
80 | return _flatten(self._process(self._load(t), mapping)) | |
78 | def getter(mapping, key): |
|
81 | ||
|
82 | def _get(self, mapping, key): | |||
79 |
|
|
83 | v = mapping.get(key) | |
80 |
|
|
84 | if v is None: | |
81 |
|
|
85 | v = self._defaults.get(key, '') | |
82 |
|
|
86 | if hasattr(v, '__call__'): | |
83 |
|
|
87 | v = v(**mapping) | |
84 |
|
|
88 | return v | |
85 |
|
89 | |||
86 |
|
|
90 | def _raw(self, mapping, x): | |
87 |
|
|
91 | return x | |
88 | def filt(mapping, parts): |
|
92 | ||
|
93 | def _filter(self, mapping, parts): | |||
89 |
|
|
94 | filters, val = parts | |
90 |
|
|
95 | x = self._get(mapping, val) | |
91 |
|
|
96 | for f in filters: | |
92 |
|
|
97 | x = f(x) | |
93 |
|
|
98 | return x | |
94 | def formatter(mapping, args): |
|
99 | ||
95 | key, format = args |
|
100 | def _format(self, mapping, args): | |
96 | v = getter(mapping, key) |
|
101 | key, parsed = args | |
|
102 | v = self._get(mapping, key) | |||
97 |
|
|
103 | if not hasattr(v, '__iter__'): | |
98 |
|
|
104 | raise SyntaxError(_("error expanding '%s%%%s'") | |
99 |
|
|
105 | % (key, format)) | |
@@ -101,40 +107,44 b' class engine(object):' | |||||
101 |
|
|
107 | for i in v: | |
102 |
|
|
108 | if isinstance(i, dict): | |
103 |
|
|
109 | lm.update(i) | |
104 |
|
|
110 | yield self._process(parsed, lm) | |
105 |
|
|
111 | else: | |
106 |
|
|
112 | # v is not an iterable of dicts, this happen when 'key' | |
107 |
|
|
113 | # has been fully expanded already and format is useless. | |
108 |
|
|
114 | # If so, return the expanded value. | |
109 |
|
|
115 | yield i | |
110 |
|
116 | |||
|
117 | def _parse(self, tmpl): | |||
|
118 | '''preparse a template''' | |||
|
119 | ||||
111 | parsed = [] |
|
120 | parsed = [] | |
112 | pos, stop = 0, len(tmpl) |
|
121 | pos, stop = 0, len(tmpl) | |
113 | while pos < stop: |
|
122 | while pos < stop: | |
114 | n = tmpl.find('{', pos) |
|
123 | n = tmpl.find('{', pos) | |
115 | if n < 0: |
|
124 | if n < 0: | |
116 | parsed.append((raw, tmpl[pos:stop])) |
|
125 | parsed.append((self._raw, tmpl[pos:stop])) | |
117 | break |
|
126 | break | |
118 | if n > 0 and tmpl[n - 1] == '\\': |
|
127 | if n > 0 and tmpl[n - 1] == '\\': | |
119 | # escaped |
|
128 | # escaped | |
120 | parsed.append((raw, tmpl[pos:n + 1])) |
|
129 | parsed.append((self._raw, tmpl[pos:n + 1])) | |
121 | pos = n + 1 |
|
130 | pos = n + 1 | |
122 | continue |
|
131 | continue | |
123 | if n > pos: |
|
132 | if n > pos: | |
124 | parsed.append((raw, tmpl[pos:n])) |
|
133 | parsed.append((self._raw, tmpl[pos:n])) | |
125 |
|
134 | |||
126 | pos = n |
|
135 | pos = n | |
127 | n = tmpl.find('}', pos) |
|
136 | n = tmpl.find('}', pos) | |
128 | if n < 0: |
|
137 | if n < 0: | |
129 | # no closing |
|
138 | # no closing | |
130 | parsed.append((raw, tmpl[pos:stop])) |
|
139 | parsed.append((self._raw, tmpl[pos:stop])) | |
131 | break |
|
140 | break | |
132 |
|
141 | |||
133 | expr = tmpl[pos + 1:n] |
|
142 | expr = tmpl[pos + 1:n] | |
134 | pos = n + 1 |
|
143 | pos = n + 1 | |
135 |
|
144 | |||
136 | if '%' in expr: |
|
145 | if '%' in expr: | |
137 |
|
|
146 | key, t = expr.split('%') | |
|
147 | parsed.append((self._format, (key, self._load(t)))) | |||
138 | elif '|' in expr: |
|
148 | elif '|' in expr: | |
139 | parts = expr.split('|') |
|
149 | parts = expr.split('|') | |
140 | val = parts[0] |
|
150 | val = parts[0] | |
@@ -142,9 +152,9 b' class engine(object):' | |||||
142 | filters = [self._filters[f] for f in parts[1:]] |
|
152 | filters = [self._filters[f] for f in parts[1:]] | |
143 | except KeyError, i: |
|
153 | except KeyError, i: | |
144 | raise SyntaxError(_("unknown filter '%s'") % i[0]) |
|
154 | raise SyntaxError(_("unknown filter '%s'") % i[0]) | |
145 | parsed.append((filt, (filters, val))) |
|
155 | parsed.append((self._filter, (filters, val))) | |
146 | else: |
|
156 | else: | |
147 |
parsed.append((get |
|
157 | parsed.append((self._get, expr)) | |
148 |
|
158 | |||
149 | return parsed |
|
159 | return parsed | |
150 |
|
160 |
General Comments 0
You need to be logged in to leave comments.
Login now