Skip to content

Commit

Permalink
Merge pull request #19 from FrancescoCaracciolo/nyarch-sync-0.4.1
Browse files Browse the repository at this point in the history
Add extensions settings
  • Loading branch information
FrancescoCaracciolo authored Oct 27, 2024
2 parents 061748f + 70c3cbb commit 37e9011
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 24 deletions.
45 changes: 31 additions & 14 deletions src/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import gi, os

from .constants import AVAILABLE_LLMS, AVAILABLE_PROMPTS, AVAILABLE_STT, AVAILABLE_TTS, PROMPTS

from .settings import Settings
from .extensions import ExtensionLoader
from gi.repository import Gtk, Adw, Gio, GLib

Expand Down Expand Up @@ -33,17 +33,20 @@ def __init__(self,app):
self.update()

def update(self):
self.main = Gtk.Box(margin_top=10,margin_start=10,margin_bottom=10,margin_end=10,valign=Gtk.Align.FILL,halign=Gtk.Align.CENTER,orientation=Gtk.Orientation.VERTICAL)
self.main.set_size_request(300, -1)
self.scrolled_window.set_child(self.main)
self.extensionloader = ExtensionLoader(self.extension_path, pip_path=self.pip_directory, extension_cache=self.extensions_cache, settings=self.settings)
self.extensionloader.load_extensions()

settings = Settings(self.app, headless=True)

self.main = Gtk.Box(margin_top=10,margin_start=10,margin_bottom=10,margin_end=10,valign=Gtk.Align.FILL,halign=Gtk.Align.CENTER,orientation=Gtk.Orientation.VERTICAL)
self.main.set_size_request(300, -1)
self.scrolled_window.set_child(self.main)
self.extensiongroup = Adw.PreferencesGroup(title=_("Extensions"))
self.main.append(self.extensiongroup)
for extension in self.extensionloader.get_extensions():
box = Gtk.Box(margin_top=10,margin_bottom=10,css_classes=["card"], hexpand=True)
box.append(Gtk.Label(label=f"{extension.name}",margin_top=10,margin_start=10,margin_end=10,margin_bottom=10))
box_elements = Gtk.Box(valign=Gtk.Align.CENTER,halign=Gtk.Align.END, hexpand= True)
button = Gtk.Button(css_classes=["flat"], margin_top=10,margin_start=10,margin_end=10,margin_bottom=10)

settings.settingsrows[(extension.key, "extension")]= {}
button = Gtk.Button(css_classes=["flat", "destructive-action"], margin_top=10,margin_start=10,margin_end=10,margin_bottom=10)
button.connect("clicked", self.delete_extension)
button.set_name(extension.id)

Expand All @@ -53,12 +56,26 @@ def update(self):
button.set_child(icon)
switch = Gtk.Switch(valign=Gtk.Align.CENTER)
switch.connect("notify::state", self.change_status)
switch.set_name(extension.id)
switch.set_active(extension not in self.extensionloader.disabled_extensions)
box_elements.append(switch)
box_elements.append(button)
box.append(box_elements)
self.main.append(box)
switch.set_name(extension.id)
if extension not in self.extensionloader.disabled_extensions:
switch.set_active(True)

if len(extension.get_extra_settings()) > 0:
row = Adw.ExpanderRow(title=extension.name)
row.add_suffix(switch)
row.add_suffix(button)
settings.add_extra_settings(settings.extensionloader.extensionsmap, extension, row)
else:
row = Adw.ActionRow(title=extension.name)
row.add_suffix(button)
row.add_suffix(switch)
# Add invisible icon for alignment purposes
invisible_icon = Gtk.Image.new_from_gicon(Gio.ThemedIcon(name="dialog-information-symbolic"))
invisible_icon.set_opacity(0)
row.add_suffix(invisible_icon)

settings.add_flatpak_waning_button(extension, row)
self.extensiongroup.add(row)
folder_button = Gtk.Button(label=_("Choose an extension"), css_classes=["suggested-action"], margin_top=10)
folder_button.connect("clicked", self.on_folder_button_clicked)
self.main.append(folder_button)
Expand Down
20 changes: 11 additions & 9 deletions src/extensions.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import sys, importlib, os, json, shutil
from typing import Any

from gi.repository import Gtk

from .handler import Handler

from .llm import LLMHandler
from .stt import STTHandler
from .tts import TTSHandler


class NewelleExtension:
class NewelleExtension(Handler):
"""The base class for all extensions"""

# Name and ID of the extension
Expand All @@ -25,12 +28,11 @@ def __init__(self, pip_path : str, extension_path: str, settings):
extension_path: path to the extension cache directory
"""
self.pip_path = pip_path
self.path = self.pip_path
self.extension_path = extension_path
self.settings = settings
pass

def install(self):
"""Function called on another thread every time the extension is enabled from the settings"""
self.key = self.id
self.schema_key = "extensions-settings"
pass

def get_llm_handlers(self) -> list[dict]:
Expand Down Expand Up @@ -132,7 +134,6 @@ def get_answer(self, codeblock: str, lang: str) -> str | None:
"""
return None


class ExtensionLoader:
"""
Class that loads the extensions
Expand Down Expand Up @@ -163,6 +164,7 @@ def __init__(self, extension_dir, project_dir=None, pip_path="", extension_cache
self.extensions_settings = json.loads(self.settings.get_string("extensions-settings"))

self.extensions : list[NewelleExtension] = []
self.extensionsmap : dict[str, NewelleExtension] = {}
self.disabled_extensions : list[NewelleExtension] = []
self.codeblocks : dict[str, NewelleExtension] = {}
self.filemap : dict[str, str] = {}
Expand Down Expand Up @@ -197,6 +199,7 @@ def load_extensions(self):
self.disabled_extensions.append(extension)
self.extensions.append(extension)
self.filemap[extension.id] = file
self.extensionsmap[extension.id] = extension
break
except Exception as e:
print("Error loding file: ", file, e)
Expand Down Expand Up @@ -301,9 +304,8 @@ def get_extension_by_id(self, id: str) -> NewelleExtension | None:
Returns:
NewelleExtension | None: the extension or None if not found
"""
for extension in self.extensions:
if extension.id == id:
return extension
if id in self.extensionsmap:
return self.extensionsmap[id]
return None

def enable(self, extension : NewelleExtension | str):
Expand Down
12 changes: 11 additions & 1 deletion src/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .gtkobj import ComboRowHelper, CopyBox, MultilineEntry
from .extra import can_escape_sandbox, override_prompts, human_readable_size

from .extensions import ExtensionLoader
from .extensions import ExtensionLoader, NewelleExtension

class Settings(Adw.PreferencesWindow):
def __init__(self,app,headless=False, *args, **kwargs):
Expand Down Expand Up @@ -290,6 +290,10 @@ def get_object(self, constants: dict[str, Any], key:str) -> (Handler):
model = constants[key]["class"](self.settings, self.directory)
elif constants == AVAILABLE_SMART_PROMPTS:
model = constants[key]["class"](self.settings, self.directory)
elif constants == self.extensionloader.extensionsmap:
model = self.extensionloader.extensionsmap[key]
if model is None:
raise Exception("Extension not found")
else:
raise Exception("Unknown constants")
return model
Expand Down Expand Up @@ -321,6 +325,8 @@ def convert_constants(self, constants: str | dict[str, Any]) -> (str | dict):
return AVAILABLE_TRANSLATORS
case "smart-prompt":
return AVAILABLE_SMART_PROMPTS
case "extension":
return self.extensionloader.extensionsmap
case _:
raise Exception("Unknown constants")
else:
Expand All @@ -336,6 +342,8 @@ def convert_constants(self, constants: str | dict[str, Any]) -> (str | dict):
return "translator"
elif constants == AVAILABLE_SMART_PROMPTS:
return "smart-prompt"
elif constants == self.extensionloader.extensionsmap:
return "extension"
else:
raise Exception("Unknown constants")

Expand All @@ -362,6 +370,8 @@ def get_constants_from_object(self, handler: Handler) -> dict[str, Any]:
return AVAILABLE_TRANSLATORS
elif issubclass(type(handler), SmartPromptHandler):
return AVAILABLE_SMART_PROMPTS
elif issubclass(type(handler), NewelleExtension):
return self.extensionloader.extensionsmap
else:
raise Exception("Unknown handler")

Expand Down
2 changes: 2 additions & 0 deletions src/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def __init__(self, *args, **kwargs):
self.extensions_cache = os.path.join(self.directory, "extensions_cache")
if not os.path.exists(self.extension_path):
os.makedirs(self.extension_path)
if not os.path.exists(self.extensions_cache):
os.makedirs(self.extensions_cache)
sys.path.append(self.pip_directory)

if not os.path.exists(self.path):
Expand Down

0 comments on commit 37e9011

Please sign in to comment.