-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenFolderModule.py
More file actions
100 lines (88 loc) · 3.34 KB
/
Copy pathopenFolderModule.py
File metadata and controls
100 lines (88 loc) · 3.34 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
import speech_recognition as sr
import os
import re
import json
import pyttsx3
def speak(text):
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.setProperty('rate', 150)
engine.say(text)
engine.runAndWait()
def load_cache():
try:
with open('cache.json', 'r') as f:
return json.load(f)
except FileNotFoundError:
return {}
def save_cache(cache):
with open('cache.json', 'w') as f:
json.dump(cache, f)
def openFolder():
r = sr.Recognizer()
root_dir_path = None
cache = load_cache()
with sr.Microphone() as source:
speak("Say the name of the root directory to search in (or leave blank to search everywhere)")
audio = r.listen(source)
try:
root_dir_name = r.recognize_google(audio).replace(" ", "")
# Remove any extra spaces
root_dir_name = root_dir_name.replace("underscore", "_")
except sr.UnknownValueError:
speak("Unable to recognize speech. Searching everywhere...")
root_dir_name = None
folder_path = None
with sr.Microphone() as source:
speak("Say the name of the folder you want to open")
audio = r.listen(source)
folder_name = r.recognize_google(audio).replace(" ", "")
# Remove any extra spaces
if root_dir_name is not None:
if root_dir_name in cache:
root_dir_path = cache[root_dir_name]
else:
for root, dirs, files in os.walk('/'):
print(f"Searching in {root} for {root_dir_name}")
for name in dirs:
m = re.search(r'\b{}\b'.format(re.escape(root_dir_name.lower())), name.lower(), re.IGNORECASE)
if m is not None:
root_dir_path = os.path.join(root, name)
cache[root_dir_name] = root_dir_path
break
if root_dir_path is not None:
break
if root_dir_path is None:
speak("Root directory not found.")
else:
for root, dirs, files in os.walk(root_dir_path):
print(f"Searching in {root}")
for name in dirs:
m = re.search(r'\b{}\b'.format(re.escape(folder_name.lower())), name.lower(), re.IGNORECASE)
if m is not None:
folder_path = os.path.join(root, name)
break
if folder_path is not None:
break
if folder_path is None:
speak("Folder not found.")
else:
speak(f"Opening folder {folder_name}...")
os.system(f'start "" "{folder_path}"')
else:
for root, dirs, files in os.walk('/'):
print(f"Searching in {root}")
for name in dirs:
m = re.search(r'\b{}\b'.format(re.escape(folder_name.lower())), name.lower(), re.IGNORECASE)
if m is not None:
folder_path = os.path.join(root, name)
break
if folder_path is not None:
break
if folder_path is None:
speak("Folder not found.")
else:
speak(f"Opening folder {folder_name}...")
os.system(f'start "" "{folder_path}"')
save_cache(cache)