forked from moduloproject/pyappsch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
textapplauncher.py
executable file
·121 lines (103 loc) · 4.16 KB
/
textapplauncher.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
#!/usr/bin/env python3
# Set up
from os import listdir, path, remove
from re import compile as recomp
import sys
from subprocess import call
name = ""
pexec = ""
# Add to CheckInput
def addToCheckInput(location):
for filename in listdir(location):
appname = open(location+filename)
for line in appname:
if "Name=" in line:
name = line.rstrip().replace('Name=', '')
regex = recomp('[^a-zA-Z]')
nospacename = regex.sub('', name)
if "Exec=" in line:
pexec = line.rstrip().replace('Exec=', '')
# Create function
with open("checkinput.py", "a") as myfile:
myfile.write(' '+nospacename+' = "'+name+'"\n')
myfile.write(" if userinput in "+nospacename+":\n")
myfile.write(" print('Program number: ', i)\n")
myfile.write(" print('Program name: "+name+"')\n")
myfile.write(" print('Exec command: "+pexec+"')\n")
myfile.write(" i = i+1\n")
myfile.write(r" print('\n')")
myfile.write("\n")
print(name)
# This allows the second time of checkinput.py to be normal
def generatechecknumbergen(location):
for filename in listdir(location):
appname = open(location+filename)
for line in appname:
if "Name=" in line:
name = line.rstrip().replace('Name=', '')
regex = recomp('[^a-zA-Z]')
nospacename = regex.sub('', name)
if "Exec=" in line:
pexec = line.rstrip().replace('Exec=', '')
# Create function
with open("checknumbergen.py", "a") as myfile:
myfile.write(' '+nospacename+' = "'+name+'"\n')
myfile.write(" if userinput in "+nospacename+":\n")
myfile.write(" print('"+pexec+"')\n")
myfile.write(r" print('\n')")
myfile.write("\n")
home = path.expanduser("~")
addToCheckInput("/usr/share/applications/")
addToCheckInput(home+"/.local/share/applications/")
# Import function
from checkinput import checkinput
userinput = input("These are the programs you can launch. Pick one: ")
# Clears the screen, making the visibility of the new results much clearer
call(["clear"])
print("These are some of the programs that you can use:\n")
# Call Function
checkinput(userinput)
# Generates checknumbergen.py
with open("checknumbergen.py", "a") as myfile:
myfile.write('def checknumbergen(userinput):\n')
generatechecknumbergen("/usr/share/applications/")
generatechecknumbergen(home+"/.local/share/applications/")
from checknumbergen import checknumbergen
# Change checkinput.py to be output of checkinput(userinput)
### THIS IS THE PROBLEM!!!!
sys.stdout = open('checkinput.py', 'w')
checknumbergen(userinput)
sys.stdout.close()
sys.stdout = open("/dev/stdout", "w")
# Removes blank lines
clean_lines = []
with open("checkinput.py", "r") as f:
lines = f.readlines()
clean_lines = [l.strip() for l in lines if l.strip()]
with open("checkinput.py", "w") as f:
f.writelines('\n'.join(clean_lines))
# Closes checknumbergen.py for later use
open('checknumbergen.py', 'w').close()
with open("confirmnumber.py", "a") as myfile:
myfile.write("#!/usr/bin/env python3\n")
myfile.write("from subprocess import call\n")
myfile.write("def confirmNumber(userNumber):\n")
numbercount = 1
with open("checkinput.py", "r") as checkinp:
checkinp = checkinp.read().splitlines()
for line in checkinp:
with open("confirmnumber.py", "a") as checknum:
checknum.write(' if userNumber == "'+str(numbercount)+'":\n')
checknum.write(' return_code = call("'+line+'", shell = True)\n')
numbercount = numbercount+1
from confirmnumber import confirmNumber
userNumber = str(input("Pick a number to launch the program: "))
confirmNumber(userNumber)
# Clear file to default
remove("checknumbergen.py")
remove("confirmnumber.py")
open('checkinput.py', 'w').close()
with open("checkinput.py", "a") as myfile:
myfile.write("#!/usr/bin/env python3\n")
myfile.write("def checkinput(userinput):\n")
myfile.write(" i = 1\n")