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__ = [
|
|
|
|
"get_base_dir",
|
2018-11-27 09:52:27 +01:00
|
|
|
"move",
|
|
|
|
"rename",
|
2018-11-24 09:27:33 +01:00
|
|
|
"stream_to_path",
|
|
|
|
"OutOfTriesException",
|
|
|
|
"UnknownFileTypeException",
|
|
|
|
"FileNotFoundException",
|
|
|
|
]
|
|
|
|
|
|
|
|
def get_base_dir(script_file):
|
2018-11-26 14:39:06 +01: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):
|
|
|
|
l = len(from_folders)
|
|
|
|
if path.parts[:l] == from_folders:
|
|
|
|
return pathlib.PurePath(*to_folders, *path.parts[l:])
|
|
|
|
|
|
|
|
def rename(path, to_name):
|
|
|
|
return pathlib.PurePath(*path.parts[:-1], to_name)
|
|
|
|
|
2018-11-24 09:27:33 +01:00
|
|
|
async def stream_to_path(resp, to_path, chunk_size=1024**2):
|
|
|
|
with open(to_path, 'wb') as fd:
|
|
|
|
while True:
|
|
|
|
chunk = await resp.content.read(chunk_size)
|
|
|
|
if not chunk:
|
|
|
|
break
|
|
|
|
fd.write(chunk)
|
|
|
|
|
|
|
|
class OutOfTriesException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class UnknownFileTypeException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class FileNotFoundException(Exception):
|
|
|
|
pass
|