From ad6a453a0f00d20f0668b17b61668cb7fa5ee4e4 Mon Sep 17 00:00:00 2001 From: Devendra Date: Fri, 25 Apr 2014 03:26:09 +0530 Subject: v1 of console --- python/examples/console.py | 326 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 326 insertions(+) create mode 100644 python/examples/console.py (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py new file mode 100644 index 0000000..bd364f7 --- /dev/null +++ b/python/examples/console.py @@ -0,0 +1,326 @@ +## www.pubnub.com - PubNub Real-time push service in the cloud. +# coding=utf8 + +## PubNub Real-time Push APIs and Notifications Framework +## Copyright (c) 2010 Stephen Blum +## http://www.pubnub.com/ + +import sys +from Pubnub import PubnubAsync as Pubnub +import threading +from datetime import datetime + +from cmd2 import Cmd, make_option, options, Cmd2TestCase +import optparse + + +of=sys.stdout + + +class color(object): + PURPLE = '\033[95m' + CYAN = '\033[96m' + DARKCYAN = '\033[36m' + BLUE = '\033[94m' + GREEN = '\033[92m' + YELLOW = '\033[93m' + RED = '\033[91m' + BOLD = '\033[1m' + UNDERLINE = '\033[4m' + END = '\033[0m' + +def print_ok(msg, channel=None): + chstr = color.PURPLE + "[" + datetime.now().strftime( + '%Y-%m-%d %H:%M:%S') + "] " + color.END + chstr += color.CYAN + "[Channel : " + channel + \ + "] " if channel is not None else "" + color.END + try: + print >>of, ("\n") + print >>of, (chstr + color.GREEN + str(msg) + color.END) + except UnicodeEncodeError as e: + print >>of, (msg) + +def print_error(msg, channel=None): + chstr = color.PURPLE + "[" + datetime.now().strftime( + '%Y-%m-%d %H:%M:%S') + "] " + color.END + chstr += color.CYAN + "[Channel : " + channel + \ + "] " if channel is not None else "" + color.END + try: + print >>of, ("\n") + print >>of, (chstr + color.RED + color.BOLD + str(msg) + color.END) + except UnicodeEncodeError as e: + print >>of, (msg) + +class DefaultPubnub(object): + def handlerFunctionClosure(self,name): + def handlerFunction(*args,**kwargs): + print_error("Pubnub not initialized. Use init command to initialize") + return handlerFunction + def __getattr__(self,name): + return self.handlerFunctionClosure(name) + +pubnub=DefaultPubnub() + + + +def kill_all_threads(): + for thread in threading.enumerate(): + if thread.isAlive(): + thread._Thread__stop() + +def get_input(message, t=None): + while True: + try: + try: + command = raw_input(message) + except NameError: + command = input(message) + except KeyboardInterrupt: + return None + + command = command.strip() + + if command is None or len(command) == 0: + raise ValueError + + if t is not None and t == bool: + valid = ["True", "true", "1", 1, "y", "Y", "yes", "Yes", "YES"] + if command in valid: + return True + else: + return False + if t is not None: + command = t(command) + else: + command = eval("'" + command + "'") + + return command + except ValueError: + print_error("Invalid input : " + command) + +def _publish_command_handler(channel, message): + def _callback(r): + print_ok(r) + + def _error(r): + print_error(r) + pubnub.publish(channel, message, _callback, _error) + + +def _subscribe_command_handler(channel): + + def _callback(r): + print_ok(r, channel) + + def _error(r): + print_error(r, channel) + pubnub.subscribe(channel, _callback, _error) + + +def _unsubscribe_command_handler(channel): + + def _callback(r): + print_ok(r) + + def _error(r): + print_error(r) + pubnub.unsubscribe(channel) + + +def _grant_command_handler(channel, auth_key, read, write, ttl): + def _callback(r): + print_ok(r) + + def _error(r): + print_error(r) + + pubnub.grant(channel, auth_key, read, write, ttl, _callback) + + +def _revoke_command_handler(channel, auth_key, ttl): + def _callback(r): + print_ok(r) + + def _error(r): + print_error(r) + + pubnub.revoke(channel, auth_key, ttl, _callback) + + +def _audit_command_handler(channel, auth_key): + def _callback(r): + print_ok(r) + + def _error(r): + print_error(r) + + pubnub.audit(channel, auth_key, _callback) + + +def _history_command_handler(channel, count): + def _callback(r): + print_ok(r) + + def _error(r): + print_error(r) + + pubnub.history(channel, count, callback=_callback, error=_error) + + +def _here_now_command_handler(channel): + def _callback(r): + print_ok(r) + + def _error(r): + print_error(r) + + pubnub.here_now(channel, callback=_callback, error=_error) + + +class DevConsole(Cmd): + multilineCommands = ['orate'] + Cmd.shortcuts.update({'&': 'speak'}) + maxrepeats = 3 + Cmd.settable.append('maxrepeats') + + def __init__(self): + Cmd.__init__(self) + self.prompt = "(PubNub Console) > " + self.intro = "Welcome to PubNub Developer Console!" ## defaults to None + + def cmdloop(self): + try: + Cmd.cmdloop(self) + except KeyboardInterrupt as e: + self.cmdloop() + + + @options([make_option('-p', '--publish-key', action="store", default="demo", help="Publish Key"), + make_option('-s', '--subscribe-key', action="store", default="demo", help="Subscribe Key"), + make_option('-k', '--secret-key', action="store", default="demo", help="cipher Key"), + make_option('-c', '--cipher-key', action="store", default="", help="Secret Key"), + make_option('-a', '--auth-key', action="store", default=None, help="Auth Key"), + make_option('--ssl-on', dest='ssl', action='store_true', default=False, help="SSL Enabled ?"), + make_option('-o', '--origin', action="store", default="pubsub.pubnub.com", help="Origin"), + make_option('-u', '--uuid', action="store", default=None, help="UUID") + ]) + def do_init(self, command, opts): + global pubnub + pubnub = Pubnub(opts.publish_key, + opts.subscribe_key, + opts.secret_key, + opts.cipher_key, + opts.auth_key, + opts.ssl, + opts.origin, + opts.uuid) + + @options([make_option('-c', '--channel', action="store", help="Channel for here now data") + ]) + def do_here_now(self, command, opts): + + if opts.channel is None: + print_error("Missing channel") + return + + _here_now_command_handler(opts.channel) + + @options([make_option('-c', '--channel', action="store", help="Channel for history data"), + make_option('-n', '--count', action="store", default=5, help="Number of messages") + ]) + def do_history(self, command, opts): + + if opts.channel is None: + print_error("Missing channel") + return + + _history_command_handler(opts.channel, opts.count) + + + @options([make_option('-c', '--channel', action="store", help="Channel on which to publish"), + make_option('-m', '--message', action="store", help="Message to be published") + ]) + def do_publish(self, command, opts): + + if opts.channel is None: + print_error("Missing channel") + return + + if opts.message is None: + print_error("Missing message") + return + + _publish_command_handler(opts.channel,opts.message) + + @options([make_option('-c', '--channel', action="store", help="Channel on which to grant"), + make_option('-a', '--auth-key', dest="auth_key", action="store", + help="Auth Key"), + make_option('-r', '--read-enabled', dest='read', action='store_true', default=False, help="Read ?"), + make_option('-w', '--write-enabled', dest='write', action='store_true', default=False, help="Write ?"), + make_option('-t', '--ttl', action="store", default=5, help="TTL") + ]) + def do_grant(self, command, opts): + if opts.channel is None: + print_error("Missing channel") + return + + opts.auth_key = pubnub.auth_key if opts.auth_key is None else opts.auth_key + + _grant_command_handler(opts.channel,opts.auth_key, opts.read, opts.write, opts.ttl) + + @options([make_option('-c', '--channel', action="store", help="Channel on which to revoke"), + make_option('-a', '--auth-key', dest="auth_key", action="store", + help="Auth Key"), + make_option('-t', '--ttl', action="store", default=5, help="TTL") + ]) + def do_revoke(self, command, opts): + if opts.channel is None: + print_error("Missing channel") + return + + opts.auth_key = pubnub.auth_key if opts.auth_key is None else opts.auth_key + + _revoke_command_handler(opts.channel,opts.auth_key, opts.ttl) + + @options([make_option('-a', '--auth-key', dest="auth_key", action="store", + help="Auth Key") + ]) + def do_audit(self, command, opts): + + opts.auth_key = pubnub.auth_key if opts.auth_key is None else opts.auth_key + + _audit_command_handler(opts.auth_key) + + + @options([make_option('-c', '--channel', action="store", help="Channel for unsubscribe") + ]) + def do_unsubscribe(self, command, opts): + if opts.channel is None: + print_error("Missing channel") + return + _unsubscribe_command_handler(opts.channel) + + + @options([make_option('-c', '--channel', action="store", help="Channel for subscribe") + ]) + def do_subscribe(self, command, opts): + + if opts is None: + print_error("Missing argument") + return + + if opts.channel is None: + print_error("Missing channel") + return + + _subscribe_command_handler(opts.channel) + + + +def main(): + app = DevConsole() + app.cmdloop() + +if __name__ == "__main__": + main() + -- cgit v1.2.3 From 9e4032331721539d2b1e74b87b86ac5c66ccc4af Mon Sep 17 00:00:00 2001 From: Devendra Date: Fri, 25 Apr 2014 10:54:36 +0530 Subject: more changes to new console --- python/examples/console.py | 78 ++++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 33 deletions(-) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index bd364f7..4dfd891 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -16,40 +16,29 @@ import optparse of=sys.stdout - -class color(object): - PURPLE = '\033[95m' - CYAN = '\033[96m' - DARKCYAN = '\033[36m' - BLUE = '\033[94m' - GREEN = '\033[92m' - YELLOW = '\033[93m' - RED = '\033[91m' - BOLD = '\033[1m' - UNDERLINE = '\033[4m' - END = '\033[0m' +color = Cmd() def print_ok(msg, channel=None): - chstr = color.PURPLE + "[" + datetime.now().strftime( - '%Y-%m-%d %H:%M:%S') + "] " + color.END - chstr += color.CYAN + "[Channel : " + channel + \ - "] " if channel is not None else "" + color.END + chstr = color.colorize("[" + datetime.now().strftime( + '%Y-%m-%d %H:%M:%S') + "] ","magenta") + chstr += color.colorize("[Channel : " + channel + \ + "] " if channel is not None else "", "cyan") try: - print >>of, ("\n") - print >>of, (chstr + color.GREEN + str(msg) + color.END) + print >>of, (chstr + color.colorize(str(msg),"green")) except UnicodeEncodeError as e: print >>of, (msg) + of.flush() def print_error(msg, channel=None): - chstr = color.PURPLE + "[" + datetime.now().strftime( - '%Y-%m-%d %H:%M:%S') + "] " + color.END - chstr += color.CYAN + "[Channel : " + channel + \ - "] " if channel is not None else "" + color.END + chstr = color.colorize("[" + datetime.now().strftime( + '%Y-%m-%d %H:%M:%S') + "] ", "magenta") + chstr += color.colorize("[Channel : " + channel + \ + "] " if channel is not None else "", "cyan") try: - print >>of, ("\n") - print >>of, (chstr + color.RED + color.BOLD + str(msg) + color.END) + print >>of, (chstr + color.colorize(color.colorize(str(msg),"red"),"bold")) except UnicodeEncodeError as e: print >>of, (msg) + of.flush() class DefaultPubnub(object): def handlerFunctionClosure(self,name): @@ -177,16 +166,16 @@ def _here_now_command_handler(channel): pubnub.here_now(channel, callback=_callback, error=_error) -class DevConsole(Cmd): - multilineCommands = ['orate'] - Cmd.shortcuts.update({'&': 'speak'}) - maxrepeats = 3 - Cmd.settable.append('maxrepeats') +class DevConsole(Cmd): + def __init__(self): Cmd.__init__(self) + global pubnub self.prompt = "(PubNub Console) > " self.intro = "Welcome to PubNub Developer Console!" ## defaults to None + self.default_channel = None + pubnub = Pubnub("demo", "demo") def cmdloop(self): try: @@ -215,10 +204,30 @@ class DevConsole(Cmd): opts.origin, opts.uuid) + + @options([make_option('-c', '--channel', action="store", help="Default Channel") + ]) + def do_set_default_channel(self, command, opts): + + if opts.channel is None: + print_error("Missing channel") + return + self.default_channel = opts.channel + + @options([make_option('-f', '--file', action="store", default="./pubnub-console.log", help="Output file") + ]) + def do_set_output_file(self, command, opts): + global of + try: + of = file(opts.file,'w+') + except IOError as e: + print_error("Could not set output file. " + e.reason) + + @options([make_option('-c', '--channel', action="store", help="Channel for here now data") ]) def do_here_now(self, command, opts): - + opts.channel = self.default_channel if opts.channel is None else opts.channel if opts.channel is None: print_error("Missing channel") return @@ -229,7 +238,7 @@ class DevConsole(Cmd): make_option('-n', '--count', action="store", default=5, help="Number of messages") ]) def do_history(self, command, opts): - + opts.channel = self.default_channel if opts.channel is None else opts.channel if opts.channel is None: print_error("Missing channel") return @@ -241,7 +250,7 @@ class DevConsole(Cmd): make_option('-m', '--message', action="store", help="Message to be published") ]) def do_publish(self, command, opts): - + opts.channel = self.default_channel if opts.channel is None else opts.channel if opts.channel is None: print_error("Missing channel") return @@ -260,6 +269,7 @@ class DevConsole(Cmd): make_option('-t', '--ttl', action="store", default=5, help="TTL") ]) def do_grant(self, command, opts): + opts.channel = self.default_channel if opts.channel is None else opts.channel if opts.channel is None: print_error("Missing channel") return @@ -274,6 +284,7 @@ class DevConsole(Cmd): make_option('-t', '--ttl', action="store", default=5, help="TTL") ]) def do_revoke(self, command, opts): + opts.channel = self.default_channel if opts.channel is None else opts.channel if opts.channel is None: print_error("Missing channel") return @@ -295,6 +306,7 @@ class DevConsole(Cmd): @options([make_option('-c', '--channel', action="store", help="Channel for unsubscribe") ]) def do_unsubscribe(self, command, opts): + opts.channel = self.default_channel if opts.channel is None else opts.channel if opts.channel is None: print_error("Missing channel") return @@ -304,7 +316,7 @@ class DevConsole(Cmd): @options([make_option('-c', '--channel', action="store", help="Channel for subscribe") ]) def do_subscribe(self, command, opts): - + opts.channel = self.default_channel if opts.channel is None else opts.channel if opts is None: print_error("Missing argument") return -- cgit v1.2.3 From 44aee82f3eadea1241ac4350b4f9f34daf58eb87 Mon Sep 17 00:00:00 2001 From: Devendra Date: Fri, 25 Apr 2014 19:42:21 +0530 Subject: improvements to console --- python/examples/console.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index 4dfd891..aa6fff3 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -12,7 +12,7 @@ from datetime import datetime from cmd2 import Cmd, make_option, options, Cmd2TestCase import optparse - +import json of=sys.stdout @@ -246,8 +246,7 @@ class DevConsole(Cmd): _history_command_handler(opts.channel, opts.count) - @options([make_option('-c', '--channel', action="store", help="Channel on which to publish"), - make_option('-m', '--message', action="store", help="Message to be published") + @options([make_option('-c', '--channel', action="store", help="Channel on which to publish") ]) def do_publish(self, command, opts): opts.channel = self.default_channel if opts.channel is None else opts.channel @@ -255,11 +254,16 @@ class DevConsole(Cmd): print_error("Missing channel") return - if opts.message is None: + if command is None: print_error("Missing message") return - _publish_command_handler(opts.channel,opts.message) + try: + message = json.loads(str(command)) + except ValueError as ve: + message = str(command) + + _publish_command_handler(opts.channel,message) @options([make_option('-c', '--channel', action="store", help="Channel on which to grant"), make_option('-a', '--auth-key', dest="auth_key", action="store", @@ -327,8 +331,6 @@ class DevConsole(Cmd): _subscribe_command_handler(opts.channel) - - def main(): app = DevConsole() app.cmdloop() -- cgit v1.2.3 From 2b72900bab9af06f37bd024c4aad8da3513a2430 Mon Sep 17 00:00:00 2001 From: Devendra Date: Fri, 25 Apr 2014 20:15:55 +0530 Subject: clean exit on typing exit --- python/examples/console.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index aa6fff3..1694ba5 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -166,6 +166,12 @@ def _here_now_command_handler(channel): pubnub.here_now(channel, callback=_callback, error=_error) +def kill_all_threads(): + for thread in threading.enumerate(): + if thread.isAlive(): + thread._Thread__stop() + + class DevConsole(Cmd): @@ -177,11 +183,13 @@ class DevConsole(Cmd): self.default_channel = None pubnub = Pubnub("demo", "demo") - def cmdloop(self): + def cmdloop_with_keyboard_interrupt(self): try: - Cmd.cmdloop(self) - except KeyboardInterrupt as e: self.cmdloop() + except KeyboardInterrupt: + print 'KB KeyboardInterrupt' + sys.stdout.write('\n') + kill_all_threads() @options([make_option('-p', '--publish-key', action="store", default="demo", help="Publish Key"), @@ -331,9 +339,20 @@ class DevConsole(Cmd): _subscribe_command_handler(opts.channel) + def do_exit(self, args): + kill_all_threads() + return -1 + + def do_EOF(self, args): + kill_all_threads() + return self.do_exit(args) + + def handler(self, signum, frame): + kill_all_threads() + def main(): app = DevConsole() - app.cmdloop() + app.cmdloop_with_keyboard_interrupt() if __name__ == "__main__": main() -- cgit v1.2.3 From 7568c30242ae3a9f4163248a29a596fab5902601 Mon Sep 17 00:00:00 2001 From: Devendra Date: Fri, 25 Apr 2014 23:46:57 +0530 Subject: adding sync async mode to console --- python/examples/console.py | 56 +++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 20 deletions(-) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index 1694ba5..dd499c5 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -19,6 +19,8 @@ of=sys.stdout color = Cmd() def print_ok(msg, channel=None): + if msg is None: + return chstr = color.colorize("[" + datetime.now().strftime( '%Y-%m-%d %H:%M:%S') + "] ","magenta") chstr += color.colorize("[Channel : " + channel + \ @@ -30,6 +32,8 @@ def print_ok(msg, channel=None): of.flush() def print_error(msg, channel=None): + if msg is None: + return chstr = color.colorize("[" + datetime.now().strftime( '%Y-%m-%d %H:%M:%S') + "] ", "magenta") chstr += color.colorize("[Channel : " + channel + \ @@ -87,13 +91,13 @@ def get_input(message, t=None): except ValueError: print_error("Invalid input : " + command) -def _publish_command_handler(channel, message): +def _publish_command_handler(channel, message,async=False): def _callback(r): print_ok(r) def _error(r): print_error(r) - pubnub.publish(channel, message, _callback, _error) + print_ok(pubnub.publish(channel, message, _callback if async is True else None, _error if async is True else None)) def _subscribe_command_handler(channel): @@ -116,54 +120,54 @@ def _unsubscribe_command_handler(channel): pubnub.unsubscribe(channel) -def _grant_command_handler(channel, auth_key, read, write, ttl): +def _grant_command_handler(channel, auth_key, read, write, ttl,async=False): def _callback(r): print_ok(r) def _error(r): print_error(r) - pubnub.grant(channel, auth_key, read, write, ttl, _callback) + print_ok(pubnub.grant(channel, auth_key, read, write, ttl, _callback if async is True else None, _error if async is True else None)) -def _revoke_command_handler(channel, auth_key, ttl): +def _revoke_command_handler(channel, auth_key, ttl,async=False): def _callback(r): print_ok(r) def _error(r): print_error(r) - pubnub.revoke(channel, auth_key, ttl, _callback) + print_ok(pubnub.revoke(channel, auth_key, ttl, _callback if async is True else None, _error if async is True else None)) -def _audit_command_handler(channel, auth_key): +def _audit_command_handler(channel, auth_key,async=False): def _callback(r): print_ok(r) def _error(r): print_error(r) - pubnub.audit(channel, auth_key, _callback) + print_ok(pubnub.audit(channel, auth_key, _callback if async is True else None, _error if async is True else None)) -def _history_command_handler(channel, count): +def _history_command_handler(channel, count,async=False): def _callback(r): print_ok(r) def _error(r): print_error(r) - pubnub.history(channel, count, callback=_callback, error=_error) + print_ok(pubnub.history(channel, count, _callback if async is True else None, _error if async is True else None)) -def _here_now_command_handler(channel): +def _here_now_command_handler(channel,async=False): def _callback(r): print_ok(r) def _error(r): print_error(r) - pubnub.here_now(channel, callback=_callback, error=_error) + print_ok(pubnub.here_now(channel, _callback if async is True else None, _error if async is True else None)) def kill_all_threads(): @@ -181,13 +185,13 @@ class DevConsole(Cmd): self.prompt = "(PubNub Console) > " self.intro = "Welcome to PubNub Developer Console!" ## defaults to None self.default_channel = None + self.async = False pubnub = Pubnub("demo", "demo") def cmdloop_with_keyboard_interrupt(self): try: self.cmdloop() except KeyboardInterrupt: - print 'KB KeyboardInterrupt' sys.stdout.write('\n') kill_all_threads() @@ -213,6 +217,13 @@ class DevConsole(Cmd): opts.uuid) + def do_set_sync(self, command): + self.async = False + + def do_set_async(self, command): + self.async = True + + @options([make_option('-c', '--channel', action="store", help="Default Channel") ]) def do_set_default_channel(self, command, opts): @@ -240,7 +251,7 @@ class DevConsole(Cmd): print_error("Missing channel") return - _here_now_command_handler(opts.channel) + _here_now_command_handler(opts.channel,async=self.async) @options([make_option('-c', '--channel', action="store", help="Channel for history data"), make_option('-n', '--count', action="store", default=5, help="Number of messages") @@ -251,7 +262,7 @@ class DevConsole(Cmd): print_error("Missing channel") return - _history_command_handler(opts.channel, opts.count) + _history_command_handler(opts.channel, opts.count,async=self.async) @options([make_option('-c', '--channel', action="store", help="Channel on which to publish") @@ -271,7 +282,7 @@ class DevConsole(Cmd): except ValueError as ve: message = str(command) - _publish_command_handler(opts.channel,message) + _publish_command_handler(opts.channel,message,async=self.async) @options([make_option('-c', '--channel', action="store", help="Channel on which to grant"), make_option('-a', '--auth-key', dest="auth_key", action="store", @@ -288,7 +299,7 @@ class DevConsole(Cmd): opts.auth_key = pubnub.auth_key if opts.auth_key is None else opts.auth_key - _grant_command_handler(opts.channel,opts.auth_key, opts.read, opts.write, opts.ttl) + _grant_command_handler(opts.channel,opts.auth_key, opts.read, opts.write, opts.ttl,async=self.async) @options([make_option('-c', '--channel', action="store", help="Channel on which to revoke"), make_option('-a', '--auth-key', dest="auth_key", action="store", @@ -303,16 +314,21 @@ class DevConsole(Cmd): opts.auth_key = pubnub.auth_key if opts.auth_key is None else opts.auth_key - _revoke_command_handler(opts.channel,opts.auth_key, opts.ttl) + _revoke_command_handler(opts.channel,opts.auth_key, opts.ttl,async=self.async) - @options([make_option('-a', '--auth-key', dest="auth_key", action="store", + @options([make_option('-c', '--channel', action="store", help="Channel on which to revoke"), + make_option('-a', '--auth-key', dest="auth_key", action="store", help="Auth Key") ]) def do_audit(self, command, opts): + opts.channel = self.default_channel if opts.channel is None else opts.channel + if opts.channel is None: + print_error("Missing channel") + return opts.auth_key = pubnub.auth_key if opts.auth_key is None else opts.auth_key - _audit_command_handler(opts.auth_key) + _audit_command_handler(opts.channel, opts.auth_key,async=self.async) @options([make_option('-c', '--channel', action="store", help="Channel for unsubscribe") -- cgit v1.2.3 From 03eedf317ae660893f61bb919b48289b0bf971f3 Mon Sep 17 00:00:00 2001 From: Devendra Date: Fri, 25 Apr 2014 23:56:15 +0530 Subject: changing history to get_history --- python/examples/console.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index dd499c5..225c7eb 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -256,7 +256,7 @@ class DevConsole(Cmd): @options([make_option('-c', '--channel', action="store", help="Channel for history data"), make_option('-n', '--count', action="store", default=5, help="Number of messages") ]) - def do_history(self, command, opts): + def do_get_history(self, command, opts): opts.channel = self.default_channel if opts.channel is None else opts.channel if opts.channel is None: print_error("Missing channel") -- cgit v1.2.3 From 769c3a908890a15c8f5e08cd651e1120194c2e74 Mon Sep 17 00:00:00 2001 From: Devendra Date: Sat, 26 Apr 2014 00:23:30 +0530 Subject: save history to file --- python/examples/console.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index 225c7eb..8a3ab3b 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -14,6 +14,22 @@ from cmd2 import Cmd, make_option, options, Cmd2TestCase import optparse import json +import atexit +import os +import readline +import rlcompleter + +historyPath = os.path.expanduser("~/.pubnub_console_history") + +def save_history(historyPath=historyPath): + import readline + readline.write_history_file(historyPath) + +if os.path.exists(historyPath): + readline.read_history_file(historyPath) + +atexit.register(save_history) + of=sys.stdout color = Cmd() -- cgit v1.2.3 From d703c34d918a75a4d5a984143c8fbd5049931b1a Mon Sep 17 00:00:00 2001 From: Devendra Date: Tue, 29 Apr 2014 03:11:45 +0530 Subject: adding start console script and helper files --- python/examples/console.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index 8a3ab3b..e96b4e5 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -5,6 +5,7 @@ ## Copyright (c) 2010 Stephen Blum ## http://www.pubnub.com/ + import sys from Pubnub import PubnubAsync as Pubnub import threading @@ -19,6 +20,9 @@ import os import readline import rlcompleter +if sys.argv[0] == "screen": + print "screen" + historyPath = os.path.expanduser("~/.pubnub_console_history") def save_history(historyPath=historyPath): -- cgit v1.2.3 From 06511b3849e80cb5692ed92f7da31043c85556a0 Mon Sep 17 00:00:00 2001 From: Devendra Date: Thu, 1 May 2014 00:14:25 +0530 Subject: more chagnes to console --- python/examples/console.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index e96b4e5..240e5c0 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -202,11 +202,12 @@ class DevConsole(Cmd): def __init__(self): Cmd.__init__(self) global pubnub - self.prompt = "(PubNub Console) > " - self.intro = "Welcome to PubNub Developer Console!" ## defaults to None + self.intro = "For Help type ? or help . To quit/exit type exit" ## defaults to None self.default_channel = None self.async = False pubnub = Pubnub("demo", "demo") + self.prompt = "(PubNub Console) [" + color.colorize(pubnub.get_origin(),"blue") + "] > " + def cmdloop_with_keyboard_interrupt(self): try: @@ -235,12 +236,17 @@ class DevConsole(Cmd): opts.ssl, opts.origin, opts.uuid) + self.prompt = "(PubNub Console) [" + color.colorize(pubnub.get_origin(),"blue") + "] > " def do_set_sync(self, command): + """unset_async + Unset Async mode""" self.async = False def do_set_async(self, command): + """set_async + Set Async mode""" self.async = True @@ -361,7 +367,9 @@ class DevConsole(Cmd): _unsubscribe_command_handler(opts.channel) - @options([make_option('-c', '--channel', action="store", help="Channel for subscribe") + @options([make_option('-c', '--channel', action="store", help="Channel for subscribe"), + make_option('-g', '--get-channel-list', action="store_true", dest="get", + default=False, help="Get susbcribed channel list") ]) def do_subscribe(self, command, opts): opts.channel = self.default_channel if opts.channel is None else opts.channel @@ -369,11 +377,11 @@ class DevConsole(Cmd): print_error("Missing argument") return - if opts.channel is None: - print_error("Missing channel") - return + if opts.channel is not None: + _subscribe_command_handler(opts.channel) - _subscribe_command_handler(opts.channel) + if opts.get is True: + print_ok(pubnub.get_channel_array()) def do_exit(self, args): kill_all_threads() -- cgit v1.2.3 From 8a642ab92c537c84960ef3943b34aac95bc39121 Mon Sep 17 00:00:00 2001 From: Devendra Date: Fri, 2 May 2014 01:58:21 +0530 Subject: modifications for susbcribe loop events, connect, disconnect, reconnect --- python/examples/console.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index 240e5c0..fd9ebf4 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -127,7 +127,17 @@ def _subscribe_command_handler(channel): def _error(r): print_error(r, channel) - pubnub.subscribe(channel, _callback, _error) + + def _disconnect(r): + print_error("DISCONNECTED", channel) + + def _reconnect(r): + print_error("RECONNECTED", channel) + + def _connect(r): + print_error("CONNECTED", channel) + + pubnub.subscribe(channel, _callback, _error,connect=_connect, disconnect=_disconnect, reconnect=_reconnect) def _unsubscribe_command_handler(channel): -- cgit v1.2.3 From ca29b41c781c3a9861141811b208432fbda8aa0a Mon Sep 17 00:00:00 2001 From: Devendra Date: Fri, 2 May 2014 20:19:57 +0530 Subject: enahancements to dev console --- python/examples/console.py | 50 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index fd9ebf4..d43ee80 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -129,13 +129,13 @@ def _subscribe_command_handler(channel): print_error(r, channel) def _disconnect(r): - print_error("DISCONNECTED", channel) + print_error("DISCONNECTED", r) def _reconnect(r): - print_error("RECONNECTED", channel) + print_error("RECONNECTED", r) def _connect(r): - print_error("CONNECTED", channel) + print_error("CONNECTED", r) pubnub.subscribe(channel, _callback, _error,connect=_connect, disconnect=_disconnect, reconnect=_reconnect) @@ -216,16 +216,47 @@ class DevConsole(Cmd): self.default_channel = None self.async = False pubnub = Pubnub("demo", "demo") - self.prompt = "(PubNub Console) [" + color.colorize(pubnub.get_origin(),"blue") + "] > " + self.prompt = self.get_prompt() + + def get_channel_origin(self): + cho = " [" + channels = pubnub.get_channel_array() + channels_str = ",".join(channels) + if len(channels) > 3: + cho += ",".join(channels[:3]) + " " + str(len(channels) - 3) + " more..." + else: + cho += ",".join(channels) + + if len(channels) > 0: + cho = color.colorize(cho,"bold") + "@" + + return cho + color.colorize(pubnub.get_origin(),"blue") + "] > " + + def get_prompt(self): + prompt = color.colorize("[" + datetime.now().strftime( + '%Y-%m-%d %H:%M:%S') + "] ", "magenta") + if self.default_channel is not None and len(self.default_channel) > 0: + prompt += " [default channel: " + color.colorize(self.default_channel,"bold") + "]" + + prompt += self.get_channel_origin() + return prompt + + def precmd(self, line): + self.prompt = self.get_prompt() + return line + + def emptyline(self): + print('empty line') + self.prompt = get_date() + " [" + color.colorize(pubnub.get_origin(),"blue") + "] > " def cmdloop_with_keyboard_interrupt(self): try: self.cmdloop() - except KeyboardInterrupt: - sys.stdout.write('\n') - kill_all_threads() - + except KeyboardInterrupt as e: + pass + sys.stdout.write('\n') + kill_all_threads() @options([make_option('-p', '--publish-key', action="store", default="demo", help="Publish Key"), make_option('-s', '--subscribe-key', action="store", default="demo", help="Subscribe Key"), @@ -268,6 +299,7 @@ class DevConsole(Cmd): print_error("Missing channel") return self.default_channel = opts.channel + self.prompt = self.get_prompt() @options([make_option('-f', '--file', action="store", default="./pubnub-console.log", help="Output file") ]) @@ -375,6 +407,7 @@ class DevConsole(Cmd): print_error("Missing channel") return _unsubscribe_command_handler(opts.channel) + self.prompt = self.get_prompt() @options([make_option('-c', '--channel', action="store", help="Channel for subscribe"), @@ -392,6 +425,7 @@ class DevConsole(Cmd): if opts.get is True: print_ok(pubnub.get_channel_array()) + self.prompt = self.get_prompt() def do_exit(self, args): kill_all_threads() -- cgit v1.2.3 From e0207efea24574bc6dac3e0aadb2d53cebe2a307 Mon Sep 17 00:00:00 2001 From: Devendra Date: Fri, 2 May 2014 22:45:17 +0530 Subject: console enhancements --- python/examples/console.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index d43ee80..3041a02 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -122,11 +122,11 @@ def _publish_command_handler(channel, message,async=False): def _subscribe_command_handler(channel): - def _callback(r): - print_ok(r, channel) + def _callback(r,ch): + print_ok(r, ch) - def _error(r): - print_error(r, channel) + def _error(r,ch=None): + print_error(r, ch if ch is not None else channel) def _disconnect(r): print_error("DISCONNECTED", r) -- cgit v1.2.3 From 3b60b175b085fbf474ca34069025f04633aa072e Mon Sep 17 00:00:00 2001 From: Devendra Date: Tue, 6 May 2014 14:20:16 +0530 Subject: console.py --- python/examples/console.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index 3041a02..8f87a31 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -140,14 +140,20 @@ def _subscribe_command_handler(channel): pubnub.subscribe(channel, _callback, _error,connect=_connect, disconnect=_disconnect, reconnect=_reconnect) -def _unsubscribe_command_handler(channel): +def _unsubscribe_command_handler(channels): def _callback(r): print_ok(r) def _error(r): print_error(r) - pubnub.unsubscribe(channel) + if not isinstance(channels,list): + ch = [] + ch.append(channels) + channels = ch + + for channel in channels: + pubnub.unsubscribe(channel) def _grant_command_handler(channel, auth_key, read, write, ttl,async=False): @@ -230,7 +236,9 @@ class DevConsole(Cmd): if len(channels) > 0: cho = color.colorize(cho,"bold") + "@" - return cho + color.colorize(pubnub.get_origin(),"blue") + "] > " + origin = pubnub.get_origin().split("://")[1] + origin += color.colorize(" (SSL)","green") if pubnub.get_origin().split("://")[0] == "https" else "" + return cho + color.colorize(origin,"blue") + "] > " def get_prompt(self): @@ -277,7 +285,7 @@ class DevConsole(Cmd): opts.ssl, opts.origin, opts.uuid) - self.prompt = "(PubNub Console) [" + color.colorize(pubnub.get_origin(),"blue") + "] > " + self.prompt = self.get_prompt() def do_set_sync(self, command): @@ -399,10 +407,14 @@ class DevConsole(Cmd): _audit_command_handler(opts.channel, opts.auth_key,async=self.async) - @options([make_option('-c', '--channel', action="store", help="Channel for unsubscribe") + @options([make_option('-c', '--channel', action="store", help="Channel for unsubscribe"), + make_option('-a', '--all', action="store_true", dest="all", + default=False, help="Unsubscribe from all channels") ]) def do_unsubscribe(self, command, opts): opts.channel = self.default_channel if opts.channel is None else opts.channel + if (opts.all is True): + opts.channel = pubnub.get_channel_array() if opts.channel is None: print_error("Missing channel") return -- cgit v1.2.3 From cc39ff042c3c7c7d35819e33a5d4d58acdf89b67 Mon Sep 17 00:00:00 2001 From: Devendra Date: Tue, 6 May 2014 15:03:54 +0530 Subject: additions to console --- python/examples/console.py | 47 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index 8f87a31..1f25eb6 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -212,6 +212,14 @@ def kill_all_threads(): thread._Thread__stop() +def get_date(full=False): + if full is True: + return color.colorize("[" + datetime.now().strftime( + '%Y-%m-%d %H:%M:%S') + "] ", "magenta") + else: + return color.colorize("[" + datetime.now().strftime( + '%H:%M:%S') + "] ", "magenta") + class DevConsole(Cmd): @@ -222,14 +230,18 @@ class DevConsole(Cmd): self.default_channel = None self.async = False pubnub = Pubnub("demo", "demo") + self.full_date = False + self.channel_truncation = 3 self.prompt = self.get_prompt() + def get_channel_origin(self): cho = " [" channels = pubnub.get_channel_array() channels_str = ",".join(channels) - if len(channels) > 3: - cho += ",".join(channels[:3]) + " " + str(len(channels) - 3) + " more..." + sl = self.channel_truncation + if len(channels) > 0 and sl > 0: + cho += ",".join(channels[:int(sl)]) + " " + str(len(channels) - int(sl)) + " more..." else: cho += ",".join(channels) @@ -242,8 +254,8 @@ class DevConsole(Cmd): def get_prompt(self): - prompt = color.colorize("[" + datetime.now().strftime( - '%Y-%m-%d %H:%M:%S') + "] ", "magenta") + prompt = get_date(self.full_date) + if self.default_channel is not None and len(self.default_channel) > 0: prompt += " [default channel: " + color.colorize(self.default_channel,"bold") + "]" @@ -298,6 +310,33 @@ class DevConsole(Cmd): Set Async mode""" self.async = True + @options([make_option('-n', '--count', action="store", default=3, help="Number of channels on prompt") + ]) + def do_set_channel_truncation(self, command, opts): + """set_channel_truncation + Set Channel Truncation""" + + self.channel_truncation = opts.count + + self.prompt = self.get_prompt() + + def do_unset_channel_truncation(self, command): + """unset_channel_truncation + Unset Channel Truncation""" + self.channel_truncation = 0 + self.prompt = self.get_prompt() + + def do_set_full_date(self, command): + """do_set_full_date + Set Full Date""" + self.full_date = True + self.prompt = self.get_prompt() + + def do_unset_full_date(self, command): + """do_unset_full_date + Unset Full Date""" + self.full_date = False + self.prompt = self.get_prompt() @options([make_option('-c', '--channel', action="store", help="Default Channel") ]) -- cgit v1.2.3 From 338ffccb4d6603a8af5951dda6fd1e1dd11473f9 Mon Sep 17 00:00:00 2001 From: Devendra Date: Tue, 6 May 2014 20:04:19 +0530 Subject: changes for python 3 compatibility --- python/examples/console.py | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index 1f25eb6..084c49a 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -20,9 +20,6 @@ import os import readline import rlcompleter -if sys.argv[0] == "screen": - print "screen" - historyPath = os.path.expanduser("~/.pubnub_console_history") def save_history(historyPath=historyPath): @@ -38,6 +35,34 @@ of=sys.stdout color = Cmd() +stop = None + +def stop_2(th): + th._Thread__stop() + +def stop_3(th): + th._stop() + +def print_console_2(of,message): + print >>of, message + +def print_console_3(of,message): + of.write(message) + of.write("\n") + +print_console = None + +if type(sys.version_info) is tuple: + print_console = print_console_2 + stop = stop_2 +else: + if sys.version_info.major == 2: + print_console = print_console_2 + stop = stop_2 + else: + print_console = print_console_3 + stop = stop_3 + def print_ok(msg, channel=None): if msg is None: return @@ -46,9 +71,9 @@ def print_ok(msg, channel=None): chstr += color.colorize("[Channel : " + channel + \ "] " if channel is not None else "", "cyan") try: - print >>of, (chstr + color.colorize(str(msg),"green")) + print_console(of, (chstr + color.colorize(str(msg),"green"))) except UnicodeEncodeError as e: - print >>of, (msg) + print_console(of, (msg)) of.flush() def print_error(msg, channel=None): @@ -59,9 +84,9 @@ def print_error(msg, channel=None): chstr += color.colorize("[Channel : " + channel + \ "] " if channel is not None else "", "cyan") try: - print >>of, (chstr + color.colorize(color.colorize(str(msg),"red"),"bold")) + print_console(of, (chstr + color.colorize(color.colorize(str(msg),"red"),"bold"))) except UnicodeEncodeError as e: - print >>of, (msg) + print_console(of, (msg)) of.flush() class DefaultPubnub(object): @@ -79,7 +104,7 @@ pubnub=DefaultPubnub() def kill_all_threads(): for thread in threading.enumerate(): if thread.isAlive(): - thread._Thread__stop() + stop(thread) def get_input(message, t=None): while True: @@ -209,7 +234,7 @@ def _here_now_command_handler(channel,async=False): def kill_all_threads(): for thread in threading.enumerate(): if thread.isAlive(): - thread._Thread__stop() + stop(thread) def get_date(full=False): @@ -240,7 +265,7 @@ class DevConsole(Cmd): channels = pubnub.get_channel_array() channels_str = ",".join(channels) sl = self.channel_truncation - if len(channels) > 0 and sl > 0: + if len(channels) > int(sl) and int(sl) > 0: cho += ",".join(channels[:int(sl)]) + " " + str(len(channels) - int(sl)) + " more..." else: cho += ",".join(channels) -- cgit v1.2.3 From 1b49e712e12ba833f460324b95969b162d464edf Mon Sep 17 00:00:00 2001 From: Devendra Date: Wed, 7 May 2014 12:13:17 +0530 Subject: console changes and pep8 compliance --- python/examples/console.py | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index 084c49a..342c70f 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -37,12 +37,16 @@ color = Cmd() stop = None +full_date = False + def stop_2(th): th._Thread__stop() def stop_3(th): th._stop() + + def print_console_2(of,message): print >>of, message @@ -63,11 +67,20 @@ else: print_console = print_console_3 stop = stop_3 + +def get_date(): + if full_date is True: + return color.colorize(datetime.now().strftime( + '%Y-%m-%d %H:%M:%S'), "magenta") + else: + return color.colorize(datetime.now().strftime( + '%H:%M:%S'), "magenta") + + def print_ok(msg, channel=None): if msg is None: return - chstr = color.colorize("[" + datetime.now().strftime( - '%Y-%m-%d %H:%M:%S') + "] ","magenta") + chstr = color.colorize("[" + get_date() + "] ","magenta") chstr += color.colorize("[Channel : " + channel + \ "] " if channel is not None else "", "cyan") try: @@ -79,8 +92,7 @@ def print_ok(msg, channel=None): def print_error(msg, channel=None): if msg is None: return - chstr = color.colorize("[" + datetime.now().strftime( - '%Y-%m-%d %H:%M:%S') + "] ", "magenta") + chstr = color.colorize("[" + get_date() + "] ", "magenta") chstr += color.colorize("[Channel : " + channel + \ "] " if channel is not None else "", "cyan") try: @@ -237,15 +249,6 @@ def kill_all_threads(): stop(thread) -def get_date(full=False): - if full is True: - return color.colorize("[" + datetime.now().strftime( - '%Y-%m-%d %H:%M:%S') + "] ", "magenta") - else: - return color.colorize("[" + datetime.now().strftime( - '%H:%M:%S') + "] ", "magenta") - - class DevConsole(Cmd): def __init__(self): @@ -255,7 +258,6 @@ class DevConsole(Cmd): self.default_channel = None self.async = False pubnub = Pubnub("demo", "demo") - self.full_date = False self.channel_truncation = 3 self.prompt = self.get_prompt() @@ -279,7 +281,7 @@ class DevConsole(Cmd): def get_prompt(self): - prompt = get_date(self.full_date) + prompt = "[" + get_date() + "]" if self.default_channel is not None and len(self.default_channel) > 0: prompt += " [default channel: " + color.colorize(self.default_channel,"bold") + "]" @@ -352,15 +354,17 @@ class DevConsole(Cmd): self.prompt = self.get_prompt() def do_set_full_date(self, command): + global full_date """do_set_full_date Set Full Date""" - self.full_date = True + full_date = True self.prompt = self.get_prompt() def do_unset_full_date(self, command): + global full_date """do_unset_full_date Unset Full Date""" - self.full_date = False + full_date = False self.prompt = self.get_prompt() @options([make_option('-c', '--channel', action="store", help="Default Channel") -- cgit v1.2.3 From 4ab1fa41e1a10efc88909bceb78222ffb406145a Mon Sep 17 00:00:00 2001 From: Devendra Date: Wed, 7 May 2014 13:01:21 +0530 Subject: pep 8 compliance --- python/examples/console.py | 314 +++++++++++++++++++++++++++------------------ 1 file changed, 188 insertions(+), 126 deletions(-) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index 342c70f..674b6d4 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -22,6 +22,7 @@ import rlcompleter historyPath = os.path.expanduser("~/.pubnub_console_history") + def save_history(historyPath=historyPath): import readline readline.write_history_file(historyPath) @@ -31,7 +32,7 @@ if os.path.exists(historyPath): atexit.register(save_history) -of=sys.stdout +of = sys.stdout color = Cmd() @@ -39,18 +40,20 @@ stop = None full_date = False + def stop_2(th): th._Thread__stop() + def stop_3(th): th._stop() +def print_console_2(of, message): + print >>of, message -def print_console_2(of,message): - print >>of, message -def print_console_3(of,message): +def print_console_3(of, message): of.write(message) of.write("\n") @@ -72,45 +75,49 @@ def get_date(): if full_date is True: return color.colorize(datetime.now().strftime( '%Y-%m-%d %H:%M:%S'), "magenta") - else: + else: return color.colorize(datetime.now().strftime( - '%H:%M:%S'), "magenta") + '%H:%M:%S'), "magenta") def print_ok(msg, channel=None): if msg is None: return - chstr = color.colorize("[" + get_date() + "] ","magenta") - chstr += color.colorize("[Channel : " + channel + \ - "] " if channel is not None else "", "cyan") + chstr = color.colorize("[" + get_date() + "] ", "magenta") + chstr += color.colorize("[Channel : " + channel + + "] " if channel is not None else "", "cyan") try: - print_console(of, (chstr + color.colorize(str(msg),"green"))) + print_console(of, (chstr + color.colorize(str(msg), "green"))) except UnicodeEncodeError as e: print_console(of, (msg)) of.flush() + def print_error(msg, channel=None): if msg is None: return chstr = color.colorize("[" + get_date() + "] ", "magenta") - chstr += color.colorize("[Channel : " + channel + \ - "] " if channel is not None else "", "cyan") + chstr += color.colorize("[Channel : " + channel + + "] " if channel is not None else "", "cyan") try: - print_console(of, (chstr + color.colorize(color.colorize(str(msg),"red"),"bold"))) + print_console(of, (chstr + color.colorize(color.colorize( + str(msg), "red"), "bold"))) except UnicodeEncodeError as e: print_console(of, (msg)) of.flush() + class DefaultPubnub(object): - def handlerFunctionClosure(self,name): - def handlerFunction(*args,**kwargs): - print_error("Pubnub not initialized. Use init command to initialize") + def handlerFunctionClosure(self, name): + def handlerFunction(*args, **kwargs): + print_error("Pubnub not initialized." + + "Use init command to initialize") return handlerFunction - def __getattr__(self,name): - return self.handlerFunctionClosure(name) -pubnub=DefaultPubnub() + def __getattr__(self, name): + return self.handlerFunctionClosure(name) +pubnub = DefaultPubnub() def kill_all_threads(): @@ -118,6 +125,7 @@ def kill_all_threads(): if thread.isAlive(): stop(thread) + def get_input(message, t=None): while True: try: @@ -148,21 +156,24 @@ def get_input(message, t=None): except ValueError: print_error("Invalid input : " + command) -def _publish_command_handler(channel, message,async=False): + +def _publish_command_handler(channel, message, async=False): def _callback(r): print_ok(r) def _error(r): print_error(r) - print_ok(pubnub.publish(channel, message, _callback if async is True else None, _error if async is True else None)) + print_ok(pubnub.publish(channel, message, + _callback if async is True else None, + _error if async is True else None)) def _subscribe_command_handler(channel): - def _callback(r,ch): + def _callback(r, ch): print_ok(r, ch) - def _error(r,ch=None): + def _error(r, ch=None): print_error(r, ch if ch is not None else channel) def _disconnect(r): @@ -174,7 +185,8 @@ def _subscribe_command_handler(channel): def _connect(r): print_error("CONNECTED", r) - pubnub.subscribe(channel, _callback, _error,connect=_connect, disconnect=_disconnect, reconnect=_reconnect) + pubnub.subscribe(channel, _callback, _error, connect=_connect, + disconnect=_disconnect, reconnect=_reconnect) def _unsubscribe_command_handler(channels): @@ -184,7 +196,7 @@ def _unsubscribe_command_handler(channels): def _error(r): print_error(r) - if not isinstance(channels,list): + if not isinstance(channels, list): ch = [] ch.append(channels) channels = ch @@ -193,54 +205,64 @@ def _unsubscribe_command_handler(channels): pubnub.unsubscribe(channel) -def _grant_command_handler(channel, auth_key, read, write, ttl,async=False): +def _grant_command_handler(channel, auth_key, read, write, ttl, async=False): def _callback(r): print_ok(r) def _error(r): print_error(r) - print_ok(pubnub.grant(channel, auth_key, read, write, ttl, _callback if async is True else None, _error if async is True else None)) + print_ok(pubnub.grant(channel, auth_key, + read, write, ttl, + _callback if async is True else None, + _error if async is True else None)) -def _revoke_command_handler(channel, auth_key, ttl,async=False): +def _revoke_command_handler(channel, auth_key, ttl, async=False): def _callback(r): print_ok(r) def _error(r): print_error(r) - print_ok(pubnub.revoke(channel, auth_key, ttl, _callback if async is True else None, _error if async is True else None)) + print_ok(pubnub.revoke(channel, auth_key, ttl, + _callback if async is True else None, + _error if async is True else None)) -def _audit_command_handler(channel, auth_key,async=False): +def _audit_command_handler(channel, auth_key, async=False): def _callback(r): print_ok(r) def _error(r): print_error(r) - print_ok(pubnub.audit(channel, auth_key, _callback if async is True else None, _error if async is True else None)) + print_ok(pubnub.audit(channel, auth_key, + _callback if async is True else None, + _error if async is True else None)) -def _history_command_handler(channel, count,async=False): +def _history_command_handler(channel, count, async=False): def _callback(r): print_ok(r) def _error(r): print_error(r) - print_ok(pubnub.history(channel, count, _callback if async is True else None, _error if async is True else None)) + print_ok(pubnub.history(channel, count, + _callback if async is True else None, + _error if async is True else None)) -def _here_now_command_handler(channel,async=False): +def _here_now_command_handler(channel, async=False): def _callback(r): print_ok(r) def _error(r): print_error(r) - print_ok(pubnub.here_now(channel, _callback if async is True else None, _error if async is True else None)) + print_ok(pubnub.here_now(channel, _callback if async is True else None, + _error if async is True else None)) def kill_all_threads(): @@ -250,42 +272,44 @@ def kill_all_threads(): class DevConsole(Cmd): - + def __init__(self): Cmd.__init__(self) global pubnub - self.intro = "For Help type ? or help . To quit/exit type exit" ## defaults to None + self.intro = "For Help type ? or help . " + + "To quit/exit type exit" self.default_channel = None self.async = False pubnub = Pubnub("demo", "demo") self.channel_truncation = 3 self.prompt = self.get_prompt() - def get_channel_origin(self): cho = " [" channels = pubnub.get_channel_array() channels_str = ",".join(channels) sl = self.channel_truncation if len(channels) > int(sl) and int(sl) > 0: - cho += ",".join(channels[:int(sl)]) + " " + str(len(channels) - int(sl)) + " more..." + cho += ",".join(channels[:int(sl)]) + " " + str( + len(channels) - int(sl)) + " more..." else: cho += ",".join(channels) if len(channels) > 0: - cho = color.colorize(cho,"bold") + "@" + cho = color.colorize(cho, "bold") + "@" origin = pubnub.get_origin().split("://")[1] - origin += color.colorize(" (SSL)","green") if pubnub.get_origin().split("://")[0] == "https" else "" - return cho + color.colorize(origin,"blue") + "] > " - + origin += color.colorize(" (SSL)", "green") if pubnub.get_origin( + ).split("://")[0] == "https" else "" + return cho + color.colorize(origin, "blue") + "] > " def get_prompt(self): prompt = "[" + get_date() + "]" if self.default_channel is not None and len(self.default_channel) > 0: - prompt += " [default channel: " + color.colorize(self.default_channel,"bold") + "]" - + prompt += " [default channel: " + color.colorize( + self.default_channel, "bold") + "]" + prompt += self.get_channel_origin() return prompt @@ -295,7 +319,8 @@ class DevConsole(Cmd): def emptyline(self): print('empty line') - self.prompt = get_date() + " [" + color.colorize(pubnub.get_origin(),"blue") + "] > " + self.prompt = get_date() + " [" + color.colorize( + pubnub.get_origin(), "blue") + "] > " def cmdloop_with_keyboard_interrupt(self): try: @@ -305,28 +330,35 @@ class DevConsole(Cmd): sys.stdout.write('\n') kill_all_threads() - @options([make_option('-p', '--publish-key', action="store", default="demo", help="Publish Key"), - make_option('-s', '--subscribe-key', action="store", default="demo", help="Subscribe Key"), - make_option('-k', '--secret-key', action="store", default="demo", help="cipher Key"), - make_option('-c', '--cipher-key', action="store", default="", help="Secret Key"), - make_option('-a', '--auth-key', action="store", default=None, help="Auth Key"), - make_option('--ssl-on', dest='ssl', action='store_true', default=False, help="SSL Enabled ?"), - make_option('-o', '--origin', action="store", default="pubsub.pubnub.com", help="Origin"), - make_option('-u', '--uuid', action="store", default=None, help="UUID") - ]) + @options([make_option('-p', '--publish-key', action="store", + default="demo", help="Publish Key"), + make_option('-s', '--subscribe-key', action="store", + default="demo", help="Subscribe Key"), + make_option('-k', '--secret-key', action="store", + default="demo", help="cipher Key"), + make_option('-c', '--cipher-key', action="store", + default="", help="Secret Key"), + make_option('-a', '--auth-key', action="store", + default=None, help="Auth Key"), + make_option('--ssl-on', dest='ssl', action='store_true', + default=False, help="SSL Enabled ?"), + make_option('-o', '--origin', action="store", + default="pubsub.pubnub.com", help="Origin"), + make_option('-u', '--uuid', action="store", + default=None, help="UUID") + ]) def do_init(self, command, opts): global pubnub pubnub = Pubnub(opts.publish_key, - opts.subscribe_key, - opts.secret_key, - opts.cipher_key, - opts.auth_key, - opts.ssl, - opts.origin, - opts.uuid) + opts.subscribe_key, + opts.secret_key, + opts.cipher_key, + opts.auth_key, + opts.ssl, + opts.origin, + opts.uuid) self.prompt = self.get_prompt() - def do_set_sync(self, command): """unset_async Unset Async mode""" @@ -337,8 +369,9 @@ class DevConsole(Cmd): Set Async mode""" self.async = True - @options([make_option('-n', '--count', action="store", default=3, help="Number of channels on prompt") - ]) + @options([make_option('-n', '--count', action="store", + default=3, help="Number of channels on prompt") + ]) def do_set_channel_truncation(self, command, opts): """set_channel_truncation Set Channel Truncation""" @@ -367,8 +400,9 @@ class DevConsole(Cmd): full_date = False self.prompt = self.get_prompt() - @options([make_option('-c', '--channel', action="store", help="Default Channel") - ]) + @options([make_option('-c', '--channel', + action="store", help="Default Channel") + ]) def do_set_default_channel(self, command, opts): if opts.channel is None: @@ -377,42 +411,48 @@ class DevConsole(Cmd): self.default_channel = opts.channel self.prompt = self.get_prompt() - @options([make_option('-f', '--file', action="store", default="./pubnub-console.log", help="Output file") - ]) + @options([make_option('-f', '--file', action="store", + default="./pubnub-console.log", help="Output file") + ]) def do_set_output_file(self, command, opts): global of try: - of = file(opts.file,'w+') + of = file(opts.file, 'w+') except IOError as e: print_error("Could not set output file. " + e.reason) - - @options([make_option('-c', '--channel', action="store", help="Channel for here now data") - ]) + @options([make_option('-c', '--channel', action="store", + help="Channel for here now data") + ]) def do_here_now(self, command, opts): - opts.channel = self.default_channel if opts.channel is None else opts.channel + opts.channel = self.default_channel \ + if opts.channel is None else opts.channel if opts.channel is None: print_error("Missing channel") return - _here_now_command_handler(opts.channel,async=self.async) + _here_now_command_handler(opts.channel, async=self.async) - @options([make_option('-c', '--channel', action="store", help="Channel for history data"), - make_option('-n', '--count', action="store", default=5, help="Number of messages") - ]) + @options([make_option('-c', '--channel', action="store", + help="Channel for history data"), + make_option('-n', '--count', action="store", + default=5, help="Number of messages") + ]) def do_get_history(self, command, opts): - opts.channel = self.default_channel if opts.channel is None else opts.channel + opts.channel = self.default_channel \ + if opts.channel is None else opts.channel if opts.channel is None: print_error("Missing channel") return - _history_command_handler(opts.channel, opts.count,async=self.async) - + _history_command_handler(opts.channel, opts.count, async=self.async) - @options([make_option('-c', '--channel', action="store", help="Channel on which to publish") - ]) + @options([make_option('-c', '--channel', action="store", + help="Channel on which to publish") + ]) def do_publish(self, command, opts): - opts.channel = self.default_channel if opts.channel is None else opts.channel + opts.channel = self.default_channel \ + if opts.channel is None else opts.channel if opts.channel is None: print_error("Missing channel") return @@ -426,61 +466,81 @@ class DevConsole(Cmd): except ValueError as ve: message = str(command) - _publish_command_handler(opts.channel,message,async=self.async) - - @options([make_option('-c', '--channel', action="store", help="Channel on which to grant"), - make_option('-a', '--auth-key', dest="auth_key", action="store", - help="Auth Key"), - make_option('-r', '--read-enabled', dest='read', action='store_true', default=False, help="Read ?"), - make_option('-w', '--write-enabled', dest='write', action='store_true', default=False, help="Write ?"), - make_option('-t', '--ttl', action="store", default=5, help="TTL") - ]) + _publish_command_handler(opts.channel, message, async=self.async) + + @options([make_option('-c', '--channel', action="store", + help="Channel on which to grant"), + make_option('-a', '--auth-key', dest="auth_key", + action="store", + help="Auth Key"), + make_option('-r', '--read-enabled', dest='read', + action='store_true', + default=False, help="Read ?"), + make_option('-w', '--write-enabled', dest='write', + action='store_true', + default=False, help="Write ?"), + make_option('-t', '--ttl', action="store", + default=5, help="TTL") + ]) def do_grant(self, command, opts): - opts.channel = self.default_channel if opts.channel is None else opts.channel + opts.channel = self.default_channel \ + if opts.channel is None else opts.channel if opts.channel is None: print_error("Missing channel") return - opts.auth_key = pubnub.auth_key if opts.auth_key is None else opts.auth_key + opts.auth_key = pubnub.auth_key \ + if opts.auth_key is None else opts.auth_key - _grant_command_handler(opts.channel,opts.auth_key, opts.read, opts.write, opts.ttl,async=self.async) + _grant_command_handler(opts.channel, opts.auth_key, + opts.read, opts.write, + opts.ttl, async=self.async) - @options([make_option('-c', '--channel', action="store", help="Channel on which to revoke"), - make_option('-a', '--auth-key', dest="auth_key", action="store", - help="Auth Key"), - make_option('-t', '--ttl', action="store", default=5, help="TTL") - ]) + @options([make_option('-c', '--channel', action="store", + help="Channel on which to revoke"), + make_option('-a', '--auth-key', dest="auth_key", action="store", + help="Auth Key"), + make_option('-t', '--ttl', action="store", + default=5, help="TTL") + ]) def do_revoke(self, command, opts): - opts.channel = self.default_channel if opts.channel is None else opts.channel + opts.channel = self.default_channel \ + if opts.channel is None else opts.channel if opts.channel is None: print_error("Missing channel") return - opts.auth_key = pubnub.auth_key if opts.auth_key is None else opts.auth_key + opts.auth_key = pubnub.auth_key \ + if opts.auth_key is None else opts.auth_key - _revoke_command_handler(opts.channel,opts.auth_key, opts.ttl,async=self.async) + _revoke_command_handler( + opts.channel, opts.auth_key, opts.ttl, async=self.async) - @options([make_option('-c', '--channel', action="store", help="Channel on which to revoke"), - make_option('-a', '--auth-key', dest="auth_key", action="store", - help="Auth Key") - ]) + @options([make_option('-c', '--channel', action="store", + help="Channel on which to revoke"), + make_option('-a', '--auth-key', dest="auth_key", action="store", + help="Auth Key") + ]) def do_audit(self, command, opts): - opts.channel = self.default_channel if opts.channel is None else opts.channel + opts.channel = self.default_channel \ + if opts.channel is None else opts.channel if opts.channel is None: print_error("Missing channel") return - opts.auth_key = pubnub.auth_key if opts.auth_key is None else opts.auth_key - - _audit_command_handler(opts.channel, opts.auth_key,async=self.async) + opts.auth_key = pubnub.auth_key \ + if opts.auth_key is None else opts.auth_key + _audit_command_handler(opts.channel, opts.auth_key, async=self.async) - @options([make_option('-c', '--channel', action="store", help="Channel for unsubscribe"), - make_option('-a', '--all', action="store_true", dest="all", - default=False, help="Unsubscribe from all channels") - ]) + @options([make_option('-c', '--channel', action="store", + help="Channel for unsubscribe"), + make_option('-a', '--all', action="store_true", dest="all", + default=False, help="Unsubscribe from all channels") + ]) def do_unsubscribe(self, command, opts): - opts.channel = self.default_channel if opts.channel is None else opts.channel + opts.channel = self.default_channel \ + if opts.channel is None else opts.channel if (opts.all is True): opts.channel = pubnub.get_channel_array() if opts.channel is None: @@ -489,13 +549,15 @@ class DevConsole(Cmd): _unsubscribe_command_handler(opts.channel) self.prompt = self.get_prompt() - - @options([make_option('-c', '--channel', action="store", help="Channel for subscribe"), - make_option('-g', '--get-channel-list', action="store_true", dest="get", - default=False, help="Get susbcribed channel list") - ]) + @options([make_option('-c', '--channel', action="store", + help="Channel for subscribe"), + make_option('-g', '--get-channel-list', action="store_true", + dest="get", + default=False, help="Get susbcribed channel list") + ]) def do_subscribe(self, command, opts): - opts.channel = self.default_channel if opts.channel is None else opts.channel + opts.channel = self.default_channel \ + if opts.channel is None else opts.channel if opts is None: print_error("Missing argument") return @@ -518,10 +580,10 @@ class DevConsole(Cmd): def handler(self, signum, frame): kill_all_threads() + def main(): app = DevConsole() app.cmdloop_with_keyboard_interrupt() - -if __name__ == "__main__": - main() +if __name__ == "__main__": + main() -- cgit v1.2.3 From 8e3ee358718686a2292b555e1184eb817b2a051a Mon Sep 17 00:00:00 2001 From: Devendra Date: Wed, 7 May 2014 17:16:11 +0530 Subject: added presence option to subscribe command --- python/examples/console.py | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index 674b6d4..3d7ba38 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -203,6 +203,7 @@ def _unsubscribe_command_handler(channels): for channel in channels: pubnub.unsubscribe(channel) + pubnub.unsubscribe(channel + '-pnpres') def _grant_command_handler(channel, auth_key, read, write, ttl, async=False): @@ -271,13 +272,23 @@ def kill_all_threads(): stop(thread) -class DevConsole(Cmd): +def get_channel_array(): + channels = pubnub.get_channel_array() + + for channel in channels: + if "-pnpres" in channel: + i = channels.index(channel.split("-pnpres")[0]) + channels[i] = channels[i] + color.colorize("(P)", "blue") + channels.remove(channel) + return channels + +class DevConsole(Cmd): def __init__(self): Cmd.__init__(self) global pubnub - self.intro = "For Help type ? or help . " + - "To quit/exit type exit" + self.intro = "For Help type ? or help . " + \ + "To quit/exit type exit" self.default_channel = None self.async = False pubnub = Pubnub("demo", "demo") @@ -286,7 +297,7 @@ class DevConsole(Cmd): def get_channel_origin(self): cho = " [" - channels = pubnub.get_channel_array() + channels = get_channel_array() channels_str = ",".join(channels) sl = self.channel_truncation if len(channels) > int(sl) and int(sl) > 0: @@ -318,7 +329,6 @@ class DevConsole(Cmd): return line def emptyline(self): - print('empty line') self.prompt = get_date() + " [" + color.colorize( pubnub.get_origin(), "blue") + "] > " @@ -553,7 +563,10 @@ class DevConsole(Cmd): help="Channel for subscribe"), make_option('-g', '--get-channel-list', action="store_true", dest="get", - default=False, help="Get susbcribed channel list") + default=False, help="Get susbcribed channel list"), + make_option('-p', '--presence', action="store_true", + dest="presence", + default=False, help="Presence events ?") ]) def do_subscribe(self, command, opts): opts.channel = self.default_channel \ @@ -565,8 +578,11 @@ class DevConsole(Cmd): if opts.channel is not None: _subscribe_command_handler(opts.channel) + if opts.presence is True: + _subscribe_command_handler(opts.channel + '-pnpres') + if opts.get is True: - print_ok(pubnub.get_channel_array()) + print_ok(get_channel_array()) self.prompt = self.get_prompt() def do_exit(self, args): -- cgit v1.2.3 From 20f55f6d739451629c4d918319362962bee5a141 Mon Sep 17 00:00:00 2001 From: Devendra Date: Wed, 7 May 2014 18:21:27 +0530 Subject: adding presence channel option to grant and revoke --- python/examples/console.py | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index 3d7ba38..7798340 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -328,9 +328,8 @@ class DevConsole(Cmd): self.prompt = self.get_prompt() return line - def emptyline(self): - self.prompt = get_date() + " [" + color.colorize( - pubnub.get_origin(), "blue") + "] > " + #def emptyline(self): + # self.prompt = self.get_prompt() def cmdloop_with_keyboard_interrupt(self): try: @@ -490,7 +489,10 @@ class DevConsole(Cmd): action='store_true', default=False, help="Write ?"), make_option('-t', '--ttl', action="store", - default=5, help="TTL") + default=5, help="TTL"), + make_option('-p', '--presence', action="store_true", + dest="presence", + default=False, help="Grant on presence channel ?") ]) def do_grant(self, command, opts): opts.channel = self.default_channel \ @@ -506,12 +508,20 @@ class DevConsole(Cmd): opts.read, opts.write, opts.ttl, async=self.async) + if opts.presence is True: + _grant_command_handler(opts.channel + '-pnpres', opts.auth_key, + opts.read, opts.write, + opts.ttl, async=self.async) + @options([make_option('-c', '--channel', action="store", help="Channel on which to revoke"), make_option('-a', '--auth-key', dest="auth_key", action="store", help="Auth Key"), make_option('-t', '--ttl', action="store", - default=5, help="TTL") + default=5, help="TTL"), + make_option('-p', '--presence', action="store_true", + dest="presence", + default=False, help="Revoke on presence channel ?") ]) def do_revoke(self, command, opts): opts.channel = self.default_channel \ @@ -526,6 +536,11 @@ class DevConsole(Cmd): _revoke_command_handler( opts.channel, opts.auth_key, opts.ttl, async=self.async) + if opts.presence is True: + _revoke_command_handler( + opts.channel + '-pnpres', opts.auth_key, + opts.ttl, async=self.async) + @options([make_option('-c', '--channel', action="store", help="Channel on which to revoke"), make_option('-a', '--auth-key', dest="auth_key", action="store", @@ -589,12 +604,12 @@ class DevConsole(Cmd): kill_all_threads() return -1 - def do_EOF(self, args): - kill_all_threads() - return self.do_exit(args) + #def do_EOF(self, args): + # kill_all_threads() + # return self.do_exit(args) - def handler(self, signum, frame): - kill_all_threads() + #def handler(self, signum, frame): + # kill_all_threads() def main(): -- cgit v1.2.3 From 9dda9200997330b686bdb26495bed1023a1d8f7e Mon Sep 17 00:00:00 2001 From: Devendra Date: Thu, 15 May 2014 12:06:49 +0530 Subject: adding sync and async tests for grant and revoke --- python/examples/console.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index 7798340..5d74517 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -7,7 +7,7 @@ import sys -from Pubnub import PubnubAsync as Pubnub +from Pubnub import Pubnub import threading from datetime import datetime -- cgit v1.2.3 From 26aca4f6f2c6167d5a83be83d1185c54522834e8 Mon Sep 17 00:00:00 2001 From: Devendra Date: Thu, 22 May 2014 20:42:40 +0530 Subject: removing requirement of adding channel for grant and audit --- python/examples/console.py | 9 --------- 1 file changed, 9 deletions(-) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index 5d74517..6147d21 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -497,9 +497,6 @@ class DevConsole(Cmd): def do_grant(self, command, opts): opts.channel = self.default_channel \ if opts.channel is None else opts.channel - if opts.channel is None: - print_error("Missing channel") - return opts.auth_key = pubnub.auth_key \ if opts.auth_key is None else opts.auth_key @@ -526,9 +523,6 @@ class DevConsole(Cmd): def do_revoke(self, command, opts): opts.channel = self.default_channel \ if opts.channel is None else opts.channel - if opts.channel is None: - print_error("Missing channel") - return opts.auth_key = pubnub.auth_key \ if opts.auth_key is None else opts.auth_key @@ -549,9 +543,6 @@ class DevConsole(Cmd): def do_audit(self, command, opts): opts.channel = self.default_channel \ if opts.channel is None else opts.channel - if opts.channel is None: - print_error("Missing channel") - return opts.auth_key = pubnub.auth_key \ if opts.auth_key is None else opts.auth_key -- cgit v1.2.3 From b60f421b301abf348ab887991809966bbb66087c Mon Sep 17 00:00:00 2001 From: Devendra Date: Fri, 23 May 2014 20:52:10 +0530 Subject: adding success message after unsubscribe --- python/examples/console.py | 1 + 1 file changed, 1 insertion(+) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index 6147d21..f67e168 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -204,6 +204,7 @@ def _unsubscribe_command_handler(channels): for channel in channels: pubnub.unsubscribe(channel) pubnub.unsubscribe(channel + '-pnpres') + print_ok('Unsubscribed from : ' + str(channels)) def _grant_command_handler(channel, auth_key, read, write, ttl, async=False): -- cgit v1.2.3 From 75033e11a429b52b0366d63bdd8f81b795a7145d Mon Sep 17 00:00:00 2001 From: Devendra Date: Sun, 15 Jun 2014 10:23:16 +0530 Subject: fixing customer issue, making changes to dev console to print pretty --- python/examples/console.py | 111 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 94 insertions(+), 17 deletions(-) (limited to 'python/examples/console.py') diff --git a/python/examples/console.py b/python/examples/console.py index f67e168..bf82b5f 100644 --- a/python/examples/console.py +++ b/python/examples/console.py @@ -20,6 +20,17 @@ import os import readline import rlcompleter +import pygments +from pygments.lexers import JsonLexer +from pygments.formatters import TerminalFormatter + +lexer = JsonLexer() +formatter = TerminalFormatter() +def highlight(msg): + return pygments.highlight(msg, lexer, formatter) + + + historyPath = os.path.expanduser("~/.pubnub_console_history") @@ -79,8 +90,7 @@ def get_date(): return color.colorize(datetime.now().strftime( '%H:%M:%S'), "magenta") - -def print_ok(msg, channel=None): +def print_ok_normal(msg, channel=None): if msg is None: return chstr = color.colorize("[" + get_date() + "] ", "magenta") @@ -90,10 +100,11 @@ def print_ok(msg, channel=None): print_console(of, (chstr + color.colorize(str(msg), "green"))) except UnicodeEncodeError as e: print_console(of, (msg)) + of.flush() -def print_error(msg, channel=None): +def print_error_normal(msg, channel=None): if msg is None: return chstr = color.colorize("[" + get_date() + "] ", "magenta") @@ -106,6 +117,37 @@ def print_error(msg, channel=None): print_console(of, (msg)) of.flush() +def print_ok_pretty(msg, channel=None): + if msg is None: + return + chstr = color.colorize("[" + get_date() + "] ", "magenta") + chstr += color.colorize("[Channel : " + channel + + "] " if channel is not None else "", "cyan") + try: + print_console(of, (chstr + highlight(json.dumps(msg, indent=2)))) + except UnicodeEncodeError as e: + print_console(of, (msg)) + + of.flush() + + +def print_error_pretty(msg, channel=None): + if msg is None: + return + chstr = color.colorize("[" + get_date() + "] ", "magenta") + chstr += color.colorize("[Channel : " + channel + + "] " if channel is not None else "", "cyan") + try: + print_console(of, (chstr + color.colorize(color.colorize( + "ERROR: ", "red"), "bold") + + highlight(json.dumps(msg, indent=2)))) + except UnicodeEncodeError as e: + print_console(of, (msg)) + of.flush() + +print_ok = print_ok_pretty +print_error = print_error_pretty + class DefaultPubnub(object): def handlerFunctionClosure(self, name): @@ -295,6 +337,15 @@ class DevConsole(Cmd): pubnub = Pubnub("demo", "demo") self.channel_truncation = 3 self.prompt = self.get_prompt() + self.publish_key = "demo" + self.subscribe_key = "demo" + self.origin = "pubsub.pubnub.com" + self.auth_key = None + self.cipher_key = None + self.secret_key = "demo" + self.ssl = False + self.uuid = None + self.disable_pretty = False def get_channel_origin(self): cho = " [" @@ -341,34 +392,60 @@ class DevConsole(Cmd): kill_all_threads() @options([make_option('-p', '--publish-key', action="store", - default="demo", help="Publish Key"), + default=None, help="Publish Key"), make_option('-s', '--subscribe-key', action="store", - default="demo", help="Subscribe Key"), + default=None, help="Subscribe Key"), make_option('-k', '--secret-key', action="store", - default="demo", help="cipher Key"), + default=None, help="cipher Key"), make_option('-c', '--cipher-key', action="store", - default="", help="Secret Key"), + default=None, help="Secret Key"), make_option('-a', '--auth-key', action="store", default=None, help="Auth Key"), make_option('--ssl-on', dest='ssl', action='store_true', default=False, help="SSL Enabled ?"), make_option('-o', '--origin', action="store", - default="pubsub.pubnub.com", help="Origin"), + default=None, help="Origin"), make_option('-u', '--uuid', action="store", - default=None, help="UUID") + default=None, help="UUID"), + make_option('--disable-pretty-print', dest='disable_pretty', action='store_true', + default=False, help="Disable Pretty Print ?") ]) def do_init(self, command, opts): global pubnub - pubnub = Pubnub(opts.publish_key, - opts.subscribe_key, - opts.secret_key, - opts.cipher_key, - opts.auth_key, - opts.ssl, - opts.origin, - opts.uuid) + global print_ok + global print_error + global print_ok_normal + global print_error_normal + global print_error_pretty + global print_ok_pretty + + self.publish_key = opts.publish_key if opts.publish_key is not None else self.publish_key + self.subscribe_key = opts.subscribe_key if opts.subscribe_key is not None else self.subscribe_key + self.secret_key = opts.secret_key if opts.secret_key is not None else self.secret_key + self.cipher_key = opts.cipher_key if opts.cipher_key is not None else self.cipher_key + self.auth_key = opts.auth_key if opts.auth_key is not None else self.auth_key + self.origin = opts.origin if opts.origin is not None else self.origin + self.uuid = opts.uuid if opts.uuid is not None else self.uuid + self.ssl = opts.ssl if opts.ssl is not None else self.ssl + self.disable_pretty = opts.disable_pretty if opts.disable_pretty is not None else self.disable_pretty + + pubnub = Pubnub(self.publish_key, + self.subscribe_key, + self.secret_key, + self.cipher_key, + self.auth_key, + self.ssl, + self.origin, + self.uuid) self.prompt = self.get_prompt() + if opts.disable_pretty is True: + print_ok = print_ok_normal + print_error = print_error_normal + else: + print_ok = print_ok_pretty + print_error = print_error_pretty + def do_set_sync(self, command): """unset_async Unset Async mode""" -- cgit v1.2.3