From: khamidou Date: Wed, 10 Feb 2010 17:16:06 +0000 (+0100) Subject: added a new CachedList class X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=aceacc74469435d9bdb2f5062c06b2cd777fcc4c;p=liszt.git added a new CachedList class --- diff --git a/liszt b/liszt index 3b66429..c55732c 100755 --- a/liszt +++ b/liszt @@ -48,15 +48,31 @@ def create_spool_file(dict): class CachedList(object): def __init__(self, listname): try: + self.listname = listname fd = open(os.path.expanduser('~/.liszt/cached/%s' % listname)) except IOError: bailout("Unable to open cached list " + listname) try: - self.list = json.load(fd) + self.cached_list = json.load(fd) except ValueError: - bailout("List %s is not a valid json file" % listname) - fd.close() + bailout("List <%s> is not a valid json file" % listname) + + def save(self): + fd = open(os.path.expanduser('~/.liszt/cached/%s' % listname), 'w+') + json.dump(self.cached_list, fd) + + def fget(self): + return self.cached_list + + def fset(self, list): + self.cached_list = list + + def fdel(self): + del self.cached_list + + contents = property(fget, fset, fdel) + class CommandParser(object): def __init__(self, args): self.dispatch = {"create" : self.parse_create, @@ -111,25 +127,31 @@ class CommandParser(object): listname = "" entryname = "" - if sys.stdin.isatty(): - sys.stdin = os.popen('/bin/sh -c less -', 'w') + if sys.stdout.isatty(): + sys.stdout = os.popen('/bin/sh -c less -', 'w') if len(args) == 0: print "List of the created lists :" - lists = open(os.path.expanduser('~/.liszt/cache/lists_list')) - for list in lists.readlines(): + lists_list = CachedList("lists_list") + + for list in lists_list.contents: print list if len(args) == 1: - listfd = open(os.path.expanduser('~/.liszt/cache/lists_list') + args[0]) - list = json.load(listfd) - print list -# for entry in entries["contents"]: -# print entry["title"] + "\t" + entry["date"] - -# elif len(args) == 2: -# entry = session.get_entry(args[0], args[1]) -# print entry["title"] + "\t" + entry["date"] + list = CachedList(args[0]) + + for entry in list.contents["contents"]: + print entry["title"] + "\t" + entry["date"] + + elif len(args) == 2: + list = CachedList(args[0]) + index = int(args[1]) + try: + entry = list.contents["contents"][index] + except IndexError: + bailout("Invalid entry index.") + + print entry["title"] + "\t" + entry["date"] def parse_remove(self, args): listname = "" diff --git a/liszt-daemon b/liszt-daemon index c021b59..34058fe 100755 --- a/liszt-daemon +++ b/liszt-daemon @@ -335,6 +335,11 @@ def cache_new_lists(): json.dump(dict, fd) fd.close() + # Also save a list of the lists : + fd = open(os.path.expanduser('~/.liszt/cached/lists_list'), 'w+') + json.dump(session.get_lists_list(), fd) + fd.close() + time.sleep(15 * 60) def sync_cached_list(path):