-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAddLibrary.py
More file actions
262 lines (194 loc) · 8.24 KB
/
Copy pathAddLibrary.py
File metadata and controls
262 lines (194 loc) · 8.24 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import sublime, sublime_plugin
import os, re
import threading
import json
from .Libraries import Libraries
try:
from urllib.request import urlopen, Request
except ImportError:
from urllib2 import urlopen, URLError
def downloadLib(folder, lib):
settings = sublime.load_settings("AddLibrary.sublime-settings")
default_lib = settings.get("default_folder")
if not os.path.exists(folder + default_lib):
os.makedirs(folder + default_lib)
nf = GetLibThread(lib, folder + default_lib)
nf.start()
# global http get
def httpGet(url):
request_obj = Request(url, method='GET')
req = urlopen(url=request_obj)
return req
class AddLibrary(sublime_plugin.TextCommand):
def __init__(self, view):
self.view = view
self.window = sublime.active_window()
self.folder_path = self.window.folders()
def run(self, edit):
self.libraries_names = Libraries().getLibrariesName()
self.install_on_folder = None
self.selected_lib = None
arr_to_list = []
for ln in self.libraries_names:
arr_to_list.append("Download - "+ln)
# select a lib to download
self.window.show_quick_panel(arr_to_list, self.selectedLibrary)
def selectedLibrary(self, option_index):
if option_index > -1:
self.selected_lib = Libraries().getLibraryByName(self.libraries_names[option_index])
self.folder_path = self.window.folders()
# if have more than a active folder in sublime
if len(self.folder_path) > 1:
name_of_folders = self.folder_path
self.window.show_quick_panel(name_of_folders, self.selectFolder)
else:
self.install_on_folder = self.folder_path[0]
downloadLib(self.install_on_folder, self.selected_lib)
def selectFolder(self, folder_index):
self.install_on_folder = self.folder_path[folder_index]
downloadLib(self.install_on_folder, self.selected_lib)
class SearchLibrary(sublime_plugin.TextCommand):
def __init__(self, view):
self.view = view
self.window = sublime.active_window()
self.result_arr = []
self.result_arr_list = []
self.lib_versions = []
self.searchURL = 'https://api.cdnjs.com/libraries?search='
self.install_on_folder = ''
def run(self, edit):
self.window.show_input_panel("Search for:", "", self.searchTerm, None, None)
def searchTerm(self, term):
if term:
search_req = httpGet(self.searchURL + term + '&fields=version,description').read().decode('utf-8')
get_results = json.loads(search_req)['results']
for lib in get_results:
self.result_arr.append(lib['name'])
self.result_arr_list.append([lib['name'],lib['name'] + '-' + lib['version'],lib['description']])
self.window.show_quick_panel(self.result_arr_list, self.selectFindedLib)
def selectFindedLib(self, result_index):
if result_index > -1:
self.selected_lib = { 'search_name': self.result_arr[result_index], 'name': self.result_arr[result_index] }
search_t = SearchLibVersions(self.result_arr[result_index])
search_t.start()
self.result_arr = []
self.result_arr_list = []
def selectFolder(self, folder_index):
self.install_on_folder = self.folder_path[folder_index]
downloadLib(self.install_on_folder, self.selected_lib)
# get lib files via thread
class GetLibThread(threading.Thread):
def __init__(self, selected_lib, install_on):
self.selected_lib = selected_lib
self.selected_lib_name = selected_lib['search_name']
self.install_on = install_on
self.apiRoot = 'https://api.cdnjs.com/libraries/'
self.apiSearch = self.apiRoot + self.selected_lib_name
self.cdnURL = 'https://cdnjs.cloudflare.com/ajax/libs/'
threading.Thread.__init__(self)
def run(self):
sublime.status_message("Downloading " + self.selected_lib['name'] + "...")
if 'dependencies' in self.selected_lib:
for d in self.selected_lib['dependencies']:
# for each dependency, create a new recursive thread
selected_dep_lib = Libraries().getLibraryBySearchName(d)
get_t = GetLibThread(selected_dep_lib, self.install_on)
get_t.start()
get_latest_req = httpGet(self.apiSearch).read().decode('utf-8')
get_latest = json.loads(get_latest_req)
if get_latest:
# get the filename
file_name_by_url = get_latest['filename']
lib_folder = self.install_on + "/" + get_latest['name'] + '-' + get_latest['version']
# create the dir
if os.path.isdir(lib_folder) == False:
os.mkdir(lib_folder)
if 'assets' in get_latest:
for asset in get_latest['assets']:
if asset['version'] == get_latest['version']:
for file_name in asset['files']:
# get the latest version
latest_url = self.cdnURL + '/'+ get_latest['name'] + '/' + get_latest['version'] + '/' + file_name
# print(latest_url)
file_path = lib_folder + '/' + file_name
new_f_t = newFileThread(latest_url, file_path, file_name)
new_f_t.start()
# get lib files via thread
class GetLibVersionThread(threading.Thread):
def __init__(self, selected_lib, install_on, version):
self.selected_lib = selected_lib
self.install_on = install_on
self.target_version = version
self.apiRoot = 'https://api.cdnjs.com/libraries/'
self.apiSearch = self.apiRoot + self.selected_lib
self.cdnURL = 'https://cdnjs.cloudflare.com/ajax/libs/'
threading.Thread.__init__(self)
def run(self):
sublime.status_message("Downloading " + self.selected_lib + "...")
settings = sublime.load_settings("AddLibrary.sublime-settings")
default_lib = settings.get("default_folder")
get_lib_req = httpGet(self.apiSearch).read().decode('utf-8')
lib_list = json.loads(get_lib_req)
# folder that lib will be
lib_folder = self.install_on + default_lib + "/" + self.selected_lib + '-' + self.target_version
if os.path.isdir(self.install_on + default_lib) == False:
os.mkdir(self.install_on + default_lib)
if os.path.isdir(lib_folder) == False:
os.mkdir(lib_folder)
for assets in lib_list['assets']:
if assets['version'] == self.target_version:
for file_name in assets['files']:
# get the latest version
file_url = self.cdnURL + '/'+ self.selected_lib + '/' + self.target_version + '/' + file_name
file_path = lib_folder + '/' + file_name
new_f_t = newFileThread(file_url, file_path, file_name)
new_f_t.start()
# create a file via thread
class newFileThread(threading.Thread):
def __init__(self, url_to_content, file_path, file_name):
self.url_to_content = url_to_content
self.file_path = file_path
self.file_name = file_name
threading.Thread.__init__(self)
def run(self):
file_content = httpGet(self.url_to_content).read().decode('utf-8')
directory = os.path.dirname(self.file_path)
if not os.path.exists(directory):
os.makedirs(directory)
# create the file
new_file = os.open( self.file_path, os.O_RDWR|os.O_CREAT )
os.write( new_file, file_content.encode('utf-8') )
os.close( new_file )
sublime.status_message("File downloaded: " + self.file_name)
class SearchLibVersions(threading.Thread):
def __init__(self, lib_name):
self.lib_name = lib_name
self.libraryURL = 'https://api.cdnjs.com/libraries/'
self.window = sublime.active_window()
self.folder_path = self.window.folders()
self.list_versions = []
threading.Thread.__init__(self)
def run(self):
versions_req = httpGet(self.libraryURL + self.lib_name).read().decode('utf-8')
self.lib_versions = json.loads(versions_req)
self.list_lib_versions = []
for version in self.lib_versions['assets']:
self.list_versions.append([ self.lib_versions['name'] + ' - version '+version['version'] ])
self.list_lib_versions.append(version['version'])
self.window.show_quick_panel(self.list_versions, self.installLibByVersion)
def installLibByVersion(self, selected_version_index):
if selected_version_index > -1:
selected_version = self.list_lib_versions[selected_version_index]
self.folder_path = self.window.folders()
# if have more than a active folder in sublime
if len(self.folder_path) > 1:
name_of_folders = self.folder_path
self.window.show_quick_panel(name_of_folders, self.selectFolder)
else:
self.install_on_folder = self.folder_path[0]
get_version_t = GetLibVersionThread(self.lib_name, self.install_on_folder, selected_version )
get_version_t.start()
def selectFolder(self, folder_index):
self.install_on_folder = self.folder_path[folder_index]
get_version_t = GetLibVersionThread(self.lib_name, self.install_on_folder, selected_version )
get_version_t.start()