mirror of
https://github.com/RedDeadDepresso/KKAFIO.git
synced 2025-12-24 02:10:00 +00:00
fix: checkboxes changes saved
This commit is contained in:
@@ -14,98 +14,98 @@ class FileManager:
|
||||
def __init__(self, config):
|
||||
self.config = config
|
||||
|
||||
def find_all_files(self, directory):
|
||||
file_list = []
|
||||
compressed_file_list = []
|
||||
compressed_extensions = [".rar", ".zip", ".7z"]
|
||||
def findAllFiles(self, directory):
|
||||
fileList = []
|
||||
archiveList = []
|
||||
archiveExtensions = [".rar", ".zip", ".7z"]
|
||||
|
||||
for root, dirs, files in os.walk(directory):
|
||||
for file in files:
|
||||
|
||||
file_path = os.path.join(root, file)
|
||||
file_size = os.path.getsize(file_path)
|
||||
_, file_extension = os.path.splitext(file_path)
|
||||
filePath = os.path.join(root, file)
|
||||
fileSize = os.path.getsize(filePath)
|
||||
_, fileExtension = os.path.splitext(filePath)
|
||||
|
||||
if file_extension in compressed_extensions:
|
||||
compressed_file_list.append((file_path, file_size, file_extension))
|
||||
if fileExtension in archiveExtensions:
|
||||
archiveList.append((filePath, fileSize, fileExtension))
|
||||
else:
|
||||
file_list.append((file_path, file_size, file_extension))
|
||||
fileList.append((filePath, fileSize, fileExtension))
|
||||
|
||||
file_list.sort(key=lambda x: x[1])
|
||||
compressed_file_list.sort(key=lambda x: x[1])
|
||||
return file_list, compressed_file_list
|
||||
fileList.sort(key=lambda x: x[1])
|
||||
archiveList.sort(key=lambda x: x[1])
|
||||
return fileList, archiveList
|
||||
|
||||
def copy_and_paste(self, type, source_path, destination_folder):
|
||||
source_path = source_path[0]
|
||||
base_name = os.path.basename(source_path)
|
||||
destination_path = os.path.join(destination_folder, base_name)
|
||||
def copyAndPaste(self, type, sourcePath, destinationFolder):
|
||||
sourcePath = sourcePath[0]
|
||||
baseName = os.path.basename(sourcePath)
|
||||
destinationPath = os.path.join(destinationFolder, baseName)
|
||||
conflicts = self.config.install_chara["FileConflicts"]
|
||||
already_exists = os.path.exists(destination_path)
|
||||
alreadyExists = os.path.exists(destinationPath)
|
||||
|
||||
if already_exists and conflicts == "Skip":
|
||||
logger.log_skipped(type, base_name)
|
||||
if alreadyExists and conflicts == "Skip":
|
||||
logger.skipped(type, baseName)
|
||||
return
|
||||
|
||||
elif already_exists and conflicts == "Replace":
|
||||
logger.log_replaced(type, base_name)
|
||||
elif alreadyExists and conflicts == "Replace":
|
||||
logger.replaced(type, baseName)
|
||||
|
||||
elif already_exists and conflicts == "Rename":
|
||||
max_retries = 3
|
||||
for attempt in range(max_retries):
|
||||
elif alreadyExists and conflicts == "Rename":
|
||||
maxRetries = 3
|
||||
for attempt in range(maxRetries):
|
||||
try:
|
||||
filename, file_extension = os.path.splitext(source_path)
|
||||
new_name = datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')
|
||||
new_source_path = f"{filename}_{new_name}{file_extension}"
|
||||
os.rename(source_path, new_source_path)
|
||||
source_path = new_source_path
|
||||
logger.log_renamed(type, base_name)
|
||||
filename, fileExtension = os.path.splitext(sourcePath)
|
||||
newName = datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')
|
||||
newSourcePath = f"{filename}_{newName}{fileExtension}"
|
||||
os.rename(sourcePath, newSourcePath)
|
||||
sourcePath = newSourcePath
|
||||
logger.renamed(type, baseName)
|
||||
|
||||
filename, file_extension = os.path.splitext(destination_path)
|
||||
destination_path = f"{filename}_{new_name}{file_extension}"
|
||||
filename, fileExtension = os.path.splitext(destinationPath)
|
||||
destinationPath = f"{filename}_{newName}{fileExtension}"
|
||||
break # Exit the loop if renaming is successful
|
||||
except PermissionError:
|
||||
if attempt < max_retries - 1:
|
||||
if attempt < maxRetries - 1:
|
||||
time.sleep(1) # Wait for 1 second before retrying
|
||||
else:
|
||||
logger.log_error(type, f"Failed to rename {base_name} after {max_retries} attempts.")
|
||||
logger.error(type, f"Failed to rename {baseName} after {maxRetries} attempts.")
|
||||
return
|
||||
|
||||
try:
|
||||
shutil.copy(source_path, destination_path)
|
||||
print(f"File copied successfully from {source_path} to {destination_path}")
|
||||
if not already_exists:
|
||||
logger.log_success(type, base_name)
|
||||
shutil.copy(sourcePath, destinationPath)
|
||||
print(f"File copied successfully from {sourcePath} to {destinationPath}")
|
||||
if not alreadyExists:
|
||||
logger.success(type, baseName)
|
||||
except FileNotFoundError:
|
||||
logger.log_error(type, f"{base_name} does not exist.")
|
||||
logger.error(type, f"{baseName} does not exist.")
|
||||
except PermissionError:
|
||||
logger.log_error(type, f"Permission denied for {base_name}")
|
||||
logger.error(type, f"Permission denied for {baseName}")
|
||||
except Exception as e:
|
||||
logger.log_error(type, f"An error occurred: {e}")
|
||||
logger.error(type, f"An error occurred: {e}")
|
||||
|
||||
def find_and_remove(self, type, source_path, destination_folder):
|
||||
source_path = source_path[0]
|
||||
base_name = os.path.basename(source_path)
|
||||
destination_path = os.path.join(destination_folder, base_name)
|
||||
if os.path.exists(destination_path):
|
||||
def findAndRemove(self, type, sourcePath, destinationFolder):
|
||||
sourcePath = sourcePath[0]
|
||||
baseName = os.path.basename(sourcePath)
|
||||
destinationPath = os.path.join(destinationFolder, baseName)
|
||||
if os.path.exists(destinationPath):
|
||||
try:
|
||||
os.remove(destination_path)
|
||||
logger.log_removed(type, base_name)
|
||||
os.remove(destinationPath)
|
||||
logger.removed(type, baseName)
|
||||
except OSError as e:
|
||||
logger.log_error(type, base_name)
|
||||
logger.error(type, baseName)
|
||||
|
||||
def create_archive(self, folders, archive_path):
|
||||
def createArchive(self, folders, archivePath):
|
||||
# Specify the full path to the 7zip executable
|
||||
path_to_7zip = patoolib.util.find_program("7z")
|
||||
if not path_to_7zip:
|
||||
logger.log_error("SCRIPT", "7zip not found. Unable to create backup")
|
||||
pathTo7zip = patoolib.util.find_program("7z")
|
||||
if not pathTo7zip:
|
||||
logger.error("SCRIPT", "7zip not found. Unable to create backup")
|
||||
raise Exception()
|
||||
|
||||
if os.path.exists(archive_path+".7z"):
|
||||
os.remove(archive_path+".7z")
|
||||
if os.path.exists(archivePath+".7z"):
|
||||
os.remove(archivePath+".7z")
|
||||
|
||||
path_to_7zip = f'"{path_to_7zip}"'
|
||||
archive_path = f'"{archive_path}"'
|
||||
exclude_folders = [
|
||||
pathTo7zip = f'"{pathTo7zip}"'
|
||||
archivePath = f'"{archivePath}"'
|
||||
excludeFolders = [
|
||||
'"Sideloader Modpack"',
|
||||
'"Sideloader Modpack - Studio"',
|
||||
'"Sideloader Modpack - KK_UncensorSelector"',
|
||||
@@ -118,52 +118,52 @@ class FileManager:
|
||||
]
|
||||
|
||||
# Create a string of folder names to exclude
|
||||
exclude_string = ''
|
||||
for folder in exclude_folders:
|
||||
exclude_string += f'-xr!{folder} '
|
||||
excludeString = ''
|
||||
for folder in excludeFolders:
|
||||
excludeString += f'-xr!{folder} '
|
||||
|
||||
# Create a string of folder names to include
|
||||
include_string = ''
|
||||
includeString = ''
|
||||
for folder in folders:
|
||||
include_string += f'"{folder}" '
|
||||
includeString += f'"{folder}" '
|
||||
|
||||
# Construct the 7zip command
|
||||
command = f'{path_to_7zip} a -t7z {archive_path} {include_string} {exclude_string}'
|
||||
command = f'{pathTo7zip} a -t7z {archivePath} {includeString} {excludeString}'
|
||||
# Call the command
|
||||
process = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
|
||||
# Print the output
|
||||
for line in process.stdout.decode().split('\n'):
|
||||
if line.strip() != "":
|
||||
logger.log_info("7-Zip", line)
|
||||
logger.info("7-Zip", line)
|
||||
# Check the return code
|
||||
if process.returncode not in [0, 1]:
|
||||
raise Exception()
|
||||
|
||||
def extract_archive(self, archive_path):
|
||||
def extractArchive(self, archivePath):
|
||||
try:
|
||||
archive_name = os.path.basename(archive_path)
|
||||
logger.log_info("ARCHIVE", f"Extracting {archive_name}")
|
||||
extract_path = os.path.join(f"{os.path.splitext(archive_path)[0]}_{datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')}")
|
||||
patoolib.extract_archive(archive_path, outdir=extract_path)
|
||||
return extract_path
|
||||
archiveName = os.path.basename(archivePath)
|
||||
logger.info("ARCHIVE", f"Extracting {archiveName}")
|
||||
extractPath = os.path.join(f"{os.path.splitext(archivePath)[0]}_{datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')}")
|
||||
patoolib.extract_archive(archivePath, outdir=extractPath)
|
||||
return extractPath
|
||||
|
||||
except patoolib.util.PatoolError as e:
|
||||
text = f"There is an error with the archive {archive_name} but it is impossible to detect the cause. Maybe it requires a password?"
|
||||
text = f"There is an error with the archive {archiveName} but it is impossible to detect the cause. Maybe it requires a password?"
|
||||
while self.config.install_chara["Password"] == "Request Password":
|
||||
try:
|
||||
dialog = customtkinter.CTkInputDialog(text=text, title="Enter Password")
|
||||
password = dialog.get_input()
|
||||
|
||||
if password is not None or "":
|
||||
patoolib.extract_archive(archive_path, outdir=extract_path, password=password)
|
||||
return extract_path
|
||||
patoolib.extract_archive(archivePath, outdir=extractPath, password=password)
|
||||
return extractPath
|
||||
else:
|
||||
break
|
||||
except:
|
||||
text = f"Wrong password or {archive_name} is corrupted. Please enter password again or click Cancel"
|
||||
text = f"Wrong password or {archiveName} is corrupted. Please enter password again or click Cancel"
|
||||
|
||||
logger.log_skipped("ARCHIVE", archive_name)
|
||||
logger.skipped("ARCHIVE", archiveName)
|
||||
|
||||
|
||||
fileManager = FileManager()
|
||||
Reference in New Issue
Block a user