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,35 +87,33 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: | |
|
101 | yield tmpl | |||
|
102 | break | |||
|
103 | ||||
104 |
|
|
104 | start, end = m.span(0) | |
105 |
|
|
105 | key, format, fl = m.groups() | |
106 | format = m.group(2) |
|
|||
107 | fl = m.group(4) |
|
|||
108 |
|
106 | |||
109 |
|
|
107 | if start: | |
110 |
|
|
108 | yield tmpl[:start] | |
|
109 | tmpl = tmpl[end:] | |||
111 |
|
110 | |||
112 | v = map.get(key, "") |
|
111 | if key in map: | |
|
112 | v = map[key] | |||
|
113 | else: | |||
|
114 | v = self.defaults.get(key, "") | |||
113 |
|
|
115 | if callable(v): | |
114 |
|
|
116 | v = v(**map) | |
115 |
|
||||
116 |
|
|
117 | if format: | |
117 |
|
|
118 | if not hasattr(v, '__iter__'): | |
118 |
|
|
119 | raise SyntaxError(_("Error expanding '%s%s'") | |
@@ -120,19 +121,12 b' class templater(object):' | |||||
120 |
|
|
121 | lm = map.copy() | |
121 |
|
|
122 | for i in v: | |
122 |
|
|
123 | lm.update(i) | |
123 |
|
|
124 | yield self(format, **lm) | |
124 |
|
125 | else: | ||
125 |
|
|
126 | if fl: | |
126 |
|
||||
127 | elif 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