Move a little more clutter from av98.py into util.py.

This commit is contained in:
Solderpunk 2023-11-19 10:41:08 +01:00
parent 73ce79310d
commit 9970f21e47
2 changed files with 28 additions and 28 deletions

30
av98.py
View File

@ -92,28 +92,6 @@ ui_out = logging.getLogger("av98_logger")
ui_handler = logging.StreamHandler()
ui_out.addHandler(ui_handler)
def fix_ipv6_url(url):
if not url.count(":") > 2: # Best way to detect them?
return url
# If there's a pair of []s in there, it's probably fine as is.
if "[" in url and "]" in url:
return url
# Easiest case is a raw address, no schema, no path.
# Just wrap it in square brackets and whack a slash on the end
if "/" not in url:
return "[" + url + "]/"
# Now the trickier cases...
if "://" in url:
schema, schemaless = url.split("://")
else:
schema, schemaless = None, url
if "/" in schemaless:
netloc, rest = schemaless.split("/",1)
schemaless = "[" + netloc + "]" + "/" + rest
if schema:
return schema + "://" + schemaless
return schemaless
standard_ports = {
"gemini": 1965,
"gopher": 70,
@ -124,7 +102,7 @@ class GeminiItem():
def __init__(self, url, name=""):
if "://" not in url:
url = "gemini://" + url
self.url = fix_ipv6_url(url)
self.url = util.fix_ipv6_url(url)
self.name = name
parsed = urllib.parse.urlparse(self.url)
self.scheme = parsed.scheme
@ -181,10 +159,6 @@ class GeminiItem():
CRLF = '\r\n'
# Cheap and cheerful URL detector
def looks_like_url(word):
return "." in word and word.startswith("gemini://")
class UserAbortException(Exception):
pass
@ -1081,7 +1055,7 @@ Current tour can be listed with `tour ls` and scrubbed with `tour clear`."""
self.waypoints = []
elif line == "*":
self.waypoints.extend(self.lookup)
elif looks_like_url(line):
elif util.looks_like_url(line):
self.waypoints.append(GeminiItem(line))
else:
for index in line.split():

26
util.py
View File

@ -1,3 +1,7 @@
# Cheap and cheerful URL detector
def looks_like_url(word):
return "." in word and word.startswith("gemini://")
def ask_yes_no(prompt, default=None):
print(prompt)
if default == True:
@ -14,3 +18,25 @@ def ask_yes_no(prompt, default=None):
return True
elif resp.strip().lower() in ("n","no"):
return False
def fix_ipv6_url(url):
if not url.count(":") > 2: # Best way to detect them?
return url
# If there's a pair of []s in there, it's probably fine as is.
if "[" in url and "]" in url:
return url
# Easiest case is a raw address, no schema, no path.
# Just wrap it in square brackets and whack a slash on the end
if "/" not in url:
return "[" + url + "]/"
# Now the trickier cases...
if "://" in url:
schema, schemaless = url.split("://")
else:
schema, schemaless = None, url
if "/" in schemaless:
netloc, rest = schemaless.split("/",1)
schemaless = "[" + netloc + "]" + "/" + rest
if schema:
return schema + "://" + schemaless
return schemaless