From 0f880965d9cb7a0ac6823a6bdecd60ab2b2ae76d Mon Sep 17 00:00:00 2001 From: arkohut <39525455+arkohut@users.noreply.github.com> Date: Fri, 4 Oct 2024 16:10:13 +0800 Subject: [PATCH] refactor(plugin): move plugin cmds to cmds --- memos/cmds/__init__.py | 0 memos/cmds/plugin.py | 82 ++++++++++++++++++++++++++++++++++++++++++ memos/commands.py | 82 +++--------------------------------------- 3 files changed, 87 insertions(+), 77 deletions(-) create mode 100644 memos/cmds/__init__.py create mode 100644 memos/cmds/plugin.py diff --git a/memos/cmds/__init__.py b/memos/cmds/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/memos/cmds/plugin.py b/memos/cmds/plugin.py new file mode 100644 index 0000000..ec8fe5b --- /dev/null +++ b/memos/cmds/plugin.py @@ -0,0 +1,82 @@ +import typer +import httpx +from tabulate import tabulate +from memos.config import settings + +BASE_URL = f"http://{settings.server_host}:{settings.server_port}" + + +plugin_app = typer.Typer() + + +def display_plugins(plugins): + table = [] + for plugin in plugins: + table.append( + [plugin["id"], plugin["name"], plugin["description"], plugin["webhook_url"]] + ) + print( + tabulate( + table, + headers=["ID", "Name", "Description", "Webhook URL"], + tablefmt="plain", + ) + ) + + +@plugin_app.command("ls") +def ls(): + response = httpx.get(f"{BASE_URL}/plugins") + plugins = response.json() + display_plugins(plugins) + + +@plugin_app.command("create") +def create(name: str, webhook_url: str, description: str = ""): + response = httpx.post( + f"{BASE_URL}/plugins", + json={"name": name, "description": description, "webhook_url": webhook_url}, + ) + if 200 <= response.status_code < 300: + print("Plugin created successfully") + else: + print(f"Failed to create plugin: {response.status_code} - {response.text}") + + +@plugin_app.command("bind") +def bind( + library_id: int = typer.Option(..., "--lib", help="ID of the library"), + plugin: str = typer.Option(..., "--plugin", help="ID or name of the plugin"), +): + try: + plugin_id = int(plugin) + plugin_param = {"plugin_id": plugin_id} + except ValueError: + plugin_param = {"plugin_name": plugin} + + response = httpx.post( + f"{BASE_URL}/libraries/{library_id}/plugins", + json=plugin_param, + ) + if response.status_code == 204: + print("Plugin bound to library successfully") + else: + print( + f"Failed to bind plugin to library: {response.status_code} - {response.text}" + ) + + +@plugin_app.command("unbind") +def unbind( + library_id: int = typer.Option(..., "--lib", help="ID of the library"), + plugin_id: int = typer.Option(..., "--plugin", help="ID of the plugin"), +): + response = httpx.delete( + f"{BASE_URL}/libraries/{library_id}/plugins/{plugin_id}", + ) + if response.status_code == 204: + print("Plugin unbound from library successfully") + else: + print( + f"Failed to unbind plugin from library: {response.status_code} - {response.text}" + ) diff --git a/memos/commands.py b/memos/commands.py index e7577b7..28b4fcb 100644 --- a/memos/commands.py +++ b/memos/commands.py @@ -1,7 +1,7 @@ import asyncio import os import logging -from datetime import datetime, timezone +from datetime import datetime from pathlib import Path from typing import List, Tuple @@ -21,19 +21,18 @@ from .record import ( run_screen_recorder_once, run_screen_recorder, load_previous_hashes, - save_previous_hashes, ) -import time # Add this import at the top of the file +import time import sys import subprocess import platform +from .cmds.plugin import plugin_app IS_THUMBNAIL = "is_thumbnail" app = typer.Typer(context_settings={"help_option_names": ["-h", "--help"]}) lib_app = typer.Typer() -plugin_app = typer.Typer() app.add_typer(plugin_app, name="plugin") app.add_typer(lib_app, name="lib") @@ -182,7 +181,7 @@ async def loop_files(library_id, folder, folder_path, force, plugins): scanned_files.add(str(absolute_file_path)) candidate_files.append(str(absolute_file_path)) - batching = 100 + batching = 200 for i in range(0, len(candidate_files), batching): batch = candidate_files[i : i + batching] @@ -708,77 +707,6 @@ def index( print("Indexing completed") -def display_plugins(plugins): - table = [] - for plugin in plugins: - table.append( - [plugin["id"], plugin["name"], plugin["description"], plugin["webhook_url"]] - ) - print( - tabulate( - table, - headers=["ID", "Name", "Description", "Webhook URL"], - tablefmt="plain", - ) - ) - - -@plugin_app.command("ls") -def ls(): - response = httpx.get(f"{BASE_URL}/plugins") - plugins = response.json() - display_plugins(plugins) - - -@plugin_app.command("create") -def create(name: str, webhook_url: str, description: str = ""): - response = httpx.post( - f"{BASE_URL}/plugins", - json={"name": name, "description": description, "webhook_url": webhook_url}, - ) - if 200 <= response.status_code < 300: - print("Plugin created successfully") - else: - print(f"Failed to create plugin: {response.status_code} - {response.text}") - - -@plugin_app.command("bind") -def bind( - library_id: int = typer.Option(..., "--lib", help="ID of the library"), - plugin: str = typer.Option(..., "--plugin", help="ID or name of the plugin"), -): - try: - plugin_id = int(plugin) - plugin_param = {"plugin_id": plugin_id} - except ValueError: - plugin_param = {"plugin_name": plugin} - - response = httpx.post( - f"{BASE_URL}/libraries/{library_id}/plugins", - json=plugin_param, - ) - if response.status_code == 204: - print("Plugin bound to library successfully") - else: - print( - f"Failed to bind plugin to library: {response.status_code} - {response.text}" - ) - - -@plugin_app.command("unbind") -def unbind( - library_id: int = typer.Option(..., "--lib", help="ID of the library"), - plugin_id: int = typer.Option(..., "--plugin", help="ID of the plugin"), -): - response = httpx.delete( - f"{BASE_URL}/libraries/{library_id}/plugins/{plugin_id}", - ) - if response.status_code == 204: - print("Plugin unbound from library successfully") - else: - print( - f"Failed to unbind plugin from library: {response.status_code} - {response.text}" - ) @app.command() @@ -1065,4 +993,4 @@ def disable(): if __name__ == "__main__": - app() + app() \ No newline at end of file