]> khamidou — GitHub mirrors - liszt.git/commitdiff
chronological operations now work
authorkhamidou <khamidou@gmail.com>
Thu, 18 Feb 2010 15:57:52 +0000 (16:57 +0100)
committerkhamidou <khamidou@gmail.com>
Thu, 18 Feb 2010 15:57:52 +0000 (16:57 +0100)
README
libliszt/utils.py
liszt
liszt-daemon

diff --git a/README b/README
index 9bdfff4fc3ee6bc86213eb6cd9b1d5b4da27add3..21782cc72737564c65913097ba415de97cd3434b 100644 (file)
--- a/README
+++ b/README
@@ -3,6 +3,11 @@ This is liszt, a command line interface to the todoliszt.com api.
 liszt supports (at the moment) four modes of operation : showing, creating, removing a list or
 updating an element of a list.
 
+Requirements
+============
+
+python-dateutil, simplejson (usually included on all the recent python installations).
+
 Basic Usage
 ===========
 
index bd8bacc62c23d17398fa4b2420710f36987ae630..126bd74d9873df4a8f41388c7ca4a82219b71f8d 100644 (file)
@@ -4,6 +4,8 @@ import os, sys
 import urllib, urllib2, cookielib
 from urllib import quote
 from urllib2 import HTTPError, URLError
+import time
+from dateutil.parser import parse as parse_date
 import atexit
 from signal import SIGTERM 
 
@@ -18,6 +20,19 @@ def bailout(msg):
     print msg
     return
 
+def sorted_dirlist(path):
+    def sortbyctime(a, b):
+        da = parse_date(a)
+        db = parse_date(b)
+        if (da > db):
+               return 1
+        elif da == db:
+               return 0
+        else:
+               return -1
+
+    return sorted(os.listdir(path), cmp=sortbyctime)
+
 
 # The Daemon class comes from http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/
 class Daemon:
@@ -236,7 +251,14 @@ class CachedList(object):
             try:
                 self.cached_list = json.load(fd)
             except ValueError:
-                bailout("List <%s> is not a valid json file" % listname)
+            # Create it.
+                try:
+                    fd = open(os.path.expanduser('~/.liszt/cached/') + listname, 'w+')
+                except IOError:
+                    bailout("IOError when trying to create a cached list.")
+            
+                self.cached_list = {"name" : listname, "contents" : []}
+                
 
         except IOError:
             # Create it.
diff --git a/liszt b/liszt
index a89a8318ec178a7c590eca53b1dcd1f255a54597..01f6f4a44183094d37e5925f72333bb503fe3ed7 100755 (executable)
--- a/liszt
+++ b/liszt
@@ -12,8 +12,10 @@ import fnmatch
 import readline
 import tempfile
 import datetime
+import time
 import locale
 import codecs
+import subprocess
 from libliszt.utils import CachedList
 
 try:
@@ -38,10 +40,11 @@ def bailout(msg):
     sys.exit(-1)
 
 def create_spool_file(dict):
+    d = datetime.datetime.now()
     # Create a spool file and save dict inside as a json dict.
-    fd, filename = tempfile.mkstemp(dir=os.path.expanduser('~/.liszt/delayed/'))
-    os.write(fd, json.dumps(dict))
-    os.close(fd)
+    fd = open(os.path.expanduser('~/.liszt/delayed/') + d.isoformat(), 'w+')
+    json.dump(dict, fd)
+    fd.close()
     
 
 def glob_listname(pattern):
@@ -120,8 +123,8 @@ class CommandParser(object):
         else:
             spldict["request"] = "create-entry"
             spldict["listname"] = listname
-            spldict["content"] = dict()
-            spldict["content"]["title"] = entryname # FIXME : specify the other options.
+            spldict["contents"] = dict()
+            spldict["contents"]["title"] = entryname # FIXME : specify the other options.
 
             cached_list = CachedList(listname)
             entry = {"title" : entryname, "done" : 0, "date" : str(datetime.datetime.now())} # FIXME : specify the other options.
@@ -211,7 +214,7 @@ class CommandParser(object):
         l = CachedList(listname)
         entries = l.contents["contents"]
 
-            # unicode magic.
+        # unicode magic.
         fd = codecs.getwriter(locale.getpreferredencoding())(fd)
 
         for entry in entries:
