def open_uri(uri, window): """Open a URI in an external (web) browser. The given argument has to be a properly formed URI including the scheme (fe. HTTP). As of now failures will be silently discarded.""" # Situation 1, user defined a way of handling the protocol protocol = uri[:uri.find(":")] protocol_handlers = NICOTINE.np.config.sections["urls"]["protocols"] if protocol in protocol_handlers and protocol_handlers[protocol]: try: execute_command(protocol_handlers[protocol], uri) return except RuntimeError as e: log.add_warning("%s", e) if protocol == "slsk": on_soul_seek_uri(uri.strip()) # Situation 2, user did not define a way of handling the protocol if sys.platform == "win32": import webbrowser webbrowser.open(uri) return try: Gtk.show_uri_on_window(window, uri, Gdk.CURRENT_TIME) except AttributeError: screen = window.get_screen() Gtk.show_uri(screen, uri, Gdk.CURRENT_TIME)
def open_uri(uri, window): """Open a URI in an external (web) browser. The given argument has to be a properly formed URI including the scheme (fe. HTTP). As of now failures will be silently discarded.""" # Situation 1, user defined a way of handling the protocol protocol = uri[:uri.find(":")] if protocol in PROTOCOL_HANDLERS: if isinstance(PROTOCOL_HANDLERS[protocol], types.MethodType): PROTOCOL_HANDLERS[protocol](uri.strip()) return if PROTOCOL_HANDLERS[protocol]: try: execute_command(PROTOCOL_HANDLERS[protocol], uri) return except RuntimeError as e: log.add_warning("%s", e) # Situation 2, user did not define a way of handling the protocol if sys.platform == "win32" and webbrowser: webbrowser.open(uri) return try: Gtk.show_uri_on_window(window, uri, Gdk.CURRENT_TIME) except AttributeError: screen = window.get_screen() Gtk.show_uri(screen, uri, Gdk.CURRENT_TIME)
def _on_play_files(self, widget, prefix=""): executable = self.frame.np.config.sections["players"]["default"] downloaddir = self.frame.np.config.sections["transfers"]["downloaddir"] if "$" not in executable: return for fn in self.selected_transfers: playfile = None if fn.file is not None and os.path.exists(fn.file.name): playfile = fn.file.name else: # If this file doesn't exist anymore, it may have finished downloading and have been renamed # try looking in the download directory and match the original filename. basename = str.split(fn.filename, '\\')[-1] path = os.sep.join([downloaddir, basename]) if os.path.exists(path): playfile = path if playfile: execute_command(executable, playfile, background=False)
def open_uri(uri, window): """Open a URI in an external (web) browser. The given argument has to be a properly formed URI including the scheme (fe. HTTP). As of now failures will be silently discarded.""" # Situation 1, user defined a way of handling the protocol protocol = uri[:uri.find(":")] protocol_handlers = config.sections["urls"]["protocols"] if protocol in protocol_handlers and protocol_handlers[protocol]: try: execute_command(protocol_handlers[protocol], uri) return except RuntimeError as e: log.add("%s", e) if protocol == "slsk": on_soul_seek_uri(uri.strip()) return # Situation 2, user did not define a way of handling the protocol try: if sys.platform == "win32": os.startfile(uri) elif sys.platform == "darwin": execute_command("open $", uri) else: Gio.AppInfo.launch_default_for_uri(uri) except Exception as error: log.add(_("Failed to open URL: %s"), str(error))
def tts_player(self, message): self.tts_playing = True try: execute_command(self.config.sections["ui"]["speechcommand"], message, background=False) except Exception as error: log.add(_("Text-to-speech for message failed: %s"), error)
def on_file_manager(self, widget): if self.selected_folder is None: return path = self.frame.np.shares.virtual2real(self.selected_folder) executable = self.frame.np.config.sections["ui"]["filemanager"] if "$" in executable: execute_command(executable, path)
def _on_play_files(self, widget, prefix=""): executable = self.frame.np.config.sections["players"]["default"] if "$" not in executable: return for fn in self.selected_transfers: file = fn.realfilename if os.path.exists(file): execute_command(executable, file, background=False)
def open_file_path(file_path, command=None): """ Currently used to either open a folder or play an audio file Tries to run a user-specified command first, and falls back to the system default. """ if command and "$" in command: execute_command(command, file_path) else: try: Gio.AppInfo.launch_default_for_uri("file:///" + file_path) except GLib.Error as error: log.add_warning(_("Failed to open folder: %s"), str(error))
def _on_play_files(self, widget, prefix=""): path = self.frame.np.shares.virtual2real(self.selected_folder) executable = self.frame.np.config.sections["players"]["default"] if "$" not in executable: return for fn in self.selected_files: file = os.sep.join([path, fn]) if os.path.exists(file): execute_command(executable, file, background=False)
def audacious_command(self, command, subcommand=''): """ Wrapper that calls audacious commandline audtool and parse the output """ try: output = execute_command("audtool %s %s" % (command, subcommand), returnoutput=True).decode().split('\n')[0] except RuntimeError: output = execute_command("audtool2 %s %s" % (command, subcommand), returnoutput=True).decode().split('\n')[0] if output.startswith('audtool'): output = None self.audacious_running = False return output
def on_open_directory(self, widget): downloaddir = self.frame.np.config.sections["transfers"]["downloaddir"] incompletedir = self.frame.np.config.sections["transfers"][ "incompletedir"] if incompletedir == "": incompletedir = downloaddir filemanager = self.frame.np.config.sections["ui"]["filemanager"] transfer = next(iter(self.selected_transfers)) if os.path.exists(transfer.path): execute_command(filemanager, transfer.path) else: execute_command(filemanager, incompletedir)
def mpd_command(self, command): output = execute_command("mpc --format $", command, returnoutput=True).decode().split('\n')[0] if output == '' or output.startswith("MPD_HOST") or output.startswith("volume: "): return None return output
def other(self): try: othercommand = self.NPCommand.get_text() if othercommand == "": return None output = execute_command(othercommand, returnoutput=True) self.title["nowplaying"] = output return True except Exception as error: log.add_warning(_("ERROR: Executing '%(command)s' failed: %(error)s"), {"command": othercommand, "error": error}) return None
def open_file_path(file_path, command=None): """ Currently used to either open a folder or play an audio file Tries to run a user-specified command first, and falls back to the system default. """ try: file_path = os.path.normpath(file_path) if command and "$" in command: execute_command(command, file_path) elif sys.platform == "win32": os.startfile(file_path) elif sys.platform == "darwin": execute_command("open $", file_path) else: Gio.AppInfo.launch_default_for_uri("file:///" + file_path.lstrip("/")) except Exception as error: log.add(_("Failed to open file path: %s"), str(error))
def other(self, command): try: if command == "": return None output = execute_command(command, returnoutput=True) self.title["nowplaying"] = output return True except Exception as error: log.add_important_error( _("Executing '%(command)s' failed: %(error)s"), { "command": command, "error": error }) return None
def on_open_directory(self, widget): downloaddir = self.frame.np.config.sections["transfers"]["downloaddir"] incompletedir = self.frame.np.config.sections["transfers"][ "incompletedir"] if incompletedir == "": incompletedir = downloaddir filemanager = self.frame.np.config.sections["ui"]["filemanager"] transfer = next(iter(self.selected_transfers)) complete_path = os.path.join(downloaddir, transfer.path) if transfer.path == "": if transfer.status == "Finished": execute_command(filemanager, downloaddir) else: execute_command(filemanager, incompletedir) elif os.path.exists(complete_path): # and tranfer.status is "Finished" execute_command(filemanager, complete_path) else: execute_command(filemanager, incompletedir)
def tts_player(self, message): self.tts_playing = True execute_command(self.frame.np.config.sections["ui"]["speechcommand"], message)