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') 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