Skip to content
Snippets Groups Projects
Commit 5bf5ee39 authored by Jan Mach's avatar Jan Mach
Browse files

Redesigned the babel_format_bytes() localization method.

The method for outputting the byte sizes in human readable and localized strings was redesigned.
parent 286918d6
No related branches found
No related tags found
No related merge requests found
......@@ -705,42 +705,30 @@ def _setup_app_babel(app):
flask.g.locale = flask.session.get('locale', get_locale())
flask.g.timezone = flask.session.get('timezone', get_timezone())
def babel_format_bytes(size, unit = 'B'):
def babel_format_bytes(size, unit = 'B', step_size = 1024):
"""
Format given numeric value to human readable string describing size in
B/KB/MB/GB/TB.
:param dict baseparams: Original query parameters.
:param dict updates: Updates for query parameters.
:return: Deep copy of original parameters modified with given updates.
:rtype: dict
"""
while True:
if unit == 'B':
if size > 1024:
size = size / 1024
unit = 'KB'
else:
break
if unit == 'KB':
if size > 1024:
size = size / 1024
unit = 'MB'
else:
break
if unit == 'MB':
if size > 1024:
size = size / 1024
unit = 'GB'
else:
break
if unit == 'GB':
if size > 1024:
size = size / 1024
unit = 'TB'
else:
break
break
:param int size: Number to be formatted.
:param enum unit: Starting unit, possible values are ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB'].
:param int step_size: Size of the step between units.
:return: Formatted and localized string.
:rtype: string
"""
units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB']
idx_max = len(units) - 1
unit = unit.upper()
for idx, val in enumerate(units):
# Skip the last step, there is no next unit defined after exabyte.
if idx == idx_max:
break
if size > step_size:
if unit == val:
size = size / step_size
unit = units[idx+1]
else:
break
return '{} {}'.format(
flask_babel.format_decimal(size),
unit
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment