-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
217 lines (191 loc) · 5.07 KB
/
functions.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import os
import shutil as sh
import webbrowser
import time
from gtts import gTTS
import playsound
import speech_recognition as fn_sr
def fn_talk(k):
input=fn_sr.Recognizer()
with fn_sr.Microphone() as source:
if(k==""): d=1
else: d=k
input.adjust_for_ambient_noise(source,duration=d)
audio=input.listen(source)
data=""
try:
data=input.recognize_google(audio)
print("Response is: " + data)
except fn_sr.UnknownValueError:
respond("Sorry I did not hear you, Please repeat again.")
return fn_talk("").lower()
return data
def fn_respond(output):
num=0
print(output)
num += 1
response=gTTS(text=output, lang='en')
file = str(num)+".mp3"
response.save(file)
playsound.playsound(file, True)
os.remove(file)
def create_folder(fname,path):
path = os.path.join(path,fname)
if os.path.exists(path):
return 0
os.mkdir(path)
return 1
def delete_folder(fname,path):
path = os.path.join(path,fname)
if os.path.exists(path):
sh.rmtree(path)
return 1
else:
return 0
def create_file(fname,typ,path):
fname=fname
count = 1
join_part= '_'+str(count)+'.'+typ
path += '/'+fname +'.'+typ
while 1:
if not os.path.exists(path):
open(path,'w')
return 1
else :
path=os.path.join(os.getcwd(),fname+join_part)
join_part= '_'+str(count)+'.'+typ
count+=1
def delete_file(fname,path):
path = os.path.join(path, fname)
if os.path.exists(path):
os.remove(path)
else :
return 0
return 1
def rename_dir_file(src,dst):
if os.path.exists(src):
if not os.path.exists(dst):
os.rename(src,dst)
else:
return 0
else:
return -1
return 1
def open_application(a):
count =0
prev_path = os.getcwd()
apps_path = '/usr/share/applications'
os.chdir(apps_path)
apps = os.listdir()
if a+".desktop" in apps:
os.system(a)
os.chdir(prev_path)
return 1
else:
for app in apps :
if a in app:
k = app
count+=1
if "office" in a:
a = a[7:]
os.system("libreoffice --"+a)
os.chdir(prev_path)
return 1
if not os.system("gnome-"+a) :
os.chdir(prev_path)
return 1
if "gnome-"+a+".desktop" in apps:
os.system("gnome-"+a)
os.chdir(prev_path)
return 1
else:
if count==1:
os.system(k)
os.chdir(prev_path)
return 1
else :
os.chdir(prev_path)
return 0
def open_folder(path):
if os.path.exists(path):
os.system("nautilus "+path)
return 1
else:
return 0
def open_file(path):
if not os.path.exists(path):
return 0
name, extension = os.path.splitext(path)
command = ''
if extension in ['.txt','.cpp','.c','.py']:
command = 'gedit'
elif extension in ['.odt','.docx']:
command = 'libreoffice --writer'
elif extension=='.xlsx':
command = 'libreoffice --calc'
elif extension=='.odg':
command = 'libreoffice --draw'
else:
return 0
command = command + ' ' + path
os.system(command)
return 1
def run_file(path):
name, extension = os.path.splitext(path)
command = ""
if extension==".cpp":
command = "g++"
elif extension==".c":
command = "gcc"
elif extension==".py":
command = "python"
else:
return 0
command = command + " " + path
if extension==".c" or extension==".cpp":
command = command + "\n ./a.out"
os.system("gnome-terminal -e 'bash -c \""+command+";bash\"'")
return 1
def fill_file(path):
if os.path.exists(path):
with open(path,'w') as writer:
fn_respond('Tell the content')
content = fn_talk(5)
writer.write(content)
return 1
else:
return 0
def change_dir(path,folder_name):
path = os.path.join(path,folder_name)
if os.path.exists(path):
os.chdir(path)
return 1
return 0
def go_back(path):
path = os.path.dirname(os.getcwd())
if os.path.exists(path):
os.chdir(path)
return 1
return 0
def web_search(query):
query_set=['gmail','youtube','drive','twitter','facebook','instagram']
if query=="" or query==" ":
webbrowser.open("https://google.com")
return 1
elif query in query_set:
webbrowser.open("https://www."+query+".com/")
else:
if (webbrowser.open("https://google.com/search?q="+query)):
return 1
else :
return 0
def shutdown(t,message):
command = "shutdown +"+str(t)+" \""+message+"\""
os.system("gnome-terminal -e 'bash -c \""+command+";bash\"'")
return 1
def restart():
command = "sudo reboot"
os.system("gnome-terminal -e 'bash -c \""+command+";bash\"'")
def stop_all():
time.sleep(3)
os.system("kill -1 -1")