Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_guess_mimetype(self):
"""Tests utils.guess_mimetype().
Guesses the mimetype of the requirements.txt file
located in the project's root directory.
"""
path = os.path.join(os.path.dirname(__file__),
"..", "..", "requirements.txt")
assert utils.guess_mimetype(path) == "text/plain"
def test_clean_file_unopened_textpath(self):
"""Tests utils.clean_file() with a text string filepath.
This test relies on the openability of the file 'fsdfgh'
located in 'test/functional/fake_dir'.
"""
path = os.path.dirname(__file__)
if isinstance(path, six.binary_type): #PY2
path = path.decode(sys.getfilesystemencoding())
path = os.path.join(path, u"..", u"functional", u"fake_dir", u"fsdfgh")
f, opened = utils.clean_file(path)
assert hasattr(f, 'read')
assert opened
# Closing file after test assertions.
f.close()
{'Content-Type': 'text/plain'}
>>> content_type_headers('example.jpeg')
{'Content-Type': 'image/jpeg'}
>>> content_type_headers('example')
{'Content-Type': 'application/octet-stream'}
Parameters
----------
filename : str
Filename to guess the content-type for
content_type : str
The Content-Type to use; if not set a content type will be guessed
"""
return {'Content-Type': content_type if content_type else utils.guess_mimetype(filename)}
def __init__(self, files, name="files", chunk_size=default_chunk_size):
self.files = utils.clean_files(files)
super(FilesStream, self).__init__(name, chunk_size=chunk_size)
stream = DirectoryStream(directory,
recursive=recursive, patterns=patterns,
dirname=dirname, chunk_size=chunk_size)
return stream.body(), stream.headers()
# Note that `os.fwalk` is never available on Windows and Python 2
if hasattr(os, "fwalk") and not isinstance(directory, int): #PY3
def auto_close_iter_fd(fd, iter):
try:
#PY2: Use `yield from` instead
for item in iter:
yield item
finally:
os.close(fd)
directory_str = utils.convert_path(directory)
dirname = os.path.basename(os.path.normpath(directory_str))
fd = os.open(directory_str, os.O_CLOEXEC | os.O_DIRECTORY)
body, headers = stream_directory_impl(fd, dirname)
return auto_close_iter_fd(fd, body), headers
else:
return stream_directory_impl(directory)
Parameters
----------
filepaths : Union[str, bytes, os.PathLike, int, io.IOBase, collections.abc.Iterable]
The filepath of a single directory or one or more files to stream
recursive : bool
Stream all content within the directory recursively?
patterns : Union[str, re.compile, collections.abc.Iterable]
Single *glob* pattern or list of *glob* patterns and compiled
regular expressions to match the paths of files and directories
to be added to IPFS (directories only)
chunk_size : int
Maximum size of each stream chunk
"""
is_dir = False
if isinstance(filepaths, utils.path_types):
is_dir = os.path.isdir(utils.convert_path(filepaths))
elif isinstance(filepaths, int):
import stat
is_dir = stat.S_ISDIR(os.fstat(filepaths).st_mode)
if is_dir:
return stream_directory(filepaths, recursive, patterns, chunk_size) + (True,)
else:
return stream_files(filepaths, chunk_size) + (False,)