Skip to content

Commit

Permalink
Split geneve.utils.download out of geneve.utils.resource
Browse files Browse the repository at this point in the history
  • Loading branch information
cavokz committed Nov 29, 2024
1 parent 7387191 commit b884f4f
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions geneve/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,30 @@ def expand_wildcards(s, alphabet, min_star_len, max_star_len):
return "".join(chars)


def download(uri, destdir, *, basedir=None, cachedir=None, cachefile=None, validate=None):
import requests

uri_parts = urlparse(str(uri))
if uri_parts.scheme.startswith("http"):
if cachedir and cachefile:
local_file = cachedir / cachefile
else:
local_file = Path(cachedir or destdir) / Path(uri_parts.path).name
if local_file.exists() and validate and not validate(local_file):
local_file.unlink()
if not local_file.exists():
local_file.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
with open(local_file, "wb") as f:
f.write(requests.get(uri).content)
elif uri_parts.scheme == "file":
local_file = Path(basedir or Path.cwd()) / (uri_parts.netloc + uri_parts.path)
elif uri_parts.scheme == "":
local_file = Path(basedir or Path.cwd()) / uri_parts.path
else:
raise ValueError(f"uri scheme not supported: {uri_parts.scheme}")
return local_file


@contextmanager
def tempdir():
tmpdir = mkdtemp()
Expand All @@ -65,28 +89,9 @@ def tempdir():

@contextmanager
def resource(uri, basedir=None, cachedir=None, cachefile=None, validate=None):
import requests

with tempdir() as tmpdir:
uri_parts = urlparse(str(uri))
if uri_parts.scheme.startswith("http"):
download_dir = Path(cachedir or tmpdir)
if cachedir and cachefile:
local_file = download_dir / cachefile
else:
local_file = download_dir / Path(uri_parts.path).name
if local_file.exists() and validate and not validate(local_file):
local_file.unlink()
if not local_file.exists():
download_dir.mkdir(parents=True, exist_ok=True, mode=0o700)
with open(local_file, "wb") as f:
f.write(requests.get(uri).content)
elif uri_parts.scheme == "file":
local_file = Path(basedir or Path.cwd()) / (uri_parts.netloc + uri_parts.path)
elif uri_parts.scheme == "":
local_file = Path(basedir or Path.cwd()) / uri_parts.path
else:
raise ValueError(f"uri scheme not supported: {uri_parts.scheme}")
local_file = download(uri, tmpdir, basedir=basedir, cachedir=cachedir, cachefile=cachefile, validate=validate)

if local_file.is_dir():
tmpdir = local_file
Expand Down

0 comments on commit b884f4f

Please sign in to comment.