diff --git a/main.pyw b/main.pyw index 021d7d0..028a145 100644 --- a/main.pyw +++ b/main.pyw @@ -47,8 +47,8 @@ def convert_filename(name): while '__' in name: name = name.replace('__', '_') name = re.sub(r'[^a-z0-9/._-]', '', name) - while name.endswith('.'): - name = name[:-1] + while name[:-4].endswith('.'): + name = name[:-5] + name[-4:] return name def from_files(sources, target): @@ -79,7 +79,7 @@ def create_from_files(sources, target): pack_png = os.path.join(os.path.dirname(sources[0]), 'pack.png') if os.path.exists(pack_png) and os.path.isfile(pack_png): shutil.copy(pack_png, os.path.join(target, 'pack.png')) - log('Copied "pack.png"') + log('Copied "pack.png".') # Copy music with open(sounds_target, 'r+') as sounds_file: @@ -107,7 +107,7 @@ def create_from_files(sources, target): else: # Copy file shutil.copy(file_path, target_file) - log('Copied "' + filename + '"') + log('Copied "' + filename + '".') continue log('Writing sounds.json...') @@ -157,7 +157,7 @@ def create_from_terraria(source, target): if os.path.exists(icon_source): icon_target = os.path.join(target, 'pack.png') shutil.copy(icon_source, icon_target) - log('Copied pack icon') + log('Copied pack icon.') # Copy music music_source = os.path.join(source, 'Content', 'Music') @@ -297,7 +297,7 @@ def main(): ''' Main function of the program. ''' - log('Program started') + log('Program started.') if sys.platform == 'win32': window.Application() else: diff --git a/window.py b/window.py index 72eafb8..142b1d5 100644 --- a/window.py +++ b/window.py @@ -6,7 +6,7 @@ import sys import shutil import json -from time import sleep +import time from urllib.error import URLError from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QWidget, QTabWidget, QHBoxLayout, QVBoxLayout, QLabel, QFileDialog, QStyle, QTextEdit, QGridLayout, QComboBox, QFrame, QLineEdit, QMessageBox, QScrollArea, QSizePolicy from PyQt6.QtGui import QIcon, QTextOption, QTextCursor @@ -80,20 +80,20 @@ def button_pushed(self): start_thread(target = self.fetch) def fetch(self): - log('Fetching playlist data...') video_titles = None url = self.parent().findChild(TextField, 'youtube_source_url').text() while True: + log('Fetching playlist data...') try: self.playlist = Playlist(url) video_titles = [v.title for v in self.playlist.videos] - log('Found playlist "' + self.playlist.title + '" with ' + str(len(video_titles)) + ' videos') + log('Found playlist "' + self.playlist.title + '" with ' + str(len(video_titles)) + ' videos.') for selector in self.parent().parent().findChildren(VideoSelector, 'video_selector'): selector.set_videos(video_titles) break except (ConnectionResetError, URLError): log('Connection to YouTube lost. Trying again in 3 seconds...') - sleep(3) + time.sleep(3) except KeyError: log('Invalid playlist URL!') return @@ -174,9 +174,7 @@ def clicked_event(self): log('Invalid pack name!') else: target = os.path.join(self.parent().findChild(TextField, 'target_path').text(), pack_name) - if os.path.exists(target) and Application.overwrite_pack(self): - shutil.rmtree(target) - log('Removed the existing directory and its contents') + overwrite_existing(self, target) if not os.path.exists(target): source_folder = self.parent().parent().findChild(TextField, 'files_source_path').text() from_files([os.path.join(source_folder, s.current_file()) for s in self.parent().parent().findChild(AppTab, 'files_tab').findChildren(FileSelector, 'files_selector')], target) @@ -190,9 +188,7 @@ def clicked_event(self): log('Invalid pack name!') else: target = os.path.join(self.parent().findChild(TextField, 'target_path').text(), pack_name) - if os.path.exists(target) and Application.overwrite_pack(self): - shutil.rmtree(target) - log('Removed the existing directory and its contents') + overwrite_existing(self, target) if not os.path.exists(target): from_youtube(playlist, [s.currentIndex() - 1 for s in self.parent().parent().findChild(AppTab, 'youtube_tab').findChildren(VideoSelector, 'video_selector')], target) @@ -203,9 +199,7 @@ def clicked_event(self): with open(os.path.join(source, 'pack.json')) as pack_file: title = json.load(pack_file)['Name'] target = os.path.join(self.parent().findChild(TextField, 'target_path').text(), title) - if os.path.exists(target) and Application.overwrite_pack(self): - shutil.rmtree(target) - log('Removed the existing directory and its contents') + overwrite_existing(self, target) if not os.path.exists(target): from_terraria(source, target) @@ -389,7 +383,7 @@ def __init__(self): app_instance = self self.create_ui() - log('Application window created') + log('Application window created.') self.exec() def create_ui(self): @@ -450,6 +444,17 @@ def log(self, line): return +def overwrite_existing(box, target): + if os.path.exists(target) and overwrite_pack(box): + try: + shutil.rmtree(target) + except PermissionError: + log('Overwrite permission denied.') + return False + log('Removed the existing directory and its contents.') + return True + + def overwrite_pack(parent): message_box = QMessageBox(QMessageBox.Icon.Warning, 'Folder already exists', 'Do you want to overwrite the existing folder?',