-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
186 lines (148 loc) · 5.17 KB
/
main.py
File metadata and controls
186 lines (148 loc) · 5.17 KB
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
"""
Desc: Easy application for sending notifications
Author: Benjamin Auinger (github.com/traceur99100)
Usage:
python3 main.py <msg> <duration>
msg: message that will be shown
duration: defines how long the notification is visible
ws = root.winfo_screenwidth() # width of the screen
hs = root.winfo_screenheight() # height of the screen
"""
### imports ###
from tkinter import *
import sys
import time
import re
### functions ###
"""
function: configFileValid()
@desc: checks if the config file has any errors
@param: content - content of the config file
"""
def configFileValid(content):
attributes = "(width|height|background-color|font|font-size|font-color|coords)"
attributeList = content.split("\n")
attributeListNew = []
# remove all comments
for attribute in attributeList:
if(not(attribute.startswith("#") or attribute.startswith("\n") or attribute == "")):
attributeListNew.append(attribute)
for attribute in attributeListNew:
if not (re.search("%s=[a-zA-Z0-9 ]+#?(.+)?" %(attributes), attribute)):
return False
return True
"""
function: getAttribute(attribute)
@desc: returns a specific attribute from the config file,
if it doesnt exist an exception will be risen
@param:
attribute - defines the attribute which will be returned from
the config file
"""
def getAttribute(attribute):
content = ""
with open("notifierrc", "r") as configFile:
content = configFile.read()
# raise an exception if the config file isnt valid
if(not configFileValid(content)):
raise ValueError("Config files contains syntax errors")
contentList = content.split("\n")
for line in contentList:
if(line.startswith(attribute)):
return line
"""
function: getWidth()
@desc: returns the defined width in the config file
"""
def getWidth():
width = getAttribute("width")
if(re.match("width=[0-9]+", width)):
return int(width.split("=")[1])
raise ValueError("width: invalid value")
"""
function: getHeight()
@desc: returns the defined height in the config file
"""
def getHeight():
height = getAttribute("height")
if(re.match("height=[0-9]+", height)):
return int(height.split("=")[1])
raise ValueError("height: invalid value")
"""
function: getCoords()
@desc: returns a tupple containing x and y coords
"""
def getCoords():
coords = getAttribute("coords")
if(re.match("coords=([0-9]+\s[0-9]+|top-left|top-right|bottom-right|bottom-left)", coords)):
coords = coords.split("=")[1]
if(coords == "top-right"):
pass
elif(coords == "top-left"):
pass
elif(coords == "bottom-right"):
pass
elif(coords == "bottom-left"):
pass
coords = coords.split(" ")
return (int(coords[0]), int(coords[1]))
raise ValueError("coords: invalid value")
"""
function: getFont()
@desc: return the font defined in the config file
"""
def getFont():
font = getAttribute("font")
if(re.match("font=[a-zA-Z_-]+", font)):
return font.split("=")[1]
raise ValueError("font: invalid value")
"""
function: getFontSize()
@desc: returns the font size defined in the config file
"""
def getFontSize():
font_size = getAttribute("font-size")
if(re.match("font-size=[0-9]+", font_size)):
return font_size.split("=")[1]
raise ValueError("font-size: invalid value")
"""
function: getBackgroundColor()
@desc: returns background-color attribute
"""
def getBackgroundColor():
background_color = getAttribute("background-color")
if(re.match("background-color=[0-9a-fA-F]{3,6}", background_color)):
return background_color.split("=")[1]
raise ValueError("background-color: invalid value")
"""
function: getFontColor()
@desc: returns font-color attribute
"""
def getFontColor():
font_color = getAttribute("font-color")
if(re.match("font-color=[0-9a-fA-F]{3,6}", font_color)):
return font_color.split("=")[1]
raise ValueError("font-color: invalid value")
"""
function: sendMessage(msg, duration)
@desc: creates a window with a message, position and size are defined by the config file
@param:
msg - message that will be shown
duration - defines how long the notification is visible
"""
def sendMessage(msg, duration):
window = Tk()
canvas = Canvas(window, width=getWidth(), height=getHeight())
canvas.pack()
canvas.create_rectangle((0,0,getWidth(), getHeight()), fill="#" + getBackgroundColor())
canvas.create_text((getWidth() // 2, getHeight() // 2), text=msg, fill="#" + getFontColor(), font=(getFont(), getFontSize()))
window.overrideredirect(1)
window.after(duration, lambda: window.destroy())
window.title(string="Notification")
window.resizable(width=False,height=False)
window.geometry('%dx%d+%d+%d' % (getWidth(), getHeight(), getCoords()[0], getCoords()[1]))
window.mainloop()
if __name__ == "__main__":
msg = sys.argv[1]
duration = sys.argv[2]
sendMessage(msg, duration)