-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
104 lines (94 loc) · 3.21 KB
/
main.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
97
98
99
100
101
102
103
104
# -*- coding: utf-8 -*-
import csv
import time
import subprocess
from random import randint
from datetime import datetime
from pyxhook import HookManager
RESOURCES_PATH = "res/"
DATABASE = RESOURCES_PATH + "data.csv"
FUNNY_FILE = RESOURCES_PATH + "funnymessages.txt"
words_dictionary = {}
index = 0
ignore_keys = ["Control_R", "Control_L", "Shift_L", "Shift_R", "Caps_Lock", "Alt_L", "Alt_R"]
allowed_characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
current_buffer = ""
def sendmessage(message):
subprocess.Popen(['notify-send', message])
return
def StartService():
with open(FUNNY_FILE, "r") as myfile:
global funnymessages
funnymessages = myfile.readlines()
for key, val in csv.reader(open(DATABASE)):
words_dictionary[key] = val
hm.start()
def StopService():
w = csv.writer(open(DATABASE, "w+"))
for key, val in words_dictionary.items():
w.writerow([key, val])
hm.cancel()
def Handle_Keyboard_Event(event):
global current_buffer
global index
_key = event.Key
if _key in ignore_keys:
return
if _key=="BackSpace" and current_buffer:
current_buffer=current_buffer[:(index - 1)] + current_buffer[index:]
index = index - 1
elif _key=="Left" and index!=0:
index = index - 1
elif _key=="Right" and len(current_buffer)!=index:
index = index + 1
elif _key=="End" or _key=="KP_End":
index=len(current_buffer)
elif ( _key=="Delete" or _key=="KP_Delete" ) and len(current_buffer)!=index:
current_buffer = current_buffer[:index] + current_buffer[( index + 1 ):]
elif allowed_characters.find(_key) != -1:
current_buffer = current_buffer[:index] + _key + current_buffer[index:]
index = index + 1
else:
current_buffer = current_buffer.lower()
if current_buffer in words_dictionary:
sendmessage("{0} `{1}`?".format(funnymessages[randint(0,len(funnymessages)-1)], current_buffer))
current_buffer = ""
index=0
# Keyboard Hookup settings
hm = HookManager()
hm.HookKeyboard()
hm.KeyUp = Handle_Keyboard_Event
def WriteScreen(message):
print "[{0}]:\t{1}".format(str(datetime.now().strftime('%Y-%m-%d %H:%M:%S')), message)
def Handle_User():
global current_buffer
cmd = str(raw_input()).strip().lower()
current_buffer = "" # empty up buffer
if cmd == "close":
StopService()
return False
else:
args = cmd.split(' ')
if args[0] == "add":
new_word = args[1]
if new_word in words_dictionary:
WriteScreen("Already exist!")
else:
words_dictionary[new_word] = True
elif args[0] == "enable":
new_word = args[1]
if new_word in words_dictionary:
words_dictionary[new_word] = True
else:
WriteScreen("No such word exist!")
elif args[0] == "disable":
new_word = args[1]
if new_word in words_dictionary:
words_dictionary[new_word] = False
else:
WriteScreen("No such word exist!")
return True
StartService()
while Handle_User():
continue
WriteScreen("Closed!")