Permalink
Browse files

Updated the command line interface.

A thoonk-cli script is now installed on setup.
  • Loading branch information...
1 parent fc6803c commit b8844fa29df9b192d49adaf2f4ee25148968cc09 @legastero legastero committed Jun 14, 2011
Showing with 105 additions and 60 deletions.
  1. +0 −6 cli.py
  2. +5 −0 scripts/thoonk-cli
  3. +1 −0 setup.py
  4. +3 −0 thoonk/__init__.py
  5. +96 −54 thoonk/cli.py
View
6 cli.py
@@ -1,6 +0,0 @@
-from thoonk.pubsub import Pubsub
-from thoonk.cli import CLInterface
-
-p = Pubsub(listen=True, db=0)
-i = CLInterface()
-p.register_interface(i)
View
@@ -0,0 +1,5 @@
+#!/usr/bin/env python
+
+from thoonk.cli import CLInterface
+
+CLInterface().start()
View
@@ -49,6 +49,7 @@
license = 'MIT',
platforms = [ 'any' ],
packages = packages,
+ scripts = ['scripts/thoonk-cli'],
py_modules = [],
requires = [],
)
View
@@ -1,2 +1,5 @@
from pubsub import Thoonk
from pubsub import Thoonk as Pubsub
+
+__version__ = '1.0.0rc1'
+__version_info__ = (1, 0, 0, 'rc1', 0)
View
@@ -1,92 +1,134 @@
import Queue
-from pubsub import ACL, Interface
+import cmd
import threading
import traceback
import sys
-from exceptions import FeedExists
-class CLInterface(Interface):
- name = "CLI"
+import thoonk
+from thoonk import Thoonk
+from thoonk.exceptions import FeedExists
+
+
+class CLInterface(cmd.Cmd):
+
+ def __init__(self, host='localhost', port=6379, db=0):
+ cmd.Cmd.__init__(self)
+ self.thoonk = Thoonk(host, port, db)
+ self.lthoonk = Thoonk(host, port, db)
+ self.intro = 'Thoonk.py v%s Client' % thoonk.__version__
+ self.prompt = '>>> '
+ self.lthoonk.register_handler('publish_notice', self.publish_notice)
+ self.lthoonk.register_handler('retract_notice', self.retract_notice)
+ self.lthoonk.register_handler('create_notice', self.create_notice)
+ self.lthoonk.register_handler('delete_notice', self.delete_notice)
def start(self):
- self.acl = ACL()
- self.thread = threading.Thread(target=self.listen)
- #self.thread.setDaemon()
+ self.thread = threading.Thread(target=self.lthoonk.listen)
+ self.thread.daemon = True
self.thread.start()
+ self.lthoonk.listen_ready.wait()
+ self.cmdloop()
+
+ def parseline(self, line):
+ line = cmd.Cmd.parseline(self, line)
+ if line[0] != 'help':
+ return (line[0], line[1].split(' '), line[2])
+ return line
- def listen(self):
- while True:
- try:
- cmd = raw_input(">")
- if cmd == 'quit':
- break
- elif cmd.strip() == "":
- continue
- if len(cmd.split()) > 1:
- args = cmd.split()[1:]
- else:
- args = []
- cmd = cmd.split()[0]
- if hasattr(self, "cmd_%s" % cmd):
- getattr(self, "cmd_%s" % cmd)(*args)
- else:
- print "Command %s not found." % cmd
- except EOFError:
- break
- except:
- traceback.print_exc()
-
- def cmd_create(self, *args):
+ def do_EOF(self, line):
+ return True
+
+ def do_quit(self, line):
+ return True
+
+ def help_quit(self):
+ print 'Quit'
+
+ def do_create(self, args):
try:
- self.pubsub.create_feed(args[0], {})
+ self.thoonk.create_feed(args[0], {})
except FeedExists:
print "Feed already exists"
- def cmd_publish(self, *args):
- self.pubsub[args[0]].publish(" ".join(args[1:]))
+ def help_create(self):
+ print 'create [feed name]'
+ print 'Create a new feed with the given name'
+
+ def do_publish(self, args):
+ self.thoonk[args[0]].publish(" ".join(args[1:]))
- def cmd_delete(self, *args):
- self.pubsub[args[0]].delete_feed()
+ def help_publish(self):
+ print 'publish [item contents]'
+ print 'Publish a string to the feed (may include spaces)'
- def cmd_retract(self, *args):
- self.pubsub[args[0]].retract(args[1])
+ def do_delete(self, args):
+ self.thoonk[args[0]].delete_feed()
- def cmd_feeds(self, *args):
- print self.pubsub.get_feeds()
+ def help_delete(self):
+ print 'delete [feed name]'
+ print 'Delete the given feed'
- def cmd_items(self, *args):
- print self.pubsub[args[0]].get_items()
+ def do_retract(self, args):
+ self.thoonk[args[0]].retract(args[1])
- def cmd_item(self, *args):
+ def help_retract(self):
+ print 'retract [feed name] [item id]'
+ print 'Remove an item from a feed.'
+
+ def do_feeds(self, args):
+ print self.thoonk.get_feeds()
+
+ def help_feeds(self):
+ print 'List existing feeds'
+
+ def do_items(self, args):
+ print self.thoonk[args[0]].get_all()
+
+ def help_items(self):
+ print 'items [feed name]'
+ print 'List items in a given feed.'
+
+ def do_item(self, args):
if len(args) == 1:
args.append(None)
- print self.pubsub[args[0]].get_item(args[1])
+ print self.thoonk[args[0]].get_item(args[1])
+
+ def help_item(self):
+ print 'item [feed name] [id]'
+ print 'Print the contents of a feed item'
- def cmd_retract(self, *args):
- print self.pubsub[args[0]].retract(args[1])
+ def do_getconfig(self, args):
+ print self.thoonk[args[0]].config
- def cmd_getconfig(self, *args):
- print self.pubsub[args[0]].config
+ def help_getconfig(self):
+ print 'getconfig [feed name]'
+ print 'Show the JSON configuration for a feed'
- def cmd_setconfig(self, *args):
+ def do_setconfig(self, args):
feed = args[0]
config = ' '.join(args[1:])
- self.pubsub[feed].config = config
+ self.thoonk[feed].config = config
print "Ok."
+ def help_setconfig(self):
+ print 'setconfig [feed name] [config]'
+ print 'Set the configuration for a feed'
+
def publish_notice(self, feed, item, id):
- print "publish: %s[%s]: %s" % (feed, id, item)
+ print "\npublish: %s[%s]: %s" % (feed, id, item)
def retract_notice(self, feed, id):
- print "retract: %s[%s]" % (feed, id)
+ print "\nretract: %s[%s]" % (feed, id)
def create_notice(self, feed):
- print "created: %s" % feed
+ print "\ncreated: %s" % feed
def delete_notice(self, feed):
- print "deleted: %s" % feed
+ print "\ndeleted: %s" % feed
def finish_notice(self, feed, id, item, result):
- print "finished: %s[%s]: %s -> %s" % (feed, id, item, result)
+ print "\nfinished: %s[%s]: %s -> %s" % (feed, id, item, result)
+if __name__ == '__main__':
+ CLInterface().start()

0 comments on commit b8844fa

Please sign in to comment.