Pycrolis commited on
Commit
604617b
·
1 Parent(s): 83001c2

feat(tools): add utility to load file from URL or local path

Browse files
Files changed (1) hide show
  1. tools/load_file.py +22 -0
tools/load_file.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ from urllib.parse import urlparse
3
+
4
+ import requests
5
+
6
+
7
+ def load_file(path_or_url: str) -> io.BytesIO:
8
+ if _is_url(path_or_url):
9
+ response = requests.get(path_or_url)
10
+ response.raise_for_status()
11
+ return io.BytesIO(response.content)
12
+ else:
13
+ with open(path_or_url, 'rb') as f:
14
+ return io.BytesIO(f.read())
15
+
16
+
17
+ def _is_url(path_or_url):
18
+ try:
19
+ result = urlparse(path_or_url)
20
+ return result.scheme in ('http', 'https')
21
+ except ValueError:
22
+ return False