Revision 1562
Added by Aaron Marcuse-Kubitza almost 13 years ago
lib/xml_func.py | ||
---|---|---|
37 | 37 |
except Exception, e: # also catch XML func internal errors |
38 | 38 |
# Save in case another exception raised, overwriting sys.exc_info() |
39 | 39 |
exc.add_traceback(e) |
40 |
str_ = str(node) |
|
40 |
str_ = strings.ustr(node)
|
|
41 | 41 |
exc.add_msg(e, 'function:\n'+str_) |
42 | 42 |
xml_dom.replace(node, node.ownerDocument.createComment( |
43 | 43 |
'\n'+term.emph_multiline(str_).replace('--','-'))) |
... | ... | |
86 | 86 |
then = items['then'] |
87 | 87 |
except KeyError, e: raise SyntaxException(e) |
88 | 88 |
else_ = items.get('else', None) |
89 |
cond = bool(cast(str, cond)) |
|
89 |
cond = bool(cast(strings.ustr, cond))
|
|
90 | 90 |
if cond: return then |
91 | 91 |
else: return else_ |
92 | 92 |
funcs['_if'] = _if |
... | ... | |
102 | 102 |
|
103 | 103 |
def _merge(items): |
104 | 104 |
items = list(conv_items(strings.ustr, items)) |
105 |
# get *once* from iter and check types
|
|
105 |
# get *once* from iter, check types
|
|
106 | 106 |
items.sort() |
107 | 107 |
return maps.merge_values(*[v for k, v in items]) |
108 | 108 |
funcs['_merge'] = _merge |
109 | 109 |
|
110 | 110 |
def _label(items): |
111 | 111 |
items = dict(conv_items(strings.ustr, items)) |
112 |
# get *once* from iter and check types
|
|
112 |
# get *once* from iter, check types
|
|
113 | 113 |
try: |
114 | 114 |
label = items['label'] |
115 | 115 |
value = items['value'] |
... | ... | |
122 | 122 |
types_by_name = {None: strings.ustr, 'str': strings.ustr, 'float': float} |
123 | 123 |
|
124 | 124 |
def _nullIf(items): |
125 |
items = dict(conv_items(str, items)) |
|
125 |
items = dict(conv_items(strings.ustr, items))
|
|
126 | 126 |
try: null = items['null'] |
127 | 127 |
except KeyError, e: raise SyntaxException(e) |
128 | 128 |
value = items.get('value', None) |
... | ... | |
145 | 145 |
value "*" means keep input value the same |
146 | 146 |
value "" means ignore input value |
147 | 147 |
''' |
148 |
items = conv_items(str, items) # get *once* from iter and check types
|
|
148 |
items = conv_items(strings.ustr, items) # get *once* from iter, check types
|
|
149 | 149 |
try: value = items.pop()[1] # last entry contains value |
150 | 150 |
except IndexError, e: raise SyntaxException(e) |
151 | 151 |
map_ = dict(items) |
... | ... | |
161 | 161 |
funcs['_map'] = _map |
162 | 162 |
|
163 | 163 |
def _replace(items): |
164 |
items = conv_items(str, items) # get *once* from iter and check types
|
|
164 |
items = conv_items(strings.ustr, items) # get *once* from iter, check types
|
|
165 | 165 |
try: value = items.pop()[1] # last entry contains value |
166 | 166 |
except IndexError, e: raise SyntaxException(e) |
167 | 167 |
try: |
... | ... | |
176 | 176 |
#### Quantities |
177 | 177 |
|
178 | 178 |
def _units(items): |
179 |
items = conv_items(str, items) # get *once* from iter and check types
|
|
179 |
items = conv_items(strings.ustr, items) # get *once* from iter, check types
|
|
180 | 180 |
try: last = items.pop() # last entry contains value |
181 | 181 |
except IndexError: return None # input is empty and no actions |
182 | 182 |
if last[0] != 'value': return None # input is empty |
... | ... | |
201 | 201 |
return tuple(d.strip() for d in (start, end)) |
202 | 202 |
|
203 | 203 |
def _rangeStart(items): |
204 |
items = dict(conv_items(str, items)) |
|
204 |
items = dict(conv_items(strings.ustr, items))
|
|
205 | 205 |
try: value = items['value'] |
206 | 206 |
except KeyError: return None # input is empty |
207 | 207 |
return parse_range(value)[0] |
208 | 208 |
funcs['_rangeStart'] = _rangeStart |
209 | 209 |
|
210 | 210 |
def _rangeEnd(items): |
211 |
items = dict(conv_items(str, items)) |
|
211 |
items = dict(conv_items(strings.ustr, items))
|
|
212 | 212 |
try: value = items['value'] |
213 | 213 |
except KeyError: return None # input is empty |
214 | 214 |
return parse_range(value)[1] |
... | ... | |
248 | 248 |
#### Dates |
249 | 249 |
|
250 | 250 |
def _date(items): |
251 |
items = dict(conv_items(str, items)) # get *once* from iter and check types |
|
251 |
items = dict(conv_items(strings.ustr, items)) |
|
252 |
# get *once* from iter, check types |
|
252 | 253 |
try: str_ = items['date'] |
253 | 254 |
except KeyError: |
254 | 255 |
# Year is required |
... | ... | |
275 | 276 |
except ValueError, e: |
276 | 277 |
if try_num > 0: raise SyntaxException(e) |
277 | 278 |
# exception still raised after retry |
278 |
msg = str(e) |
|
279 |
msg = strings.ustr(e)
|
|
279 | 280 |
if msg == 'month must be in 1..12': # try swapping month and day |
280 | 281 |
items['month'], items['day'] = items['day'], items['month'] |
281 | 282 |
else: raise SyntaxException(e) |
... | ... | |
292 | 293 |
funcs['_date'] = _date |
293 | 294 |
|
294 | 295 |
def _dateRangeStart(items): |
295 |
items = dict(conv_items(str, items)) |
|
296 |
items = dict(conv_items(strings.ustr, items))
|
|
296 | 297 |
try: value = items['value'] |
297 | 298 |
except KeyError: return None # input is empty |
298 | 299 |
return dates.parse_date_range(value)[0] |
299 | 300 |
funcs['_dateRangeStart'] = _dateRangeStart |
300 | 301 |
|
301 | 302 |
def _dateRangeEnd(items): |
302 |
items = dict(conv_items(str, items)) |
|
303 |
items = dict(conv_items(strings.ustr, items))
|
|
303 | 304 |
try: value = items['value'] |
304 | 305 |
except KeyError: return None # input is empty |
305 | 306 |
return dates.parse_date_range(value)[1] |
... | ... | |
337 | 338 |
def _simplifyPath(items): |
338 | 339 |
items = dict(items) |
339 | 340 |
try: |
340 |
next = cast(str, items['next']) |
|
341 |
require = cast(str, items['require']) |
|
341 |
next = cast(strings.ustr, items['next'])
|
|
342 |
require = cast(strings.ustr, items['require'])
|
|
342 | 343 |
root = items['path'] |
343 | 344 |
except KeyError, e: raise SyntaxException(e) |
344 | 345 |
|
Also available in: Unified diff
xml_func.py: Made everything Unicode-safe by using strings.ustr instead of str