forked from cmccabe/linkulator2
a more universal handler configuration.
This commit is contained in:
parent
b6782d7a80
commit
4375aad574
17
config.py
17
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()
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue