visioncapsule-tools 0.3.19__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- visioncapsule_tools/__init__.py +2 -0
- visioncapsule_tools/capsule_downloader.py +574 -0
- visioncapsule_tools/cli.py +43 -0
- visioncapsule_tools/command_utils.py +25 -0
- visioncapsule_tools/print_utils.py +35 -0
- visioncapsule_tools/translations/portal.en.yml +16 -0
- visioncapsule_tools-0.3.19.dist-info/COPYING +26 -0
- visioncapsule_tools-0.3.19.dist-info/METADATA +20 -0
- visioncapsule_tools-0.3.19.dist-info/RECORD +11 -0
- visioncapsule_tools-0.3.19.dist-info/WHEEL +4 -0
- visioncapsule_tools-0.3.19.dist-info/entry_points.txt +3 -0
|
@@ -0,0 +1,574 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright (c) 2021 Dilili Labs, Inc. All rights reserved. Contains Dilili Labs Proprietary Information. RESTRICTED COMPUTER SOFTWARE. LIMITED RIGHTS DATA.
|
|
3
|
+
#
|
|
4
|
+
|
|
5
|
+
import glob
|
|
6
|
+
import os
|
|
7
|
+
import shutil
|
|
8
|
+
from argparse import ArgumentParser
|
|
9
|
+
|
|
10
|
+
import requests
|
|
11
|
+
import wget
|
|
12
|
+
from bs4 import BeautifulSoup
|
|
13
|
+
from PyInquirer import Separator, prompt
|
|
14
|
+
|
|
15
|
+
from brainframe_apps.capsule_control import (
|
|
16
|
+
VISIONCAPSULES_EXT,
|
|
17
|
+
find_localfiles,
|
|
18
|
+
get_local_vcap_dir,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
from visioncapsule_tools.command_utils import command, subcommand_parse_args, by_name
|
|
23
|
+
|
|
24
|
+
TBODY_PRODUCTION = "✨"
|
|
25
|
+
TBODY_REQUIRED_INPUT_TYPE = "Type:"
|
|
26
|
+
TBODY_REQUIRED_INPUT_DETECTIONS = "Detections:"
|
|
27
|
+
TBODY_REQUIRED_INPUT_TRACKED = "Tracked:"
|
|
28
|
+
TBODY_OUTPUT_TYPE = "Type:"
|
|
29
|
+
TBODY_OUTPUT_CLASSIFIES = "Classifies:"
|
|
30
|
+
TBODY_OUTPUT_DETECTIONS = "Detections:"
|
|
31
|
+
TBODY_OUTPUT_ENCODED = "Encoded:"
|
|
32
|
+
TBODY_OUTPUT_TRACKED = "Tracked:"
|
|
33
|
+
TBODY_OUTPUT_EXPAND = "Expand"
|
|
34
|
+
|
|
35
|
+
TBODY_DOWNLOAD_URL = "a"
|
|
36
|
+
TBODY_FILE_NAME = "FileName"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class Thead:
|
|
40
|
+
thead_name: str
|
|
41
|
+
thead_description: str
|
|
42
|
+
thead_hardware: str
|
|
43
|
+
thead_required_input: str
|
|
44
|
+
thead_output: str
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def connect_marketplace(marketplace_url, marketplace_path):
|
|
48
|
+
# Retrieve table from remote website
|
|
49
|
+
print("Connect to VisionCapsules Marketplace at " + marketplace_url)
|
|
50
|
+
|
|
51
|
+
response = requests.get(marketplace_url + marketplace_path)
|
|
52
|
+
html = response.content.decode("utf-8") # text
|
|
53
|
+
soup = BeautifulSoup(html, features="lxml")
|
|
54
|
+
table = soup.find("table")
|
|
55
|
+
|
|
56
|
+
# Parse the table
|
|
57
|
+
thead = parse_thead(table)
|
|
58
|
+
vcap_table = parse_table(marketplace_url, table, thead)
|
|
59
|
+
|
|
60
|
+
return vcap_table, thead
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def parse_thead(table):
|
|
64
|
+
thead = Thead()
|
|
65
|
+
|
|
66
|
+
# Parse table head
|
|
67
|
+
rows = table.find("thead").find_all("tr")
|
|
68
|
+
|
|
69
|
+
for row in rows:
|
|
70
|
+
cols = row.find_all("th")
|
|
71
|
+
thead.thead_name = cols[0].string.strip()
|
|
72
|
+
thead.thead_description = cols[1].string.strip()
|
|
73
|
+
thead.thead_hardware = cols[2].string.strip()
|
|
74
|
+
thead.thead_required_input = cols[3].string.strip()
|
|
75
|
+
thead.thead_output = cols[4].string.strip()
|
|
76
|
+
|
|
77
|
+
return thead
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def parse_table(marketplace_url, table, thead):
|
|
81
|
+
HREF_STARTSWITH = "/release"
|
|
82
|
+
|
|
83
|
+
# Parse table body
|
|
84
|
+
|
|
85
|
+
tbody = table.find("tbody")
|
|
86
|
+
rows = tbody.find_all("tr")
|
|
87
|
+
|
|
88
|
+
vcap_table = []
|
|
89
|
+
|
|
90
|
+
VisionCapsules_FullList = []
|
|
91
|
+
for row in rows:
|
|
92
|
+
tbody_production = ""
|
|
93
|
+
tbody_required_input_type = ""
|
|
94
|
+
tbody_required_input_detections = ""
|
|
95
|
+
tbody_required_input_tracked = False
|
|
96
|
+
tbody_output_type = ""
|
|
97
|
+
tbody_output_classifies = ""
|
|
98
|
+
tbody_output_detections = ""
|
|
99
|
+
tbody_output_encoded = False
|
|
100
|
+
tbody_output_tracked = False
|
|
101
|
+
tbody_output_expand = False
|
|
102
|
+
tbody_download_url = ""
|
|
103
|
+
tbody_file_name = ""
|
|
104
|
+
|
|
105
|
+
# Parse vcap download URL
|
|
106
|
+
link = row.find("a")
|
|
107
|
+
if link.has_attr("href"):
|
|
108
|
+
linktext = link["href"]
|
|
109
|
+
startswith = linktext.startswith(HREF_STARTSWITH)
|
|
110
|
+
endswith = linktext.endswith(VISIONCAPSULES_EXT)
|
|
111
|
+
if startswith and endswith:
|
|
112
|
+
download_url = marketplace_url + linktext
|
|
113
|
+
VisionCapsules_FullList.append(download_url)
|
|
114
|
+
|
|
115
|
+
tbody_download_url = download_url
|
|
116
|
+
tbody_file_name = download_url.split("/")[-1]
|
|
117
|
+
|
|
118
|
+
cols = row.find_all("td")
|
|
119
|
+
|
|
120
|
+
# Parse the text in the first 3 columns: Name, Description, Hardware
|
|
121
|
+
tbody_name = cols[0].string
|
|
122
|
+
tbody_description = cols[1].string
|
|
123
|
+
if tbody_description.startswith(TBODY_PRODUCTION):
|
|
124
|
+
tbody_description = tbody_description.replace(TBODY_PRODUCTION, "").strip()
|
|
125
|
+
tbody_production = "Ready"
|
|
126
|
+
|
|
127
|
+
tbody_hardware = cols[2].get_text(separator=" ").strip()
|
|
128
|
+
|
|
129
|
+
# Parse the text in the Required Input column
|
|
130
|
+
vcap_required_input = cols[3].find_all("strong")
|
|
131
|
+
items = cols[3].find_all("strong")
|
|
132
|
+
for item in vcap_required_input:
|
|
133
|
+
item_name = item.contents[0]
|
|
134
|
+
|
|
135
|
+
next_item = item.nextSibling
|
|
136
|
+
item_value = next_item.string
|
|
137
|
+
|
|
138
|
+
if item_name == TBODY_REQUIRED_INPUT_TYPE:
|
|
139
|
+
tbody_required_input_type = item_value
|
|
140
|
+
if item_name == TBODY_REQUIRED_INPUT_DETECTIONS:
|
|
141
|
+
tbody_required_input_detections = str(item_value)
|
|
142
|
+
if item_name == TBODY_REQUIRED_INPUT_TRACKED:
|
|
143
|
+
tbody_required_input_tracked = item_value
|
|
144
|
+
|
|
145
|
+
# Parse the text in the Output column
|
|
146
|
+
vcap_output = cols[4].find("summary")
|
|
147
|
+
if vcap_output:
|
|
148
|
+
tbody_output_expand = True
|
|
149
|
+
|
|
150
|
+
vcap_output = cols[4].find_all("strong")
|
|
151
|
+
for item in vcap_output:
|
|
152
|
+
|
|
153
|
+
item_name = item.contents[0]
|
|
154
|
+
|
|
155
|
+
next_item = item.nextSibling
|
|
156
|
+
item_value = next_item.string
|
|
157
|
+
|
|
158
|
+
next_item = next_item.nextSibling
|
|
159
|
+
item_detail = next_item
|
|
160
|
+
|
|
161
|
+
if item_name == TBODY_OUTPUT_TYPE:
|
|
162
|
+
tbody_output_type = item_value
|
|
163
|
+
if item_name == TBODY_OUTPUT_DETECTIONS:
|
|
164
|
+
tbody_output_detections = item_value
|
|
165
|
+
if item_name == TBODY_OUTPUT_ENCODED:
|
|
166
|
+
tbody_output_encoded = item_value
|
|
167
|
+
if item_name == TBODY_OUTPUT_TRACKED:
|
|
168
|
+
tbody_output_tracked = item_value
|
|
169
|
+
if item_name == TBODY_OUTPUT_CLASSIFIES:
|
|
170
|
+
if item_detail:
|
|
171
|
+
tbody_output_classifies = str(item_detail)
|
|
172
|
+
while next_item:
|
|
173
|
+
next_item = next_item.nextSibling # skip a <br>
|
|
174
|
+
if next_item:
|
|
175
|
+
next_item = next_item.nextSibling
|
|
176
|
+
item_detail2 = next_item
|
|
177
|
+
tbody_output_classifies = (
|
|
178
|
+
tbody_output_classifies + ". " + str(item_detail2)
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
vcap_item = {
|
|
182
|
+
thead.thead_name: tbody_name,
|
|
183
|
+
thead.thead_description: tbody_description,
|
|
184
|
+
TBODY_PRODUCTION: tbody_production,
|
|
185
|
+
thead.thead_hardware: tbody_hardware,
|
|
186
|
+
# thead_required_input: vcap_required_input,
|
|
187
|
+
TBODY_REQUIRED_INPUT_TYPE: tbody_required_input_type,
|
|
188
|
+
TBODY_REQUIRED_INPUT_DETECTIONS: tbody_required_input_detections,
|
|
189
|
+
TBODY_REQUIRED_INPUT_TRACKED: tbody_required_input_tracked,
|
|
190
|
+
# thead_output: vcap_output,
|
|
191
|
+
TBODY_OUTPUT_TYPE: tbody_output_type,
|
|
192
|
+
TBODY_OUTPUT_CLASSIFIES: tbody_output_classifies,
|
|
193
|
+
TBODY_OUTPUT_DETECTIONS: tbody_output_detections,
|
|
194
|
+
TBODY_OUTPUT_ENCODED: tbody_output_encoded,
|
|
195
|
+
TBODY_OUTPUT_TRACKED: tbody_output_tracked,
|
|
196
|
+
TBODY_OUTPUT_EXPAND: tbody_output_expand,
|
|
197
|
+
TBODY_DOWNLOAD_URL: tbody_download_url,
|
|
198
|
+
TBODY_FILE_NAME: tbody_file_name,
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
vcap_table.append(vcap_item)
|
|
202
|
+
|
|
203
|
+
return vcap_table
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def retrieve_vcap_url(vcap_table, vcap_file_name):
|
|
207
|
+
for vcap_rol in vcap_table:
|
|
208
|
+
vcap_remote_file_name = vcap_rol.get(TBODY_FILE_NAME)
|
|
209
|
+
|
|
210
|
+
if vcap_file_name.strip() == vcap_remote_file_name.strip():
|
|
211
|
+
vcap_url = vcap_rol.get(TBODY_DOWNLOAD_URL)
|
|
212
|
+
return vcap_url
|
|
213
|
+
else:
|
|
214
|
+
pass
|
|
215
|
+
|
|
216
|
+
print(f"Error: {vcap_file_name} not found on the VisionCapsule marketplace")
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
# The vcap_table is from the market place; the vcap_local_list is used to mark the
|
|
220
|
+
# VisionCapsules to be Selected on the Choices list.
|
|
221
|
+
def build_vcap_list(vcap_local_list, vcap_table, thead):
|
|
222
|
+
vcap_choices = []
|
|
223
|
+
last_selection_name = ""
|
|
224
|
+
for vcap_rol in vcap_table:
|
|
225
|
+
|
|
226
|
+
vcap_url = vcap_rol.get(TBODY_DOWNLOAD_URL)
|
|
227
|
+
|
|
228
|
+
# Use the first word of the vcap file name as the section name
|
|
229
|
+
vcap_remote_file_name = vcap_rol.get(TBODY_FILE_NAME)
|
|
230
|
+
selection_name = vcap_remote_file_name.split("_")[0]
|
|
231
|
+
|
|
232
|
+
exists = False
|
|
233
|
+
for vcap_local_file_path in vcap_local_list:
|
|
234
|
+
|
|
235
|
+
vcap_local_file_name = vcap_local_file_path.split("/")[-1]
|
|
236
|
+
if vcap_remote_file_name == vcap_local_file_name:
|
|
237
|
+
exists = True
|
|
238
|
+
break
|
|
239
|
+
else:
|
|
240
|
+
exists = False
|
|
241
|
+
|
|
242
|
+
if selection_name != last_selection_name:
|
|
243
|
+
SeparatorText = "======== " + selection_name + " ========"
|
|
244
|
+
vcap_choices.append(Separator(SeparatorText))
|
|
245
|
+
last_selection_name = selection_name
|
|
246
|
+
|
|
247
|
+
# Add VisionCapsules download url line
|
|
248
|
+
vcap_choices.append({"name": vcap_url, "checked": exists})
|
|
249
|
+
|
|
250
|
+
# Add VisionCapsules Name, Description
|
|
251
|
+
vcap_text = vcap_rol.get(thead.thead_name) + ": "
|
|
252
|
+
if vcap_rol.get(TBODY_PRODUCTION):
|
|
253
|
+
vcap_text = (
|
|
254
|
+
vcap_text + "(Production " + vcap_rol.get(TBODY_PRODUCTION) + ") "
|
|
255
|
+
)
|
|
256
|
+
|
|
257
|
+
vcap_text = vcap_text + vcap_rol.get(thead.thead_description)
|
|
258
|
+
|
|
259
|
+
vcap_choices.append(Separator(" " + vcap_text))
|
|
260
|
+
|
|
261
|
+
# Add VisionCapsules Hardware, Required Input, Output
|
|
262
|
+
vcap_text = thead.thead_hardware + ": " + vcap_rol.get(thead.thead_hardware)
|
|
263
|
+
vcap_choices.append(Separator(" " + vcap_text))
|
|
264
|
+
|
|
265
|
+
vcap_text = (
|
|
266
|
+
thead.thead_required_input + ": " + vcap_rol.get(TBODY_REQUIRED_INPUT_TYPE)
|
|
267
|
+
)
|
|
268
|
+
if vcap_rol.get(TBODY_REQUIRED_INPUT_TRACKED):
|
|
269
|
+
vcap_text = vcap_text + ". " + TBODY_REQUIRED_INPUT_TRACKED + "True "
|
|
270
|
+
if vcap_rol.get(vcap_rol.get(TBODY_REQUIRED_INPUT_DETECTIONS)):
|
|
271
|
+
vcap_text = vcap_text + ". " + vcap_rol.get(TBODY_REQUIRED_INPUT_DETECTIONS)
|
|
272
|
+
vcap_choices.append(Separator(" " + vcap_text))
|
|
273
|
+
|
|
274
|
+
vcap_text = thead.thead_output + ": " + vcap_rol.get(TBODY_OUTPUT_TYPE)
|
|
275
|
+
if vcap_rol.get(TBODY_OUTPUT_ENCODED):
|
|
276
|
+
vcap_text = vcap_text + ". " + TBODY_OUTPUT_ENCODED + "True "
|
|
277
|
+
if vcap_rol.get(TBODY_OUTPUT_TRACKED):
|
|
278
|
+
vcap_text = vcap_text + ". " + TBODY_OUTPUT_TRACKED + "True "
|
|
279
|
+
if vcap_rol.get(TBODY_OUTPUT_CLASSIFIES):
|
|
280
|
+
vcap_text = (
|
|
281
|
+
vcap_text
|
|
282
|
+
+ ". "
|
|
283
|
+
+ TBODY_OUTPUT_CLASSIFIES
|
|
284
|
+
+ " "
|
|
285
|
+
+ vcap_rol.get(TBODY_OUTPUT_CLASSIFIES)
|
|
286
|
+
)
|
|
287
|
+
if vcap_rol.get(TBODY_OUTPUT_DETECTIONS):
|
|
288
|
+
vcap_text = (
|
|
289
|
+
vcap_text
|
|
290
|
+
+ ". "
|
|
291
|
+
+ TBODY_OUTPUT_DETECTIONS
|
|
292
|
+
+ " "
|
|
293
|
+
+ vcap_rol.get(TBODY_OUTPUT_DETECTIONS)
|
|
294
|
+
)
|
|
295
|
+
vcap_choices.append(Separator(" " + vcap_text))
|
|
296
|
+
return vcap_choices
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
def prompt_questions(vcap_choices):
|
|
300
|
+
questions = [
|
|
301
|
+
{
|
|
302
|
+
"type": "checkbox",
|
|
303
|
+
"qmark": "[?]",
|
|
304
|
+
"message": "Select VisionCapsules",
|
|
305
|
+
"name": "vcap choices",
|
|
306
|
+
"choices": vcap_choices,
|
|
307
|
+
"validate": lambda answer: "Thanks for using VisionCapsules."
|
|
308
|
+
if len(answer) == 0
|
|
309
|
+
else True,
|
|
310
|
+
}
|
|
311
|
+
]
|
|
312
|
+
|
|
313
|
+
answers = prompt(questions)
|
|
314
|
+
|
|
315
|
+
vcap_selected_list = answers.get("vcap choices")
|
|
316
|
+
return vcap_selected_list
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
def download_capsules(vcap_url, capsules_path, vcap_local_frig):
|
|
320
|
+
vcap_local_file_name_to_be_downloaded = (
|
|
321
|
+
capsules_path + "/" + vcap_url.split("/")[-1]
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
if not os.path.exists(vcap_local_file_name_to_be_downloaded):
|
|
325
|
+
vcap_local_frig_file_name = vcap_local_frig + "/" + vcap_url.split("/")[-1]
|
|
326
|
+
|
|
327
|
+
if os.path.exists(vcap_local_frig_file_name):
|
|
328
|
+
unfreeze_capsule(
|
|
329
|
+
vcap_local_frig_file_name, vcap_local_file_name_to_be_downloaded
|
|
330
|
+
)
|
|
331
|
+
else:
|
|
332
|
+
print("\nDownloading " + vcap_url)
|
|
333
|
+
try:
|
|
334
|
+
wget.download(vcap_url, out=capsules_path)
|
|
335
|
+
except:
|
|
336
|
+
print("Failed.")
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
def update_capsules(vcap_local_list, vcap_url_list, capsules_path, vcap_local_frig):
|
|
340
|
+
# Download selected VisionCapsules
|
|
341
|
+
for vcap_url in vcap_url_list:
|
|
342
|
+
# This function will download a capsule if it is not available on local; or unfreeze a capsule if
|
|
343
|
+
# the capsule is available from vcap_local_frig
|
|
344
|
+
download_capsules(vcap_url, capsules_path, vcap_local_frig)
|
|
345
|
+
|
|
346
|
+
# Freeze unselected VisionCapsules
|
|
347
|
+
for vcap_local_file in vcap_local_list:
|
|
348
|
+
|
|
349
|
+
# Is this a developer capsule? If yes, then we don't freeze it; developer needs to manually change it
|
|
350
|
+
# If there is a folder there, then it is a developer capsule
|
|
351
|
+
developer_vcap_dir = os.path.splitext(vcap_local_file)[0]
|
|
352
|
+
if os.path.isdir(developer_vcap_dir):
|
|
353
|
+
continue
|
|
354
|
+
|
|
355
|
+
# Is this capsule on the vcap_url_list? If yes, then we don't freeze it.
|
|
356
|
+
vcap_local_file_name = vcap_local_file.split("/")[-1]
|
|
357
|
+
freeze_me = True
|
|
358
|
+
for vcap_url in vcap_url_list:
|
|
359
|
+
if vcap_local_file_name in vcap_url:
|
|
360
|
+
freeze_me = False
|
|
361
|
+
break
|
|
362
|
+
|
|
363
|
+
# Now we freeze unselected capsules
|
|
364
|
+
if freeze_me:
|
|
365
|
+
freeze_capsule(vcap_local_file, vcap_local_frig)
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def freeze_capsule(vcap_local_file, vcap_local_frig):
|
|
369
|
+
try:
|
|
370
|
+
os.makedirs(vcap_local_frig, exist_ok=True)
|
|
371
|
+
vcap_local_frig_file_name = (
|
|
372
|
+
vcap_local_frig + "/" + vcap_local_file.split("/")[-1]
|
|
373
|
+
)
|
|
374
|
+
|
|
375
|
+
shutil.move(vcap_local_file, vcap_local_frig_file_name)
|
|
376
|
+
print(f"{vcap_local_file} has frozen: {vcap_local_frig_file_name}")
|
|
377
|
+
|
|
378
|
+
except OSError as e:
|
|
379
|
+
print(
|
|
380
|
+
"381 Error: %s : %s : %s" % (vcap_local_file, vcap_local_frig, e.strerror)
|
|
381
|
+
)
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def unfreeze_capsule(vcap_local_frig_file_name, vcap_local_file):
|
|
385
|
+
try:
|
|
386
|
+
shutil.move(vcap_local_frig_file_name, vcap_local_file)
|
|
387
|
+
print(f"{vcap_local_file} has frozen.")
|
|
388
|
+
|
|
389
|
+
except OSError as e:
|
|
390
|
+
print(
|
|
391
|
+
"Error: %s : %s : %s"
|
|
392
|
+
% (vcap_local_file, vcap_local_frig_file_name, e.strerror)
|
|
393
|
+
)
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
def remove_capsules(vcap_local_file):
|
|
397
|
+
try:
|
|
398
|
+
os.remove(vcap_local_file)
|
|
399
|
+
print(f"{vcap_local_file} has been removed.")
|
|
400
|
+
|
|
401
|
+
except OSError as e:
|
|
402
|
+
print("Error: %s : %s" % (vcap_local_file, e.strerror))
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
def remove_all_capsules(vcap_local_file):
|
|
406
|
+
files = glob.glob(vcap_local_file + "/*")
|
|
407
|
+
for f in files:
|
|
408
|
+
try:
|
|
409
|
+
os.remove(f)
|
|
410
|
+
print(f"{f} has been removed.")
|
|
411
|
+
|
|
412
|
+
except OSError as e:
|
|
413
|
+
print("Error: %s : %s" % (f, e.strerror))
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
def delete_capsules(vcap_files, vcap_dir):
|
|
417
|
+
if vcap_files:
|
|
418
|
+
for file_name in vcap_files:
|
|
419
|
+
remove_capsules(vcap_dir + "/" + file_name)
|
|
420
|
+
else:
|
|
421
|
+
remove_all_capsules(vcap_dir)
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
def freeze_all_capsules(vcap_local_file, vcap_local_frig):
|
|
425
|
+
files = glob.glob(vcap_local_file + "/*")
|
|
426
|
+
for f in files:
|
|
427
|
+
try:
|
|
428
|
+
freeze_capsule(f, vcap_local_frig)
|
|
429
|
+
|
|
430
|
+
except OSError as e:
|
|
431
|
+
print("Error: %s : %s" % (f, e.strerror))
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
def freeze_capsules(vcap_local_files, vcap_local_dir, vcap_local_frig):
|
|
435
|
+
if vcap_local_files:
|
|
436
|
+
for file_name in vcap_local_files:
|
|
437
|
+
file_path = vcap_local_dir + "/" + file_name
|
|
438
|
+
freeze_capsule(file_path, vcap_local_frig)
|
|
439
|
+
else:
|
|
440
|
+
freeze_all_capsules(vcap_local_dir, vcap_local_frig)
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
def list_local_capsules(vcap_dir, vcap_frig):
|
|
444
|
+
print("Current local active VisionCapsules:")
|
|
445
|
+
|
|
446
|
+
vcap_files = [f for f in os.listdir(vcap_dir) if f.endswith(VISIONCAPSULES_EXT)]
|
|
447
|
+
if vcap_files is not None:
|
|
448
|
+
print("\n + " + "\n + ".join(vcap_files) + "\n")
|
|
449
|
+
else:
|
|
450
|
+
print(" empty\n")
|
|
451
|
+
|
|
452
|
+
print("Current local frozen VisionCapsules:")
|
|
453
|
+
|
|
454
|
+
vcap_frig_files = [f for f in os.listdir(vcap_frig) if f.endswith(VISIONCAPSULES_EXT)]
|
|
455
|
+
if vcap_frig_files is not None:
|
|
456
|
+
print("\n + " + "\n + ".join(vcap_frig_files) + "\n")
|
|
457
|
+
else:
|
|
458
|
+
print(" empty\n")
|
|
459
|
+
|
|
460
|
+
return
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
def capsule_downloader_load_capsules(prompt, vcap_files, vcap_dir, vcap_frig):
|
|
464
|
+
WEBSITE_URL = "https://aotu.ai"
|
|
465
|
+
WEBSITE_PATH = "/docs/downloads/"
|
|
466
|
+
|
|
467
|
+
# Download table from VisionCapsules marketplace
|
|
468
|
+
vcap_table, thead = connect_marketplace(WEBSITE_URL, WEBSITE_PATH)
|
|
469
|
+
|
|
470
|
+
# Build VisionCapsules lists from local path. We only care about capsule files.
|
|
471
|
+
vcap_local_list = find_localfiles("*" + VISIONCAPSULES_EXT, vcap_dir)
|
|
472
|
+
|
|
473
|
+
# Here we build up the final capsule list based on the input from prompt question or command line input.
|
|
474
|
+
vcap_url_list = []
|
|
475
|
+
if prompt:
|
|
476
|
+
# Build VisionCapsules question list
|
|
477
|
+
vcap_choices = build_vcap_list(vcap_local_list, vcap_table, thead)
|
|
478
|
+
|
|
479
|
+
# The prompt is a concept of the marketplace. A list of capsules downloaded from the website live
|
|
480
|
+
# will show up as prompt questions for user to select. If a capsule has been downloaded, the item
|
|
481
|
+
# will be marked as selected.
|
|
482
|
+
vcap_url_list = prompt_questions(vcap_choices)
|
|
483
|
+
|
|
484
|
+
if vcap_files:
|
|
485
|
+
# If user specify a capsule filename from the command line, we first check if the capsule filename
|
|
486
|
+
# is available from the marketplace. If yes, we will append to the url list.
|
|
487
|
+
for file_name in vcap_files:
|
|
488
|
+
vcap_url = retrieve_vcap_url(vcap_table, file_name)
|
|
489
|
+
if vcap_url:
|
|
490
|
+
# download_capsules(vcap_url, args.vcap_dir)
|
|
491
|
+
vcap_url_list.append(vcap_url)
|
|
492
|
+
else:
|
|
493
|
+
# This might be a capsule in capsule_frig?
|
|
494
|
+
vcap_url_list.append(file_name)
|
|
495
|
+
|
|
496
|
+
# Now we know what are the capsules user wants to use (vcap_url_list); and what are the capsules on
|
|
497
|
+
# the local storage (vcap_local_list), it is the time to download the VisionCapsules that is not local.
|
|
498
|
+
update_capsules(vcap_local_list, vcap_url_list, vcap_dir, vcap_frig)
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
def __parse_args__(default_vcap_local_dir, default_vcap_local_frig):
|
|
502
|
+
parser = ArgumentParser(
|
|
503
|
+
description="A helper utility for downloading BrainFrame capsules."
|
|
504
|
+
)
|
|
505
|
+
|
|
506
|
+
parser.add_argument(
|
|
507
|
+
"--vcap-dir",
|
|
508
|
+
default=default_vcap_local_dir,
|
|
509
|
+
help=f"The path to the BrainFrame capsule directory. default is {default_vcap_local_dir}",
|
|
510
|
+
)
|
|
511
|
+
|
|
512
|
+
parser.add_argument(
|
|
513
|
+
"--vcap-frig",
|
|
514
|
+
default=default_vcap_local_frig,
|
|
515
|
+
help=f"The path to the BrainFrame capsule frig. default is {default_vcap_local_frig}",
|
|
516
|
+
)
|
|
517
|
+
|
|
518
|
+
parser.add_argument(
|
|
519
|
+
"--vcap-files",
|
|
520
|
+
nargs="+",
|
|
521
|
+
help="The capsule files to be downloaded. A selection list will be prompt if the argument is not provided",
|
|
522
|
+
)
|
|
523
|
+
|
|
524
|
+
parser.add_argument(
|
|
525
|
+
"--delete",
|
|
526
|
+
action="store_true",
|
|
527
|
+
help="The capsule file to be removed. default is all local capsules will be deleted",
|
|
528
|
+
)
|
|
529
|
+
|
|
530
|
+
parser.add_argument(
|
|
531
|
+
"--freeze",
|
|
532
|
+
action="store_true",
|
|
533
|
+
help="Freeze the capsules",
|
|
534
|
+
)
|
|
535
|
+
|
|
536
|
+
parser.add_argument(
|
|
537
|
+
"--list",
|
|
538
|
+
action="store_true",
|
|
539
|
+
help="List the local capsules",
|
|
540
|
+
)
|
|
541
|
+
|
|
542
|
+
parser.add_argument(
|
|
543
|
+
"--download",
|
|
544
|
+
action="store_true",
|
|
545
|
+
default=True,
|
|
546
|
+
help="Download capsules",
|
|
547
|
+
)
|
|
548
|
+
|
|
549
|
+
args = subcommand_parse_args(parser)
|
|
550
|
+
return args
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
@command("capsule_downloader")
|
|
554
|
+
def capsule_downloader_main():
|
|
555
|
+
default_vcap_local_dir, _ = get_local_vcap_dir()
|
|
556
|
+
args = __parse_args__(default_vcap_local_dir, './')
|
|
557
|
+
|
|
558
|
+
if args.delete:
|
|
559
|
+
delete_capsules(args.vcap_files, args.vcap_dir)
|
|
560
|
+
elif args.freeze:
|
|
561
|
+
freeze_capsules(args.vcap_files, args.vcap_dir, args.vcap_frig)
|
|
562
|
+
elif args.list:
|
|
563
|
+
list_local_capsules(args.vcap_dir, args.vcap_frig)
|
|
564
|
+
elif args.download:
|
|
565
|
+
capsule_downloader_load_capsules(
|
|
566
|
+
args.download, args.vcap_files, args.vcap_dir, args.vcap_frig
|
|
567
|
+
)
|
|
568
|
+
|
|
569
|
+
list_local_capsules(args.vcap_dir, args.vcap_frig)
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
if __name__ == "__main__":
|
|
573
|
+
by_name["capsule_downloader"]()
|
|
574
|
+
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import os
|
|
2
|
+
os.environ['BF_LOG_PRINT'] = 'TRUE'
|
|
3
|
+
|
|
4
|
+
import signal
|
|
5
|
+
import sys
|
|
6
|
+
from argparse import ArgumentParser
|
|
7
|
+
|
|
8
|
+
import i18n
|
|
9
|
+
from visioncapsule_tools import print_utils
|
|
10
|
+
from visioncapsule_tools import command_utils
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def cli_main():
|
|
14
|
+
i18n.load_path.append(_TRANSLATIONS_PATH)
|
|
15
|
+
|
|
16
|
+
parser = ArgumentParser(
|
|
17
|
+
description=i18n.t("portal.description"), usage=i18n.t("portal.usage")
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
parser.add_argument(
|
|
21
|
+
"command", default=None, nargs="?", help=i18n.t("portal.command-help")
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
args = parser.parse_args(sys.argv[1:2])
|
|
25
|
+
|
|
26
|
+
if args.command is None:
|
|
27
|
+
parser.print_help()
|
|
28
|
+
elif args.command in command_utils.by_name:
|
|
29
|
+
command = command_utils.by_name[args.command]
|
|
30
|
+
command()
|
|
31
|
+
else:
|
|
32
|
+
error_message = i18n.t("portal.unknown-command")
|
|
33
|
+
error_message = error_message.format(command=args.command)
|
|
34
|
+
print_utils.print_color(
|
|
35
|
+
error_message, color=print_utils.Color.RED, file=sys.stderr
|
|
36
|
+
)
|
|
37
|
+
parser.print_help()
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
_TRANSLATIONS_PATH = os.path.join(os.path.dirname(__file__), "translations")
|
|
41
|
+
|
|
42
|
+
if __name__ == "__main__":
|
|
43
|
+
cli_main()
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from argparse import ArgumentParser
|
|
3
|
+
|
|
4
|
+
by_name = {}
|
|
5
|
+
"""A dict that maps command names to their corresponding function"""
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def command(name):
|
|
9
|
+
"""A decorator that associates command functions to their command name."""
|
|
10
|
+
|
|
11
|
+
def wrapper(function):
|
|
12
|
+
by_name[name] = function
|
|
13
|
+
|
|
14
|
+
return wrapper
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def subcommand_parse_args(parser: ArgumentParser):
|
|
18
|
+
arg_list = sys.argv[2:]
|
|
19
|
+
args = parser.parse_args(arg_list)
|
|
20
|
+
|
|
21
|
+
# Run in non-interactive mode if any flags were provided
|
|
22
|
+
if len(arg_list) > 0:
|
|
23
|
+
args.noninteractive = True
|
|
24
|
+
|
|
25
|
+
return args
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import readline
|
|
3
|
+
import sys
|
|
4
|
+
from enum import Enum
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
import i18n
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Color(Enum):
|
|
11
|
+
"""ANSI escape codes representing colors in the terminal theme."""
|
|
12
|
+
|
|
13
|
+
MAGENTA = "\033[95m"
|
|
14
|
+
BLUE = "\033[94m"
|
|
15
|
+
GREEN = "\033[92m"
|
|
16
|
+
YELLOW = "\033[93m"
|
|
17
|
+
RED = "\033[91m"
|
|
18
|
+
END = "\033[0m"
|
|
19
|
+
BOLD = "\033[1m"
|
|
20
|
+
UNDERLINE = "\033[4m"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def print_color(message, color: Color, **kwargs) -> None:
|
|
24
|
+
color = _check_no_color(color)
|
|
25
|
+
print(f"{color.value}{message}{Color.END.value}", **kwargs)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _check_no_color(color: Color) -> Color:
|
|
29
|
+
"""Turns off color if the user wants.
|
|
30
|
+
See: https://no-color.org/
|
|
31
|
+
"""
|
|
32
|
+
if "NO_COLOR" in os.environ:
|
|
33
|
+
return Color.END
|
|
34
|
+
return color
|
|
35
|
+
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
en:
|
|
2
|
+
description: "visioncapsule-tools is a group of commands written in Python. They
|
|
3
|
+
help to download, package and test VisionCapsules. To see options of each
|
|
4
|
+
command, run \"visioncapsule-tools {command} --help\"."
|
|
5
|
+
usage: >-
|
|
6
|
+
visioncapsule-tools <command> [<args>]
|
|
7
|
+
|
|
8
|
+
commands:
|
|
9
|
+
capsule_downloader
|
|
10
|
+
|
|
11
|
+
Examples:
|
|
12
|
+
visioncapsule-tools capsule_downloader
|
|
13
|
+
|
|
14
|
+
command-help: "The visioncapsule-tools command to run"
|
|
15
|
+
unknown-command: "Unknown command: {command}"
|
|
16
|
+
interrupted: "The operation was interrupted"
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Copyright (C) 2020, Dilili Labs INC, all rights reserved.
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
|
4
|
+
modification, are permitted provided that the following conditions are met:
|
|
5
|
+
|
|
6
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
7
|
+
list of conditions and the following disclaimer.
|
|
8
|
+
|
|
9
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
10
|
+
this list of conditions and the following disclaimer in the documentation
|
|
11
|
+
and/or other materials provided with the distribution.
|
|
12
|
+
|
|
13
|
+
3. Neither the name of the copyright holder nor the names of its contributors
|
|
14
|
+
may be used to endorse or promote products derived from this software without
|
|
15
|
+
specific prior written permission.
|
|
16
|
+
|
|
17
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
18
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
19
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
20
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
21
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
22
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
23
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
24
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
25
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
26
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: visioncapsule-tools
|
|
3
|
+
Version: 0.3.19
|
|
4
|
+
Summary: A tool to help users easily download VisionCapsules
|
|
5
|
+
License: BSD license
|
|
6
|
+
Author: Stephen Li
|
|
7
|
+
Author-email: stephen@aotu.ai
|
|
8
|
+
Requires-Python: >=3.6.6.2,<4.0
|
|
9
|
+
Classifier: License :: Other/Proprietary License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Requires-Dist: PyInquirer (>=1.0.3,<2.0.0)
|
|
17
|
+
Requires-Dist: beautifulsoup4 (>=4.9.3,<5.0.0)
|
|
18
|
+
Requires-Dist: lxml (>=4.6.3,<5.0.0)
|
|
19
|
+
Requires-Dist: requests (>=2.26.0,<3.0.0)
|
|
20
|
+
Requires-Dist: wget (>=3.2,<4.0)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
visioncapsule_tools/__init__.py,sha256=96MpvpnRmaBwGlPjHjzUPsc6Q3dkv_i5CV7ALvBaAcY,82
|
|
2
|
+
visioncapsule_tools/capsule_downloader.py,sha256=l3T1DwFSQQz2nwJ56thU0sSfkPWzpgY80S9ITUKRrsY,19620
|
|
3
|
+
visioncapsule_tools/cli.py,sha256=owNx_lxFLtnqd8ahnzDsprYQwVhDPSgJViXuYd03_Bc,1123
|
|
4
|
+
visioncapsule_tools/command_utils.py,sha256=8vmBRhFWsn642U74MeW4Hvgv8P5Fn_HJ6rf8xrkLPfk,571
|
|
5
|
+
visioncapsule_tools/print_utils.py,sha256=Ah3uQqWluk3i5REY8Mk7XH5TmXdZecVdKzePFLHlmVQ,734
|
|
6
|
+
visioncapsule_tools/translations/portal.en.yml,sha256=_97aE259Xp0isef8OP28VqMBnU61NDZVSAsauh33MWs,530
|
|
7
|
+
visioncapsule_tools-0.3.19.dist-info/METADATA,sha256=mznL_MAbliVobDVAOratGPG-pijP-YsRcsfj127Y3OY,778
|
|
8
|
+
visioncapsule_tools-0.3.19.dist-info/entry_points.txt,sha256=6Revs3MOZhLM8xRYGfsmCYYypHvDAt1T-uaPffOA3ac,72
|
|
9
|
+
visioncapsule_tools-0.3.19.dist-info/COPYING,sha256=gUdx8BDkY31TjlE3BXyajv8oSs-1hxBD4NUn-hUZi6w,1502
|
|
10
|
+
visioncapsule_tools-0.3.19.dist-info/WHEEL,sha256=vVCvjcmxuUltf8cYhJ0sJMRDLr1XsPuxEId8YDzbyCY,88
|
|
11
|
+
visioncapsule_tools-0.3.19.dist-info/RECORD,,
|