From 72b8c0a660de7db18957f477ec877e958e8d4c07 Mon Sep 17 00:00:00 2001 From: William Ting Date: Sat, 6 Jul 2013 20:23:34 -0500 Subject: [PATCH] minor refactor --- bin/autojump | 137 +++++++++++++++++++++++++-------------------------- 1 file changed, 67 insertions(+), 70 deletions(-) diff --git a/bin/autojump b/bin/autojump index f747441..998885e 100755 --- a/bin/autojump +++ b/bin/autojump @@ -41,7 +41,7 @@ except ImportError: class Database: """ - Object for interfacing with autojump database file. + Abstraction for interfacing with with autojump database file. """ def __init__(self, config): @@ -53,42 +53,6 @@ class Database: def __len__(self): return len(self.data) - def add(self, path, increment=10): - """ - Increase weight of existing paths or initialize new ones to 10. - """ - if path == self.config['home']: - return - - path = path.rstrip(os.sep) - - if self.data[path]: - self.data[path] = math.sqrt((self.data[path]**2) + (increment**2)) - else: - self.data[path] = increment - - self.save() - - def decrease(self, path, increment=15): - """ - Decrease weight of existing path. Unknown ones are ignored. - """ - if path == self.config['home']: - return - - if self.data[path] < increment: - self.data[path] = 0 - else: - self.data[path] -= increment - - self.save() - - def get_weight(self, path): - """ - Return path weight. - """ - return self.data[path] - def load(self, error_recovery = False): """ Open database file, recovering from backup if needed. @@ -123,39 +87,6 @@ class Database: shutil.copy(self.filename + '.bak', self.filename) return self.load(True) - def maintenance(self): - """ - Decay weights by 10%, periodically remove bottom 10% entries. - """ - try: - items = self.data.iteritems() - except AttributeError: - items = self.data.items() - - for path, _ in items: - self.data[path] *= 0.9 - - if len(self.data) > self.config['max_paths']: - remove_cnt = int(0.1 * len(self.data)) - for path in sorted(self.data, key=self.data.get)[:remove_cnt]: - del self.data[path] - - self.save() - - def purge(self): - """ - Deletes all entries that no longer exist on system. - """ - removed = [] - - for path in list(self.data.keys()): - if not os.path.exists(path): - removed.append(path) - del self.data[path] - - self.save() - return removed - def save(self): """ Save database atomically and preserve backup, creating new database if @@ -196,6 +127,72 @@ class Database: print("Error while creating backup autojump file. (%s)" % ex, file=sys.stderr) + def add(self, path, increment=10): + """ + Increase weight of existing paths or initialize new ones to 10. + """ + if path == self.config['home']: + return + + path = path.rstrip(os.sep) + + if self.data[path]: + self.data[path] = math.sqrt((self.data[path]**2) + (increment**2)) + else: + self.data[path] = increment + + self.save() + + def decrease(self, path, increment=15): + """ + Decrease weight of existing path. Unknown paths are ignored. + """ + if path == self.config['home']: + return + + if self.data[path] < increment: + self.data[path] = 0 + else: + self.data[path] -= increment + + self.save() + + def get_weight(self, path): + return self.data[path] + + def maintenance(self): + """ + Decay weights by 10%, periodically remove bottom 10% entries. + """ + try: + items = self.data.iteritems() + except AttributeError: + items = self.data.items() + + for path, _ in items: + self.data[path] *= 0.9 + + if len(self.data) > self.config['max_paths']: + remove_cnt = int(0.1 * len(self.data)) + for path in sorted(self.data, key=self.data.get)[:remove_cnt]: + del self.data[path] + + self.save() + + def purge(self): + """ + Remove non-existent paths. + """ + removed = [] + + for path in list(self.data.keys()): + if not os.path.exists(path): + removed.append(path) + del self.data[path] + + self.save() + return removed + def set_defaults(): config = {}