diff --git a/bin/yakity b/bin/yakity index 1b58618..e395cbc 100755 --- a/bin/yakity +++ b/bin/yakity @@ -11,6 +11,8 @@ from yakity import commandline, configs def main(environ, argv): parser = optparse.OptionParser() parser.add_option("-c", "--configfile", default="yakity.conf") + parser.add_option("-!", "--no-auto-join", action="store_false", + default=True, dest="auto_join") options, args = parser.parse_args(argv[1:]) @@ -20,7 +22,7 @@ def main(environ, argv): print >> sys.stderr, "missing command" return 1 - getattr(commandline, args[0])(conf, *args[1:]) + getattr(commandline, args[0])(options, *args[1:]) return 0 diff --git a/examples/chatserver.py b/examples/chatserver.py new file mode 100644 index 0000000..1fb4f40 --- /dev/null +++ b/examples/chatserver.py @@ -0,0 +1,238 @@ +#!/usr/bin/env python +# vim: fileencoding=utf8:et:sta:ai:sw=4:ts=4:sts=4 + +from yakity import configs, client, wsgi_api +from feather import wsgi + + +CONF = configs.get_configs("examples/single.conf") + +def app(environ, start_response): + if environ['PATH_INFO'] in ('/', '/index.html'): + start_response("200 OK", [ + ("Content-Type", "text/html"), + ("Content-Length", str(len(homepage))) + ]) + return [homepage] + + return wsgi_api.WSGIApp(CONF)(environ, start_response) + + +HOMEPAGE = """ + + + + + + + +
+
+ + + + + + + + + + + +
user name:
room name:
+
+
+ +
+
+
+
+ + + + + +""".strip() + + +wsgi.serve(("localhost", 8989), app, worker_count=1) diff --git a/yakity/commandline.py b/yakity/commandline.py index e8a1a05..0a87a1e 100644 --- a/yakity/commandline.py +++ b/yakity/commandline.py @@ -10,7 +10,8 @@ signal = greenhouse.patched("signal") -def runservice(conf, instance_name): +def runservice(options, instance_name): + conf = configs.get_configs(options.configfile) instance = [inst for inst in conf.instances if inst.name == instance_name] if not instance: print >> sys.stderr, "unknown shard %r" % instance_name @@ -28,7 +29,8 @@ def runservice(conf, instance_name): pass -def listen(conf, roomname, username=None): +def listen(options, roomname, username=None): + conf = configs.get_configs(options.configfile) yak = client.Yakity(conf, client.prepare_client( conf, room_hint=roomname, user_hint=username), None) @@ -59,7 +61,8 @@ def listen(conf, roomname, username=None): pass -def speak(conf, roomname, username): +def converse(options, roomname, username): + conf = configs.get_configs(options.configfile) def int_handler(signum, frame): greenhouse.end(speaker_glet) signal.signal(signal.SIGINT, int_handler) @@ -73,14 +76,36 @@ def int_handler(signum, frame): @greenhouse.greenlet def speaker_glet(): try: - yak.join(roomname) + if options.auto_join: + yak.join(roomname) print "(ctrl-c to exit)" while 1: greenhouse.stdout.write("%s> " % roomname) line = greenhouse.stdin.readline().rstrip() if line: yak.say(roomname, line) finally: - yak.depart(roomname) + if options.auto_join: + yak.depart(roomname) finished.set() finished.wait() + + +def join(options, roomname, username): + conf = configs.get_configs(options.configfile) + yak = client.Yakity(conf, client.prepare_client( + conf, room_hint=roomname, user_hint=username), username) + yak.join(roomname) + + +def depart(options, roomname, username): + conf = configs.get_configs(options.configfile) + yak = client.Yakity(conf, client.prepare_client( + conf, room_hint=roomname, user_hint=username), username) + yak.depart(roomname) + +def say(options, roomname, username, msg): + conf = configs.get_configs(options.configfile) + yak = client.Yakity(conf, client.prepare_client( + conf, room_hint=roomname, user_hint=username), username) + yak.say(roomname, msg) diff --git a/yakity/wsgi_api.py b/yakity/wsgi_api.py index c3e41b7..8fad101 100644 --- a/yakity/wsgi_api.py +++ b/yakity/wsgi_api.py @@ -5,9 +5,9 @@ import time import traceback -import yajl - +import greenhouse from . import client +import yajl class WSGIApp(object): @@ -50,8 +50,7 @@ def _handle(self, environ, start_response): yak.depart(roomname) return '{"success":true}' - def __call__(self, environ, start_response): - # auth required + def _auth(self, environ, start_response): if 'HTTP_AUTHORIZATION' not in environ: start_response("401 Unauthorized", [ ("Content-Type", "text/plain"), @@ -70,6 +69,12 @@ def __call__(self, environ, start_response): return ["basic authorization required."] environ['auth'] = token.decode("base64").split(":", 1) + def __call__(self, environ, start_response): + # auth required + result = self._auth(environ, start_response) + if result is not None: + return result + try: result = self._handle(environ, start_response) rc = "200 OK"