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 >>> d = text_to_dict('''multiple=1 | 287 >>> text_to_dict('''multiple=1 |
288 ... multiple= 2 | 288 ... multiple= 2 |
289 ... single =3 | 289 ... single =3 |
290 ... ''') | 290 ... ''') |
291 >>> d['single'] | 291 {'single': '3', 'multiple': ['1', '2']} |
292 '3' | |
293 >>> d['multiple'] | |
294 ['1', '2'] | |
295 | 292 |
296 """ | 293 """ |
297 res = {} | 294 res = {} |
298 if not text: | 295 if not text: |
299 return res | 296 return res |
300 for line in text.splitlines(): | 297 for line in text.splitlines(): |
301 line = line.strip() | 298 line = line.strip() |
302 if line and not line.startswith('#'): | 299 if line and not line.startswith('#'): |
303 key, value = [w.strip() for w in line.split('=', 1)] | 300 key, value = [w.strip() for w in line.split('=', 1)] |
304 if key in res: | 301 if key in res: |
305 try: | 302 try: |
306 res[key].append(value) | 303 res[key].append(value) |
307 except AttributeError: | 304 except AttributeError: |
308 res[key] = [res[key], value] | 305 res[key] = [res[key], value] |
309 else: | 306 else: |
310 res[key] = value | 307 res[key] = value |
311 return res | 308 return res |
312 | 309 |
313 | 310 |
314 _BLANK_URE = r'(\s|,)+' | 311 _BLANK_URE = r'(\s|,)+' |
315 _BLANK_RE = re.compile(_BLANK_URE) | 312 _BLANK_RE = re.compile(_BLANK_URE) |
316 __VALUE_URE = r'-?(([0-9]+\.[0-9]*)|((0x?)?[0-9]+))' | 313 __VALUE_URE = r'-?(([0-9]+\.[0-9]*)|((0x?)?[0-9]+))' |
317 __UNITS_URE = r'[a-zA-Z]+' | 314 __UNITS_URE = r'[a-zA-Z]+' |
318 _VALUE_RE = re.compile(r'(?P<value>%s)(?P<unit>%s)?'%(__VALUE_URE, __UNITS_URE)) | 315 _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)) | |
321 | 316 |
322 BYTE_UNITS = { | 317 BYTE_UNITS = { |
323 "b": 1, | 318 "b": 1, |
324 "kb": 1024, | 319 "kb": 1024, |
325 "mb": 1024 ** 2, | 320 "mb": 1024 ** 2, |
326 "gb": 1024 ** 3, | 321 "gb": 1024 ** 3, |
327 "tb": 1024 ** 4, | 322 "tb": 1024 ** 4, |
328 } | 323 } |
329 | 324 |
330 TIME_UNITS = { | 325 TIME_UNITS = { |
(...skipping 19 matching lines...) Expand all Loading... |
350 :param inter: used to parse every intermediate value (need __sum__) | 345 :param inter: used to parse every intermediate value (need __sum__) |
351 | 346 |
352 :type blank_reg: regexp | 347 :type blank_reg: regexp |
353 :param blank_reg: should match every blank char to ignore. | 348 :param blank_reg: should match every blank char to ignore. |
354 | 349 |
355 :type value_reg: regexp with "value" and optional "unit" group | 350 :type value_reg: regexp with "value" and optional "unit" group |
356 :param value_reg: match a value and it's unit into the | 351 :param value_reg: match a value and it's unit into the |
357 """ | 352 """ |
358 if inter is None: | 353 if inter is None: |
359 inter = final | 354 inter = final |
360 fstring = _BLANK_RE.sub('', string) | 355 string = _BLANK_RE.sub('', string) |
361 if not (fstring and _VALIDATION_RE.match(fstring)): | |
362 raise ValueError("Invalid unit string: %r." % string) | |
363 values = [] | 356 values = [] |
364 for match in value_reg.finditer(fstring): | 357 for match in value_reg.finditer(string): |
365 dic = match.groupdict() | 358 dic = match.groupdict() |
| 359 #import sys |
| 360 #print >> sys.stderr, dic |
366 lit, unit = dic["value"], dic.get("unit") | 361 lit, unit = dic["value"], dic.get("unit") |
367 value = inter(lit) | 362 value = inter(lit) |
368 if unit is not None: | 363 if unit is not None: |
369 try: | 364 try: |
370 value *= units[unit.lower()] | 365 value *= units[unit.lower()] |
371 except KeyError: | 366 except KeyError: |
372 raise KeyError('invalid unit %s. valid units are %s' % | 367 raise KeyError('invalid unit %s. valid units are %s' % |
373 (unit, units.keys())) | 368 (unit, units.keys())) |
374 values.append(value) | 369 values.append(value) |
375 return final(sum(values)) | 370 return final(sum(values)) |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 out.write(colorize_ansi(line, style['remove'])) | 523 out.write(colorize_ansi(line, style['remove'])) |
529 elif line[0] == '+': | 524 elif line[0] == '+': |
530 out.write(colorize_ansi(line, style['add'])) | 525 out.write(colorize_ansi(line, style['add'])) |
531 elif line[:4] == '--- ': | 526 elif line[:4] == '--- ': |
532 out.write(colorize_ansi(line, style['separator'])) | 527 out.write(colorize_ansi(line, style['separator'])) |
533 elif line[:4] == '+++ ': | 528 elif line[:4] == '+++ ': |
534 out.write(colorize_ansi(line, style['separator'])) | 529 out.write(colorize_ansi(line, style['separator'])) |
535 else: | 530 else: |
536 out.write(line) | 531 out.write(line) |
537 | 532 |
OLD | NEW |