summaryrefslogtreecommitdiffstats
path: root/server/src/cmd_ui.py
blob: 94cbf614643791445c2b07ce4b0ccf0406c7b0be (plain) (blame)
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
"""Command line user interface"""

import cmd
import re
import threading

import authority_string_transformer

INTRO = 'Type the command "help" for help document.'
PROMPT = '> '


class CmdUI(cmd.Cmd):  # pylint: disable=R0904
    """Command line user interface.

    It's a simple UI for doing operation on server, supplied commands:
        - Add/delete/reset a user
        - List (online) users
        - Save/load the user list to/from a file.
        - Exit.
        - Prints the help document.

    And it is also the main output interface of the whole program.

    Attributes:
        _users_text_manager: An instance of UsersTextManager.
        _tcp_server: An instance of TcpServer.
        _shared_vim_server: An instance of SharedVimServer.
        _exit_flag: Whether this UI should stop or not.
        _thread: Instance of Thread.
        _init_cmds: Initialize commands.
    """
    def __init__(self,
                 init_cmds, users_text_manager, tcp_server, shared_vim_server):
        """Constructor.

        Args:
            init_cmds: Lists of commands to run after startup.
            users_text_manager: An instance of UsersTextManager.
            tcp_server: An instance of TCPServer.
            shared_vim_server: An instance of SharedVimServer.
        """
        super(CmdUI, self).__init__()
        self.prompt = PROMPT
        self._users_text_manager = users_text_manager
        self._tcp_server = tcp_server
        self._shared_vim_server = shared_vim_server
        self._stop_flag = False
        self._thread = None
        self._init_cmds = init_cmds

    def do_add(self, text):
        """Adds a user, [usage] add <identity> <nickname> <authority>"""
        try:
            identity, nickname, authority_str = _split_text(text, 3)
            if identity in self._users_text_manager.get_users_info():
                self.write('The identity %r is already in used.\n' % identity)
                return
            authority = authority_string_transformer.to_number(authority_str)
            self._users_text_manager.add_user(identity, nickname, authority)
            user_info = self._users_text_manager.get_users_info()[identity]
            self.write('Added %s => %s\n' % (identity, str(user_info)))
        except _SplitTextError:
            self.write('Format error!\n' +
                       '[usage] add <identity> <nickname> <authority>\n')
        except authority_string_transformer.Error as e:
            self.write('Fail: %r\n' % e)

    def do_delete(self, text):
        """Deletes a user, [usage] delete <identity>"""
        try:
            identity = _split_text(text, 1)[0]
            if identity not in self._users_text_manager.get_users_info():
                self.write('The identity %r is not in used.\n' % identity)
                return
            self._users_text_manager.delete_user(identity)
            self.write('Done\n')
        except _SplitTextError:
            self.write('Format error!\n' +
                       '[usage] delete <identity>\n')

    def do_deleteall(self, text):
        """Deletes all users, [usage] deleteall"""
        try:
            _split_text(text, 0)
            for identity in self._users_text_manager.get_users_info():
                self._users_text_manager.delete_user(identity)
            self.write('Done\n')
        except _SplitTextError:
            self.write('Format error!\n' +
                       '[usage] deleteall\n')

    def do_reset(self, text):
        """Resets a user, [usage] reset <identity>"""
        try:
            iden = _split_text(text, 1)[0]
            if iden not in self._users_text_manager.get_users_info():
                self.write('The User with identity %r is not exist.\n' % iden)
                return
            self._users_text_manager.reset_user(iden)
            user_info = self._users_text_manager.get_users_info()[iden]
            self.write('Reseted %s ==> %s\n' % (iden, str(user_info)))
        except _SplitTextError:
            self.write('Format error!\n' +
                       '[usage] reset <identity>\n')

    def do_list(self, text):
        """Lists users, [usage] list"""
        try:
            _split_text(text, 0)
            infos = self._users_text_manager.get_users_info().items()
            for iden, user in sorted(infos, key=lambda x: x[0]):
                self.write('%-10s => %s' % (iden, str(user)))
        except _SplitTextError:
            self.write('Format error!\n' +
                       '[usage] list\n')

    def do_online(self, text):
        """Lists online users, [usage] online"""
        try:
            _split_text(text, 0)
            infos = self._users_text_manager.get_users_info(
                must_online=True).items()
            for iden, user in sorted(infos, key=lambda x: x[0]):
                self.write('%-10s => %s' % (iden, str(user)))
        except _SplitTextError:
            self.write('Format error!\n' +
                       '[usage] online\n')

    def do_load(self, text):
        """Loads users from a file, [usage] load <filename>"""
        try:
            filename = _split_text(text, 1)[0]
            with open(filename, 'r') as f:
                for line in f.readlines():
                    line = line if not line.endswith('\n') else line[ : -1]
                    if not line:
                        continue
                    self.do_add(line)
            self.write('Done')
        except _SplitTextError:
            self.write('Format error!\n' +
                       '[usage] load <filename>\n')
        except IOError as e:
            self.write('Error occured when opening the file: %r' % e)

    def do_save(self, text):
        """Saves users list to file, [usage] save <filename>"""
        try:
            filename = _split_text(text, 1)[0]
            with open(filename, 'w') as f:
                users_info = self._users_text_manager.get_users_info().items()
                for iden, user in sorted(users_info, key=lambda x: x[0]):
                    auth_str = authority_string_transformer.to_string(
                        user.authority)
                    f.write('%s %s %s\n' % (iden, user.nick_name, auth_str))
        except _SplitTextError:
            self.write('Format error!\n' +
                       '[usage] save <filename>\n')
        except IOError as e:
            self.write('Cannot open file? %s' % str(e))

    def do_port(self, text):
        """Print the server's port."""
        try:
            _split_text(text, 0)
            self.write('Server port = %r\n' % self._tcp_server.port)
        except _SplitTextError:
            self.write('Format error!\n' +
                       '[usage] port\n')

    def do_exit(self, text):
        """Exits the program."""
        try:
            _split_text(text, 0)
            self._shared_vim_server.stop()
        except _SplitTextError:
            self.write('Format error!\n'
                       '[usage] exit\n')

    def do_echo(self, text):  # pylint: disable=R0201
        """Echo."""
        print(text)

    def do_eval(self, text):
        """For debug, evaluate an peice of python code and prints the result."""
        try:
            result = eval(text)  # pylint: disable=W0123
            self.write('The result is %r\n' % result)
        except Exception as e:  # pylint: disable=W0703
            self.write('Exception occured: %r' % e)

    def do_help(self, text):
        """Prints the help document, [usage] help"""
        try:
            _split_text(text, 0)
            commands = [m[3 : ] for m in dir(self) if m.startswith('do_')]
            self.write('Commands: \n' + ' '.join(commands))
        except _SplitTextError:
            self.write('Format error!\n' +
                       '[usage] help\n')

    def do_EOF(self, text):  # pylint: disable=C0103
        """Same as exit"""
        self.do_exit(text)

    def emptyline(self):
        """Do nothing."""
        pass

    def postcmd(self, unused_stop, unused_text):
        """Checks whether it should be stop or not."""
        return self._stop_flag

    def write(self, text):
        """Writes text by this UI.

        It will call the "do_echo" command.

        Args:
            text: String to be printed.
        """
        for line in text.splitlines():
            self.onecmd('echo ' + line)

    def preloop(self):
        for c in self._init_cmds:
            self.onecmd(c)

    def start(self):
        """Starts this CmdUI."""
        self._thread = threading.Thread(target=self.cmdloop,
                                        args=(INTRO,))
        self._thread.start()

    def stop(self):
        """Stops the command line UI."""
        self._stop_flag = True
        self.onecmd('echo bye~\n')

    def join(self):
        """Joins this thread."""
        self._thread.join()

    def flush(self):
        """Flush the screen."""
        pass


class _SplitTextError(Exception):
    """Error raised by the function _split_text()."""
    pass

def _split_text(text, num):
    """Split the text into tuple.

    Args:
        text: The string to be splitted.
        num: Number of elements in the result tuple.

    Return:
        A <num>-tuple.
    """
    words = [word for word in re.split(r'[ \t]', text) if word]
    if len(words) != num:
        raise _SplitTextError()
    return tuple(words)