forked from rajputonnet/Friday_3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookup_drive_change.py
97 lines (75 loc) · 3 KB
/
lookup_drive_change.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
import ast
import os
from watchdog.events import PatternMatchingEventHandler
from watchdog.observers import Observer
import search_directory
lookup_dict = search_directory.file_system_dict
class ExampleHandler(PatternMatchingEventHandler):
@staticmethod
def get_dup_key(key, keys):
count = 0
for k in keys:
if k in key:
count += 1
return count
@staticmethod
def get_event(self, event_name, method):
src = event_name.src_path
key = src.rsplit('\\')[-1].lower()
if method == 'on_created':
lookup_dict.update({key + '_{}'.format(
ExampleHandler.get_dup_key(key, lookup_dict.keys())): src})
print('KEY ADDED')
print(key)
elif method == 'on_deleted':
key_list = lookup_dict.keys()
try:
for key in list(key_list):
if lookup_dict.get(key) == src:
del lookup_dict[key]
print('KEY DELETED')
print(key)
except KeyError:
pass
elif method == 'on_moved' or method=='on_modified':
key_list = lookup_dict.keys()
try:
for key in list(key_list):
if lookup_dict.get(key) == src:
del lookup_dict[key]
print('KEY MOVED')
print(key)
lookup_dict.update({key + '_{}'.format(
ExampleHandler.get_dup_key(key, lookup_dict.keys())): src})
dir_txt = {}
file_name='dict.txt'
if os.path.exists(file_name):
with open(file_name, 'r') as f:
h = ast.literal_eval(f.read())
dir_txt.update(h)
f.close()
with open(file_name, 'w') as f:
dir_txt.update({key + '_{}'.format(
ExampleHandler.get_dup_key(key, lookup_dict.keys())): src})
f.write(str(dir_txt))
f.close()
print('NEW KEY ADDED')
print(key)
except KeyError:
pass
def on_created(self, event): # when file is created
ExampleHandler.get_event(self, event, 'on_created')
def on_modified(self, event):
ExampleHandler.get_event(self, event, 'on_modified')
def on_deleted(self, event):
ExampleHandler.get_event(self, event, 'on_deleted')
def on_moved(self, event):
ExampleHandler.get_event(self, event, 'on_moved')
# search_directory.find()
def lookup(drive):
observer = Observer()
event_handler = ExampleHandler(ignore_patterns=['*.tmp','*AppData*','*Temp*','*$*','*ProgramData*','*__*']) # create event handler
# set observer to use created handler in directory
observer.schedule(event_handler, path='D:\\Akhilesh\\', recursive=True)
observer.start()
observer.join()