File size: 3,632 Bytes
54f81bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python

import logging
import os
import sys
from pprint import pformat

from twisted.internet import reactor, stdio, defer
from twisted.protocols.basic import LineReceiver
import twisted.python.log as twisted_log

from exchangelib import bitstamp, bitfinex, btce, huobi

log = logging.getLogger(__name__)

EXCHANGES = ('bitstamp', 'bitfinex', 'btce', 'huobi')
# todo redo EXCHANGE_COMMANDS with an api interface
EXCHANGE_COMMANDS = ('ticker', 'orderbook', 'trades')
COMMANDS = ()


class ApiRepl(LineReceiver):
    """"""
    # IMPORTANT NOTE: do NOT use print, only self.transport.write.
    delimiter = '\n'
    default_prompt = '> '

    def __init__(self):
        self.add_to_prompt = list()
        self.exchanges = {'bitstamp': bitstamp,
                          'bitfinex': bitfinex,
                          'btce': btce,
                          'huobi': huobi}

    def connectionMade(self):
        self.intro()
        self.prompt()

    @defer.inlineCallbacks
    def lineReceived(self, line):
        if line:
            # this needs to yield so results get printed before the next prompt
            yield self.process_cmd(line)
        self.prompt()

    def process_cmd(self, cmd):
        """:type cmd: str or unicode"""
        if cmd.startswith('?') or cmd == 'help':
            self.help(cmd[2:])
        elif cmd == 'exit' or cmd == 'q' or cmd == 'quit':
            self.quit()
        elif cmd == 'purge cache':
            # todo cache purge command
            pass
        else:
            return self.exchange_cmd(cmd)
        # todo a watch (+ un watch) command for polling

    def prompt(self):
        prompt = '.'.join(self.add_to_prompt) + self.default_prompt
        self.write(prompt)

    @defer.inlineCallbacks
    def exchange_cmd(self, cmd):
        for exchange in EXCHANGES:
            if cmd.startswith(exchange):
                api = self.exchanges[exchange]
                subcmd = cmd[len(exchange)+1:]

                # todo add a-z checking for subcmd to avoid it being stuff like __ attributes
                if subcmd:
                    try:
                        func = getattr(api, subcmd)
                        """:type: Callable"""
                    except AttributeError as e:
                        # todo improve this error message
                        self.writeln("'{}' is not a valid command.".format(subcmd))
                    else:
                        # todo pass options as well
                        ret = yield func()
                        fmted = pformat(ret)
                        self.writeln(fmted)
                        defer.returnValue(fmted)
                else:
                    # todo no sub command specified, print info
                    pass
                break

    def intro(self):
        self.writeln("Welcome! Enter ? for a list of commands.")

    def help(self, subcmd):
        subcmd = subcmd or ''
        self.writeln("<exchange> <command> <args>.\n Valid exchanges: {}. \n Valid commands: {}.".format(
            ', '.join(EXCHANGES), ', '.join(EXCHANGE_COMMANDS)))

    @staticmethod
    def quit():
        reactor.stop()

    def write(self, line):
        self.transport.write(line)

    def writeln(self, line):
        self.write(line + '\n')


def main():
    if os.name == 'nt':
        print("This application does not work on Windows due to a Twisted bug. Sorry!")
        sys.exit(0)
    twisted_log.PythonLoggingObserver().start()
    logging.basicConfig(level=logging.WARNING)
    stdio.StandardIO(ApiRepl())
    reactor.run()


if __name__ == '__main__':
    main()