aboutsummaryrefslogtreecommitdiffstats
path: root/python/examples/console.py
blob: bfa44863fd12a488c6b171c80d9e93c81b08bcdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
## 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 Pubnub
import threading
from datetime import datetime

from cmd2 import Cmd, make_option, options, Cmd2TestCase
import optparse
import json

import atexit
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")


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

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_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 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_normal(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(str(msg), "green")))
    except UnicodeEncodeError as e:
        print_console(of, (msg))

    of.flush()


def print_error_normal(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(
            str(msg), "red"), "bold")))
    except UnicodeEncodeError as e:
        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):
        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():
            stop(thread)


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


def _subscribe_command_handler(channel):

    def _callback(r, ch):
        print_ok(r, ch)

    def _error(r, ch=None):
        print_error(r, ch if ch is not None else channel)

    def _disconnect(r):
        print_ok("DISCONNECTED", r)

    def _reconnect(r):
        print_ok("RECONNECTED", r)

    def _connect(r):
        print_ok("CONNECTED", r)

    pubnub.subscribe(channel, _callback, _error, connect=_connect,
                     disconnect=_disconnect, reconnect=_reconnect)


def _unsubscribe_command_handler(channels):

    def _callback(r):
        print_ok(r)

    def _error(r):
        print_error(r)

    unsub_list = []
    current_channel_list = pubnub.get_channel_array()

    for channel in channels:
        pubnub.unsubscribe(channel)
        if (channel in current_channel_list):
            unsub_list.append(channel)

        #pubnub.unsubscribe(channel + '-pnpres')
        #if (channel + '-pnpres' in current_channel_list):
        #    unsub_list.append(channel + ' (Presence)')

    if len(unsub_list) > 0:
        print_ok('Unsubscribed from : ' + str(unsub_list))
    else:
        print_error('Not subscribed to : ' + str(channels))


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


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


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


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


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


def kill_all_threads():
    for thread in threading.enumerate():
        if thread.isAlive():
            stop(thread)


def get_channel_array():
    channels = pubnub.get_channel_array()

    for channel in channels:
        if "-pnpres" in channel:
            chname = channel.split("-pnpres")[0]
            if chname not in channels:
                continue
            i = channels.index(chname)
            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" + "\n" + \
            "Commands can also be provided on command line while starting console (in quotes) ex. " + \
            "pubnub-console 'init -p demo -s demo'"
        self.default_channel = None
        self.async = False
        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 = ""
        channels = 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..."
        else:
            cho += ",".join(channels)

        if len(channels) > 0:
            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") + "] > "

    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 += self.get_channel_origin()
        return prompt

    def precmd(self, line):
        self.prompt = self.get_prompt()
        return line

    #def emptyline(self):
    #    self.prompt = self.get_prompt()

    def cmdloop_with_keyboard_interrupt(self):
        try:
            self.cmdloop()
        except KeyboardInterrupt as e:
            pass
        sys.stdout.write('\n')
        kill_all_threads()

    @options([make_option('-p', '--publish-key', action="store",
                          default=None, help="Publish Key"),
              make_option('-s', '--subscribe-key', action="store",
                          default=None, help="Subscribe Key"),
              make_option('-k', '--secret-key', action="store",
                          default=None, help="cipher Key"),
              make_option('-c', '--cipher-key', action="store",
                          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=None, help="Origin"),
              make_option('-u', '--uuid', action="store",
                          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
        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"""
        self.async = False

    def do_set_async(self, command):
        """set_async
        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):
        global full_date
        """do_set_full_date
        Set Full Date"""
        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"""
        full_date = False
        self.prompt = self.get_prompt()

    @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
        self.prompt = self.get_prompt()

    @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

        _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")
              ])
    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")
            return

        _history_command_handler(opts.channel, opts.count, async=self.async)

    @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
        if opts.channel is None:
            print_error("Missing channel")
            return

        if command is None:
            print_error("Missing message")
            return

        try:
            message = json.loads(str(command))
        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"),
              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 \
            if opts.channel is None else opts.channel

        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)

        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"),
              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 \
            if opts.channel is None else opts.channel

        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)

        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",
                          help="Auth Key")
              ])
    def do_audit(self, command, opts):
        opts.channel = self.default_channel \
            if opts.channel is None else opts.channel

        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"),
              make_option('-p', '--presence', action="store_true",
                          dest="presence",
                          default=False, help="Unsubscribe from presence events ?")
              ])
    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 = []
            chs = pubnub.get_channel_array()
            for ch in chs:
                if '-pnpres' not in ch:
                    opts.channel.append(ch)
                elif opts.presence is True:
                    opts.channel.append(ch)

        if opts.channel is None:
            print_error("Missing channel")
            return

        if not isinstance(opts.channel, list):
            ch = []
            ch.append(opts.channel)
            opts.channel = ch

        channels = []
        if opts.presence is True and opts.all is False:
            for c in opts.channel:
                if '-pnpres' not in c:
                    channels.append(c + '-pnpres')

        for c in opts.channel:
            channels.append(c)

        _unsubscribe_command_handler(channels)
        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"),
              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 \
            if opts.channel is None else opts.channel
        if opts is None:
            print_error("Missing argument")
            return

        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(get_channel_array())
        self.prompt = self.get_prompt()

    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_with_keyboard_interrupt()

if __name__ == "__main__":
    main()