felicity-lims/felicity/utils/dirs.py

37 lines
844 B
Python
Raw Normal View History

2022-01-19 08:40:02 +08:00
from datetime import datetime
2022-11-06 20:09:44 +08:00
from distutils import dir_util
from pathlib import Path
2022-01-19 08:40:02 +08:00
2024-01-28 21:17:16 +08:00
def delete_file(file_name: str) -> bool:
2022-01-19 08:40:02 +08:00
file_path = Path(file_name)
if file_path.is_file():
file_path.unlink()
return True
# Also return true to allow deletion of image reference that no longer exists.
return True
def resolve_media_dirs_for(target: str) -> str:
"""
Creates directories if not exist
"""
2022-11-06 20:09:44 +08:00
str_path = "media/" + target + "/" + datetime.now().strftime("%Y/%m/%d") + "/"
2022-01-19 08:40:02 +08:00
path = Path(str_path)
if not path.is_dir():
dir_util.mkpath(str_path)
return str_path
def create_dir(dir_name: str):
path = Path(dir_name)
if not path.is_dir():
dir_util.mkpath(dir_name)
def resolve_root_dirs():
_dirs = ["media"]
for _dir in _dirs:
create_dir(_dir)