File size: 8,363 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 |
"""
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 json
from lib.db.db import DB
from lib.common.decorators import Backup
from lib.common.decorators import Restore
DB_REPOS_TABLE = 'repos'
DB_PLUGINS_TABLE = 'plugins'
DB_INSTANCE_TABLE = 'instance'
DB_CONFIG_NAME = 'db_files-plugins_db'
sqlcmds = {
'ct': [
"""
CREATE TABLE IF NOT EXISTS repos (
id VARCHAR(255) NOT NULL,
name VARCHAR(255) NOT NULL,
url VARCHAR(255) NOT NULL,
json TEXT NOT NULL,
UNIQUE(id)
)
""",
"""
CREATE TABLE IF NOT EXISTS plugins (
id VARCHAR(255) NOT NULL,
repo VARCHAR(255) NOT NULL,
namespace VARCHAR(255) NOT NULL,
installed BOOLEAN NOT NULL,
json TEXT NOT NULL,
UNIQUE(repo, namespace, id)
)
""",
"""
CREATE TABLE IF NOT EXISTS instance (
repo VARCHAR(255) NOT NULL,
namespace VARCHAR(255) NOT NULL,
instance VARCHAR(255) NOT NULL,
description TEXT,
UNIQUE(repo, namespace, instance)
)
"""
],
'dt': [
"""
DROP TABLE IF EXISTS instance
""",
"""
DROP TABLE IF EXISTS plugins
""",
"""
DROP TABLE IF EXISTS repos
"""
],
'repos_add':
"""
INSERT OR REPLACE INTO repos (
id, name, url, json
) VALUES ( ?, ?, ?, ? )
""",
'repos_get':
"""
SELECT * FROM repos WHERE id LIKE ?
""",
'repos_del':
"""
DELETE FROM repos WHERE id=?
""",
'plugins_add':
"""
INSERT OR REPLACE INTO plugins (
id, repo, namespace, installed, json
) VALUES ( ?, ?, ?, ?, ? )
""",
'plugins_get':
"""
SELECT * FROM plugins WHERE repo LIKE ? AND id LIKE ?
AND installed=?
""",
'plugins_name_get':
"""
SELECT * FROM plugins WHERE repo LIKE ? AND namespace LIKE ?
AND installed=?
""",
'plugins_all_get':
"""
SELECT * FROM plugins WHERE repo LIKE ? AND id LIKE ?
""",
'plugins_all_name_get':
"""
SELECT * FROM plugins WHERE repo LIKE ? AND namespace LIKE ?
""",
'plugins_del':
"""
DELETE FROM plugins WHERE repo=? AND id=?
""",
'instance_add':
"""
INSERT OR REPLACE INTO instance (
repo, namespace, instance, description
) VALUES ( ?, ?, ?, ? )
""",
'instance_get':
"""
SELECT * FROM instance WHERE repo LIKE ?
AND namespace LIKE ? ORDER BY namespace, instance
""",
'instance_del':
"""
DELETE FROM instance WHERE repo LIKE ? AND
namespace LIKE ? AND instance LIKE ?
"""
}
class DBPlugins(DB):
def __init__(self, _config):
super().__init__(_config, _config['datamgmt'][DB_CONFIG_NAME], sqlcmds)
def save_repo(self, _repo_dict):
self.add(DB_REPOS_TABLE, (
_repo_dict['id'],
_repo_dict['name'],
_repo_dict['repo_url'],
json.dumps(_repo_dict)))
def save_plugin(self, _plugin_dict):
self.add(DB_PLUGINS_TABLE, (
_plugin_dict['id'],
_plugin_dict['repoid'],
_plugin_dict['name'],
_plugin_dict['version']['installed'],
json.dumps(_plugin_dict)))
def save_instance(self, _repo_id, _namespace, _instance, _descr):
self.add(DB_INSTANCE_TABLE, (
_repo_id,
_namespace,
_instance,
_descr))
def get_repos(self, _id):
if not _id:
_id = '%'
rows = self.get_dict(DB_REPOS_TABLE, (_id,))
plugin_list = []
for row in rows:
plugin_list.append(json.loads(row['json']))
if len(plugin_list) == 0:
plugin_list = None
return plugin_list
def del_repo(self, _id):
"""
If a plugin is installed, it must be removed before the
repo can be deleted. Once all plugins are not installed,
then will remove the repo, plugin and all instances
"""
plugins_installed = self.get_plugins(True, _id)
self.logger.warning('################## TBD, aborting delete {}'.format(len(plugins_installed)))
#self.delete(DB_INSTANCE_TABLE, (_id, '%', '%',))
#self.delete(DB_PLUGINS_TABLE, (_id, '%',))
#self.delete(DB_REPOS_TABLE, (_id,))
def get_plugins(self, _installed, _repo_id=None, _plugin_id=None):
if not _repo_id:
_repo_id = '%'
if not _plugin_id:
_plugin_id = '%'
if _installed is None:
rows = self.get_dict(DB_PLUGINS_TABLE+'_all', (_repo_id, _plugin_id,))
else:
rows = self.get_dict(DB_PLUGINS_TABLE, (_repo_id, _plugin_id, _installed,))
plugin_list = []
for row in rows:
plugin_list.append(json.loads(row['json']))
if len(plugin_list) == 0:
plugin_list = None
return plugin_list
def get_plugins_by_name(self, _installed, _repo_id=None, _plugin_name=None):
if not _repo_id:
_repo_id = '%'
if not _plugin_name:
_plugin_name = '%'
if _installed is None:
rows = self.get_dict(DB_PLUGINS_TABLE+'_all_name', (_repo_id, _plugin_name,))
else:
rows = self.get_dict(DB_PLUGINS_TABLE+'_name', (_repo_id, _plugin_name, _installed,))
plugin_list = []
for row in rows:
plugin_list.append(json.loads(row['json']))
if len(plugin_list) == 0:
plugin_list = None
return plugin_list
def del_plugin(self, _repo_id, _plugin_id):
"""
Deletes the instance rows first due to constaints, then
deletes the plugin
"""
self.delete(DB_INSTANCE_TABLE, (_repo_id, _plugin_id, '%',))
self.delete(DB_PLUGINS_TABLE, (_repo_id, _plugin_id,))
def del_instance(self, _repo, _namespace, _instance):
if not _repo:
_repo = '%'
return self.delete(DB_INSTANCE_TABLE, (_repo, _namespace, _instance,))
def get_instances(self, _repo=None, _namespace=None):
"""
createa a dict of namespaces that contain an array of instances
"""
if not _repo:
_repo = '%'
if not _namespace:
_namespace = '%'
rows_dict = {}
rows = self.get_dict(DB_INSTANCE_TABLE, (_repo, _namespace,))
for row in rows:
if row['namespace'] not in rows_dict:
rows_dict[row['namespace']] = []
instances = rows_dict[row['namespace']]
instances.append(row['instance'])
# rows_dict[row['namespace']] = row['instance']
return rows_dict
def get_instances_full(self, _repo=None, _namespace=None):
if not _repo:
_repo = '%'
if not _namespace:
_namespace = '%'
rows_dict = {}
rows = self.get_dict(DB_INSTANCE_TABLE, (_repo, _namespace,))
for row in rows:
rows_dict[row['namespace']] = row
return rows_dict
@Backup(DB_CONFIG_NAME)
def backup(self, backup_folder):
self.export_sql(backup_folder)
@Restore(DB_CONFIG_NAME)
def restore(self, backup_folder):
msg = self.import_sql(backup_folder)
if msg is None:
return 'Plugin Manifest Database Restored'
else:
return msg
|