@@ -229,10 +232,20 @@ class CommandParser(object):
         # Now, reopen the fd, as a python file handle.
         fd = open(filename)
         lines = fd.read().split('+')
-            
+
+        # Delete the list and recreate the entries.
+        dict = {"request" : "delete-list", "listname" : listname,}
+        create_spool_file(dict)
+        time.sleep(0.2) # Make sure that the list is removed before doing anything
+        # Recreate it
+        # Delete the list and recreate the entries.
+        dict = {"request" : "create-list", "listname" : listname,}
+        create_spool_file(dict)
+
+        l.contents["contents"] = []
+
         for i, line in enumerate(lines):
             line = line.strip()
-
             if line == "":
                 continue
 
@@ -241,22 +254,16 @@ class CommandParser(object):
             if len(fields) != 3:
                 bailout("Error at line %d : one of the fields is not correctly formatted." % i)
 
-            dict = {"request" : "update-entry", "listname" : listname,
+            dict = {"request" : "create-entry", "listname" : listname,
                     "index" : i, "contents" : {"title" : fields[0], "date" : fields[1], "done": fields[2]}}
 
             create_spool_file(dict)
-
+            
             # update the cached list
-            try:
-                l.contents["contents"][i]["title"] = fields[0]
-                l.contents["contents"][i]["date"] = fields[1]
-                l.contents["contents"][i]["done"] = fields[2]
-            except IndexError:
-                continue
-                
-        # and get rid of the element that got removed :
-        for i in range(len(l.contents["contents"]) - len(lines)):
-            l.contents["contents"].pop()
+
+            cdict = {"title" : fields[0], "date": fields[1], "done" : fields[2]}
+            l.contents["contents"].append(cdict)
+            time.sleep(0.2)
 
         l.save()
         os.unlink(filename)
@@ -286,7 +293,7 @@ class CommandParser(object):
             
             if line != '':
                 title = unicode(line.strip(), encoding="utf8")
-                dict = {"request" : "create-entry", "listname" : listname, "content" : {"title" : title}}
+                dict = {"request" : "create-entry", "listname" : listname, "contents" : {"title" : title}}
                 create_spool_file(dict)
                 cached_list = CachedList(listname)
 
@@ -301,6 +308,6 @@ if __name__ == "__main__":
     if len(sys.argv) <= 1:
         help()
         sys.exit(0)
-
-    os.system("./liszt-daemon")
+    
+    subprocess.Popen(["./liszt-daemon"])
     parser = CommandParser(sys.argv[1:])
index 0f571c2d2c31eaffa85c2d2dc1fe34aa8a1031d9..65cb508f6c7074452a41d5507a6cc30b08065b2f 100755 (executable)
@@ -17,7 +17,7 @@ import threading
 import time
 import ConfigParser
 
-from libliszt.utils import Daemon, LisztSession, CachedList
+from libliszt.utils import Daemon, LisztSession, CachedList, sorted_dirlist
 
 
 try:
@@ -52,7 +52,7 @@ def process_json_delayed_file(path, session):
         session.create_list(data["listname"])
     elif data["request"] == "create-entry":
         try:
-            session.create_list_entry(data["listname"], data["content"]["title"])      # FIXME : use the other options if defined.
+            session.create_list_entry(data["listname"], data["contents"]["title"])     # FIXME : use the other options if defined.
         except KeyError:
             return
 
@@ -60,12 +60,12 @@ def process_json_delayed_file(path, session):
         session.remove_list(data["listname"])
     elif data["request"] == "delete-entry":
         try:
-            session.remove_list_entry(data["listname"], data["content"]["index"])
+            session.remove_list_entry(data["listname"], data["contents"]["index"])
         except KeyError:
             return
     elif data["request"] == "update-entry":
         try:
-            session.update_list_entry(data["listname"], data["content"]["index"], data["content"])
+            session.update_list_entry(data["listname"], data["contents"]["index"], data["content"])
         except KeyError:
             return
         
@@ -73,7 +73,7 @@ def process_json_delayed_file(path, session):
 
 def watch_spool_dir():
     while True:
-        for file in os.listdir(os.path.expanduser('~/.liszt/delayed/')):
+        for file in sorted_dirlist(os.path.expanduser('~/.liszt/delayed/')):
             process_json_delayed_file(os.path.expanduser('~/.liszt/delayed/') + file, session)