-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.py
More file actions
202 lines (136 loc) · 5.65 KB
/
plugin.py
File metadata and controls
202 lines (136 loc) · 5.65 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
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
import globals
from utils import download_progress_bar, break_to_size, read_file, colorprint, write_file
import time
from typing import Dict
import json
import textwrap
import os
# This is being declared globally in order to prevent the data file being read from disk every single time it is required
repo_dict : Dict = {"this_is_the_default_dictionary": ""}
installed_dict: Dict = {"this_is_the_default_dictionary": ""}
def dump_dict():
write_file(globals.jsondata.plugins, json.dumps(installed_dict, sort_keys=True))
def init_dict():
global repo_dict
global installed_dict
if repo_dict.get('this_is_the_default_dictionary', 'already_init') != 'already_init':
repo_dict = json.loads(read_file(globals.jsondata.repo))['plugins']
repo_dict = {key: repo_dict[key] for key in sorted(repo_dict)}
if installed_dict.get('this_is_the_default_dictionary', 'already_init') != 'already_init':
installed_dict = json.loads(read_file(globals.jsondata.plugins))
installed_dict = {key: installed_dict[key] for key in sorted(installed_dict)}
def generate():
global installed_dict
init_dict()
plugins_string = ""
for plugin in installed_dict:
if installed_dict[plugin]['enabled']:
plugins_string = plugins_string + f"source {globals.plugins_dir}/{plugin}/{installed_dict[plugin]['entry']}\n"
write_file(globals.modules.plugins, plugins_string)
def view_all():
global repo_dict
global installed_dict
init_dict()
list_text = []
print()
list_text.append(colorprint("{:20}{:50}{:12}{:12}".format("Plugin", "Description", "Installed", "Enabled"), "bold"))
for key in repo_dict:
plugin = repo_dict[key]
installed_text = "YES" if key in installed_dict else "NO"
enabled_text = "YES" if installed_text == "YES" and installed_dict[key]['enabled'] == True else "NO"
description_arr = textwrap.wrap(plugin['description'], 45)
list_text.append("{:20}{:50}{:12}{:12}".format(key, description_arr[0], installed_text, enabled_text))
for i in range(1, len(description_arr)):
list_text.append("{:20}{:50}{:12}{:12}".format("", description_arr[i], "", ""))
list_text.append("\n")
print("\n".join(list_text))
def view_local():
global installed_dict
global repo_dict
init_dict()
list_text = []
print()
if len(installed_dict) != 0:
list_text.append(colorprint("{:20}{:50}{:12}".format("Plugin", "Description", "Enabled"), "bold"))
else:
print(colorprint("You have no installed plugins!\n", "red"))
return
for key in installed_dict:
plugin = installed_dict[key]
enabled_text = "YES" if plugin['enabled'] == True else "NO"
description_arr = textwrap.wrap(repo_dict[key]['description'], 45)
list_text.append("{:20}{:50}{:12}".format(key, description_arr[0], enabled_text))
for i in range(1, len(description_arr)):
list_text.append("{:20}{:50}{:12}".format("", description_arr[i], ""))
list_text.append("\n")
print("\n".join(list_text))
def view_remote():
global installed_dict
global repo_dict
init_dict()
list_text = []
print()
list_text.append(colorprint("{:20}{:50}".format("Plugin", "Description"), "bold"))
for key in repo_dict:
plugin = repo_dict[key]
if key in installed_dict:
continue
description_arr = textwrap.wrap(plugin['description'], 45)
list_text.append("{:20}{:50}".format(key, description_arr[0]))
for i in range(1, len(description_arr)):
list_text.append("{:20}{:50}".format("", description_arr[i]))
list_text.append("\n")
if len(list_text) == 1:
print(colorprint("You have already installed all available plugins.\n", "red"))
return
print("\n".join(list_text))
def add(plugin):
global repo_dict
global installed_dict
init_dict()
if plugin in installed_dict:
print(colorprint(f"Plugin '{plugin}' is already installed!", "red"))
return
if plugin not in repo_dict:
print(colorprint(f"Plugin '{plugin}' was not found!", "red"))
return
if os.system(f"zsh {globals.repo_dir}/plugins/{plugin}/add.zsh") != 0:
print(colorprint(f"Installation of plugin '{plugin}' was unsuccessful.", "red"))
installed_dict[plugin] = {
"enabled": True,
"entry": repo_dict[plugin]['entry']
}
dump_dict()
generate()
print(colorprint(f"Successfully added plugin '{plugin}'", "green"))
def remove(plugin):
global repo_dict
global installed_dict
init_dict()
if plugin not in installed_dict:
print(colorprint(f"Plugin '{plugin}' is not installed!", "red"))
return
if os.system(f"zsh {globals.repo_dir}/plugins/{plugin}/remove.zsh") != 0:
print(colorprint(f"Removal of plugin '{plugin}' was unsuccessful.", "red"))
del installed_dict[plugin]
dump_dict()
generate()
print(colorprint(f"Successfully removed plugin '{plugin}'", "green"))
def enable(plugin):
global installed_dict
init_dict()
if plugin not in installed_dict:
print(colorprint(f"Plugin {plugin} is not installed!", "red"))
installed_dict[plugin]['enabled'] = True
dump_dict()
generate()
print(colorprint(f"Successfully enabled plugin '{plugin}'", "green"))
def disable(plugin):
global installed_dict
init_dict()
if plugin not in installed_dict:
print(colorprint(f"Plugin {plugin} is not installed!", "red"))
installed_dict[plugin]['enabled'] = False
dump_dict()
generate()
print(colorprint(f"Successfully disabled plugin '{plugin}'", "green"))