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
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:
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.
import readline
import tempfile
import datetime
+import time
import locale
import codecs
+import subprocess
from libliszt.utils import CachedList
try:
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):
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.
l = CachedList(listname)
entries = l.contents["contents"]
- # unicode magic.
+ # unicode magic.
fd = codecs.getwriter(locale.getpreferredencoding())(fd)
for entry in entries:
# 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
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)
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)
if len(sys.argv) <= 1:
help()
sys.exit(0)
-
- os.system("./liszt-daemon")
+
+ subprocess.Popen(["./liszt-daemon"])
parser = CommandParser(sys.argv[1:])
import time
import ConfigParser
-from libliszt.utils import Daemon, LisztSession, CachedList
+from libliszt.utils import Daemon, LisztSession, CachedList, sorted_dirlist
try:
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
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
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)