Skip to content

Commit

Permalink
feat(PluginSystem): make use of Selector correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
t00m committed May 25, 2024
1 parent 7f83188 commit 6f00adc
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 349 deletions.
50 changes: 29 additions & 21 deletions MiAZ/backend/pluginsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class MiAZPluginType(IntEnum):

class MiAZPluginManager(GObject.GObject):
def __init__(self, app):
super().__init__()
GObject.signal_new('plugins-updated',
MiAZPluginManager,
GObject.SignalFlags.RUN_LAST, None, () )
Expand Down Expand Up @@ -83,9 +84,27 @@ def import_plugin(self, plugin_path):
ENV = self.app.get_env()
azip.extractall(ENV['LPATH']['PLUGINS'])
self.engine.rescan_plugins()
config = self.app.get_config('Plugin')
config.add_available(key=fn1_name)
self.log.debug("Plugin '%s' added to '%s'", os.path.basename(plugin_path), ENV['LPATH']['PLUGINS'])
# ~ self.emit('plugins-updated')
return valid

def remove_plugin(self, plugin: Peas.PluginInfo):
"""Remove plugin for user space plugins"""
# FIXME: Make sure the plugin is deleted and unloaded
ENV = self.app.get_env()
module = plugin.get_module_name()
self.unload_plugin(plugin)
plugin_head = os.path.join(ENV['LPATH']['PLUGINS'], '%s.plugin' % module)
plugin_body = os.path.join(ENV['LPATH']['PLUGINS'], '%s.py' % module)
os.unlink(plugin_head)
os.unlink(plugin_body)
config = self.app.get_config('Plugin')
config.remove_available(key=module)
# ~ self.emit('plugins-updated')
return True

def rescan_plugins(self):
try:
self.engine.rescan_plugins()
Expand All @@ -111,7 +130,11 @@ def load_plugin(self, plugin: Peas.PluginInfo) -> bool:
return False

def unload_plugin(self, plugin: Peas.PluginInfo):
self.engine.unload_plugin(plugin)
try:
self.engine.unload_plugin(plugin)
self.log.debug("Plugin unloaded")
except Exception as error:
self.log.error(error)

def get_engine(self):
return self.engine
Expand All @@ -131,7 +154,7 @@ def get_plugin_type(self, plugin_info):
else:
return MiAZPluginType.SYSTEM

def get_extension(self, module_name):
def get_extension(self, module_name: str):
"""Gets the extension identified by the specified name.
Args:
module_name (str): The name of the extension.
Expand All @@ -144,7 +167,7 @@ def get_extension(self, module_name):

return self.extension_set.get_extension(plugin)

def get_plugin_info(self, module_name):
def get_plugin_info(self, module_name: str):
"""Gets the plugin info for the specified plugin name.
Args:
module_name (str): The name from the .plugin file of the module.
Expand All @@ -171,32 +194,17 @@ def _setup_extension_set(self):

def _setup_plugins_dir(self):
# System plugins
# Mandatory set of plugins for every repository
ENV = self.app.get_env()
if os.path.exists(ENV['GPATH']['PLUGINS']):
self.engine.add_search_path(ENV['GPATH']['PLUGINS'])
self.log.debug("Added System plugin dir: %s", ENV['GPATH']['PLUGINS'])

# GLobal user plugins
# All user space plugins are available for all repositories
# However, each repository can use none, any or all of them
self.add_user_plugins_dir()

# ~ # User plugins for a specific repo
# ~ self.add_repo_plugins_dir()

# ~ def add_repo_plugins_dir(self):
# ~ try:
# ~ repo = self.backend.repo_config()
# ~ dir_conf = repo['dir_conf']
# ~ dir_plugins = os.path.join(dir_conf, 'plugins')
# ~ dir_plugins_available = os.path.join(dir_plugins, 'available')
# ~ dir_plugins_used = os.path.join(dir_plugins, 'used')
# ~ os.makedirs(dir_plugins, exist_ok=True)
# ~ os.makedirs(dir_plugins_available, exist_ok=True)
# ~ os.makedirs(dir_plugins_used, exist_ok=True)
# ~ self.engine.add_search_path(dir_plugins_used)
# ~ self.log.debug("Added User plugin dir: %s", dir_plugins_used)
# ~ except KeyError:
# ~ self.log.warning("There isn't any repo loaded right now!")

def add_user_plugins_dir(self):
ENV = self.app.get_env()
os.makedirs(ENV['LPATH']['PLUGINS'], exist_ok=True)
Expand Down
2 changes: 1 addition & 1 deletion MiAZ/frontend/desktop/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,4 +573,4 @@ def remove_widget(self, name: str):
def statusbar_message(self, message: str):
"""Statusbar message"""
statusbar = self.get_widget('statusbar')
statusbar.statusbar_message(message)
statusbar.message(message)
31 changes: 14 additions & 17 deletions MiAZ/frontend/desktop/widgets/configview.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,32 +304,29 @@ def __init__(self, app):
super().__init__(app, 'Plugin')
self._update_view_available()

