You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.0 KiB

import os
import socket
from pyrocko import util
op = os.path
DATA_DIR = op.join(op.dirname(op.abspath(__file__)), 'data')
os.makedirs(DATA_DIR, exist_ok=True)
class NoInternetConnection(Exception):
def _download_data(fn, dldir=False):
fpath = data_file_path(fn)
if not op.exists(fpath):
if not have_internet():
raise NoInternetConnection(
'need internet access to download data')
url = '' + fn
print('downloading %s' % url)
if dldir:
util.download_dir(url, fpath)
util.download_file(url, fpath)
return fpath
def download_dir(fn):
return _download_data(fn, dldir=True)
def download_file(fn):
return _download_data(fn)
def data_file_path(fn):
return op.join(DATA_DIR, fn)
def have_internet():
return 0 < len([
(s.connect(('', 80)), s.close())
for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]])
except OSError:
return False