From 7d5f4dd154b0b532d0deeef195cea63e371bcde5 Mon Sep 17 00:00:00 2001 From: "Stefanos I. Tsaklidis" Date: Thu, 22 Aug 2019 17:45:13 +0300 Subject: [PATCH 1/4] Catch IO error --- youtube_dl_gui/logmanager.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/youtube_dl_gui/logmanager.py b/youtube_dl_gui/logmanager.py index 86e5834c..334de85b 100644 --- a/youtube_dl_gui/logmanager.py +++ b/youtube_dl_gui/logmanager.py @@ -5,6 +5,7 @@ from __future__ import unicode_literals + import os.path from time import strftime @@ -14,6 +15,8 @@ check_path ) +from wx import CallAfter +from wx.lib.pubsub import pub as Publisher class LogManager(object): @@ -79,13 +82,18 @@ def _write(self, data, mode): """ check_path(self.config_path) - with open(self.log_file, mode) as log: - if mode == 'a' and self.add_time: - msg = self.TIME_TEMPLATE.format(time=strftime('%c'), error_msg=data) - else: - msg = data - - log.write(msg.encode(self._encoding, 'ignore')) + try: + with open(self.log_file, mode) as log: + if mode == 'a' and self.add_time: + msg = self.TIME_TEMPLATE.format(time=strftime('%c'), error_msg=data) + else: + msg = data + + log.write(msg.encode(self._encoding, 'ignore')) + except Exception as e: + # Add a GUI popup to inform the user + print('Cant log to file, Permissions Denied.') + return False def _init_log(self): """Initialize the log file if not exist. """ From b8ffff1174058756a9d2e0c00c5bc04e6795655c Mon Sep 17 00:00:00 2001 From: "Stefanos I. Tsaklidis" Date: Fri, 23 Aug 2019 12:30:47 +0300 Subject: [PATCH 2/4] Inform user for permissions --- youtube_dl_gui/__init__.py | 7 +++++++ youtube_dl_gui/utils.py | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/youtube_dl_gui/__init__.py b/youtube_dl_gui/__init__.py index d8c0fe2b..9822e399 100644 --- a/youtube_dl_gui/__init__.py +++ b/youtube_dl_gui/__init__.py @@ -51,6 +51,7 @@ get_config_path, get_locale_file, os_path_exists, + check_pers, YOUTUBEDL_BIN ) @@ -83,10 +84,16 @@ def main(): """The real main. Creates and calls the main app windows. """ youtubedl_path = os.path.join(opt_manager.options["youtubedl_path"], YOUTUBEDL_BIN) + app = wx.App() + if not check_pers(): + wx.MessageBox(_("The path:{0} is not writable".format(config_path)), _("Permissions denied"), wx.OK | wx.ICON_ERROR) + return False + frame = MainFrame(opt_manager, log_manager) frame.Show() + if opt_manager.options["disable_update"] and not os_path_exists(youtubedl_path): wx.MessageBox(_("Failed to locate youtube-dl and updates are disabled"), _("Error"), wx.OK | wx.ICON_ERROR) frame.close() diff --git a/youtube_dl_gui/utils.py b/youtube_dl_gui/utils.py index 729c0079..104199b3 100644 --- a/youtube_dl_gui/utils.py +++ b/youtube_dl_gui/utils.py @@ -16,6 +16,7 @@ import sys import json import math +import errno import locale import subprocess @@ -193,6 +194,17 @@ def get_config_path(): return os.path.join(path, __appname__.lower()) +def check_pers(): + path = get_config_path() + try: + with open(path + '/tst.log', 'w') as f: + # opened for writing. write to it here + return True + except IOError as x: + if x.errno == errno.EACCES: + print('Permissions Denied, check {0}'.format(path)) + return False + def shutdown_sys(password=None): """Shuts down the system. Returns True if no errors occur else False. From 632fed0ad2e0aab0ac32590764f49ac75f908dba Mon Sep 17 00:00:00 2001 From: "Stefanos I. Tsaklidis" Date: Fri, 23 Aug 2019 12:33:29 +0300 Subject: [PATCH 3/4] Make the check on load --- youtube_dl_gui/logmanager.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/youtube_dl_gui/logmanager.py b/youtube_dl_gui/logmanager.py index 334de85b..cb60a021 100644 --- a/youtube_dl_gui/logmanager.py +++ b/youtube_dl_gui/logmanager.py @@ -82,18 +82,13 @@ def _write(self, data, mode): """ check_path(self.config_path) - try: - with open(self.log_file, mode) as log: - if mode == 'a' and self.add_time: - msg = self.TIME_TEMPLATE.format(time=strftime('%c'), error_msg=data) - else: - msg = data - - log.write(msg.encode(self._encoding, 'ignore')) - except Exception as e: - # Add a GUI popup to inform the user - print('Cant log to file, Permissions Denied.') - return False + with open(self.log_file, mode) as log: + if mode == 'a' and self.add_time: + msg = self.TIME_TEMPLATE.format(time=strftime('%c'), error_msg=data) + else: + msg = data + + log.write(msg.encode(self._encoding, 'ignore')) def _init_log(self): """Initialize the log file if not exist. """ From 70704da556dd821556f08cd037279afab237774d Mon Sep 17 00:00:00 2001 From: "Stefanos I. Tsaklidis" Date: Fri, 23 Aug 2019 17:11:00 +0300 Subject: [PATCH 4/4] Remove unused imports --- youtube_dl_gui/logmanager.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/youtube_dl_gui/logmanager.py b/youtube_dl_gui/logmanager.py index cb60a021..8aa9b86e 100644 --- a/youtube_dl_gui/logmanager.py +++ b/youtube_dl_gui/logmanager.py @@ -15,8 +15,6 @@ check_path ) -from wx import CallAfter -from wx.lib.pubsub import pub as Publisher class LogManager(object):