-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.py
96 lines (65 loc) · 2.18 KB
/
message.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import enum
import tkinter
import gui.base
import config
# Messages that are generated before the UI is initialized.
_messages = []
_handler = None
class MessageHandler:
def __init__(self, parent):
self.textbox = tkinter.Text(
parent,
font=(config.messagefont, config.messagefontsize, 'normal'),
background="black")
self.textbox.config(state="disabled")
gui.base.setup_scrollbars(parent, self.textbox)
self._configcolors()
for msg in _messages:
self.write(msg)
def write(self, msg):
t = self.textbox
t.config(state="normal")
t.insert("end", str(msg).strip() + "\n", str(msg.level))
t.config(state="disabled")
t.yview("end")
def _configcolors(self):
tc = self.textbox.tag_config
tc(str(Level.debug), foreground="grey")
tc(str(Level.info), foreground="lightgrey")
tc(str(Level.warning), foreground="orange")
tc(str(Level.error), foreground="orangered")
class Level(enum.Enum):
invalid = 0
debug = 1
info = 2
warning = 3
error = 4
class Message:
def __init__(self, level, *args, sep=" "):
self.level = level
self.parts = [str(arg) for arg in args]
self.separator = sep
def __str__(self):
return self.separator.join(self.parts)
def init(parent):
global _handler
_handler = MessageHandler(parent)
def debug(*args, sep=" "):
if config.debug:
_handler.write(Message(Level.debug, *args, sep=sep))
def info(*args, sep=" "):
_handler.write(Message(Level.info, *args, sep=sep))
def warn(*args, sep=" "):
_handler.write(Message(Level.warning, *args, sep=sep))
def error(*args, sep=" "):
_handler.write(Message(Level.error, *args, sep=sep))
class _TmpMessageHandler:
"""A handler that is active before the UI is intitalized.
It will be intialized in the bottom of this source file.
"""
def write(self, msg):
_messages.append(msg)
_handler = _TmpMessageHandler()