refactor: add a simple config file to extensions

It makes possible to automagically keep the extensions admin page updated.
This commit is contained in:
Eneko Illarramendi 2020-02-18 20:27:04 +01:00
parent 792b787f1a
commit e5915bc493
5 changed files with 147 additions and 151 deletions

View File

@ -10,7 +10,7 @@ from . import bolt11
from .core import core_app from .core import core_app
from .db import init_databases, open_db from .db import init_databases, open_db
from .extensions.withdraw import withdraw_ext 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 from .settings import WALLET, DEFAULT_USER_WALLET_NAME, FEE_RESERVE
@ -33,6 +33,7 @@ Talisman(
) )
# filters # filters
app.jinja_env.globals["EXTENSIONS"] = [ext for ext in ExtensionManager().extensions if ext.is_valid]
app.jinja_env.filters["megajson"] = megajson app.jinja_env.filters["megajson"] = megajson
# blueprints # blueprints

View File

@ -1,6 +1,7 @@
import os import os
import sqlite3 import sqlite3
from .helpers import ExtensionManager
from .settings import LNBITS_PATH, LNBITS_DATA_FOLDER 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")), ("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]: for extension in ExtensionManager().extensions:
schemas.append((f"ext_{extension}", os.path.join(LNBITS_PATH, "extensions", extension, "schema.sql"))) 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])]: for schema in [s for s in schemas if os.path.exists(s[1])]:
with open_db(schema[0]) as db: with open_db(schema[0]) as db:

View File

@ -0,0 +1,5 @@
{
"name": "LNURLw",
"short_description": "Make LNURL withdraw links.",
"ion_icon": "beer"
}

View File

@ -1,6 +1,41 @@
import json import json
import os
import sqlite3 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): class MegaEncoder(json.JSONEncoder):
def default(self, obj): def default(self, obj):

View File