def plugins_updated(self, *args):
# ~ self._update_view_used()
self._update_view_available()
self.log.debug("Selector plugin views updated")

def _setup_view_finish(self):
# Setup Available and Used Column Views
self.viewAv = MiAZColumnViewPlugin(self.app)
self.add_columnview_available(self.viewAv)
self.viewSl = MiAZColumnViewPlugin(self.app)
self.add_columnview_used(self.viewSl)

def _update_view_available(self):
plugin_manager = self.app.get_service('plugin-manager')
items = []
item_type = self.config.model
for plugin in plugin_manager.plugins:
ptype = plugin_manager.get_plugin_type(plugin)
if ptype == MiAZPluginType.USER:
pid = plugin.get_module_name()
title = plugin.get_description() #+ ' (v%s)' % plugin.get_version()
items.append(item_type(id=pid, title=title))
self.viewAv.update(items)

# ~ def _update_view_used(self):
# ~ def _update_view_available(self):
# ~ plugin_manager = self.app.get_service('plugin-manager')
# ~ items = []
# ~ item_type = self.config.model
# ~ countries = self.config.load_used()
# ~ for code in countries:
# ~ items.append(item_type(id=code, title=countries[code], icon='%s.svg' % code))
# ~ self.viewSl.update(items)
# ~ for plugin in plugin_manager.plugins:
# ~ ptype = plugin_manager.get_plugin_type(plugin)
# ~ if ptype == MiAZPluginType.USER:
# ~ pid = plugin.get_module_name()
# ~ title = plugin.get_description() #+ ' (v%s)' % plugin.get_version()
# ~ items.append(item_type(id=pid, title=title))
# ~ self.viewAv.update(items)

def _on_item_used_remove(self, *args):
plugin_manager = self.app.get_service('plugin-manager')
Expand Down
67 changes: 25 additions & 42 deletions MiAZ/frontend/desktop/widgets/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ def _create_widget_for_repositories(self):

# Load last active repo
repos_used = self.config['Repository'].load_used()
self.log.debug("Repositories in use: %s", ','.join(repos_used.keys()))
repo_active = self.config['App'].get('current')
self.log.debug("Current active: %s", repo_active)
if repo_active in repos_used:
model = self.dd_repo.get_model()
n = 0
Expand Down Expand Up @@ -162,39 +160,39 @@ def _create_widget_for_plugins(self):
notebook = Gtk.Notebook()
notebook.set_show_border(False)
notebook.set_tab_pos(Gtk.PositionType.LEFT)
widget = self._create_widget_for_system_plugins()
widget = self._create_view_plugins_system()
label = self.factory.create_notebook_label(icon_name='miaz-app-settings', title='System')
notebook.append_page(widget, label)
widget = self._create_widget_for_user_plugins()
widget = self._create_view_plugins_user()
label = self.factory.create_notebook_label(icon_name='miaz-res-people', title='User')
notebook.append_page(widget, label)
vbox.append(notebook)
return vbox

def _create_widget_for_system_plugins(self):
from MiAZ.backend.pluginsystem import MiAZPluginType
def _create_view_plugins_system(self):
vbox = self.factory.create_box_vertical(margin=0, spacing=0, hexpand=True, vexpand=True)
scrwin = self.factory.create_scrolledwindow()
self.app.add_widget('app-settings-plugins-system-scrwin', scrwin)
vbox.append(scrwin)
pm = self.app.get_service('plugin-manager')
# ~ pm.add_repo_plugins_dir()
view = MiAZColumnViewPlugin(self.app)
view.set_hexpand(True)
view.set_vexpand(True)
self.app.add_widget('app-settings-plugins-system-view', view)
scrwin.set_child(view)

box = Gtk.ListBox.new()
box.set_vexpand(True)
scrwin.set_child(box)
# System Plugins
items = []
item_type = Plugin
for plugin in pm.plugins:
if pm.get_plugin_type(plugin) == MiAZPluginType.SYSTEM:
title = "<b>%s</b>" % plugin.get_name()
subtitle = plugin.get_description() + ' (v%s)' % plugin.get_version()
active = plugin.is_loaded()
row = self.factory.create_actionrow(title=title, subtitle=subtitle)
box.append(row)
pid = plugin.get_module_name()
title = plugin.get_description()
items.append(item_type(id=pid, title=title))
view.update(items)
return vbox

def _create_widget_for_user_plugins(self):
# Trick to remove widgets from listbox in Gtk 4.8.3 (Debian 11)
# as the method remove_all is not avaiable only since 4.12 :(
self.listboxwidgets = []
def _create_view_plugins_user(self):
vbox = self.factory.create_box_vertical(margin=0, spacing=0, hexpand=True, vexpand=True)

