Skip to content

Commit

Permalink
Add PermissionError handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamppix committed Dec 13, 2022
1 parent 064ce93 commit 3ac6bd9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
12 changes: 6 additions & 6 deletions main.pyw
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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...')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -297,7 +297,7 @@ def main():
'''
Main function of the program.
'''
log('Program started')
log('Program started.')
if sys.platform == 'win32':
window.Application()
else:
Expand Down
33 changes: 19 additions & 14 deletions window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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?',
Expand Down

0 comments on commit 3ac6bd9

Please sign in to comment.