@ -1,6 +1,9 @@
<!-- @format --> <!-- @format -->
{% extends "base.html" %} {% block messages %} {% extends "base.html" %}
{% block messages %}
<a href="#" class="dropdown-toggle" data-toggle="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-bell-o"></i> <i class="fa fa-bell-o"></i>
<span class="label label-danger">!</span> <span class="label label-danger">!</span>
@ -9,7 +12,9 @@
<li class="header"><b>Instant wallet, bookmark to save</b></li> <li class="header"><b>Instant wallet, bookmark to save</b></li>
<li></li> <li></li>
</ul> </ul>
{% endblock %} {% block menuitems %} {% endblock %}
{% block menuitems %}
<li class="treeview"> <li class="treeview">
<a href="#"> <a href="#">
<i class="fa fa-bitcoin"></i> <span>Wallets</span> <i class="fa fa-bitcoin"></i> <span>Wallets</span>
@ -18,7 +23,7 @@
<ul class="treeview-menu"> <ul class="treeview-menu">
{% for w in user_wallets %} {% for w in user_wallets %}
<li> <li>
<a href="wallet?wal={{ w.id }}&usr={{ w.user }}" <a href="{{ url_for('wallet') }}?wal={{ w.id }}&usr={{ w.user }}"
><i class="fa fa-bolt"></i> {{ w.name }}</a ><i class="fa fa-bolt"></i> {{ w.name }}</a
> >
</li> </li>
@ -27,68 +32,45 @@
<div id="sidebarmake"></div> <div id="sidebarmake"></div>
</ul> </ul>
</li> </li>
<li class="active treeview"> <li class="active treeview">
<a href="#"> <a href="#">
<i class="fa fa-th"></i> <span>Extensions</span> <i class="fa fa-th"></i> <span>Extensions</span>
<i class="fa fa-angle-left pull-right"></i> <i class="fa fa-angle-left pull-right"></i>
</a> </a>
<ul class="treeview-menu"> <ul class="treeview-menu">
{% for extension in EXTENSIONS %}
{% if extension.code in user_ext %}
{% if "lnevents" in user_ext %}
<li> <li>
<a href="lnevents?usr={{ user }}" <a href="{{ url_for(extension.code + '.index') }}?usr={{ user }}"><i class="fa fa-plus"></i> {{ extension.name }}</a>
><i class="fa fa-plus"></i> LNEvents</a>
</li> </li>
{% endif %} {% endif %}
{% endfor %}
{% if "lnjoust" in user_ext %}
<li> <li>
<a href="lnjoust?usr={{ user }}" <a href="{{ url_for('extensions') }}?usr={{ user }}">Manager </a></li>
><i class="fa fa-plus"></i> LNJoust</a>
</li>
{% endif %}
{% if "withdraw" in user_ext %}
<li>
<a href="withdraw?usr={{ user }}"
><i class="fa fa-plus"></i> LNURLw</a>
</li>
{% endif %}
<li>
<a href="extensions?usr={{ user }}"
>Manager </a>
</li>
</ul> </ul>
</li> </li>
{% endblock %}
{% endblock %} {% block body %} {% block body %}
<!-- Right side column. Contains the navbar and content of the page --> <!-- Right side column. Contains the navbar and content of the page -->
<div class="content-wrapper"> <div class="content-wrapper">
<!-- Content Header (Page header) --> <!-- Content Header (Page header) -->
<section class="content-header"> <section class="content-header">
<h1> <div id="wonga"></div>
Wallet <h1>Wallet <small>Control panel</small></h1>
<small
>Control panel
<div id="wonga"></div
></small>
</h1>
<ol class="breadcrumb"> <ol class="breadcrumb">
<li> <li>
<a href="#"><i class="fa fa-dashboard"></i> Home</a> <a href="#"><i class="fa fa-dashboard"></i> Home</a>
</li> </li>
<li class="active">Extensions</li> <li class="active">Extensions</li>
</ol> </ol>
<br /><br />
<br />
<br />
<div class="alert alert-danger alert-dismissable"> <div class="alert alert-danger alert-dismissable">
<h4> <h4>Bookmark to save your wallet. Wallet is in BETA, use with caution.</h4>
Bookmark to save your wallet. Wallet is in BETA, use with caution.
</h4>
</div> </div>
</section> </section>
@ -97,68 +79,40 @@
<!-- Small boxes (Stat box) --> <!-- Small boxes (Stat box) -->
<div class="row"> <div class="row">
{% if "withdraw" not in user_ext %} {% for extension in EXTENSIONS %}
<div class="col-lg-3 col-xs-6"> <div class="col-lg-3 col-xs-6">
<!-- small box --> <!-- small box -->
<div class="small-box bg-blue"> <div class="small-box bg-blue">
<div class="inner"> <div class="inner">
<h3> {% if extension.code in user_ext %}
LNURLw <a href="{{ url_for(extension.code + '.index') }}?usr={{ user }}" style="color: inherit">
</h3> {% endif %}
<p> <h3>{{ extension.name }}</h3>
Make LNURL withdraw links <p>{{ extension.short_description }}</p>
</p> {% if extension.code in user_ext %}
</div>
<div class="icon">
<i class="ion ion-beer"></i>
</div>
<a href="extensions?usr={{user}}&enable=withdraw" class="small-box-footer">
Activate <i class="fa fa-arrow-circle-right"></i>
</a> </a>
</div>
</div><!-- ./col -->
{% else %}
<div class="col-lg-3 col-xs-6">
<!-- small box -->
<div class="small-box bg-blue">
<div class="inner">
<a href="withdraw?usr={{user}}" style="color: inherit;">
<h3>
LNURLw
</h3>
<p>
Make LNURL withdraw links
</p>
</div>
<div class="icon">
<i class="ion ion-beer"></i>
</div>
</a>
<a href="extensions?usr={{user}}&disable=withdraw" class="small-box-footer">
Deactivate <i class="fa fa-arrow-circle-right"></i>
</a>
</div>
</div><!-- ./col -->
{% endif %} {% endif %}
</div> </div>
<div class="icon">
<i class="ion ion-{{ extension.ion_icon }}"></i>
</div>
{% if extension.code in user_ext %}
<a href="{{ url_for('extensions') }}?usr={{user}}&disable={{ extension.code }}" class="small-box-footer">Disable <i class="fa fa-arrow-circle-right"></i></a>
{% else %}
<a href="{{ url_for('extensions') }}?usr={{user}}&enable={{ extension.code }}" class="small-box-footer">Enable <i class="fa fa-arrow-circle-right"></i></a>
{% endif %}
</div>
</div>
{% endfor %}
</div>
<!-- /.content --> <!-- /.content -->
</section> </section>
</div>
<script> <script>
window.user = {{ user | megajson | safe }} window.user = {{ user | megajson | safe }}
window.user_wallets = {{ user_wallets | megajson | safe }} window.user_wallets = {{ user_wallets | megajson | safe }}
window.user_ext = {{ user_ext | megajson | safe }} window.user_ext = {{ user_ext | megajson | safe }}
</script> </script>
</div>
{% endblock %} {% endblock %}