-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfile_helper.py
More file actions
33 lines (24 loc) · 914 Bytes
/
file_helper.py
File metadata and controls
33 lines (24 loc) · 914 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import tempfile
import requests
class FileHelper:
"""A Helper Class for files.
Attributes:
cache (Set): Class variable which stores hashes of file URLs so we don't
download the same file twice in a test session.
"""
cache = {}
@classmethod
def get_file(cls, url):
"""Class method which takes a URL, downloads the file (if not
already downloaded for this test session) and returns a file object for
the file in read-binary mode.
Args:
url (string): The URL of the required file.
Returns:
FileObject: The file object of the required file (opened with "rb").
"""
if url not in cls.cache:
cls.cache[url] = tempfile.NamedTemporaryFile()
cls.cache[url].write(requests.get(url).content)
cls.cache[url].seek(0)
return cls.cache[url]