Skip to content

Commit 390411e

Browse files
author
Benjamin Moody
committed
openurl: support file:// URLs.
file:// URLs are not supported by python-requests, but it is easy enough to support them here, and can be useful for testing remote-file APIs using local data.
1 parent 649535d commit 390411e

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

wfdb/io/_url.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import io
22
import logging
3+
import os
34
import platform
45
import re
56
import threading
6-
import os
7+
import urllib.parse
8+
import urllib.request
79

810
from wfdb.version import __version__
911

@@ -722,6 +724,17 @@ def openurl(url, mode='r', *, buffering=-1,
722724
(io.BufferedIOBase) or text file API (io.TextIOBase).
723725
724726
"""
727+
(scheme, netloc, path, _, _, _) = urllib.parse.urlparse(url)
728+
if scheme == '':
729+
raise NetFileError('no scheme specified for URL: %r' % (url,), url=url)
730+
731+
if scheme == 'file':
732+
if netloc.lower() not in ('', 'localhost'):
733+
raise NetFileError('invalid file URL: %r' % (url,))
734+
local_path = urllib.request.url2pathname(path)
735+
return open(local_path, mode, buffering=buffering,
736+
encoding=encoding, errors=errors, newline=newline)
737+
725738
nf = NetFile(url, buffering=buffering)
726739

727740
if check_access:

0 commit comments

Comments
 (0)