File size: 18,388 Bytes
27867f1 |
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 |
"""
MIT License
Copyright (C) 2023 ROCKY4546
https://github.com/rocky4546
This file is part of Cabernet
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
"""
import logging
import lib.common.utils as utils
import lib.common.exceptions as exceptions
from lib.common.decorators import getrequest
from lib.common.decorators import postrequest
from lib.web.pages.templates import web_templates
from lib.db.db_plugins import DBPlugins
from lib.plugins.plugin_manager.plugin_manager import PluginManager
@getrequest.route('/api/pluginsform')
def get_plugins_form_html(_webserver, _namespace=None, _sort_col=None, _sort_dir=None, filter_dict=None):
plugins_form = PluginsFormHTML(_webserver.config)
_area = _webserver.query_data.get('area')
_plugin = _webserver.query_data.get('plugin')
_repo = _webserver.query_data.get('repo')
if _area is None and _plugin is None and _repo is None:
_webserver.do_mime_response(
404, 'text/html', web_templates['htmlError']
.format('404 - Badly formed request'))
elif _area:
try:
form = plugins_form.get(_area)
_webserver.do_mime_response(200, 'text/html', form)
except exceptions.CabernetException as ex:
_webserver.do_mime_response(
404, 'text/html', web_templates['htmlError']
.format('404 - Badly formed area request'))
elif _plugin and _repo:
try:
form = plugins_form.get_plugin(_repo, _plugin)
_webserver.do_mime_response(200, 'text/html', form)
except exceptions.CabernetException as ex:
_webserver.do_mime_response(
404, 'text/html', web_templates['htmlError']
.format('404 - Badly formed plugin request'))
else:
# case where plugin and repo are not provided together
_webserver.do_mime_response(
404, 'text/html', web_templates['htmlError']
.format('404 - Badly formed plugin/repo request'))
@postrequest.route('/api/pluginsform')
def post_plugins_html(_webserver):
action = _webserver.query_data.get('action')
pluginid = _webserver.query_data.get('pluginId')
repoid = _webserver.query_data.get('repoId')
if action and pluginid and repoid:
action = action[0]
pluginid = pluginid[0]
repoid = repoid[0]
if action == "deletePlugin":
pm = PluginManager(_webserver.plugins)
results = pm.delete_plugin(repoid, pluginid, _webserver.sched_queue)
_webserver.do_mime_response(200, 'text/html', 'STATUS: Deleting plugin: {}:{}<br> '.format(repoid, pluginid) + str(results))
if action == "addInstance":
pm = PluginManager(_webserver.plugins)
results = pm.add_instance(repoid, pluginid, _webserver.sched_queue)
_webserver.do_mime_response(200, 'text/html', 'STATUS: Adding Instance plugin: {}:{}<br> '.format(repoid, pluginid) + str(results))
elif action == "installPlugin":
pm = PluginManager(_webserver.plugins)
results = pm.install_plugin(repoid, pluginid, _webserver.sched_queue)
_webserver.do_mime_response(200, 'text/html', 'STATUS: Installing plugin: {}:{}<br> '.format(repoid, pluginid) + str(results))
elif action == "upgradePlugin":
pm = PluginManager(_webserver.plugins)
results = pm.upgrade_plugin(repoid, pluginid, _webserver.sched_queue)
_webserver.do_mime_response(200, 'text/html', 'STATUS: Installing plugin: {}:{}<br> '.format(repoid, pluginid) + str(results))
else:
_webserver.do_mime_response(200, 'text/html', "doing something else"+str(action[0]))
else:
_webserver.do_mime_response(
404, 'text/html', web_templates['htmlError']
.format('404 - Badly formed request'))
class PluginsFormHTML:
def __init__(self, _config):
self.logger = logging.getLogger(__name__)
self.config = _config
self.plugin_db = DBPlugins(self.config)
self.active_tab_name = None
self.num_of_plugins = 0
self.plugin_data = None
self.area = None
def get(self, _area):
self.area = _area
return ''.join([self.header, self.body])
def get_plugin(self, _repo_id, _plugin_id):
plugin_defn = self.plugin_db.get_plugins(
_installed=None,
_repo_id=_repo_id,
_plugin_id=_plugin_id)
if not plugin_defn:
self.logger.warning(
'HTTP request: Unknown plugin: {}'
.format(_plugin_id))
raise exceptions.CabernetException(
'Unknown Plugin: {}'
.format(_plugin_id))
plugin_defn = plugin_defn[0]
return ''.join([self.get_plugin_header(plugin_defn), self.get_menu_section(plugin_defn), self.get_plugin_section(plugin_defn)])
def get_menu_top_section(self, _plugin_defn):
return ''.join([
'<ul class="menuList">',
'<li class="menuItem" style="display: flex;">',
'<div style="position: relative; padding-left: 0.5em;">',
'<img class="icon-size" src="/api/manifest?plugin=',
_plugin_defn['name'], '&key=icon" alt="',
_plugin_defn['name'],'">',
'</div>',
'<div style="position: relative; padding: 0.5em;display: flex; flex-direction: column; justify-content: center;">',
_plugin_defn['name'],
'</div>',
'</li>',
'</ul>'
])
def get_menu_items(self, _plugin_defn):
if not _plugin_defn['external']:
menu_list=''
elif _plugin_defn['version']['installed']:
# delete and possible upgrade...
menu_list = ''
if _plugin_defn['version']['latest'] != _plugin_defn['version']['current']:
menu_list = ''.join([
'<li class="menuItem">',
'<button type="submit" name="action" value="upgradePlugin" class="menuButton" ',
'title="Download and install latest version of plugin. Restart required">',
'<i class="md-icon" style="padding-right: 5px; font-size: 1.7em;">upgrade</i>',
'Upgrade Plugin',
'</button>',
'</li>'
])
menu_list += ''.join([
'<li class="menuItem">',
'<button type="submit" name="action" value="deletePlugin" class="menuButton" ',
'title="Deletes the plugin folder and scheduled events">',
'<i class="md-icon" style="padding-right: 5px; font-size: 1.7em;">delete</i>',
'Delete Plugin',
'</button>',
'</li>'
])
else:
# install
menu_list = ''.join([
'<li class="menuItem">',
'<button type="submit" name="action" value="installPlugin" class="menuButton" ',
'title="Download and install latest version of plugin, updates handler and schedule events">',
'<i class="md-icon" style="padding-right: 5px; font-size: 1.7em;">download</i>',
'Install Plugin',
'</button>',
'</li>'
])
return menu_list
def get_menu_section(self, _plugin_defn):
pluginid = _plugin_defn['id']
repoid = _plugin_defn['repoid']
return ''.join([
'<div id="menuActionStatus"></div>',
'<form action="/api/pluginsform" method="post">'
'<input type="hidden" name="action" value="">',
'<input type="hidden" name="pluginId" value="',
pluginid, '">',
'<input type="hidden" name="repoId" value="',
repoid, '">',
'<div id="pluginActions" class="menuCanvas listItemHide">',
'<div class="menuPanel">',
self.get_menu_top_section(_plugin_defn),
'<ul class="menuList">',
self.get_menu_items(_plugin_defn),
'</ul>',
'</div>',
'</div></form>'])
def get_plugin_header(self, _plugin_defn):
instances = self.plugin_db.get_instances(_namespace=_plugin_defn['name'])
if instances:
# array of instance names
instances = instances[_plugin_defn['name']]
else:
instances = None
if not _plugin_defn['version'].get('latest'):
_plugin_defn['version']['latest'] = None
html = ''.join([
'<div><div style="display: flex;"><div class="pluginIcon">',
'<a href="#" onclick=\'display_plugins()\'>',
'<div ><i class="md-icon">arrow_back</i></div></a></div>',
'<div class="pluginSectionName">',
str(_plugin_defn['name']), '</div></div>',
'<div>', str(_plugin_defn['summary']), '</div>',
'<div style="position: relative; padding-left: 0.5em;">',
'<img class="image-size" src="/api/manifest?plugin=',
_plugin_defn['name'], '&key=icon" alt="',
_plugin_defn['name'],'">',
'</div>',
'<div>',
str(_plugin_defn['description']),
'</div>',
'<div style="background: var(--docked-drawer-background);">'
])
return html
def get_plugin_section(self, _plugin_defn):
pluginid = _plugin_defn['id']
repoid = _plugin_defn['repoid']
instances = self.plugin_db.get_instances(_namespace=_plugin_defn['name'])
if instances:
# array of instance names
instances = instances[_plugin_defn['name']]
else:
instances = None
if not _plugin_defn['version'].get('latest'):
_plugin_defn['version']['latest'] = None
latest_version = _plugin_defn['version']['latest']
upgrade_available = ''
if latest_version != _plugin_defn['version']['current'] and _plugin_defn['external']:
upgrade_available = ''.join([
'<form action="/api/pluginsform" method="post">'
'<input type="hidden" name="action" value="">',
'<input type="hidden" name="pluginId" value="',
pluginid, '">',
'<input type="hidden" name="repoId" value="',
repoid, '">',
'<button class="menuIconButton" type="submit" name="action" value="upgradePlugin" style="margin-left:10px;">Upgrade to {}</button>' \
.format(latest_version),
'</form>'
])
if _plugin_defn['version']['installed']:
version_installed_div = ''.join([
'<div class="pluginSection">',
'<div class="pluginSectionName">Version Installed: </div>',
'<div style="float:left; margin-top:3px;" class="pluginValue">',
str(_plugin_defn['version']['current']), '</div>',
upgrade_available,
'</div>',
])
else:
version_installed_div = ''
if _plugin_defn.get('changelog'):
changelog_div = ''.join([
'<div class="pluginSection">',
'<div class="pluginSectionName">Change Log: </div>',
'<div style="float:left; margin-top:3px;" class="pluginValue">',
'<code>',
str(_plugin_defn['changelog']), '</code></div>',
'</div>',
])
else:
changelog_div = ''
html = ''.join([
'<button class="menuIconButton menuSection" type="button" onclick="show_menu(this, \'pluginActions\');">',
'<i class="md-icon" STYLE="font-size: 1.5em;">menu</i>',
'</button>',
version_installed_div,
'<div class="pluginSection">',
'<div class="pluginSectionName">Latest Version: </div>',
'<div class="pluginValue">',
str(_plugin_defn['version']['latest']),
'</div>',
'</div>',
changelog_div,
'<div class="pluginSection">',
'<div class="pluginSectionName">Dependencies: </div>',
'<div class="pluginValue">',
str(_plugin_defn['dependencies']),
'</div>',
'</div>',
'<div class="pluginSection">',
'<div class="pluginSectionName">Source: </div>',
'<div class="pluginValue">',
str(_plugin_defn['source']),
'</div>',
'</div>',
'<div class="pluginSection">',
'<div class="pluginSectionName">License: </div>',
'<div class="pluginValue">',
str(_plugin_defn['license']),
'</div>',
'</div>',
'<div class="pluginSection">',
'<div class="pluginSectionName">Author: </div>',
'<div class="pluginValue">',
str(_plugin_defn['provider-name']),
'</div>',
'</div>',
'<div class="pluginSection">',
'<div class="pluginSectionName">Origin: </div>',
'<div class="pluginValue">',
'Cabernet Plugin Repository',
'</div>',
'</div>',
'<div class="pluginSection">',
'<div class="pluginSectionName">Category: </div>',
'<div class="pluginValue">',
str(_plugin_defn['category']),
'</div>',
'</div>',
'<div class="pluginSection">',
'<div class="pluginSectionName">Related Website: </div>',
'<div class="pluginValue">',
str(_plugin_defn['website']),
'</div>',
'</div>',
'<div class="pluginSection">',
'<div class="pluginSectionName">Instances: </div>',
'<div class="pluginValue">',
str(instances),
'</div>',
'</div>',
'</div>',
'</div>'
])
return html
def form_plugins(self, _is_installed):
plugin_defns = self.plugin_db.get_plugins(
_is_installed, None, None)
if not plugin_defns:
if self.area == 'My_Plugins':
return ''.join([
'No plugins are installed. Go to Catalog and select a plugin to install.'
])
elif self.area == 'Catalog':
return ''.join([
'All available plugins are installed'
])
plugins_list = ''
for plugin_defn in sorted(plugin_defns, key=lambda p: p['id']):
repo_id = plugin_defn['repoid']
plugin_id = plugin_defn['id']
plugin_name = plugin_defn['name']
img_size = self.lookup_config_size()
latest_version = plugin_defn['version']['latest']
upgrade_available = ''
if _is_installed and plugin_defn['external']:
if latest_version != plugin_defn['version']['current']:
upgrade_available = '<div class="bottom-left">Upgrade to {}</div>' \
.format(latest_version)
current_version = plugin_defn['version']['current']
elif not _is_installed:
current_version = plugin_defn['version']['latest']
else:
current_version = 'Internal'
plugins_list += ''.join([
'<button class="plugin_item" type="button"',
' onclick=\'load_plugin_url("/api/pluginsform?',
'plugin=', plugin_id,
'&repo=', repo_id,
'")\'>',
'<div>',
'<div style="position: relative;">',
'<img src="/api/manifest?plugin=',
plugin_name, '&key=icon" width="', str(img_size), '" alt="',
plugin_name,'">',
upgrade_available,
'</div><div>', plugin_name,
'</div><div class="pluginText-secondary">',
str(current_version), '</div></div>',
'</button>'
])
return plugins_list
@property
def header(self):
return ''.join([
'<html><head>',
'</head>'
])
@property
def form(self):
if self.area == 'My_Plugins':
forms_html = ''.join([
'<div class="plugin_list">',
self.form_plugins(True), '</div>'])
elif self.area == 'Catalog':
forms_html = ''.join([
'Plugins Available To Install:'
'<div class="plugin_list">',
self.form_plugins(_is_installed=False),
'</div>'])
else:
self.logger.warning('HTTP request: unknown area: {}'.format(self.area))
raise exceptions.CabernetException('Unknown Tab: {}'.format(self.area))
return forms_html
@property
def body(self):
return ''.join([
'<body>', self.form, '</body>'])
def lookup_config_size(self):
size_text = self.config['channels']['thumbnail_size']
if size_text == 'None':
return 0
elif size_text == 'Tiny(16)':
return 16
elif size_text == 'Small(48)':
return 48
elif size_text == 'Medium(128)':
return 128
elif size_text == 'Large(180)':
return 180
elif size_text == 'X-Large(270)':
return 270
elif size_text == 'Full-Size':
return None
else:
return None
|