comparison MoinMoin/wikiutil.py @ 2453:adf5ec4874ed

fix ParameterParser, rewrite ParameterParser tests for py.test (and add more tests), refactor Hits macro
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 21 Jul 2007 21:30:47 +0200
parents 45641cbe7729
children fe71235f1f7c
comparison
equal deleted inserted replaced
2452:839b360413a9 2453:adf5ec4874ed
1 # -*- coding: iso-8859-1 -*- 1 # -*- coding: iso-8859-1 -*-
2 """ 2 """
3 MoinMoin - Wiki Utility Functions 3 MoinMoin - Wiki Utility Functions
4 4
5 @copyright: 2000-2004 Juergen Hermann <jh@web.de>, 5 @copyright: 2000-2004 Juergen Hermann <jh@web.de>,
6 2004 by Florian Festi,
7 2006 by Mikko Virkkil,
6 2005-2007 MoinMoin:ThomasWaldmann, 8 2005-2007 MoinMoin:ThomasWaldmann,
7 2007 MoinMoin:ReimarBauer 9 2007 MoinMoin:ReimarBauer
8 @license: GNU GPL, see COPYING for details. 10 @license: GNU GPL, see COPYING for details.
9 """ 11 """
10 12
1301 1303
1302 Another way of calling would be: 1304 Another way of calling would be:
1303 ("John Smith", male=True) 1305 ("John Smith", male=True)
1304 this will result in the following dict: 1306 this will result in the following dict:
1305 {"name": "John Smith", "age": None, "male": True} 1307 {"name": "John Smith", "age": None, "male": True}
1306
1307 @copyright: 2004 by Florian Festi,
1308 2006 by Mikko Virkkilä
1309 @license: GNU GPL, see COPYING for details.
1310 """ 1308 """
1311 1309
1312 def __init__(self, pattern): 1310 def __init__(self, pattern):
1313 #parameter_re = "([^\"',]*(\"[^\"]*\"|'[^']*')?[^\"',]*)[,)]" 1311 #parameter_re = "([^\"',]*(\"[^\"]*\"|'[^']*')?[^\"',]*)[,)]"
1314 name = "(?P<%s>[a-zA-Z_][a-zA-Z0-9_]*)" 1312 name = "(?P<%s>[a-zA-Z_][a-zA-Z0-9_]*)"
1354 """ 1352 """
1355 (4, 2) 1353 (4, 2)
1356 """ 1354 """
1357 #Default list to "None"s 1355 #Default list to "None"s
1358 parameter_list = [None] * len(self.param_list) 1356 parameter_list = [None] * len(self.param_list)
1359 parameter_dict = {} 1357 parameter_dict = dict([(key, None) for key in self.param_dict])
1360 check_list = [0] * len(self.param_list) 1358 check_list = [0] * len(self.param_list)
1361 1359
1362 i = 0 1360 i = 0
1363 start = 0 1361 start = 0
1364 named = False 1362 named = False
1365 1363
1366 if not params:
1367 params = '""'
1368
1369 while start < len(params): 1364 while start < len(params):
1370 match = re.match(self.param_re, params[start:]) 1365 match = re.match(self.param_re, params[start:])
1371 if not match: 1366 if not match:
1372 raise ValueError, "Misformatted value" 1367 raise ValueError("malformed parameters")
1373 start += match.end() 1368 start += match.end()
1374 value = None
1375 if match.group("int"): 1369 if match.group("int"):
1376 value = int(match.group("int")) 1370 value = int(match.group("int"))
1377 type = 'i' 1371 type = 'i'
1378 elif match.group("bool"): 1372 elif match.group("bool"):
1379 value = (match.group("bool") == "1") or (match.group("bool") == "True") or (match.group("bool") == "true") 1373 value = (match.group("bool") == "1") or (match.group("bool") == "True") or (match.group("bool") == "true")
1386 type = 's' 1380 type = 's'
1387 elif match.group("name_param"): 1381 elif match.group("name_param"):
1388 value = match.group("name_param") 1382 value = match.group("name_param")
1389 type = 'n' 1383 type = 'n'
1390 else: 1384 else:
1391 value = None 1385 raise ValueError("Parameter parser code does not fit param_re regex")
1392 1386
1393 parameter_list.append(value) 1387 parameter_list.append(value)
1394 if match.group("name"): 1388 name = match.group("name")
1395 if match.group("name") not in self.param_dict: 1389 if name:
1390 if name not in self.param_dict:
1396 # TODO we should think on inheritance of parameters 1391 # TODO we should think on inheritance of parameters
1397 raise ValueError, "Unknown parameter name '%s'" % match.group("name") 1392 raise ValueError("unknown parameter name '%s'" % name)
1398 nr = self.param_dict[match.group("name")] 1393 nr = self.param_dict[name]
1399 if check_list[nr]: 1394 if check_list[nr]:
1400 #raise ValueError, "Parameter specified twice" 1395 raise ValueError("parameter '%s' specified twice" % name)
1401 #TODO: Something saner that raising an exception. This is pretty good, since it ignores it.
1402 pass
1403 else: 1396 else:
1404 check_list[nr] = 1 1397 check_list[nr] = 1
1405 parameter_dict[match.group("name")] = value 1398 parameter_dict[name] = value
1406 parameter_list[nr] = value 1399 parameter_list[nr] = value
1407 named = True 1400 named = True
1408 elif named: 1401 elif named:
1409 raise ValueError, "Only named parameters allowed" 1402 raise ValueError("only named parameters allowed after first named parameter")
1410 else: 1403 else:
1411 nr = i 1404 nr = i
1412 parameter_list[nr] = value 1405 parameter_list[nr] = value
1413 1406
1414 # Let's populate and map our dictionary to what's been found 1407 # Let's populate and map our dictionary to what's been found