2021-03-11 19:16:15 +01:00
|
|
|
"""History management."""
|
|
|
|
|
2021-06-03 16:45:35 +02:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from bebop.fs import get_history_path
|
2021-03-28 18:55:52 +02:00
|
|
|
|
2021-05-08 22:41:42 +02:00
|
|
|
|
2021-06-03 16:45:35 +02:00
|
|
|
class History:
|
|
|
|
"""Basic browsing history manager."""
|
2021-02-18 01:40:05 +01:00
|
|
|
|
2021-05-09 00:46:26 +02:00
|
|
|
def __init__(self, limit):
|
2021-02-18 01:40:05 +01:00
|
|
|
self.urls = []
|
2021-05-09 00:46:26 +02:00
|
|
|
self.backlist = []
|
|
|
|
self.limit = limit
|
2021-02-18 01:40:05 +01:00
|
|
|
|
|
|
|
def push(self, url):
|
2021-05-09 00:46:26 +02:00
|
|
|
"""Add an URL to the history."""
|
|
|
|
# Append url to our URLs, bubbling it up if it's already there.
|
2021-05-08 22:41:42 +02:00
|
|
|
try:
|
|
|
|
self.urls.remove(url)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
self.urls.append(url)
|
2021-05-09 00:46:26 +02:00
|
|
|
if len(self.urls) > self.limit:
|
|
|
|
self.urls.pop(0)
|
|
|
|
|
|
|
|
# Also simply push it to the backlist.
|
|
|
|
self.backlist.append(url)
|
|
|
|
if len(self.backlist) > self.limit:
|
|
|
|
self.backlist.pop(0)
|
2021-05-08 22:41:42 +02:00
|
|
|
|
2021-05-09 00:46:26 +02:00
|
|
|
def get_previous(self, actual_previous=False):
|
2021-05-08 22:41:42 +02:00
|
|
|
"""Return previous URL, or None if there is only one or zero URL."""
|
|
|
|
try:
|
2021-05-09 00:46:26 +02:00
|
|
|
if actual_previous:
|
|
|
|
return self.backlist[-1]
|
|
|
|
# The backlist should be populated with the first link visited and
|
|
|
|
# never completely emptied afterwards, or we end up in situation
|
|
|
|
# where you can't get away from internal pages.
|
|
|
|
if len(self.backlist) > 1:
|
|
|
|
self.backlist.pop()
|
|
|
|
return self.backlist[-1]
|
2021-05-08 22:41:42 +02:00
|
|
|
except IndexError:
|
|
|
|
return None
|
2021-02-18 01:40:05 +01:00
|
|
|
|
2021-05-08 22:41:42 +02:00
|
|
|
def to_gemtext(self):
|
2021-05-09 00:46:26 +02:00
|
|
|
"""Generate a simple Gemtext page of the current history.
|
|
|
|
|
|
|
|
Present a page that follows the "by last visited" behaviour of Firefox
|
|
|
|
for the lack of a better idea, avoiding duplicated entries.
|
|
|
|
"""
|
|
|
|
urls = []
|
|
|
|
seen = set()
|
|
|
|
for url in reversed(self.urls):
|
|
|
|
if url in seen:
|
|
|
|
continue
|
|
|
|
urls.append(url)
|
|
|
|
seen.add(url)
|
2021-05-09 02:03:45 +02:00
|
|
|
return "# History\n\n" + "\n".join("=> " + url for url in urls)
|
2021-06-03 16:45:35 +02:00
|
|
|
|
|
|
|
def save(self):
|
|
|
|
"""Save current history to user data."""
|
|
|
|
history_path = get_history_path()
|
|
|
|
try:
|
|
|
|
with open(history_path, "wt") as history_file:
|
|
|
|
for url in self.urls:
|
|
|
|
history_file.write(url + "\n")
|
|
|
|
except OSError as exc:
|
|
|
|
logging.error(f"Failed to save history {history_path}: {exc}")
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def load(self):
|
|
|
|
"""Load saved history from user data."""
|
|
|
|
history_path = get_history_path()
|
|
|
|
self.urls = []
|
|
|
|
try:
|
|
|
|
with open(history_path, "rt") as history_file:
|
|
|
|
for url in history_file:
|
|
|
|
self.urls.append(url.rstrip())
|
|
|
|
except OSError as exc:
|
|
|
|
logging.error(f"Failed to load history {history_path}: {exc}")
|
|
|
|
return False
|
|
|
|
return True
|