Initial multiple repository support

And lots of code reshuffling which likely should've been done in
separate commits..
This commit is contained in:
Ellwood Zwovic
2017-08-12 19:36:39 -07:00
parent 7340d8dadb
commit 6691b0b226
8 changed files with 463 additions and 349 deletions

View File

@@ -4,6 +4,18 @@ import shutil
import logging
def format_filename(s: str, ext=None) -> str:
"""Take a string and turn it into a reasonable filename"""
import string
if ext is None:
ext = ""
valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
filename = ''.join(char for char in s if char in valid_chars)
filename = filename.replace(' ','_')
filename.lower()
filename += ext
return filename
def download(url: str, destination: Path, progress_callback=None) -> Path:
"""
Downloads file at the given url, and if progress_callback is specified,
@@ -17,12 +29,11 @@ def download(url: str, destination: Path, progress_callback=None) -> Path:
log = logging.getLogger('%s.download' % __name__)
if progress_callback is None:
# assing to do nothing function
# assign to do-nothing function
progress_callback = lambda x: None
progress_callback(0)
# derive filename from url if `destination` is an existing directory, otherwise use `destination` directly
if destination.is_dir():
# TODO: get filename from Content-Disposition header, if available.
@@ -35,10 +46,10 @@ def download(url: str, destination: Path, progress_callback=None) -> Path:
log.info('Downloading %s -> %s', url, local_fpath)
try:
resp = requests.get(url, stream=True, verify=True)
except requests.exceptions.RequestException as err:
raise exceptions.DownloadException(err) from err
# try:
resp = requests.get(url, stream=True, verify=True)
# except requests.exceptions.RequestException as err:
# raise exceptions.DownloadException(err) from err
try:
resp.raise_for_status()
@@ -187,3 +198,21 @@ def install(src_file: Path, dest_dir: Path):
raise exceptions.InstallException("Failed to copy file to '%s': %s" % (dest_dir, err)) from err
log.debug("Installation succeeded")
# def load_repository(repo_storage_path: Path, repo_name: str) -> Repository:
# """Loads <repo_name>.json from <repo_storage_path>"""
# pass
#
# def download_repository(repo_storage_path: Path, repo_name: str):
# """Loads <repo_name>.json from <repo_storage_path>"""
# pass
# this is done in Repository
def add_repojson_to_url(url: str) -> str:
"""Add `repo.json` to the path component of a url"""
from urllib.parse import urlsplit, urlunsplit
parsed_url = urlsplit(url)
new_path = parsed_url.path + "/repo.json"
return urlunsplit((parsed_url.scheme, parsed_url.netloc, new_path, parsed_url.query, parsed_url.fragment))