From e5915bc4935838f29bcd8889819e5fd267d5c613 Mon Sep 17 00:00:00 2001 From: Eneko Illarramendi Date: Tue, 18 Feb 2020 20:27:04 +0100 Subject: [PATCH] refactor: add a simple config file to extensions It makes possible to automagically keep the extensions admin page updated. --- lnbits/__init__.py | 3 +- lnbits/db.py | 5 +- lnbits/extensions/withdraw/config.json | 5 + lnbits/helpers.py | 35 ++++ lnbits/templates/extensions.html | 250 ++++++++++--------------- 5 files changed, 147 insertions(+), 151 deletions(-) create mode 100644 lnbits/extensions/withdraw/config.json diff --git a/lnbits/__init__.py b/lnbits/__init__.py index e2cfc219..2bb0b571 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 a43c92c5..42405a6a 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 00000000..b34bb107 --- /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 eca91a64..f95dbd77 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 2c3b976a..308e46e4 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 %}