aboutsummaryrefslogtreecommitdiffstats
path: root/yt_dlp/utils.py
diff options
context:
space:
mode:
authorpukkandan <pukkandan.ytdlp@gmail.com>2022-11-17 08:40:34 +0530
committerpukkandan <pukkandan.ytdlp@gmail.com>2022-11-17 08:40:34 +0530
commit64c464a144e2a96ec21a717d191217edda9107a4 (patch)
tree57a33c2fb9d60444ba4f2edf9baca844225c38cb /yt_dlp/utils.py
parent4de88a6a362a6f976ebac5d384a79ca59606ec0a (diff)
downloadhypervideo-pre-64c464a144e2a96ec21a717d191217edda9107a4.tar.lz
hypervideo-pre-64c464a144e2a96ec21a717d191217edda9107a4.tar.xz
hypervideo-pre-64c464a144e2a96ec21a717d191217edda9107a4.zip
[utils] Move `FileDownloader.parse_bytes` into utils
Diffstat (limited to 'yt_dlp/utils.py')
-rw-r--r--yt_dlp/utils.py19
1 files changed, 14 insertions, 5 deletions
diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py
index 7cba13678..9b6977b6d 100644
--- a/yt_dlp/utils.py
+++ b/yt_dlp/utils.py
@@ -2289,15 +2289,24 @@ def format_bytes(bytes):
return format_decimal_suffix(bytes, '%.2f%sB', factor=1024) or 'N/A'
-def lookup_unit_table(unit_table, s):
+def lookup_unit_table(unit_table, s, strict=False):
+ num_re = NUMBER_RE if strict else NUMBER_RE.replace(R'\.', '[,.]')
units_re = '|'.join(re.escape(u) for u in unit_table)
- m = re.match(
- r'(?P<num>[0-9]+(?:[,.][0-9]*)?)\s*(?P<unit>%s)\b' % units_re, s)
+ m = (re.fullmatch if strict else re.match)(
+ rf'(?P<num>{num_re})\s*(?P<unit>{units_re})\b', s)
if not m:
return None
- num_str = m.group('num').replace(',', '.')
+
+ num = float(m.group('num').replace(',', '.'))
mult = unit_table[m.group('unit')]
- return int(float(num_str) * mult)
+ return round(num * mult)
+
+
+def parse_bytes(s):
+ """Parse a string indicating a byte quantity into an integer"""
+ return lookup_unit_table(
+ {u: 1024**i for i, u in enumerate(['', *'KMGTPEZY'])},
+ s.upper(), strict=True)
def parse_filesize(s):