aboutsummaryrefslogtreecommitdiffstats
path: root/common.py
diff options
context:
space:
mode:
authorGuilhem Moulin <guilhem@fripost.org>2024-06-03 16:06:25 +0200
committerGuilhem Moulin <guilhem@fripost.org>2024-06-03 16:17:15 +0200
commit60fd075506093d94107515788511f4a56f876817 (patch)
tree263edcf772d133b97c6d01215858cf0b643a8ce3 /common.py
parent675eadfc7f2a82b9e0f2dbf24e3cb8315c73356a (diff)
webmap-download: Move format_bytes() and format_time() to common.py.
Diffstat (limited to 'common.py')
-rw-r--r--common.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/common.py b/common.py
index 00d1add..0036df4 100644
--- a/common.py
+++ b/common.py
@@ -4,6 +4,7 @@ from fnmatch import fnmatchcase
from pathlib import Path, PosixPath
from urllib.parse import urlparse, urlunparse
from stat import S_ISDIR
+from math import modf
from xdg.BaseDirectory import xdg_config_home
import logging
import yaml
@@ -118,6 +119,22 @@ def load_config(path=None, groupnames=None):
config['layers'] = layers
sys.modules[__name__].config = config
+def format_bytes(n):
+ if n < 768:
+ return f'{n}B'
+ elif n < 768*1024:
+ return f'{n/1024:.2f}kiB'
+ elif n < 768*1024*1024:
+ return f'{n/1048576:.2f}MiB'
+ else:
+ return f'{n/1073741824:.2f}GiB'
+
+def format_time(s):
+ fs, s = modf(s)
+ m, s = divmod(int(s), 60)
+ h, m = divmod(m, 60)
+ return f'{h:02d}:{m:02d}:{s + fs:06.3f}'
+
######
# The function definitions below are taken from cpython's source code