From 1ca5f0a64932a56dcbd7ca8fa2e464cec5e3ebc8 Mon Sep 17 00:00:00 2001 From: Brobin Date: Tue, 28 Apr 2015 00:25:21 -0500 Subject: [PATCH] refactored to an ordered set, make config directory if it doens't exist. remove item from set and list when pop is called remove occasionaly throws an error --- rtv/history.py | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/rtv/history.py b/rtv/history.py index ffcd245..a91e391 100644 --- a/rtv/history.py +++ b/rtv/history.py @@ -10,8 +10,10 @@ def history_path(): """ HOME = os.path.expanduser('~') XDG_CONFIG_HOME = os.getenv('XDG_CACHE_HOME', os.path.join(HOME, '.config')) - path = os.path.join(XDG_CONFIG_HOME, 'rtv', 'history.log') - return path + path = os.path.join(XDG_CONFIG_HOME, 'rtv') + if not os.path.exists(path): + os.makedirs(path) + return os.path.join(path, 'history.log') def load_history(): @@ -21,8 +23,9 @@ def load_history(): path = history_path() if os.path.exists(path): with open(path) as history_file: - return set([line.replace('\n', '') for line in history_file]) - return set() + history = [line.strip() for line in history_file] + return OrderedSet(history) + return OrderedSet() def save_history(history): @@ -34,4 +37,28 @@ def save_history(history): for i in range(200): if not history: break - history_file.write(history.pop() + '\n') \ No newline at end of file + history_file.write(history.pop() + '\n') + + +class OrderedSet(object): + """ + A simple implementation of an ordered set. A set is used to check + for membership, and a list is used to maintain ordering. + """ + + def __init__(self, elements=[]): + self._set = set(elements) + self._list = elements + + def __contains__(self, item): + return item in self._set + + def __len__(self): + return len(self._list) + + def add(self, item): + self._set.add(item) + self._list.append(item) + + def pop(self): + return self._list.pop()