-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainscript.py
150 lines (120 loc) · 3.8 KB
/
mainscript.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
from libs.mainutils import *
from sys import argv
config = readconfig(argv[1])
arguments: list = [
"list",
"add",
"remove",
"load",
"addcommand",
"removecommand",
]
# Reading the config
with open(argv[1], "r") as f:
lines = f.readlines()
for line in lines:
if line[0] == "#":
continue
else:
line = line.split("=")
if line[0] == "PATH":
path = line[1]
elif line[0] == "WS_PATH":
ws_path = line[1]
# If there's not argument, print the help
if len(argv) < 3:
print(helpmessage)
exit()
else:
arg = argv[2]
workspaces: list = []
wsIDS: list = []
# Reading the workspaces
with open(f"{ws_path}/workspaces", 'r') as f:
lines = f.readlines()
for line in lines:
if line[0] == "#" or line[0] == "\n":
continue
else:
if line[0] == "@":
works = ws(int(line[1:-1]), [])
workspaces.append(works)
elif len(workspaces) != 0:
workspaces[-1].addcommand(line[:-1])
else:
continue
if arg in arguments:
if arg == "list":
listing(workspaces)
elif arg == "add":
to_add = ws(int(input("Workspace ID: ")), [])
# Check if the workspace already exists
if index(workspaces, to_add.ID) != None:
print("\033[95mWorkspace already exists\033[0m")
exit()
workspaces.append(to_add)
save(workspaces, ws_path)
listing(workspaces)
print(f"\033[93mWorkspace {to_add.ID} added\033[0m")
exit()
elif arg == "remove":
if len(argv) < 4:
print("\033[31mWorkspace ID not provided\n\nsimple-workspace remove <Workspace ID>\033[0m")
exit()
ws_id = int(argv[3])
idx = index(workspaces, ws_id)
if idx == None:
panic()
listing(workspaces, workspaces[idx])
yn = input("Are you sure? (y/n)").lower()[0]
print(yn)
if yn == "y":
del workspaces[idx]
save(workspaces, ws_path)
print("\033[93mWorkspace removed\033[0m")
exit()
print("Aborted")
exit()
elif arg == "load":
if len(argv) < 4:
print("\033[31mError: No workspace ID given\n\nsimple-workspace load <Workspace ID>\033[0m")
exit()
ws_id = int(argv[3])
ws_index = index(workspaces, ws_id)
for command in workspaces[ws_index].commands:
os.system(command)
exit()
elif arg == "addcommand":
if len(argv) < 4:
print("\033[31mError: No workspace ID specified\n\nsimple-workspaces addcommand <Workspace ID>\033[0m")
exit()
ws_id = int(argv[3])
ws_index = index(workspaces, ws_id)
if ws_index == None:
panic()
ws = workspaces[ws_index]
listing(workspaces, ws)
command = input("Command: ")
ws.addcommand(command)
save(workspaces, ws_path)
print("\033[93mCommand added: \033[0m", command)
exit()
elif arg == "removecommand":
# removecommand need 3 arguments: removecommand, workspace id, command id
if len(argv) < 4:
print("\033[31mError: Not enough arguments\n\nsimple-workspaces removecommand <Workspace ID> <Command ID>\033[0m")
exit()
ws_id = int(argv[3])
ws_index = index(workspaces, ws_id)
if ws_index == None:
panic()
ws = workspaces[ws_index]
listing(workspaces, ws)
to_remove = int(input("Command ID to remove: "))
ws.commands.pop(to_remove)
save(workspaces, ws_path)
listing(workspaces, ws)
exit()
# TODO Uninstall
else:
print(helpmessage)