|
import filecmp
|
|
import os
|
|
import shutil
|
|
import tempfile
|
|
from inspect import currentframe, getframeinfo
|
|
from pathlib import Path
|
|
|
|
|
|
def validate_files(paths):
|
|
valid_files = [path for path in paths if file_is_valid(path)]
|
|
return valid_files
|
|
|
|
|
|
def file_is_valid(path):
|
|
if path is not None and path.is_file():
|
|
size = path.stat().st_size
|
|
if size > 0:
|
|
return True
|
|
return False
|
|
|
|
|
|
def check_valid_path(path_str):
|
|
|
|
path = Path(path_str)
|
|
|
|
|
|
if not path.exists():
|
|
raise FileNotFoundError(f"The path '{path_str}' does not exist.")
|
|
|
|
|
|
if path.is_dir():
|
|
|
|
files_with_content = [file for file in path.iterdir() if file.is_file() and file.stat().st_size > 0]
|
|
if not files_with_content:
|
|
raise ValueError(f"The directory '{path_str}' does not contain any files with content.")
|
|
elif path.is_file():
|
|
|
|
if path.stat().st_size == 0:
|
|
raise ValueError(f"The file '{path_str}' does not contain any content.")
|
|
else:
|
|
raise ValueError(f"The path '{path_str}' is neither a valid directory nor a valid file.")
|
|
|
|
return path
|
|
|
|
|
|
def check_existing_path(path_str):
|
|
|
|
path = Path(path_str)
|
|
|
|
|
|
if not path.exists():
|
|
raise FileNotFoundError(f"The path '{path_str}' does not exist.")
|
|
|
|
|
|
if not path.is_dir() and not path.is_file():
|
|
raise ValueError(f"The path '{path_str}' is neither a valid directory nor a valid file.")
|
|
|
|
return path_str
|
|
|
|
|
|
class TempFile:
|
|
|
|
def __init__(self, final_path: Path, file_ext: str = None):
|
|
self.final_path: Path = None if final_path is None else Path(
|
|
final_path)
|
|
self.file_ext = file_ext
|
|
os.makedirs(Path(Path(getframeinfo(
|
|
currentframe()).filename).resolve().parent, "temp"), exist_ok=True)
|
|
self.temp_file: tempfile.NamedTemporaryFile = tempfile.NamedTemporaryFile(dir=Path(Path(getframeinfo(currentframe()).filename).resolve().parent, "temp"),
|
|
delete=False, suffix=file_ext)
|
|
|
|
self.temp_file_name = self.temp_file.name
|
|
self.temp_file_path: Path = Path(self.temp_file.name)
|
|
|
|
self.temp_file.close()
|
|
|
|
|
|
def getpath(self):
|
|
if self.temp_file_path.is_file():
|
|
return self.temp_file_path
|
|
elif file_is_valid(self.final_path):
|
|
return self.final_path
|
|
else:
|
|
return None
|
|
|
|
|
|
def getvalidpath(self):
|
|
if file_is_valid(self.temp_file_path):
|
|
return self.temp_file_path
|
|
elif file_is_valid(self.final_path):
|
|
return self.final_path
|
|
else:
|
|
return None
|
|
|
|
|
|
def save(self, overwrite_if_valid: bool = True, update_path: Path = None):
|
|
|
|
if update_path is None:
|
|
path: Path = self.final_path
|
|
else:
|
|
path: Path = update_path
|
|
|
|
try:
|
|
|
|
if not file_is_valid(self.final_path) or overwrite_if_valid:
|
|
os.makedirs(path.parent, exist_ok=True)
|
|
shutil.move(self.temp_file_path, path)
|
|
self.final_path = path
|
|
except Exception as e:
|
|
print(f"Error saving file: {e}")
|
|
return
|
|
|
|
|
|
def destroy(self):
|
|
try:
|
|
|
|
if file_is_valid(self.temp_file_path):
|
|
os.remove(self.temp_file_path)
|
|
except Exception:
|
|
return
|
|
|
|
|
|
def copy_file_if_different(src_file: Path, dst_file: Path, silent: bool = False):
|
|
if file_is_valid(dst_file):
|
|
|
|
if filecmp.cmp(src_file, dst_file) and not silent:
|
|
print(f"{dst_file} already exists and is the same. No need to copy.")
|
|
return
|
|
|
|
os.makedirs(dst_file.parent, exist_ok=True)
|
|
shutil.copyfile(src_file, dst_file)
|
|
if not silent:
|
|
print(f"copied to {dst_file}")
|
|
|
|
|
|
def delete_folder(path: Path):
|
|
if path.is_dir():
|
|
shutil.rmtree(path)
|
|
|
|
|
|
def update_folder_times(folder_path):
|
|
folder_path = Path(folder_path)
|
|
|
|
|
|
newest_file_time = None
|
|
|
|
for item in folder_path.iterdir():
|
|
|
|
if item.is_file():
|
|
file_time = item.stat().st_mtime
|
|
|
|
|
|
if newest_file_time is None or file_time > newest_file_time:
|
|
newest_file_time = file_time
|
|
|
|
|
|
elif item.is_dir():
|
|
subfolder_newest_time = update_folder_times(item)
|
|
|
|
|
|
if newest_file_time is None or (subfolder_newest_time is not None and subfolder_newest_time > newest_file_time):
|
|
newest_file_time = subfolder_newest_time
|
|
|
|
|
|
if newest_file_time is not None:
|
|
|
|
newest_file_time = int(newest_file_time)
|
|
os.utime(path=folder_path, times=(newest_file_time, newest_file_time))
|
|
|
|
return newest_file_time
|
|
|