66import requests
77
88
9- DB_INDEX_URL = 'http://physionet.org/physiobank/database/'
9+ # The physiobank index url
10+ PB_INDEX_URL = 'http://physionet.org/physiobank/database/'
11+
12+ class Config (object ):
13+ pass
14+
15+ # The configuration database index url. Uses physiobank index by default.
16+ config = Config ()
17+ config .db_index_url = PB_INDEX_URL
18+
19+
20+ def set_db_index_url (db_index_url = PB_INDEX_URL ):
21+ """
22+ Set the database index url to a custom value, to stream remote
23+ files from another location.
24+
25+ Parameters
26+ ----------
27+ db_index_url : str, optional
28+ The desired new database index url. Leave as default to reset
29+ to the physiobank index url.
30+
31+ """
32+ config .db_index_url = db_index_url
1033
1134
1235def _remote_file_size (url = None , file_name = None , pb_dir = None ):
@@ -34,7 +57,7 @@ def _remote_file_size(url=None, file_name=None, pb_dir=None):
3457
3558 # Option to construct the url
3659 if file_name and pb_dir :
37- url = posixpath .join (DB_INDEX_URL , pb_dir , file_name )
60+ url = posixpath .join (config . db_index_url , pb_dir , file_name )
3861
3962 response = requests .head (url , headers = {'Accept-Encoding' : 'identity' })
4063 # Raise HTTPError if invalid url
@@ -60,7 +83,7 @@ def _stream_header(file_name, pb_dir):
6083
6184 """
6285 # Full url of header location
63- url = posixpath .join (DB_INDEX_URL , pb_dir , file_name )
86+ url = posixpath .join (config . db_index_url , pb_dir , file_name )
6487 response = requests .get (url )
6588
6689 # Raise HTTPError if invalid url
@@ -117,7 +140,7 @@ def _stream_dat(file_name, pb_dir, byte_count, start_byte, dtype):
117140 """
118141
119142 # Full url of dat file
120- url = posixpath .join (DB_INDEX_URL , pb_dir , file_name )
143+ url = posixpath .join (config . db_index_url , pb_dir , file_name )
121144
122145 # Specify the byte range
123146 end_byte = start_byte + byte_count - 1
@@ -149,7 +172,7 @@ def _stream_annotation(file_name, pb_dir):
149172
150173 """
151174 # Full url of annotation file
152- url = posixpath .join (DB_INDEX_URL , pb_dir , file_name )
175+ url = posixpath .join (config . db_index_url , pb_dir , file_name )
153176
154177 # Get the content
155178 response = requests .get (url )
@@ -171,7 +194,7 @@ def get_dbs():
171194 >>> dbs = get_dbs()
172195
173196 """
174- url = posixpath .join (DB_INDEX_URL , 'DBS' )
197+ url = posixpath .join (config . db_index_url , 'DBS' )
175198 response = requests .get (url )
176199
177200 dbs = response .content .decode ('ascii' ).splitlines ()
@@ -201,7 +224,7 @@ def get_record_list(db_dir, records='all'):
201224
202225 """
203226 # Full url physiobank database
204- db_url = posixpath .join (DB_INDEX_URL , db_dir )
227+ db_url = posixpath .join (config . db_index_url , db_dir )
205228
206229 # Check for a RECORDS file
207230 if records == 'all' :
@@ -221,7 +244,7 @@ def get_record_list(db_dir, records='all'):
221244def get_annotators (db_dir , annotators ):
222245
223246 # Full url physiobank database
224- db_url = posixpath .join (DB_INDEX_URL , db_dir )
247+ db_url = posixpath .join (config . db_index_url , db_dir )
225248
226249 if annotators is not None :
227250 # Check for an ANNOTATORS file
@@ -283,7 +306,7 @@ def dl_pb_file(inputs):
283306 basefile , subdir , db , dl_dir , keep_subdirs , overwrite = inputs
284307
285308 # Full url of file
286- url = posixpath .join (DB_INDEX_URL , db , subdir , basefile )
309+ url = posixpath .join (config . db_index_url , db , subdir , basefile )
287310
288311 # Supposed size of the file
289312 remote_file_size = _remote_file_size (url )
@@ -381,7 +404,7 @@ def dl_files(db, dl_dir, files, keep_subdirs=True, overwrite=False):
381404 """
382405
383406 # Full url physiobank database
384- db_url = posixpath .join (DB_INDEX_URL , db )
407+ db_url = posixpath .join (config . db_index_url , db )
385408 # Check if the database is valid
386409 response = requests .get (db_url )
387410 response .raise_for_status ()
0 commit comments