]> khamidou — GitHub mirrors - liszt.git/commitdiff
adding a userspace daemon to increase speed
authorkhamidou <khamidou@gmail.com>
Tue, 9 Feb 2010 09:09:42 +0000 (10:09 +0100)
committerkhamidou <khamidou@gmail.com>
Tue, 9 Feb 2010 09:09:42 +0000 (10:09 +0100)
liszt-daemon [new file with mode: 0644]
liszt-setup [new file with mode: 0644]
maketar.sh [new file with mode: 0755]

diff --git a/liszt-daemon b/liszt-daemon
new file mode 100644 (file)
index 0000000..2ade118
--- /dev/null
@@ -0,0 +1,136 @@
+#!/usr/bin/env python
+# This is the daemon that actually does the network operations.
+
+import sys
+import os
+from datetime import datetime
+import urllib, urllib2, cookielib
+from urllib import quote
+from urllib2 import HTTPError, URLError
+import subprocess
+from subprocess import Popen
+import fnmatch
+import tempfile
+
+try:
+    import json 
+except ImportError:
+    import simplejson as json                                                                                                                         
+
+
+base = "http://soundbush.com:8080/api/v1"
+
+conf = ConfigParser.RawConfigParser()
+conf.read(os.path.expanduser('~/.liszt/config'))
+username = conf.get('Credentials', 'username')
+password = conf.get('Credentials', 'password')
+
+
+class LisztSession:
+    def __init__(self):
+
+        self.jar = cookielib.LWPCookieJar()
+        if os.path.isfile('~/.liszt/cookiejar'):
+            self.jar.load('~/.liszt/cookiejar')
+
+        self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.jar))
+        urllib2.install_opener(self.opener)
+
+    def login(self, username, password):
+        d = {'username' : username, 'password': password}
+        ed = urllib.urlencode(d)
+        try:
+            urllib2.urlopen(base + "/login/", ed)
+        except HTTPError:
+            bailout("Unable to log you in - are you credentials correct ?")
+        except URLError:
+            bailout("Unable to connect to the remote host.")
+
+    def create_list(self, listname):
+        try:
+            urllib2.urlopen(base + "/create/" + quote(listname) + "/")
+        except HTTPError:
+            bailout("Unable to create the list " + listname)
+
+    def create_list_entry(self, listname, entry):
+        try:
+            urllib2.urlopen(base + "/create/" + quote(listname) + "/" + quote(entry) + "/")
+        except HTTPError:
+            bailout("Unable to create the list entry in list " + listname)
+
+    def get_lists_list(self):
+        try:
+            req = urllib2.urlopen(base + "/getlists/")
+        except HTTPError:
+            bailout("Unable to get a list of the available lists")
+            
+        return json.load(req)
+
+    def get_list(self, listname):
+        ln = self.glob_listname(listname)
+
+        try:
+            req = urllib2.urlopen(base + "/get/" + quote(ln) + "/")
+        except HTTPError:
+            bailout("Unable to open the list " + listname)
+
+        return json.load(req)
+
+    def get_entry(self, listname, index):
+        ln = self.glob_listname(listname)
+
+        try:
+            req = urllib2.urlopen(base + "/get/" + quote(ln) + "/" + str(index) + "/")
+        except HTTPError:
+            bailout("Unable to get entry " + str(index) + " from list " + listname)
+
+        return json.load(req)
+
+    def remove_list(self, listname):
+        try:
+            urllib2.urlopen(base + "/remove/" + quote(listname) + "/")
+        except HTTPError:
+            bailout("Unable to remove the list " + listname)
+
+    def remove_list_entry(self, listname, index):
+        try:
+            urllib2.urlopen(base + "/remove/" + quote(listname) + "/" + quote(index) + "/")
+        except HTTPError:
+            bailout("Unable to remove the entry " + index + "from list " + listname)
+
+    def update_list_entry(self, listname, index, contents):
+        ec = urllib.urlencode(contents)
+        try:
+            urllib2.urlopen(base + "/update/" + listname + "/" + str(index) + "/", ec)
+        except HTTPError:
+            bailout("Unable to update the list entry")
+        
+    def glob_listname(self, pattern):
+        
+        def match_listname(l):
+            # This function doesn't seem very pythonic...
+            if len(l) > 1:
+                print str(len(l)) + " lists satifisfy the pattern : "
+                print l
+
+                try:
+                    pat = raw_input("Enter a matching pattern to clarify your choice : ")
+                except EOFError:
+                    sys.exit(-1)
+
+                return match_listname(fnmatch.filter(l, pat))
+            else:
+                return l[0]
+
+        try:
+            lists = self.get_lists_list()
+            res = fnmatch.filter(lists, pattern)
+
+            if len(res) > 1:
+                return match_listname(res)
+            else:
+                return res[0]
+
+        except HTTPError:
+            bailout("Unable to glob list pattern")
+
diff --git a/liszt-setup b/liszt-setup
new file mode 100644 (file)
index 0000000..33ed1ee
--- /dev/null
@@ -0,0 +1,2 @@
+#!/usr/bin/env python
+# This program setups the ~/.liszt directory.
diff --git a/maketar.sh b/maketar.sh
new file mode 100755 (executable)
index 0000000..75619ca
--- /dev/null
@@ -0,0 +1 @@
+tar cvzf liszt-latest.tar.gz ../liszt/ --exclude=.git