aboutsummaryrefslogtreecommitdiffstats
path: root/webmap-download
diff options
context:
space:
mode:
Diffstat (limited to 'webmap-download')
-rwxr-xr-xwebmap-download62
1 files changed, 1 insertions, 61 deletions
diff --git a/webmap-download b/webmap-download
index 2c475fe..5e191ad 100755
--- a/webmap-download
+++ b/webmap-download
@@ -32,8 +32,6 @@ from os import (
O_TMPFILE,
path as os_path,
curdir as os_curdir,
- pardir as os_pardir,
- sep as os_sep
)
import os
import sys
@@ -48,7 +46,7 @@ from typing import Optional, NoReturn, Never
import requests
import common
-from common import BadConfiguration, getSourcePathLockFileName
+from common import parse_config_dl, getSourcePathLockFileName
def download_trystream(url : str, **kwargs) -> requests.Response:
"""GET a url, trying a number of times. Return immediately after the
@@ -167,64 +165,6 @@ def download(dest : str,
common.format_time(elapsed),
common.format_bytes(int(size/elapsed)))
-def _check_key_type(k : str, v : str, known_keys : list[type, tuple[set[str]]]) -> bool:
- for t, ks in known_keys:
- if k in ks and isinstance(v, t):
- return True
- return False
-
-def parse_config_dl(downloads) -> dict[str, dict[str, str|int]]:
- """Parse and validate the "downloads" section from the configuration dictionary"""
-
- if not isinstance(downloads, list):
- raise BadConfiguration(f'Invalid download recipe: {downloads}')
-
- known_keys = [
- (str, {'path', 'url'}),
- (int, {'max-age', 'max-size'})
- ]
-
- destinations = {}
- known_keys_set = {k for _,ks in known_keys for k in ks}
- for dl in downloads:
- if 'url' in dl:
- dls = [dl]
- elif 'basedir' in dl and 'baseurl' in dl and 'files' in dl and 'path' not in dl:
- dls = []
- for filename in dl['files']:
- dl2 = {
- 'path' : os_path.join(dl['basedir'], filename),
- 'url' : dl['baseurl'] + filename
- }
- for k, v in dl.items():
- if k not in ('basedir', 'baseurl', 'files'):
- dl2[k] = v
- dls.append(dl2)
- else:
- raise BadConfiguration(f'Invalid download recipe: {dl}')
-
- for dl in dls:
- path = dl.get('path', None)
- if path is None or path in ('', os_curdir, os_pardir) or path.endswith(os_sep):
- raise BadConfiguration(f'Invalid destination path "{path}"')
- if path in destinations:
- raise BadConfiguration(f'Duplicate download recipe for "{path}"')
- dl2 = {}
- for k, v in dl.items():
- if k == 'path':
- continue
- if k not in known_keys_set:
- logging.warning('Ignoring unknown setting "%s" in download recipe for "%s"',
- k, path)
- elif not _check_key_type(k, v, known_keys):
- logging.warning('Ignoring setting "%s" in download recipe for "%s"'
- ' (invalid type)', k, path)
- else:
- dl2[k] = v
- destinations[path] = dl2
-
- return destinations
-
# pylint: disable-next=missing-function-docstring
def main() -> NoReturn:
common.init_logger(app=os_path.basename(__file__), level=logging.INFO)