diff --git a/trackma/ui/cli.py b/trackma/ui/cli.py index 76ed5b4d..36cefec1 100644 --- a/trackma/ui/cli.py +++ b/trackma/ui/cli.py @@ -531,21 +531,8 @@ def do_openfolder(self, args): """ try: - show = self._get_show(args[0]) - filename = self.engine.get_episode_path(show) - with open(os.devnull, 'wb') as DEVNULL: - if sys.platform == 'darwin': - subprocess.Popen(["open", - os.path.dirname(filename)], stdout=DEVNULL, stderr=DEVNULL) - elif sys.platform == 'win32': - subprocess.Popen(["explorer", - os.path.dirname(filename)], stdout=DEVNULL, stderr=DEVNULL) - else: - subprocess.Popen(["/usr/bin/xdg-open", - os.path.dirname(filename)], stdout=DEVNULL, stderr=DEVNULL) - except OSError: - # xdg-open failed. - self.display_error("Could not open folder.") + show_id = self._get_show(args[0])['id'] + utils.open_folder(self.engine, show_id, error_callback=self.display_error) except utils.TrackmaError as e: self.display_error(e) diff --git a/trackma/ui/curses.py b/trackma/ui/curses.py index 7b7f3a1b..b4a48d4d 100644 --- a/trackma/ui/curses.py +++ b/trackma/ui/curses.py @@ -365,27 +365,7 @@ def do_play(self): def do_openfolder(self): item = self._get_selected_item() - - try: - show = self.engine.get_show_info(item.showid) - filename = self.engine.get_episode_path(show) - with open(os.devnull, 'wb') as DEVNULL: - if sys.platform == 'darwin': - subprocess.Popen(["open", - os.path.dirname(filename)], stdout=DEVNULL, stderr=DEVNULL) - elif sys.platform == 'win32': - subprocess.Popen(["explorer", - os.path.dirname(filename)], stdout=DEVNULL, stderr=DEVNULL) - else: - subprocess.Popen(["/usr/bin/xdg-open", - os.path.dirname(filename)], stdout=DEVNULL, stderr=DEVNULL) - except OSError: - # xdg-open failed. - raise utils.EngineError("Could not open folder.") - - except utils.EngineError: - # Show not in library. - self.error("No folder found.") + utils.open_folder(self.engine, item.showid, error_callback=self.error) def do_play_random(self): try: diff --git a/trackma/ui/gtk/window.py b/trackma/ui/gtk/window.py index dcebccb3..5cfb2037 100644 --- a/trackma/ui/gtk/window.py +++ b/trackma/ui/gtk/window.py @@ -573,29 +573,7 @@ def _open_website(self, show_id): Gtk.show_uri(None, show['url'], Gdk.CURRENT_TIME) def _open_folder(self, show_id): - show = self._engine.get_show_info(show_id) - try: - filename = self._engine.get_episode_path(show) - with open(os.devnull, 'wb') as DEVNULL: - if sys.platform == 'darwin': - subprocess.Popen(["open", os.path.dirname(filename)], - stdout=DEVNULL, - stderr=DEVNULL) - elif sys.platform == 'win32': - subprocess.Popen(["explorer", os.path.dirname(filename)], - stdout=DEVNULL, - stderr=DEVNULL) - else: - subprocess.Popen(["/usr/bin/xdg-open", os.path.dirname(filename)], - stdout=DEVNULL, - stderr=DEVNULL) - except OSError: - # xdg-open failed. - raise utils.EngineError("Could not open folder.") - - except utils.EngineError: - # Show not in library. - self._error_dialog_idle("No folder found.") + utils.open_folder(self._engine, show_id, error_callback=self._error_dialog_idle) def _copy_title(self, show_id): show = self._engine.get_show_info(show_id) diff --git a/trackma/ui/qt/mainwindow.py b/trackma/ui/qt/mainwindow.py index d08db66f..db51103d 100644 --- a/trackma/ui/qt/mainwindow.py +++ b/trackma/ui/qt/mainwindow.py @@ -1183,26 +1183,7 @@ def s_altname(self): self.ws_changed_show(show, altname=new_altname) def s_open_folder(self): - show = self.worker.engine.get_show_info(self.selected_show_id) - try: - filename = self.worker.engine.get_episode_path(show) - with open(os.devnull, 'wb') as DEVNULL: - if sys.platform == 'darwin': - subprocess.Popen(["open", - os.path.dirname(filename)], stdout=DEVNULL, stderr=DEVNULL) - elif sys.platform == 'win32': - subprocess.Popen(["explorer", - os.path.dirname(filename)], stdout=DEVNULL, stderr=DEVNULL) - else: - subprocess.Popen(["/usr/bin/xdg-open", - os.path.dirname(filename)], stdout=DEVNULL, stderr=DEVNULL) - except OSError: - # xdg-open failed. - raise utils.EngineError("Could not open folder.") - - except utils.EngineError: - # Show not in library. - self.error("No folder found.") + utils.open_folder(self.worker.engine, self.selected_show_id, error_callback=self.error) def s_retrieve(self): queue = self.worker.engine.get_queue() diff --git a/trackma/utils.py b/trackma/utils.py index c4ddd309..61f6d1a1 100644 --- a/trackma/utils.py +++ b/trackma/utils.py @@ -490,6 +490,53 @@ def get_terminal_size(fd=1): return hw +# Default 'open folder' function +def open_folder(engine, show_id, error_callback=None): + """ + Tries to open the folder containing the show of the passed ID. + Takes an optional 'error_callback' parameter that lets the invoker specify an error callback method. + If no error callback is passed, it defaults to simply raising. + """ + + try: + show_to_open = engine.get_show_info(show_id) + filename = engine.get_episode_path(show_to_open) + + with open(os.devnull, 'wb') as DEVNULL: + command = [] + + match sys.platform: + case 'darwin': + command = ["open", os.path.dirname(filename)] + case 'win32': + command = ["explorer", os.path.dirname(filename)] + case _: + command = ["xdg-open", os.path.dirname(filename)] + + process = subprocess.Popen(command, + stdout=DEVNULL, + stderr=subprocess.PIPE, + universal_newlines=True) + + _, stderr = process.communicate() + + if process.returncode != 0: + # Command failed. + raise OSError(stderr) + + except OSError as e: + if error_callback: + error_callback(f'Could not open folder.\n{e}') + else: + raise e + + except Exception as e: + if error_callback: + error_callback(e) + else: + raise e + + def show(): return { 'id': 0,