From 4375aad5745e7705169af7ac87fea3ffd6a585a2 Mon Sep 17 00:00:00 2001 From: Sam Hunter Date: Thu, 12 Aug 2021 14:58:32 +0200 Subject: [PATCH] a more universal handler configuration. --- config.py | 17 ++++++++++++----- linkulator.py | 23 +++-------------------- 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/config.py b/config.py index aa42bc8..1e26d57 100644 --- a/config.py +++ b/config.py @@ -28,13 +28,23 @@ class DefaultUser: settingsfile: Path = datadir / PATHS.settingsfile lastlogin: str = "0.0" browser: str = "lynx" - gemini: str = "amfora" + handler: dict = { + "http": "lynx", + "https": "lynx", + "gopher": "lynx", + "gemini": "amfora", + "ssh": "putty" + } + + def save(self): """Saves config data to file""" config = configparser.ConfigParser() config["User Status"] = {"lastlogin": time()} config["User Settings"] = {"browser": self.browser, "gemini": self.gemini} + config["URL Handler"] = self.handler + with open(self.settingsfile, "w") as file: config.write(file) @@ -45,10 +55,7 @@ class DefaultUser: if len(ret) > 0: self.lastlogin = config["User Status"]["lastlogin"] self.browser = config["User Settings"]["browser"] - try: - self.gemini = config["User Settings"]["gemini"] - except: - self.gemini = self.gemini + self.handler = config["URL Handler"] USER = DefaultUser() diff --git a/linkulator.py b/linkulator.py index 9b14050..a8bcd68 100755 --- a/linkulator.py +++ b/linkulator.py @@ -215,28 +215,11 @@ def search(): def view_link_in_browser(url): """Attempts to view the specified URL in the configured browser""" - if which(config.USER.browser) is None: - print( - "Sorry, " - + config.USER.browser - + " is not installed on your system. Ask your sysadmin to install it." - ) - return - if which(config.USER.gemini) is None: - print( - "Sorry, " - + config.USER.gemini - + " is not installed on your system. Ask your sysadmin to install it." - ) - return - url_scheme = urlparse(url).scheme - if url_scheme in ["gopher", "https", "http"]: - subprocess.call([config.USER.browser, url]) - elif url_scheme in ["gemini"]: - subprocess.call([config.USER.gemini, url]) + if url_scheme in config.USER.handler and which(config.USER.handler[url_scheme]): + subprocess.call([config.USER.handler[url_scheme], url]) else: - print("Sorry, that url doesn't start with gopher://, http://, https:// or gemini://") + print("Sorry, there is no handler assigned to '{}'".format(url_scheme)) try_anyway = input( "Do you want to try it in {} anyway? Y/[N]".format(config.USER.browser) ).lower()