2018-11-24 09:27:33 +01:00
|
|
|
import os
|
2018-11-26 14:39:06 +01:00
|
|
|
import pathlib
|
2018-11-24 09:27:33 +01:00
|
|
|
|
|
|
|
__all__ = [
|
2019-04-24 14:34:20 +02:00
|
|
|
"get_base_dir",
|
|
|
|
"move",
|
|
|
|
"rename",
|
|
|
|
"stream_to_path",
|
2019-04-25 20:52:48 +02:00
|
|
|
"ContentTypeException",
|
2019-04-24 14:34:20 +02:00
|
|
|
"FileNotFoundException",
|
2018-11-24 09:27:33 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
def get_base_dir(script_file):
|
2019-04-24 14:34:20 +02:00
|
|
|
return pathlib.Path(os.path.dirname(os.path.abspath(script_file)))
|
2018-11-24 09:27:33 +01:00
|
|
|
|
2018-11-27 09:52:27 +01:00
|
|
|
def move(path, from_folders, to_folders):
|
2019-04-24 14:34:20 +02:00
|
|
|
l = len(from_folders)
|
|
|
|
if path.parts[:l] == from_folders:
|
|
|
|
return pathlib.PurePath(*to_folders, *path.parts[l:])
|
2018-11-27 09:52:27 +01:00
|
|
|
|
|
|
|
def rename(path, to_name):
|
2019-04-24 14:34:20 +02:00
|
|
|
return pathlib.PurePath(*path.parts[:-1], to_name)
|
2018-11-27 09:52:27 +01:00
|
|
|
|
2019-04-25 20:52:48 +02:00
|
|
|
def stream_to_path(response, to_path, chunk_size=1024**2):
|
2019-04-24 14:34:20 +02:00
|
|
|
with open(to_path, 'wb') as fd:
|
2019-04-25 20:52:48 +02:00
|
|
|
for chunk in response.iter_content(chunk_size=chunk_size):
|
2019-04-24 14:34:20 +02:00
|
|
|
fd.write(chunk)
|
2018-11-24 09:27:33 +01:00
|
|
|
|
2019-04-25 20:52:48 +02:00
|
|
|
class ContentTypeException(Exception):
|
2019-04-24 14:34:20 +02:00
|
|
|
pass
|
2018-11-24 09:27:33 +01:00
|
|
|
|
|
|
|
class FileNotFoundException(Exception):
|
2019-04-24 14:34:20 +02:00
|
|
|
pass
|