[utils] Move FileDownloader.parse_bytes into utils

This commit is contained in:
pukkandan
2022-11-17 08:40:34 +05:30
parent 4de88a6a36
commit 64c464a144
3 changed files with 25 additions and 21 deletions

View File

@@ -15,7 +15,6 @@ from ..minicurses import (
from ..utils import (
IDENTITY,
NO_DEFAULT,
NUMBER_RE,
LockingUnsupportedError,
Namespace,
RetryManager,
@@ -24,6 +23,7 @@ from ..utils import (
encodeFilename,
format_bytes,
join_nonempty,
parse_bytes,
remove_start,
sanitize_open,
shell_quote,
@@ -180,12 +180,7 @@ class FileDownloader:
@staticmethod
def parse_bytes(bytestr):
"""Parse a string indicating a byte quantity into an integer."""
matchobj = re.match(rf'(?i)^({NUMBER_RE})([kMGTPEZY]?)$', bytestr)
if matchobj is None:
return None
number = float(matchobj.group(1))
multiplier = 1024.0 ** 'bkmgtpezy'.index(matchobj.group(2).lower())
return int(round(number * multiplier))
parse_bytes(bytestr)
def slow_down(self, start_time, now, byte_counter):
"""Sleep if the download speed is over the rate limit."""