~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Vincent Ladeuil
  • Date: 2011-12-16 16:20:01 UTC
  • mto: This revision was merged to the branch mainline in revision 6387.
  • Revision ID: v.ladeuil+lp@free.fr-20111216162001-qe39b3khelh34u1v
Add int_SI_from_store as a config option helper

Show diffs side-by-side

added added

removed removed

Lines of Context:
2455
2455
    return int(unicode_str)
2456
2456
 
2457
2457
 
 
2458
_unit_sfxs = dict(K=10**3, M=10**6, G=10**9)
 
2459
 
 
2460
def int_SI_from_store(unicode_str):
 
2461
    regexp = "^(\d+)(([" + ''.join(_unit_sfxs) + "])b?)?$"
 
2462
    p = re.compile(regexp, re.IGNORECASE)
 
2463
    m = p.match(unicode_str)
 
2464
    val = None
 
2465
    if m is not None:
 
2466
        val, _, unit = m.groups()
 
2467
        val = int(val)
 
2468
        if unit:
 
2469
            try:
 
2470
                coeff = _unit_sfxs[unit.upper()]
 
2471
            except KeyError:
 
2472
                raise ValueError(gettext('{0} is not an SI unit.').format(unit))
 
2473
            val *= coeff
 
2474
    return val
 
2475
 
 
2476
 
2458
2477
def float_from_store(unicode_str):
2459
2478
    return float(unicode_str)
2460
2479