# Add/Remove
Expand All @@ -204,12 +202,11 @@ def _create_widget_for_user_plugins(self):
hbox.append(self.factory.create_button(icon_name='miaz-list-remove', title='Remove plugin', callback=self._on_plugin_remove))
vbox.append(hbox)

# Plugins
# User Plugins
scrwin = self.factory.create_scrolledwindow()
self.app.add_widget('app-settings-plugins-user-scrwin', scrwin)
vbox.append(scrwin)
pm = self.app.get_service('plugin-manager')
# ~ pm.add_repo_plugins_dir()
view = MiAZColumnViewPlugin(self.app)
view.set_hexpand(True)
view.set_vexpand(True)
Expand All @@ -231,21 +228,10 @@ def update_user_plugins(self):
pid = plugin.get_module_name()
plugin_path = os.path.join(ENV['LPATH']['PLUGINS'], '%s.plugin' % pid)
if os.path.exists(plugin_path):
title = plugin.get_description() #+ ' (v%s)' % plugin.get_version()
title = plugin.get_description()
items.append(item_type(id=pid, title=title))
self.log.debug("Updating with plugin '%s'", pid)
view.update(items)

# ~ # Update listbox
# ~ for plugin in pm.plugins:
# ~ if pm.get_plugin_type(plugin) == MiAZPluginType.USER:
# ~ title = "<b>%s</b>" % plugin.get_name()
# ~ subtitle = plugin.get_description() + ' (v%s)' % plugin.get_version()
# ~ active = plugin.is_loaded()
# ~ row = self.factory.create_actionrow(title=title, subtitle=subtitle)
# ~ listbox.append(row)
# ~ self.listboxwidgets.append(row)

def on_filechooser_response(self, dialog, response, data):
if response == Gtk.ResponseType.ACCEPT:
plugin_manager = self.app.get_service('plugin-manager')
Expand Down Expand Up @@ -277,19 +263,15 @@ def _on_plugin_add(self, *args):
filechooser_dialog.show()

def _on_plugin_remove(self, *args):
ENV = self.app.get_env()
plugin_manager = self.app.get_service('plugin-manager')
view = self.app.get_widget('app-settings-plugins-user-view')
module = view.get_selected_items()[0]
plugin = plugin_manager.get_plugin_info(module.id)
plugin_manager.unload_plugin(plugin)
plugin_head = os.path.join(ENV['LPATH']['PLUGINS'], '%s.plugin' % module.id)
plugin_body = os.path.join(ENV['LPATH']['PLUGINS'], '%s.py' % module.id)
self.util.filename_delete(plugin_head)
self.util.filename_delete(plugin_body)
self.log.debug("Plugin '%s' deleted", module.id)
self.app.message("Plugin '%s' deleted" % module.id)
self.update_user_plugins()
deleted = plugin_manager.remove_plugin(plugin)
if deleted:
self.log.debug("Plugin '%s' deleted", module.id)
self.app.statusbar_message("Plugin '%s' deleted" % module.id)
self.update_user_plugins()

def get_plugin_status(self, name: str) -> bool:
plugins = self.config['App'].get('plugins')
Expand Down Expand Up @@ -358,3 +340,4 @@ def create_tab(item_type):
page, label = create_tab(item_type)
self.notebook.append_page(page, label)


3 changes: 2 additions & 1 deletion MiAZ/frontend/desktop/widgets/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def __init__(self, app, name, title, **kwargs):
self.name = name
self.title = title
self.set_title(title)
self.log = get_logger(name)
logname = "Miaz.%s" % name.replace('-', '.').title()
self.log = get_logger(logname)
self.app.add_widget('window-%s' % name, self)
self.connect('close-request', self._on_window_close_request)
evk = Gtk.EventControllerKey.new()
Expand Down
2 changes: 1 addition & 1 deletion MiAZ/frontend/desktop/widgets/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def _on_selection_changed(self, selection, position, n_items):
docs = self.util.get_files()
self.log.debug(', '.join([item.id for item in self.selected_items]))
label.set_markup("<small>%d</small> / %d / <big>%d</big>" % (len(self.selected_items), len(model), len(docs)))
self.app.message("Selected %d of %d documents in current view (total documents: %d)" % (len(self.selected_items), len(model), len(docs)))
self.app.statusbar_message("Selected %d of %d documents in current view (total documents: %d)" % (len(self.selected_items), len(model), len(docs)))
# ~ if len(self.selected_items) == 1:
# ~ menu = self.app.get_widget('workspace-menu-single')
# ~ self.popDocsSel.set_menu_model(menu)
Expand Down
10 changes: 0 additions & 10 deletions data/examples/user_plugins/hello.plugin

This file was deleted.

61 changes: 0 additions & 61 deletions data/examples/user_plugins/hello.py

This file was deleted.

Binary file added data/examples/user_plugins/noupdate.zip
Binary file not shown.
10 changes: 0 additions & 10 deletions data/examples/user_plugins/scan.plugin

This file was deleted.

Loading

0 comments on commit 6f00adc

Please sign in to comment.