history: simplify

This commit is contained in:
Jimmy 2015-03-12 21:35:56 +13:00
parent 1efe18ecc6
commit 61e732f217

View File

@ -62,11 +62,11 @@ class WebHistory(QWebHistoryInterface):
Attributes: Attributes:
_lineparser: The AppendLineParser used to save the history. _lineparser: The AppendLineParser used to save the history.
_old_urls: A set of URLs read from the on-disk history. _history_dict: A set of URLs read from the on-disk history.
_new_history: A list of HistoryEntry items of the current session. _new_history: A list of HistoryEntry items of the current session.
_saved_count: How many HistoryEntries have been written to disk. _saved_count: How many HistoryEntries have been written to disk.
_old_hit: How many times an URL was found in _old_urls. _old_hit: How many times an URL was found in _history_dict.
_old_miss: How many times an URL was not found in _old_urls. _old_miss: How many times an URL was not found in _history_dict.
""" """
item_added = pyqtSignal(HistoryEntry) item_added = pyqtSignal(HistoryEntry)
@ -75,7 +75,7 @@ class WebHistory(QWebHistoryInterface):
super().__init__(parent) super().__init__(parent)
self._lineparser = lineparser.AppendLineParser( self._lineparser = lineparser.AppendLineParser(
standarddir.data(), 'history', parent=self) standarddir.data(), 'history', parent=self)
self._old_urls = {} self._history_dict = {}
with self._lineparser.open(): with self._lineparser.open():
for line in self._lineparser: for line in self._lineparser:
atime, url = line.rstrip().split(maxsplit=1) atime, url = line.rstrip().split(maxsplit=1)
@ -84,7 +84,7 @@ class WebHistory(QWebHistoryInterface):
# information about previous hits change the items in # information about previous hits change the items in
# old_urls to be lists or change HistoryEntry to have a # old_urls to be lists or change HistoryEntry to have a
# list of atimes. # list of atimes.
self._old_urls[url] = HistoryEntry(atime, url) self._history_dict[url] = HistoryEntry(atime, url)
self._new_history = [] self._new_history = []
self._saved_count = 0 self._saved_count = 0
self._old_hit = 0 self._old_hit = 0
@ -99,7 +99,7 @@ class WebHistory(QWebHistoryInterface):
return self._new_history[key] return self._new_history[key]
def __iter__(self): def __iter__(self):
return self._old_urls.values().__iter__() return iter(self._history_dict.values())
def get_recent(self): def get_recent(self):
"""Get the most recent history entries.""" """Get the most recent history entries."""
@ -122,7 +122,7 @@ class WebHistory(QWebHistoryInterface):
if not config.get('general', 'private-browsing'): if not config.get('general', 'private-browsing'):
entry = HistoryEntry(time.time(), url_string) entry = HistoryEntry(time.time(), url_string)
self._new_history.append(entry) self._new_history.append(entry)
self._old_urls[url_string] = entry self._history_dict[url_string] = entry
self.item_added.emit(entry) self.item_added.emit(entry)
def historyContains(self, url_string): def historyContains(self, url_string):
@ -134,13 +134,7 @@ class WebHistory(QWebHistoryInterface):
Return: Return:
True if the url is in the history, False otherwise. True if the url is in the history, False otherwise.
""" """
if url_string in self._old_urls: return url_string in self._history_dict
self._old_hit += 1
return True
else:
self._old_miss += 1
return url_string in (entry.url for entry in self._new_history)
def init(): def init():
"""Initialize the web history.""" """Initialize the web history."""