Back to index

obnam  1.1
sizeparse.py
Go to the documentation of this file.
00001 # Copyright 2010  Lars Wirzenius
00002 # 
00003 # This program is free software: you can redistribute it and/or modify
00004 # it under the terms of the GNU General Public License as published by
00005 # the Free Software Foundation, either version 3 of the License, or
00006 # (at your option) any later version.
00007 # 
00008 # This program is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 # 
00013 # You should have received a copy of the GNU General Public License
00014 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015 
00016 
00017 import re
00018 
00019 import obnamlib
00020 
00021 
00022 class UnitError(obnamlib.Error):
00023 
00024     def __str__(self):
00025         return self.msg
00026 
00027 
00028 class SizeSyntaxError(UnitError):
00029 
00030     def __init__(self, string):
00031         self.msg = '"%s" is not a valid size' % string
00032 
00033 
00034 class UnitNameError(UnitError):
00035 
00036     def __init__(self, string):
00037         self.msg = '"%s" is not a valid unit' % string
00038 
00039 
00040 class ByteSizeParser(object):
00041 
00042     '''Parse sizes of data in bytes, kilobytes, kibibytes, etc.'''
00043     
00044     pat = re.compile(r'^(?P<size>\d+(\.\d+)?)\s*'
00045                      r'(?P<unit>[kmg]?i?b?)?$', re.I)
00046     
00047     units = {
00048         'b': 1,
00049         'k': 1000,
00050         'kb': 1000,
00051         'kib': 1024,
00052         'm': 1000**2,
00053         'mb': 1000**2,
00054         'mib': 1024**2,
00055         'g': 1000**3,
00056         'gb': 1000**3,
00057         'gib': 1024**3,
00058     }
00059     
00060     def __init__(self):
00061         self.set_default_unit('B')
00062         
00063     def set_default_unit(self, unit):
00064         if unit.lower() not in self.units:
00065             raise UnitNameError(unit)
00066         self.default_unit = unit
00067         
00068     def parse(self, string):
00069         m = self.pat.match(string)
00070         if not m:
00071             raise SizeSyntaxError(string)
00072         size = float(m.group('size'))
00073         unit = m.group('unit')
00074         if not unit:
00075             unit = self.default_unit
00076         elif unit.lower() not in self.units:
00077             raise UnitNameError(unit)
00078         factor = self.units[unit.lower()]
00079         return int(size * factor)
00080