This repository has been archived by the owner on Nov 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConfigMGRClass.py
138 lines (113 loc) · 3.83 KB
/
ConfigMGRClass.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
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
import pickle
import os
import io
import winreg
import random
import ctypes
def update_and_display_notification(msg, flag=0):
if flag:
#yes or no
type_ = 0x04 #MB_YESNO
else:
#ok
type_ = 0 #MB_OK
MessageBox = ctypes.windll.user32.MessageBoxW
rtn = MessageBox(None, msg, 'PyAV', type_)
if rtn == 6:
return True
else:
return False
'''
def update_and_display_notification(msg):
message = f'm =MsgBox("{msg}", 16, "PyAV")'
with open('notification.vbs', 'w') as f:
f.write(message)
os.startfile('notification.vbs')
'''
class REG():
'''
0 = apikey
1 = state
2 = exclusions
'''
def __init__(self):
pass
def check_keys(self):
try:
hkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, 'Software\PyAV',0, winreg.KEY_READ)
except:
return False
if len([winreg.EnumValue(hkey, i) for i in range(3)]) == 3:
return True
else:
return False
def load_defaults(self):
try:
self.clean_reg()
except:
pass
winreg.CreateKey(winreg.HKEY_CURRENT_USER, 'SOFTWARE\PyAV')
self.set_key('apikey', ['5fe2ba1c10c5770341fa142b1b97de16dc8ffe4f5af965463c279b3f8d538785'])
self.set_key('state', '1')
self.set_key('exclusions', ['C:\\Windows', 'C:\\Python362'])
def clean_reg(self):
hkey = winreg.CreateKey(winreg.HKEY_CURRENT_USER, 'SOFTWARE')
winreg.DeleteKey(hkey, 'PyAV')
def pickle_value(self, value):
f = io.BytesIO()
pickle.dump(value,f)
f.seek(0)
value = f.read()
return value
def unpickle_value(self, value):
f = io.BytesIO()
f.write(value)
f.seek(0)
value = pickle.load(f)
return value
def set_key(self, key, value):
value = self.pickle_value(value)
hkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, 'Software\PyAV',0, winreg.KEY_SET_VALUE)
winreg.SetValueEx(hkey, key, 0, winreg.REG_BINARY, value)
def read_key(self, index):
hkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, 'Software\PyAV',0, winreg.KEY_READ)
value = winreg.EnumValue(hkey, index)
return self.unpickle_value(value[1])
def add_api_key(self, api_key):
if len(api_key) != 64:
raise OSError('invalid api key')
else:
curr_keys = self.read_key(0)
if api_key in curr_keys:
raise OSError('key already in config')
else:
curr_keys.append(api_key)
self.set_key('apikey', curr_keys)
return True
def retrive_api_key(self):
keys = self.read_key(0)
choice = random.choice(keys)
return choice
def change_state(self, state):
self.set_key('state', state)
def get_state(self):
state = self.read_key(1)
return state
def add_exclusion(self, path):
if not os.path.exists(path):
raise OSError('specified exclusion path does not exist')
curr_exs = self.get_exclusion()
if path in curr_exs:
raise OSError('already in exlusions')
curr_exs.append(path)
self.set_key('exclusions', curr_exs)
return True
def remove_exclusion(self, path):
exs = self.get_exclusion()
if path not in exs:
raise OSError('path not in exclusions')
exs.remove(path)
self.set_key('exclusions', exs)
def get_exclusion(self):
exs = self.read_key(2)
return exs