Show More
@@ -44,6 +44,9 b' class templater(object):' | |||||
44 | filter uses function to transform value. syntax is |
|
44 | filter uses function to transform value. syntax is | |
45 | {key|filter1|filter2|...}.''' |
|
45 | {key|filter1|filter2|...}.''' | |
46 |
|
46 | |||
|
47 | template_re = re.compile(r"(?:(?:#(?=[\w\|%]+#))|(?:{(?=[\w\|%]+})))" | |||
|
48 | r"(\w+)(?:(?:%(\w+))|((?:\|\w+)*))[#}]") | |||
|
49 | ||||
47 | def __init__(self, mapfile, filters={}, defaults={}, cache={}): |
|
50 | def __init__(self, mapfile, filters={}, defaults={}, cache={}): | |
48 | '''set up template engine. |
|
51 | '''set up template engine. | |
49 | mapfile is name of file to read map definitions from. |
|
52 | mapfile is name of file to read map definitions from. | |
@@ -84,55 +87,46 b' class templater(object):' | |||||
84 | '''perform expansion. |
|
87 | '''perform expansion. | |
85 | t is name of map element to expand. |
|
88 | t is name of map element to expand. | |
86 | map is added elements to use during expansion.''' |
|
89 | map is added elements to use during expansion.''' | |
87 | m = self.defaults.copy() |
|
|||
88 | m.update(map) |
|
|||
89 | if not self.cache.has_key(t): |
|
90 | if not self.cache.has_key(t): | |
90 | try: |
|
91 | try: | |
91 | self.cache[t] = file(self.map[t]).read() |
|
92 | self.cache[t] = file(self.map[t]).read() | |
92 | except IOError, inst: |
|
93 | except IOError, inst: | |
93 | raise IOError(inst.args[0], _('template file %s: %s') % |
|
94 | raise IOError(inst.args[0], _('template file %s: %s') % | |
94 | (self.map[t], inst.args[1])) |
|
95 | (self.map[t], inst.args[1])) | |
95 |
|
|
96 | tmpl = self.cache[t] | |
96 |
|
97 | |||
97 | template_re = re.compile(r"(?:(?:#(?=[\w\|%]+#))|(?:{(?=[\w\|%]+})))" |
|
|||
98 | r"(\w+)((%\w+)*)((\|\w+)*)[#}]") |
|
|||
99 |
|
||||
100 | def template(self, tmpl, **map): |
|
|||
101 | while tmpl: |
|
98 | while tmpl: | |
102 | m = self.template_re.search(tmpl) |
|
99 | m = self.template_re.search(tmpl) | |
103 | if m: |
|
100 | if not m: | |
104 |
|
|
101 | yield tmpl | |
105 |
|
|
102 | break | |
106 | format = m.group(2) |
|
|||
107 | fl = m.group(4) |
|
|||
108 |
|
103 | |||
109 | if start: |
|
104 | start, end = m.span(0) | |
110 | yield tmpl[:start] |
|
105 | key, format, fl = m.groups() | |
111 |
|
106 | |||
112 | v = map.get(key, "") |
|
107 | if start: | |
113 | if callable(v): |
|
108 | yield tmpl[:start] | |
114 | v = v(**map) |
|
109 | tmpl = tmpl[end:] | |
115 |
|
110 | |||
116 |
|
|
111 | if key in map: | |
117 | if not hasattr(v, '__iter__'): |
|
112 | v = map[key] | |
118 | raise SyntaxError(_("Error expanding '%s%s'") |
|
113 | else: | |
119 | % (key, format)) |
|
114 | v = self.defaults.get(key, "") | |
120 | lm = map.copy() |
|
115 | if callable(v): | |
121 |
|
|
116 | v = v(**map) | |
122 | lm.update(i) |
|
117 | if format: | |
123 | yield self(format[1:], **lm) |
|
118 | if not hasattr(v, '__iter__'): | |
124 |
|
119 | raise SyntaxError(_("Error expanding '%s%s'") | ||
125 | v = "" |
|
120 | % (key, format)) | |
126 |
|
121 | lm = map.copy() | ||
127 |
|
|
122 | for i in v: | |
|
123 | lm.update(i) | |||
|
124 | yield self(format, **lm) | |||
|
125 | else: | |||
|
126 | if fl: | |||
128 | for f in fl.split("|")[1:]: |
|
127 | for f in fl.split("|")[1:]: | |
129 | v = self.filters[f](v) |
|
128 | v = self.filters[f](v) | |
130 |
|
||||
131 | yield v |
|
129 | yield v | |
132 | tmpl = tmpl[end:] |
|
|||
133 | else: |
|
|||
134 | yield tmpl |
|
|||
135 | break |
|
|||
136 |
|
130 | |||
137 | agescales = [("second", 1), |
|
131 | agescales = [("second", 1), | |
138 | ("minute", 60), |
|
132 | ("minute", 60), |
General Comments 0
You need to be logged in to leave comments.
Login now