From 194dc0afb16827e2998a268309359a44ca9f98c8 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Sat, 23 Sep 2017 16:00:21 -0500 Subject: [PATCH] Add download_from_web_if_not_present --- pytools/__init__.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pytools/__init__.py b/pytools/__init__.py index 5752e53..87e7c1c 100644 --- a/pytools/__init__.py +++ b/pytools/__init__.py @@ -124,6 +124,11 @@ Name generation .. autofunction:: generate_unique_names .. autofunction:: generate_numbered_unique_names .. autofunction:: UniqueNameGenerator + +Auxiliary files +--------------- + +.. autofunction:: download_from_web_if_not_present """ @@ -1940,6 +1945,28 @@ class MinRecursionLimit(object): # }}} +# {{{ download from web if not present + +def download_from_web_if_not_present(url, local_name=None): + """ + .. versionadded:: 2017.5 + """ + + from os.path import basename, exists + if local_name is None: + local_name = basename(url) + + if not exists(local_name): + from six.moves.urllib.request import urlopen + with urlopen(url) as inf: + contents = inf.read() + + with open(local_name, "wb") as outf: + outf.write(contents) + +# }}} + + def _test(): import doctest doctest.testmod() -- GitLab