summaryrefslogtreecommitdiff
path: root/server/console.py
blob: b7cd37289f83ba75b97a4aea98c8e6bf9a1e2138 (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
'''
    A vital class taking care of the logging
    & formatting the data, this will then be
    written to standard output or returned.

    Verified: 2021 February 7
    * Follows PEP8
    * Tested Platforms
        * Windows 10
    * Third Party Modules
        * colorama
'''

from server.state import ServerStatic, Dynamic
from server.helper import ServerHelper
from server.settings import Settings
from shared.helper import Helper
from shared.state import Static

import threading
import os

import colorama
colorama.init()


class Color:

    @staticmethod
    def green(message):
        return colorama.Fore.LIGHTGREEN_EX \
               + message + colorama.Style.RESET_ALL

    @staticmethod
    def red(message):
        return colorama.Fore.LIGHTRED_EX \
               + message + colorama.Style.RESET_ALL

    @staticmethod
    def yellow(message):
        return colorama.Fore.LIGHTYELLOW_EX \
               + message + colorama.Style.RESET_ALL

    @staticmethod
    def blue(message):
        return colorama.Fore.LIGHTBLUE_EX \
               + message + colorama.Style.RESET_ALL

    @staticmethod
    def cyan(message):
        return colorama.Fore.LIGHTCYAN_EX \
               + message + colorama.Style.RESET_ALL

    @staticmethod
    def magenta(message):
        return colorama.Fore.LIGHTMAGENTA_EX \
                + message + colorama.Style.RESET_ALL

    @staticmethod
    def white(message):
        return colorama.Fore.LIGHTWHITE_EX \
               + message + colorama.Style.RESET_ALL


class Console:

    LOCK = threading.Lock()

    __LOGS = {
        ServerStatic.TRACEBACK: 'traceback.txt',
        ServerStatic.CONSOLE: 'console.txt',
        ServerStatic.ACTION: 'action.txt',
        ServerStatic.HTTP: 'http.txt'
    }

    __STATUS = {
        Static.SUCCESS: ('[+]', '!', Color.green),
        Static.DANGER: ('[-]', '.', Color.red),
        Static.WARNING: ('[!]', '.', Color.yellow),
        Static.INFO: ('[*]', '.', Color.blue)
    }

    __END_PREFIX = (Color.red('[Server]'),
                    Color.red('[Session]'))

    __END_SUFFIX = ' {}{}'.format(
        Color.yellow(ServerStatic.NAME),
        Color.red('>'))

    __DEFAULT_STATUS = 'RAW'
    __TABLE_SEPERATOR = '-'
    __TABLE_MARGIN = 2

    @staticmethod
    def printf(message='', status=None, end=True, raw=False,
               ret=False, loading=False, newline=False, lock=True):
        if raw:
            if message:
                if not loading:
                    Console.log(message + '\n')

                if ret:
                    result = message + '\n'
                else:
                    result = Color.cyan(message)
            else:
                result = ''
        else:
            result = Console.__message(message, status, ret)

        if ret:
            return result
        else:
            if lock:
                with Console.LOCK:
                    print(Console.__newline(newline) + result,
                          end=Console.__end(message, end, raw, loading))
            else:
                print(Console.__newline(newline) + result,
                      end=Console.__end(message, end, raw, loading))

    @staticmethod
    def __message(message, status, ret, void=False):
        pure_status, color_status = Console.__status(status)

        if Settings.CONSOLE_LOG or ret:
            pure = ''

        if not (ret or void):
            color = ''

        for line in message.split('\n'):
            if line:
                if Settings.CONSOLE_LOG or ret:
                    pure += '{} {}\n'.format(
                        pure_status[0],
                        line + pure_status[1])

                if not (ret or void):
                    color += '{} {}\n'.format(
                        color_status[0],
                        Color.white(line + pure_status[1]))

        if Settings.CONSOLE_LOG:
            Console.log(pure, status)

        if not void:
            if ret:
                return pure
            else:
                return color

    @staticmethod
    def __end(message, end, raw, loading):
        if loading:
            return '\r'
        elif end:
            if message == '':
                result = ''
            elif raw:
                result = '\n\n'
            else:
                result = '\n'

            if Dynamic.SESSION:
                result += Console.__END_PREFIX[1]
            else:
                result += Console.__END_PREFIX[0]

            return result + Console.__END_SUFFIX
        elif raw:
            return '\n'
        else:
            return ''

    @staticmethod
    def __status(status):
        status = Console.__STATUS[status]
        return (status[:2], (status[2](status[0]), status[1]))

    @staticmethod
    def __newline(check):
        if check:
            return '\n'
        else:
            return ''

    @staticmethod
    def tabulate(data, headers, ret=False, text=False, prefix=''):
        lengths = Console.__max_length(data, headers)

        if Settings.CONSOLE_LOG or text:
            text_table = ''

        if ret:
            html_headers = ''
            html_rows = ''
        elif not text:
            stdout_result = ''

        for index, column in enumerate(headers):
            table_header = (
                str(column)
                + ServerHelper.repeat(lengths[index] - len(str(column)))
                + ServerHelper.repeat(Console.__TABLE_MARGIN))

            if Settings.CONSOLE_LOG or text:
                text_table += table_header

            if ret:
                html_headers += f'<th>{table_header}</th>'
            elif not text:
                stdout_result += Color.cyan(table_header)

        if Settings.CONSOLE_LOG or text:
            text_table += '\n'

        if not (ret or text):
            stdout_result += '\n'

        for length in lengths:
            table_seperator = (
                ServerHelper.repeat(length, Console.__TABLE_SEPERATOR)
                + ServerHelper.repeat(Console.__TABLE_MARGIN))

            if Settings.CONSOLE_LOG or text:
                text_table += table_seperator

            if not (ret or text):
                stdout_result += Color.white(table_seperator)

        if Settings.CONSOLE_LOG or text:
            text_table += '\n'

        if not (ret or text):
            stdout_result += '\n'

        for row in data:
            if ret:
                html_row = ''

            for index, column in enumerate(row):
                table_row = (
                    str(column)
                    + ServerHelper.repeat(lengths[index] - len(str(column)))
                    + ServerHelper.repeat(Console.__TABLE_MARGIN))

                if Settings.CONSOLE_LOG or text:
                    text_table += table_row

                if ret:
                    html_row += f'<td>{table_row}</td>'
                elif not text:
                    stdout_result += Color.cyan(table_row)

            if Settings.CONSOLE_LOG or text:
                text_table += '\n'

            if ret:
                html_rows += f'<tr>{html_row}<tr>'
            elif not text:
                stdout_result += '\n'

        if Settings.CONSOLE_LOG:
            Console.log(prefix + text_table)

        if text:
            return prefix + text_table
        elif ret:
            table = (f'<table><thead><tr>{html_headers}'
                     f'</tr></thead><tbody>{html_rows}'
                     '</tbody></table>')

            if prefix:
                return prefix + table
            else:
                return dict(html=True, message=table)
        else:
            if prefix:
                print(Color.magenta(prefix) + stdout_result, end='')
            else:
                with Console.LOCK:
                    print(stdout_result)
                    Console.printf(raw=True, lock=False)

    @staticmethod
    def __max_length(data, headers):
        result = [[len(header)] for header in headers]

        for item in data:
            for index, sub_item in enumerate(item):
                result[index].append(len(str(sub_item)))

        return [max(length) for length in result]

    @staticmethod
    def log(message, status=__DEFAULT_STATUS, log_type=ServerStatic.CONSOLE):
        if getattr(Settings, log_type):
            if not os.path.isdir(ServerStatic.LOGS_DIR):
                os.makedirs(ServerStatic.LOGS_DIR, exist_ok=True)

            Helper.write_file(os.path.join(
                ServerStatic.LOGS_DIR,
                Console.__LOGS[log_type]
            ), '{} {} {}\n{}\n'.format(
                Helper.timestamp(),
                ServerStatic.SEPERATOR,
                status, message))

    @staticmethod
    def alert(message, status):
        Console.__message(message, status, False, True)
        return dict(alert=True, message=message, type=status)

    @staticmethod
    def init():
        print(Color.green(r'''
 $$$$$$\   $$$$$$\  $$$$$$$\         $$\    $$\ $$$$$$\ $$$$$$$$\ $$\      $$\
$$  __$$\ $$  __$$\ $$  __$$\        $$ |   $$ |\_$$  _|$$  _____|$$ | $\  $$ |
$$ /  \__|$$ /  $$ |$$ |  $$ |       $$ |   $$ |  $$ |  $$ |      $$ |$$$\ $$ |
$$ |$$$$\ $$ |  $$ |$$ |  $$ |$$$$$$\\$$\  $$  |  $$ |  $$$$$\    $$ $$ $$\$$ |
$$ |\_$$ |$$ |  $$ |$$ |  $$ |\______|\$$\$$  /   $$ |  $$  __|   $$$$  _$$$$ |
$$ |  $$ |$$ |  $$ |$$ |  $$ |         \$$$  /    $$ |  $$ |      $$$  / \$$$ |
\$$$$$$  | $$$$$$  |$$$$$$$  |          \$  /   $$$$$$\ $$$$$$$$\ $$  /   \$$ |
 \______/  \______/ \_______/            \_/    \______|\________|\__/     \__|
 ''') + Color.red(r'''
          _       __
         | |     / /____ _ ____   _____ __ ___
 ______  | | /| / // __ `// __ \ / ___// // _ \
/_____/  | |/ |/ // /_/ // / / /(__  )/ //  __/
         |__/|__/ \__,_//_/ /_//____//_/ \___/
'''))

        Console.printf(ServerStatic.LOADING_AMID,
                       raw=True, loading=True, lock=False)
        Helper.clear_pyinstaller_temp()

        if not ServerStatic.TERMINAL:
            Console.printf(Helper.join(
                f'HTTP & WS Host Address: {ServerStatic.GUI_HOST}',
                f'TCP Host Address: {ServerStatic.HOST}'
            ), Static.INFO, lock=False)
        else:
            Console.printf(f'TCP Host Address: {ServerStatic.HOST}',
                           Static.INFO, lock=False)