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 --- Pubnub.py | 52 ++++++++++++++++++++++++---------------------- python/examples/console.py | 50 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 69 insertions(+), 33 deletions(-) diff --git a/Pubnub.py b/Pubnub.py index 3e8fd64..fc822f7 100644 --- a/Pubnub.py +++ b/Pubnub.py @@ -713,7 +713,7 @@ class PubnubCoreAsync(PubnubBase): for i in l: func(i) - def subscribe(self, channel, callback, error=None, + def subscribe(self, channels, callback, error=None, connect=None, disconnect=None, reconnect=None, sync=False): """ #** @@ -756,11 +756,13 @@ class PubnubCoreAsync(PubnubBase): self.susbcribe_sync(args) return - def _invoke(func, msg=None): + def _invoke(func, msg=None, channel=None): if func is not None: - if msg is not None: + if msg is not None and channel is not None: + func(msg,channel) + elif msg is not None: func(msg) - else: + else : func() def _invoke_connect(): @@ -802,31 +804,32 @@ class PubnubCoreAsync(PubnubBase): chobj = self.subscriptions[ch] if chobj['subscribed'] is True: return chobj - - ## New Channel? - if not channel in self.subscriptions or \ - self.subscriptions[channel]['subscribed'] is False: - with self._channel_list_lock: - self.subscriptions[channel] = { - 'name': channel, - 'first': False, - 'connected': False, - 'disconnected': True, - 'subscribed': True, - 'callback': callback, - 'connect': connect, - 'disconnect': disconnect, - 'reconnect': reconnect, - 'error': error - } - + channels = channels if isinstance(channels,list) else channels.split(",") + for channel in channels: + ## New Channel? + if not channel in self.subscriptions or \ + self.subscriptions[channel]['subscribed'] is False: + with self._channel_list_lock: + self.subscriptions[channel] = { + 'name': channel, + 'first': False, + 'connected': False, + 'disconnected': True, + 'subscribed': True, + 'callback': callback, + 'connect': connect, + 'disconnect': disconnect, + 'reconnect': reconnect, + 'error': error + } + ''' ## return if already connected to channel if channel in self.subscriptions and \ 'connected' in self.subscriptions[channel] and \ self.subscriptions[channel]['connected'] is True: _invoke(error, "Already Connected") return - + ''' ## SUBSCRIPTION RECURSION def _connect(): @@ -845,6 +848,7 @@ class PubnubCoreAsync(PubnubBase): _invoke_error(err=response['message']) else: _invoke_disconnect() + self.timetoken = 0 self.timeout(1, _connect) def sub_callback(response): @@ -1129,8 +1133,6 @@ def _requests_request(url, timeout=320): msg = str(error) return (json.dumps(msg), 0) except requests.exceptions.Timeout as error: - #print(error); - #print('timeout'); msg = str(error) return (json.dumps(msg), 0) 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