From 74090cbc28cc4a11d4a29e384869deb5c8c26be4 Mon Sep 17 00:00:00 2001 From: Nevysha Date: Sat, 8 Jul 2023 13:09:18 +0200 Subject: [PATCH] get / post / del config --- .gitignore | 3 ++- preload.py | 6 +++++ scripts/lib/api.py | 43 +++++++++++++++++++++++++++++--- scripts/lib/config.py | 54 +++++++++++++++++++++++++++++++++++++++++ scripts/lib/lobe_log.py | 19 +++++++++++++++ scripts/settings.py | 16 ++++++++++++ src/store/index.tsx | 12 +++++++++ 7 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 preload.py create mode 100644 scripts/lib/lobe_log.py diff --git a/.gitignore b/.gitignore index 84a223c..1290196 100644 --- a/.gitignore +++ b/.gitignore @@ -41,4 +41,5 @@ test-output # misc # add other ignore file below -__pycache__ \ No newline at end of file +__pycache__ +/lobe_theme_config.json diff --git a/preload.py b/preload.py new file mode 100644 index 0000000..6f447e8 --- /dev/null +++ b/preload.py @@ -0,0 +1,6 @@ +def preload(parser): + parser.add_argument( + "--lobe-debug", + action="store_true", + help="Enable debug logging", + ) diff --git a/scripts/lib/api.py b/scripts/lib/api.py index 27427d8..7d73adf 100644 --- a/scripts/lib/api.py +++ b/scripts/lib/api.py @@ -1,6 +1,43 @@ +import json + +from fastapi import FastAPI, Response, Request + +from scripts.lib.config import LobeConfig +from scripts.lib.lobe_log import LobeLog + + class LobeApi: - def __init__(self, lobe_model_path): + def __init__(self, config: LobeConfig): + self.config = config pass - def create_api_route(self, app): - pass + def create_api_route(self, app: FastAPI): + + @app.get("/lobe/config") + async def lobe_config_get(): + LobeLog.debug("lobe_config_get") + + if self.config.is_empty(): + return Response(content=self.config.json(), media_type="application/json", status_code=404) + return Response(content=self.config.json(), media_type="application/json", status_code=200) + + @app.post("/lobe/config") + async def lobe_config_save(request: Request): + LobeLog.debug("lobe_config_save") + + data = await request.json() + self.config.save(data) + return Response( + content=json.dumps({"message": "Config saved successfully"}), + media_type="application/json", status_code=200 + ) + + @app.delete("/lobe/config") + async def lobe_config_delete(): + LobeLog.debug("lobe_config_delete") + + self.config.delete() + return Response( + content=json.dumps({"message": "Config deleted successfully"}), + media_type="application/json", status_code=200 + ) diff --git a/scripts/lib/config.py b/scripts/lib/config.py index e69de29..1dcd195 100644 --- a/scripts/lib/config.py +++ b/scripts/lib/config.py @@ -0,0 +1,54 @@ +import json +import os +from pathlib import Path + +from scripts.lib.lobe_log import LobeLog + +EXTENSION_FOLDER = Path(__file__).parent.parent.parent +CONFIG_FILENAME = Path(EXTENSION_FOLDER, "lobe_theme_config.json") + + +LobeLog.debug(f"EXTENSION_FOLDER: {EXTENSION_FOLDER}") +LobeLog.debug(f"CONFIG_FILENAME: {CONFIG_FILENAME}") + + +class LobeConfig: + def __init__(self): + self.config_file = CONFIG_FILENAME + self.config = None + self.load_config() + + def load_config(self): + if os.path.exists(self.config_file): + LobeLog.debug(f"Loading config from {self.config_file}") + + with open(self.config_file, 'r') as f: + self.config = json.load(f) + else: + LobeLog.debug(f"Config file not found") + self.config = LobeConfig.default() + + def is_empty(self): + return "empty" in self.config and self.config['empty'] + + def json(self): + return json.dumps(self.config) + + def delete(self): + if os.path.exists(self.config_file): + os.remove(self.config_file) + self.config = LobeConfig.default() + return True + + return False + + def save(self, settings): + self.config = settings + with open(self.config_file, 'w') as f: + f.write(json.dumps(self.config, indent=2)) + f.close() + + @staticmethod + def default(): + # default config is handled from client side @see src/store/index.tsx + return {'empty': True} diff --git a/scripts/lib/lobe_log.py b/scripts/lib/lobe_log.py new file mode 100644 index 0000000..90116cc --- /dev/null +++ b/scripts/lib/lobe_log.py @@ -0,0 +1,19 @@ +from modules import shared + + +class LobeLogClass: + + def __init__(self): + # added a launch argument to enable debug mode @see preload.py + # just add --lobe-debug to the launch arguments + self.logging_enabled = shared.cmd_opts.lobe_debug + + def debug(self, message: str): + if self.logging_enabled: + print(f"[Lobe:DEBUG]: {message}") + + def info(self, message: str): + print(f"[Lobe]: {message}") + + +LobeLog = LobeLogClass() diff --git a/scripts/settings.py b/scripts/settings.py index 791cc05..4693b5c 100644 --- a/scripts/settings.py +++ b/scripts/settings.py @@ -1,10 +1,17 @@ +from fastapi import FastAPI + import modules.scripts as scripts import gradio as gr import os +from typing import Any from modules import shared from modules import script_callbacks +from scripts.lib.lobe_log import LobeLog +from scripts.lib.api import LobeApi +from scripts.lib.config import LobeConfig + def on_ui_settings(): section = ('lobe_theme', "Lobe Theme") @@ -21,4 +28,13 @@ def on_ui_settings(): ) +def init_lobe(_: Any, app: FastAPI, **kwargs): + LobeLog.info("Initializing Lobe") + + config = LobeConfig() + api = LobeApi(config) + api.create_api_route(app) + + script_callbacks.on_ui_settings(on_ui_settings) +script_callbacks.on_app_started(init_lobe) diff --git a/src/store/index.tsx b/src/store/index.tsx index 96e41dc..be36bf2 100644 --- a/src/store/index.tsx +++ b/src/store/index.tsx @@ -119,6 +119,18 @@ export const useAppStore = create()( }, onSetSetting: (setting) => { localStorage.setItem(SETTING_KEY, JSON.stringify(setting)); + //TODO FIX ME (nevysha: I needed this for testing purpose) + // sending settings to server. + (async() => { + await fetch('/lobe/config', { + body: JSON.stringify(setting), + headers: { + 'Content-Type': 'application/json', + }, + method: 'POST', + }); + })(); + set(() => ({ setting }), false, 'onSetSetting'); }, onSetThemeMode: (themeMode) => {