OLD | NEW |
1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. | 1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr | 2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
3 # | 3 # |
4 # This file is part of logilab-common. | 4 # This file is part of logilab-common. |
5 # | 5 # |
6 # logilab-common is free software: you can redistribute it and/or modify it unde
r | 6 # logilab-common is free software: you can redistribute it and/or modify it unde
r |
7 # the terms of the GNU Lesser General Public License as published by the Free | 7 # the terms of the GNU Lesser General Public License as published by the Free |
8 # Software Foundation, either version 2.1 of the License, or (at your option) an
y | 8 # Software Foundation, either version 2.1 of the License, or (at your option) an
y |
9 # later version. | 9 # later version. |
10 # | 10 # |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 if '://' in url_or_path: | 277 if '://' in url_or_path: |
278 return url_or_path.rstrip('/').rsplit('/', 1) | 278 return url_or_path.rstrip('/').rsplit('/', 1) |
279 return osp.split(url_or_path.rstrip(osp.sep)) | 279 return osp.split(url_or_path.rstrip(osp.sep)) |
280 | 280 |
281 | 281 |
282 def text_to_dict(text): | 282 def text_to_dict(text): |
283 """parse multilines text containing simple 'key=value' lines and return a | 283 """parse multilines text containing simple 'key=value' lines and return a |
284 dict of {'key': 'value'}. When the same key is encountered multiple time, | 284 dict of {'key': 'value'}. When the same key is encountered multiple time, |
285 value is turned into a list containing all values. | 285 value is turned into a list containing all values. |
286 | 286 |
287 >>> text_to_dict('''multiple=1 | 287 >>> d = text_to_dict('''multiple=1 |
288 ... multiple= 2 | 288 ... multiple= 2 |
289 ... single =3 | 289 ... single =3 |
290 ... ''') | 290 ... ''') |
291 {'single': '3', 'multiple': ['1', '2']} | 291 >>> d['single'] |
| 292 '3' |
| 293 >>> d['multiple'] |
| 294 ['1', '2'] |
292 | 295 |
293 """ | 296 """ |
294 res = {} | 297 res = {} |
295 if not text: | 298 if not text: |
296 return res | 299 return res |
297 for line in text.splitlines(): | 300 for line in text.splitlines(): |
298 line = line.strip() | 301 line = line.strip() |
299 if line and not line.startswith('#'): | 302 if line and not line.startswith('#'): |
300 key, value = [w.strip() for w in line.split('=', 1)] | 303 key, value = [w.strip() for w in line.split('=', 1)] |
301 if key in res: | 304 if key in res: |
302 try: | 305 try: |
303 res[key].append(value) | 306 res[key].append(value) |
304 except AttributeError: | 307 except AttributeError: |
305 res[key] = [res[key], value] | 308 res[key] = [res[key], value] |
306 else: | 309 else: |
307 res[key] = value | 310 res[key] = value |
308 return res | 311 return res |
309 | 312 |
310 | 313 |
311 _BLANK_URE = r'(\s|,)+' | 314 _BLANK_URE = r'(\s|,)+' |
312 _BLANK_RE = re.compile(_BLANK_URE) | 315 _BLANK_RE = re.compile(_BLANK_URE) |
313 __VALUE_URE = r'-?(([0-9]+\.[0-9]*)|((0x?)?[0-9]+))' | 316 __VALUE_URE = r'-?(([0-9]+\.[0-9]*)|((0x?)?[0-9]+))' |
314 __UNITS_URE = r'[a-zA-Z]+' | 317 __UNITS_URE = r'[a-zA-Z]+' |
315 _VALUE_RE = re.compile(r'(?P<value>%s)(?P<unit>%s)?'%(__VALUE_URE, __UNITS_URE)) | 318 _VALUE_RE = re.compile(r'(?P<value>%s)(?P<unit>%s)?'%(__VALUE_URE, __UNITS_URE)) |
| 319 _VALIDATION_RE = re.compile(r'^((%s)(%s))*(%s)?$' % (__VALUE_URE, __UNITS_URE, |
| 320 __VALUE_URE)) |
316 | 321 |
317 BYTE_UNITS = { | 322 BYTE_UNITS = { |
318 "b": 1, | 323 "b": 1, |
319 "kb": 1024, | 324 "kb": 1024, |
320 "mb": 1024 ** 2, | 325 "mb": 1024 ** 2, |
321 "gb": 1024 ** 3, | 326 "gb": 1024 ** 3, |
322 "tb": 1024 ** 4, | 327 "tb": 1024 ** 4, |
323 } | 328 } |
324 | 329 |
325 TIME_UNITS = { | 330 TIME_UNITS = { |
(...skipping 19 matching lines...) Expand all Loading... |
345 :param inter: used to parse every intermediate value (need __sum__) | 350 :param inter: used to parse every intermediate value (need __sum__) |
346 | 351 |
347 :type blank_reg: regexp | 352 :type blank_reg: regexp |
348 :param blank_reg: should match every blank char to ignore. | 353 :param blank_reg: should match every blank char to ignore. |
349 | 354 |
350 :type value_reg: regexp with "value" and optional "unit" group | 355 :type value_reg: regexp with "value" and optional "unit" group |
351 :param value_reg: match a value and it's unit into the | 356 :param value_reg: match a value and it's unit into the |
352 """ | 357 """ |
353 if inter is None: | 358 if inter is None: |
354 inter = final | 359 inter = final |
355 string = _BLANK_RE.sub('', string) | 360 fstring = _BLANK_RE.sub('', string) |
| 361 if not (fstring and _VALIDATION_RE.match(fstring)): |
| 362 raise ValueError("Invalid unit string: %r." % string) |
356 values = [] | 363 values = [] |
357 for match in value_reg.finditer(string): | 364 for match in value_reg.finditer(fstring): |
358 dic = match.groupdict() | 365 dic = match.groupdict() |
359 #import sys | |
360 #print >> sys.stderr, dic | |
361 lit, unit = dic["value"], dic.get("unit") | 366 lit, unit = dic["value"], dic.get("unit") |
362 value = inter(lit) | 367 value = inter(lit) |
363 if unit is not None: | 368 if unit is not None: |
364 try: | 369 try: |
365 value *= units[unit.lower()] | 370 value *= units[unit.lower()] |
366 except KeyError: | 371 except KeyError: |
367 raise KeyError('invalid unit %s. valid units are %s' % | 372 raise KeyError('invalid unit %s. valid units are %s' % |
368 (unit, units.keys())) | 373 (unit, units.keys())) |
369 values.append(value) | 374 values.append(value) |
370 return final(sum(values)) | 375 return final(sum(values)) |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 out.write(colorize_ansi(line, style['remove'])) | 528 out.write(colorize_ansi(line, style['remove'])) |
524 elif line[0] == '+': | 529 elif line[0] == '+': |
525 out.write(colorize_ansi(line, style['add'])) | 530 out.write(colorize_ansi(line, style['add'])) |
526 elif line[:4] == '--- ': | 531 elif line[:4] == '--- ': |
527 out.write(colorize_ansi(line, style['separator'])) | 532 out.write(colorize_ansi(line, style['separator'])) |
528 elif line[:4] == '+++ ': | 533 elif line[:4] == '+++ ': |
529 out.write(colorize_ansi(line, style['separator'])) | 534 out.write(colorize_ansi(line, style['separator'])) |
530 else: | 535 else: |
531 out.write(line) | 536 out.write(line) |
532 | 537 |
OLD | NEW |