This repository was archived by the owner on May 17, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprefs.py
More file actions
614 lines (485 loc) · 21.8 KB
/
prefs.py
File metadata and controls
614 lines (485 loc) · 21.8 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# ##### BEGIN GPL LICENSE BLOCK #####
#
# <A central Updater for all your Addons>
# Copyright (C) <2023> <Blender Defender>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
import bpy
from bpy.types import (
AddonPreferences,
Context,
PropertyGroup,
UILayout,
)
from bpy.props import (
BoolProperty,
CollectionProperty,
EnumProperty,
FloatProperty,
IntProperty,
StringProperty
)
import addon_utils
import json
import time
from os import path as p
import textwrap
import requests
import threading
import locale
from .objects.updater import UPDATE_CONTEXTS, Updater
from .functions.json_functions import (
decode_json
)
from .functions.main_functions import (
expand_all,
filter_search,
get_addons_filtered
)
from .issue_types import (
BL_INFO_VERSION_PROBLEMS,
SAM_NOT_SUPPORTED,
ENDPOINT_URL_INVALID,
all_issue_types
)
# Variables for the UI.
TABS_HEIGHT = 130
ISSUE_HEADING_INSET = 40
ISSUE_ITEM_INSET = 300
ISSUE_ITEMS_DISTANCE = 45
UPDATE_DETAILS_INSET = 350
RELEASE_DESCRIPTION_LINE_DISTANCE = 60
RELEASE_DESCRIPTION_LINE_LENGTH_ADJUSTMENT = 300
# These variables are necessary for the update checking progress bar.
addons_total = 0
addon_index = 0
checking_for_updates = False
# These variables are necessary for the update all progress bar.
updatable_addons = 0
updated_addons = 0
updating_all = False
# Create the lists for addons, that either have available updates
# or addons, that have issues and can't be updated.
updates = []
unavailable_addons = []
expanded_categories = {issue_type: True for issue_type in all_issue_types}
filtered_categories = {issue_type: False for issue_type in all_issue_types}
# Create an empty object for an addon that will appear in the manager.
managed_addon = {}
# Store the results of the marketplace in a global variable, to avoid unnecessary reloads.
is_loading_addons = False
marketplace_addons = []
class ExpandUI(PropertyGroup):
expand: BoolProperty(
name="Expand", description="Expand to see more detail on this update.", default=False)
class SUPERADDONMANAGER_APT_preferences(AddonPreferences):
"""Preferences of Super Addon Manager."""
bl_idname = __package__
layout_tab: EnumProperty(items=[
("updater", "Updater", "Check for updates and install them."),
("manager", "Manager", "Manage your installed addons."),
("settings", "Settings", "Change the settings of Super Addon Manager.")],
name="UI Section",
description="Display the different UI Elements of Super Addon Manager.",
default="settings")
update_check_progress_bar: FloatProperty(
name="%",
description="Percentage addons checked for updates",
subtype='PERCENTAGE',
min=0,
max=100,
options=set(), # Not animatable!
get=(lambda self: 0 if addons_total ==
0 else 100 * addon_index / addons_total),
set=(lambda self, value: None),
)
update_all_progress_bar: FloatProperty(
name="%",
description="Percentage addons updated",
subtype='PERCENTAGE',
min=0,
max=100,
options=set(), # Not animatable!
get=(lambda self: 0 if updatable_addons ==
0 else 100 * updated_addons / updatable_addons),
set=(lambda self, value: None),
)
download_directory: StringProperty(
name="Download Directory",
description="The directory where the downloaded files are stored.",
default=p.join(p.expanduser('~'), 'downloads'),
subtype="DIR_PATH"
)
check_interval_months: IntProperty(
name='Months',
description="Number of months between checking for updates",
default=0,
min=0,
)
check_interval_days: IntProperty(
name='Days',
description="Number of days between checking for updates",
default=7,
min=0,
max=31,
)
check_interval_hours: IntProperty(
name='Hours',
description="Number of hours between checking for updates",
default=0,
min=0,
max=23,
)
check_interval_minutes: IntProperty(
name='Minutes',
description="Number of minutes between checking for updates",
default=0,
min=0,
max=59,
)
auto_check_for_updates: BoolProperty(
name="Automatically check for updates",
description=f"If enabled, Super Addon Manager automatically checks for updates using a custum interval.",
default=True,
)
error_search_term: StringProperty(
name="Search addon",
description="Search for an addon in the error list.",
options={"TEXTEDIT_UPDATE"},
update=expand_all,
)
check_experimental_updates: BoolProperty(
name="Check Experimental Updates",
description="Check for addon updates using the GitHub and GitLab API, if Super Addon Manager isn't supported. This might drastically increase the number of addons that you can check for updates. This is experimental. Super Addon Manager can't guarantee, that the new addon versions are compatible with the current Blender Version.",
default=True
)
use_experimental_installer: BoolProperty(
name="Automatically install experimental updates",
description="When an experimental update is found, it will be installed without manually downloading the files. This may cause issues, when a downloaded version is incompatible with the current Blender version.",
default=False
)
update_details_expand: CollectionProperty(type=ExpandUI)
manager_search_term: StringProperty(
name="Search addon",
description="Search for installed addons.",
options={"TEXTEDIT_UPDATE"},
# update=get_addons_filtered,
)
dev_icon: IntProperty(max=3, min=0)
def draw(self, context: Context):
layout: UILayout = self.layout
layout.enabled = not checking_for_updates
# Layout Tabs to switch between Updater, Manager and Settings.
row = layout.row(align=True)
row.scale_y = TABS_HEIGHT / 100
row.prop(self, "layout_tab", expand=True)
# layout.prop(self, "dev_icon") # TODO: #46 Decide on an icon.
if self.layout_tab == "updater":
self.draw_updater(context, layout)
if self.layout_tab == "manager":
self.draw_manager(context, layout)
if self.layout_tab == "settings":
self.draw_preferences(context, layout)
def draw_updater(self, context: Context, layout: UILayout):
"""Layout the Updater with update check, found updates and found problems."""
path = p.join(p.dirname(__file__), "updater_status.json")
# Updating all progress bar.
if updating_all:
layout.label(
text=f"Updating: {updated_addons}/{updatable_addons}", icon='INFO')
layout.prop(self, "update_all_progress_bar")
# Checking for updates progress bar.
if checking_for_updates:
layout.label(
text=f"Checking for updates: {addon_index}/{addons_total}", icon='INFO')
layout.prop(self, "update_check_progress_bar")
# Featured button.
if not checking_for_updates and not updating_all:
self.layout_featured_button(context, layout, updates)
# Display a list of updates, if there are any.
if len(updates) > 0:
layout.separator(factor=0)
self.layout_updates(context, layout)
# Show a warning, that some addons couldn't be properly updated,
# if at least one addon ran into any kind of issue.
if len(unavailable_addons) > 0:
layout.separator()
self.layout_issues(context, layout)
# Display when the last update check was.
d = decode_json(path)
if d is not None:
last_check = time.localtime(d['last_check'])
layout.separator(factor=0.5)
row = layout.row()
row.label(
text=f"Last update check: {time.strftime('%A, %d/%m/%Y %H:%M', last_check)}", icon="INFO")
def draw_manager(self, context: Context, layout: UILayout):
"""Layout the addon manager."""
enabled_addons = {addon.module for addon in context.preferences.addons}
info_row = layout.row()
# Distribute the label and the search box properly.
info_row.alignment = "LEFT"
info_row.scale_x = 5
# Display a message, that there were issues while checking for updates.
info_row.label(
text="Manage your installed addons.")
info_row.prop(self, "manager_search_term", icon="VIEWZOOM", text="")
if self.manager_search_term != "":
for index, addon in enumerate(get_addons_filtered(self.manager_search_term)):
row = layout.row()
row.label(text=addon["name"])
row.operator(
"superaddonmanager.select_addon_to_manage").index = index
if self.manager_search_term == "" and managed_addon:
bl_info: dict = addon_utils.module_bl_info(managed_addon["module"])
module_name = managed_addon["module"].__name__
is_enabled = module_name in enabled_addons
row = layout.row()
row.operator("preferences.addon_disable" if is_enabled else "preferences.addon_enable",
icon="CHECKBOX_HLT" if is_enabled else "CHECKBOX_DEHLT",
emboss=False,
text=""
).module = module_name
row.label(text=bl_info.get("name", ""))
if managed_addon["is_folder_addon"] and managed_addon["is_user_addon"]:
row.operator("superaddonmanager.install_unsupported_update")
row.operator("superaddonmanager.restore_backup")
self.layout_marketplace(context, layout)
def draw_preferences(self, context: Context, layout: UILayout):
"""Layout the Super Addon Manager Preferences."""
# # props = layout.box()
# # props.label(text="Super Addon Manager Settings:")
# Layout the setting for the Download directory.
layout.row().prop(self, "download_directory")
layout.separator(factor=1)
# Layout automatic update check related properties.
layout.row(align=True).prop(self, "auto_check_for_updates")
time_props = layout.grid_flow(row_major=True, align=True)
time_props.enabled = self.auto_check_for_updates
time_props.prop(self, "check_interval_months")
time_props.prop(self, "check_interval_days")
time_props.prop(self, "check_interval_hours")
time_props.prop(self, "check_interval_minutes")
layout.separator(factor=1)
# Layout experimental updates related properties.
layout.label(text="Experimental Updates:", icon="ERROR")
layout.row().prop(self, "check_experimental_updates")
exp_row = layout.row()
exp_row.enabled = self.check_experimental_updates
exp_row.prop(self, "use_experimental_installer")
def layout_featured_button(self, context: Context, layout: UILayout, updates: list):
# Layout the "Update All"-Operator when at least two addons have updates.
if len(updates) > 0:
row = layout.row()
row.scale_y = 2
flow = row.grid_flow(row_major=True, align=True)
# Check for Updates Operator.
flow.operator("superaddonmanager.update_all")
op = flow.operator("superaddonmanager.check_for_updates",
text="", icon="FILE_REFRESH")
op.is_background_check = False
else:
row = layout.row()
row.scale_y = 2
flow = row.grid_flow(row_major=True, align=True)
# Check for Updates Operator.
op = flow.operator("superaddonmanager.check_for_updates")
op.is_background_check = False
def layout_updates(self, context: Context, layout: UILayout):
"""Layout all Addons that can be Updated one by one."""
layout.label(
text=f"Super Addon Manager has found {len(updates)} Updates:")
for index, addon in enumerate(updates):
updater: Updater = addon["updater"]
expand_details: ExpandUI = self.update_details_expand[index]
row = layout.row()
icon = "TRIA_DOWN" if expand_details.expand else "TRIA_RIGHT"
row.prop(expand_details, "expand",
text="", icon=icon, emboss=False, toggle=True,)
# Start a new subrow, where the addon name and the update button will be placed.
subrow = row.row()
subrow.label(text=addon["addon_name"])
# Display the automatic update operator, when the addon needs to be downloaded.
# The operator will run for automatic and manual downloads.
if updater.update_context == UPDATE_CONTEXTS["DOWNLOAD"]:
icon = "ERROR" if addon["is_experimental"] else "NONE"
op = subrow.operator(
"superaddonmanager.automatic_update", icon=icon)
op.is_experimental = addon["is_experimental"]
else:
# If the update was downloaded, display the manual update operator.
op = subrow.operator("superaddonmanager.manual_update",
text="Update from local file.")
op.index = index
# Layout Update details.
if expand_details.expand:
new_row = layout.row()
new_row.separator(factor=UPDATE_DETAILS_INSET / 100)
new_row.label(
text=f"New Version: {str(updater.addon_version)}")
if updater.release_description:
self.layout_release_description(
context, layout, updater.release_description)
def layout_marketplace(self, context: Context, layout: UILayout):
layout = layout.box()
global marketplace_addons
if marketplace_addons == [] and not is_loading_addons:
def load_marketplace_addons():
global is_loading_addons
global marketplace_addons
is_loading_addons = True
res_data = requests.get(
"https://super-addon-manager.netlify.app/api/addon-browser").text
res_data: dict = json.loads(res_data)
if not res_data.get("success", False):
print("Error with the request")
marketplace_addons = res_data.get("addons", [])
is_loading_addons = False
threading.Thread(target=load_marketplace_addons,
daemon=True).start()
row = layout.row()
row.label(text="Discover")
if is_loading_addons:
row = layout.row()
row.label(text="Loading addons...")
for addon in marketplace_addons:
addon: dict
row = layout.row()
row.label(text=addon.get("name", "Unknown"))
addon_price = addon.get("price", 0) # ! For testing purposes: 5
if addon_price == 0:
op = row.operator(
"superaddonmanager.install_new_addon", text="Install (Free)")
op.addon_name = addon.get("name", "Unknown")
op.addon_id = addon.get("id")
op.endpoint_url = addon.get("endpoint_url")
else:
locale.setlocale(locale.LC_MONETARY, "EN")
op = row.operator(
"wm.url_open", text=f"Install ({locale.currency(addon_price, grouping=True)})")
op.url = addon.get("doc_url")
row = layout.row()
row.label(text=addon.get("description"))
layout.separator(factor=2)
def layout_release_description(self, context: Context, layout: UILayout, release_description: str):
row = layout.row()
row.separator(factor=UPDATE_DETAILS_INSET / 100)
box = row.box()
box_row = box.row()
box_row.label(text="Release Notes:")
# Break up lines along line breaks.
release_description_lines: list = release_description.strip().split("\n")
width = context.region.width
ui_scale = context.preferences.view.ui_scale
font_size = context.preferences.ui_styles[0].widget_label.points
# Calculate a line length that should work with the given Blender settings.
line_length = (width * 2) / (ui_scale * font_size) - \
(RELEASE_DESCRIPTION_LINE_LENGTH_ADJUSTMENT / 100)
# Layout the text, line by line.
for line in release_description_lines:
wrapp = textwrap.TextWrapper(int(line_length))
wrapp_lines = wrapp.wrap(line)
for wline in wrapp_lines:
box_row = box.row(align=True)
box_row.alignment = "EXPAND"
box_row.scale_y = RELEASE_DESCRIPTION_LINE_DISTANCE / 100
box_row.label(text=wline)
layout.separator()
def layout_issues(self, context: Context, layout: UILayout):
"""Layout all issues."""
path = p.join(p.dirname(__file__), "updater_status.json")
d = decode_json(path)
box = layout.box()
info_row = box.row()
# Distribute the label and the search box properly.
info_row.alignment = "LEFT"
info_row.scale_x = 5
# Display a message, that there were issues while checking for updates.
info_row.label(
text=f"{len(unavailable_addons)} errors occured when checking for Updates", icon="ERROR")
info_row.prop(self, "error_search_term", icon="VIEWZOOM", text="")
prev_error = None
for index, addon in enumerate(filter(lambda el: filter_search(el, self.error_search_term), unavailable_addons)):
if addon["issue_type"] != prev_error:
# Change the previous error to be the current error code.
prev_error = addon["issue_type"]
icon = "TRIA_DOWN" if getattr(self,
prev_error) else "TRIA_RIGHT"
# Start a new section for a new error code.
container = box.column()
expand = container.row(align=True)
expand.emboss = "NONE"
expand.alignment = "LEFT"
expand.prop(self, prev_error,
text=self.convert_error_message(prev_error), icon=icon)
container.separator(factor=ISSUE_HEADING_INSET / 100)
# Display the error codes, if the area is expanded.
if getattr(self, prev_error):
row = container.row()
row.separator(factor=ISSUE_ITEM_INSET / 100)
row.label(text=addon["addon_name"])
icons = ["URL", "INFO", "HELP",
"QUESTION", "RESTRICT_SELECT_OFF"]
icon = icons[d["tests"]["icons"]]
op = row.operator(
"superaddonmanager.generate_issue_report", text="Request Support", icon=icon)
op.addon_index = index
container.separator(factor=ISSUE_ITEMS_DISTANCE / 100)
def convert_error_message(self, msg):
error_code_labels = {
SAM_NOT_SUPPORTED: "Super Addon Manager is not supported",
BL_INFO_VERSION_PROBLEMS: "Problems with the current addon version",
ENDPOINT_URL_INVALID: "URL Invalid",
}
if msg in error_code_labels.keys():
string = error_code_labels[msg]
else:
string = ""
for i in msg.split("_"):
string += i.capitalize() + " "
return string
classes = (
ExpandUI,
SUPERADDONMANAGER_APT_preferences,
)
def add_issue_types_to_prefs():
if '__annotations__' not in SUPERADDONMANAGER_APT_preferences.__dict__:
setattr(SUPERADDONMANAGER_APT_preferences, '__annotations__', {})
def make_fns(issue_type: str):
''' Capture the issue_type in a closure and generate some functions that
proxy the get/set from expanded_categories for that issue_type '''
def get_expanded(self):
return expanded_categories[issue_type] or filtered_categories[issue_type]
def set_expanded(self, value: bool):
expanded_categories[issue_type] = value
return get_expanded, set_expanded
for issue_type in all_issue_types:
# Define a property+annotation that proxies each issue_type from expanded_categories
get_expanded, set_expanded = make_fns(issue_type)
setattr(SUPERADDONMANAGER_APT_preferences, issue_type, True)
SUPERADDONMANAGER_APT_preferences.__annotations__[
issue_type] = BoolProperty(default=True,
description=f"Error Code: '{issue_type}'",
get=get_expanded,
set=set_expanded)
def register():
add_issue_types_to_prefs()
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in reversed(classes):
bpy.utils.unregister_class(cls)