Skip to content

Commit

Permalink
Merge pull request #405 from Pingdred/reduce_db_access
Browse files Browse the repository at this point in the history
Reduced MadHatter access to db
  • Loading branch information
pieroit authored Aug 8, 2023
2 parents 9bbeeb2 + 087a387 commit 9fc9434
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions core/cat/mad_hatter/mad_hatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ class MadHatter:

def __init__(self, ccat):
self.ccat = ccat

self.plugins = {} # plugins dictionary

self.hooks = [] # list of active plugins hooks
self.tools = [] # list of active plugins tools

self.active_plugins = []

self.find_plugins()

def install_plugin(self, package_plugin):
Expand Down Expand Up @@ -55,12 +60,10 @@ def install_plugin(self, package_plugin):

def uninstall_plugin(self, plugin_id):

active_plugins = self.load_active_plugins_from_db()

if self.plugin_exists(plugin_id):

# deactivate plugin if it is active (will sync cache)
if plugin_id in active_plugins:
if plugin_id in self.active_plugins:
self.toggle_plugin(plugin_id)

# remove plugin from cache
Expand All @@ -77,6 +80,8 @@ def find_plugins(self):
# and stored in a dictionary plugin_id -> plugin_obj
self.plugins = {}

self.active_plugins = self.load_active_plugins_from_db()

# plugins are found in the plugins folder,
# plus the default core plugin s(where default hooks and tools are defined)
core_plugin_folder = "cat/mad_hatter/core_plugin/"
Expand All @@ -85,19 +90,16 @@ def find_plugins(self):
plugins_folder = self.ccat.get_plugin_path()

all_plugin_folders = [core_plugin_folder] + glob.glob(f"{plugins_folder}*/")

# db contains the list of active plugins
active_plugins = self.load_active_plugins_from_db()

log("ACTIVE PLUGINS:", "INFO")
log(active_plugins, "INFO")
log(self.active_plugins, "INFO")

# discover plugins, folder by folder
for folder in all_plugin_folders:

# is the plugin active?
folder_base = os.path.basename(os.path.normpath(folder))
is_active = folder_base in active_plugins
is_active = folder_base in self.active_plugins

self.load_plugin(folder, is_active)

Expand All @@ -119,11 +121,9 @@ def sync_hooks_and_tools(self):
self.hooks = []
self.tools = []

active_plugins = self.load_active_plugins_from_db()

for _, plugin in self.plugins.items():
# load hooks and tools
if plugin.id in active_plugins:
if plugin.id in self.active_plugins:

# fix tools so they have an instance of the cat # TODO: make the cat a singleton
for t in plugin.tools:
Expand Down Expand Up @@ -213,26 +213,23 @@ def toggle_plugin(self, plugin_id):
log(f"toggle plugin {plugin_id}", "WARNING")

if self.plugin_exists(plugin_id):

# get active plugins from db
active_plugins = self.load_active_plugins_from_db()

plugin_is_active = plugin_id in active_plugins
plugin_is_active = plugin_id in self.active_plugins

# update list of active plugins
if plugin_is_active:
# Deactivate the plugin
self.plugins[plugin_id].deactivate()
# Remove the plugin from the list of active plugins
active_plugins.remove(plugin_id)
self.active_plugins.remove(plugin_id)
else:
# Activate the plugin
self.plugins[plugin_id].activate()
# Ass the plugin in the list of active plugins
active_plugins.append(plugin_id)
self.active_plugins.append(plugin_id)

# update DB with list of active plugins, delete duplicate plugins
self.save_active_plugins_to_db(list(set(active_plugins)))
self.save_active_plugins_to_db(list(set(self.active_plugins)))

# update cache and embeddings
self.sync_hooks_and_tools()
Expand Down

0 comments on commit 9fc9434

Please sign in to comment.