Index: third_party/logilab/common/textutils.py |
=================================================================== |
--- third_party/logilab/common/textutils.py (revision 292986) |
+++ third_party/logilab/common/textutils.py (working copy) |
@@ -284,11 +284,14 @@ |
dict of {'key': 'value'}. When the same key is encountered multiple time, |
value is turned into a list containing all values. |
- >>> text_to_dict('''multiple=1 |
+ >>> d = text_to_dict('''multiple=1 |
... multiple= 2 |
... single =3 |
... ''') |
- {'single': '3', 'multiple': ['1', '2']} |
+ >>> d['single'] |
+ '3' |
+ >>> d['multiple'] |
+ ['1', '2'] |
""" |
res = {} |
@@ -313,6 +316,8 @@ |
__VALUE_URE = r'-?(([0-9]+\.[0-9]*)|((0x?)?[0-9]+))' |
__UNITS_URE = r'[a-zA-Z]+' |
_VALUE_RE = re.compile(r'(?P<value>%s)(?P<unit>%s)?'%(__VALUE_URE, __UNITS_URE)) |
+_VALIDATION_RE = re.compile(r'^((%s)(%s))*(%s)?$' % (__VALUE_URE, __UNITS_URE, |
+ __VALUE_URE)) |
BYTE_UNITS = { |
"b": 1, |
@@ -352,12 +357,12 @@ |
""" |
if inter is None: |
inter = final |
- string = _BLANK_RE.sub('', string) |
+ fstring = _BLANK_RE.sub('', string) |
+ if not (fstring and _VALIDATION_RE.match(fstring)): |
+ raise ValueError("Invalid unit string: %r." % string) |
values = [] |
- for match in value_reg.finditer(string): |
+ for match in value_reg.finditer(fstring): |
dic = match.groupdict() |
- #import sys |
- #print >> sys.stderr, dic |
lit, unit = dic["value"], dic.get("unit") |
value = inter(lit) |
if unit is not None: |