diff --git a/lnbits/__init__.py b/lnbits/__init__.py index e2cfc2192..2bb0b5714 100644 --- a/lnbits/__init__.py +++ b/lnbits/__init__.py @@ -10,7 +10,7 @@ from . import bolt11 from .core import core_app from .db import init_databases, open_db from .extensions.withdraw import withdraw_ext -from .helpers import megajson +from .helpers import ExtensionManager, megajson from .settings import WALLET, DEFAULT_USER_WALLET_NAME, FEE_RESERVE @@ -33,6 +33,7 @@ Talisman( ) # filters +app.jinja_env.globals["EXTENSIONS"] = [ext for ext in ExtensionManager().extensions if ext.is_valid] app.jinja_env.filters["megajson"] = megajson # blueprints diff --git a/lnbits/db.py b/lnbits/db.py index a43c92c52..42405a6a8 100644 --- a/lnbits/db.py +++ b/lnbits/db.py @@ -1,6 +1,7 @@ import os import sqlite3 +from .helpers import ExtensionManager from .settings import LNBITS_PATH, LNBITS_DATA_FOLDER @@ -50,8 +51,8 @@ def init_databases() -> None: ("database", os.path.join(LNBITS_PATH, "data", "schema.sql")), ] - for extension in [x[1] for x in os.walk(os.path.join(LNBITS_PATH, "extensions"))][0]: - schemas.append((f"ext_{extension}", os.path.join(LNBITS_PATH, "extensions", extension, "schema.sql"))) + for extension in ExtensionManager().extensions: + schemas.append((f"ext_{extension.code}", os.path.join(extension.path, "schema.sql"))) for schema in [s for s in schemas if os.path.exists(s[1])]: with open_db(schema[0]) as db: diff --git a/lnbits/extensions/withdraw/config.json b/lnbits/extensions/withdraw/config.json new file mode 100644 index 000000000..b34bb1076 --- /dev/null +++ b/lnbits/extensions/withdraw/config.json @@ -0,0 +1,5 @@ +{ + "name": "LNURLw", + "short_description": "Make LNURL withdraw links.", + "ion_icon": "beer" +} diff --git a/lnbits/helpers.py b/lnbits/helpers.py index eca91a643..f95dbd776 100644 --- a/lnbits/helpers.py +++ b/lnbits/helpers.py @@ -1,6 +1,41 @@ import json +import os import sqlite3 +from types import SimpleNamespace +from typing import List + +from .settings import LNBITS_PATH + + +class ExtensionManager: + def __init__(self): + self._extension_folders: List[str] = [x[1] for x in os.walk(os.path.join(LNBITS_PATH, "extensions"))][0] + + @property + def extensions(self) -> List[SimpleNamespace]: + output = [] + + for extension in self._extension_folders: + try: + with open(os.path.join(LNBITS_PATH, "extensions", extension, "config.json")) as json_file: + config = json.load(json_file) + is_valid = True + except Exception: + config = {} + is_valid = False + + output.append(SimpleNamespace(**{ + **{ + "code": extension, + "is_valid": is_valid, + "path": os.path.join(LNBITS_PATH, "extensions", extension), + }, + **config + })) + + return output + class MegaEncoder(json.JSONEncoder): def default(self, obj): diff --git a/lnbits/templates/extensions.html b/lnbits/templates/extensions.html index 2c3b976a7..308e46e43 100644 --- a/lnbits/templates/extensions.html +++ b/lnbits/templates/extensions.html @@ -1,164 +1,118 @@ -{% extends "base.html" %} {% block messages %} - - - ! - - -{% endblock %} {% block menuitems %} -
  • - - Wallets - - - -
  • - -
  • - - Extensions - - - -
  • - -{% endblock %} {% block body %} +{% block body %}
    - -
    -

    - Wallet - Control panel -
    -

    - -

    -
    -

    - Bookmark to save your wallet. Wallet is in BETA, use with caution. -

    -
    -
    + +
    +
    +

    Wallet Control panel

    + - -
    - -
    +
    +
    - {% if "withdraw" not in user_ext %} -
    - -
    -
    -

    - LNURLw -

    -

    - Make LNURL withdraw links -

    -
    -
    - -
    - - Activate - -
    -
    - {% else %} +
    +

    Bookmark to save your wallet. Wallet is in BETA, use with caution.

    +
    +
    -
    - + +
    + +
    -
    + {% for extension in EXTENSIONS %} +
    + +
    + +
    + +
    + {% if extension.code in user_ext %} + Disable + {% else %} + Enable + {% endif %} +
    +
    + {% endfor %} - -
    - - - -
    -
    - - Deactivate - - -
    - -
    - - {% endif %} -
    - - -
    - - +
    + + + + {% endblock %}