Add BytestringException, ParseError.
Allows for more granular catching on the caller side.
This commit is contained in:
parent
7be7c2c908
commit
8a0ed3d131
1 changed files with 19 additions and 8 deletions
|
@ -41,6 +41,11 @@ UNIT_STRINGS = {
|
||||||
REVERSED_UNIT_STRINGS = {value: key for (key, value) in UNIT_STRINGS.items()}
|
REVERSED_UNIT_STRINGS = {value: key for (key, value) in UNIT_STRINGS.items()}
|
||||||
UNIT_SIZES = sorted(UNIT_STRINGS.keys(), reverse=True)
|
UNIT_SIZES = sorted(UNIT_STRINGS.keys(), reverse=True)
|
||||||
|
|
||||||
|
class BytestringException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class ParseError(BytestringException, ValueError):
|
||||||
|
pass
|
||||||
|
|
||||||
def bytestring(size, decimal_places=3, force_unit=None):
|
def bytestring(size, decimal_places=3, force_unit=None):
|
||||||
'''
|
'''
|
||||||
|
@ -108,7 +113,7 @@ def normalize_unit_string(string):
|
||||||
unit_string_l = unit_string.lower()
|
unit_string_l = unit_string.lower()
|
||||||
if string in (unit_string_l, unit_string_l[0], unit_string_l.replace('i', '')):
|
if string in (unit_string_l, unit_string_l[0], unit_string_l.replace('i', '')):
|
||||||
return unit_string
|
return unit_string
|
||||||
raise ValueError(f'Unrecognized unit string "{string}".')
|
raise ParseError(f'Unrecognized unit string "{string}".')
|
||||||
|
|
||||||
def parsebytes(string):
|
def parsebytes(string):
|
||||||
'''
|
'''
|
||||||
|
@ -120,20 +125,26 @@ def parsebytes(string):
|
||||||
|
|
||||||
matches = re.findall(r'[\d\.-]+', string)
|
matches = re.findall(r'[\d\.-]+', string)
|
||||||
if len(matches) == 0:
|
if len(matches) == 0:
|
||||||
raise ValueError('No numbers found.')
|
raise ParseError('No numbers found.')
|
||||||
if len(matches) > 1:
|
if len(matches) > 1:
|
||||||
raise ValueError('Too many numbers found.')
|
raise ParseError('Too many numbers found.')
|
||||||
number = matches[0]
|
number = matches[0]
|
||||||
|
|
||||||
if not string.startswith(number):
|
if not string.startswith(number):
|
||||||
raise ValueError('Number is not at start of string.')
|
raise ParseError('Number is not at start of string.')
|
||||||
|
|
||||||
# if the string has no text besides the number, return that int of bytes.
|
number_string = number
|
||||||
unit_string = string.replace(number, '')
|
|
||||||
|
try:
|
||||||
|
number = float(number)
|
||||||
|
except ValueError as exc:
|
||||||
|
raise ParseError(number) from exc
|
||||||
|
|
||||||
|
# if the string has no text besides the number, treat it as int of bytes.
|
||||||
|
unit_string = string.replace(number_string, '')
|
||||||
if unit_string == '':
|
if unit_string == '':
|
||||||
return int(float(number))
|
return int(number)
|
||||||
|
|
||||||
number = float(number)
|
|
||||||
unit_string = normalize_unit_string(unit_string)
|
unit_string = normalize_unit_string(unit_string)
|
||||||
multiplier = REVERSED_UNIT_STRINGS[unit_string]
|
multiplier = REVERSED_UNIT_STRINGS[unit_string]
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue