-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipe_monitor.py
53 lines (39 loc) · 1.55 KB
/
recipe_monitor.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
import threading
import hashlib
class RecipeMonitor(threading.Thread):
def __init__(self, recipe_processor, gui_element, queue):
threading.Thread.__init__(self, daemon=True)
self.__recipe_processor = recipe_processor
self.__gui_element = gui_element
self.__queue = queue
self.__recipe_path = r'recipe\recipe.xml'
self.__previous_md5 = None
self.start()
def run(self):
while True:
self.__detect_file()
def __detect_file(self):
imported_recipe = self.__open_recipe_file()
if imported_recipe:
new_md5 = self.__calculate_checksum(imported_recipe)
if new_md5 != self.__previous_md5:
self.__previous_md5 = new_md5
self.__process_file(imported_recipe)
def __open_recipe_file(self):
try:
with open(self.__recipe_path, 'rb') as recipe_file:
imported_recipe = recipe_file.read()
except:
pass
else:
return imported_recipe
def __calculate_checksum(self, imported_recipe):
md5 = hashlib.md5(imported_recipe).hexdigest()
return md5
def __process_file(self, imported_recipe):
self.__recipe_processor.import_file(imported_recipe)
recipe = self.__recipe_processor.get_recipe()
additional_parameters = self.__recipe_processor.get_additional_parameters()
self.__queue.put(recipe)
self.__queue.put(additional_parameters)
self.__gui_element.event_generate('<<processed_recipe>>')