2021-08-27 20:54:42 +02:00
|
|
|
import glob
|
2019-12-14 21:26:26 +01:00
|
|
|
import json
|
2020-02-18 20:27:04 +01:00
|
|
|
import os
|
2021-08-27 20:54:42 +02:00
|
|
|
from typing import Any, List, NamedTuple, Optional
|
|
|
|
|
|
|
|
import jinja2
|
2020-08-31 04:19:43 +02:00
|
|
|
import shortuuid # type: ignore
|
2019-12-14 21:26:26 +01:00
|
|
|
|
2021-08-27 20:54:42 +02:00
|
|
|
from lnbits.jinja2_templating import Jinja2Templates
|
|
|
|
from lnbits.requestvars import g
|
2020-02-18 20:27:04 +01:00
|
|
|
|
2021-08-28 11:16:59 +02:00
|
|
|
import lnbits.settings as settings
|
2020-02-18 20:27:04 +01:00
|
|
|
|
|
|
|
|
2020-03-04 23:11:15 +01:00
|
|
|
class Extension(NamedTuple):
|
|
|
|
code: str
|
|
|
|
is_valid: bool
|
|
|
|
name: Optional[str] = None
|
|
|
|
short_description: Optional[str] = None
|
|
|
|
icon: Optional[str] = None
|
|
|
|
contributors: Optional[List[str]] = None
|
2021-03-28 03:10:24 +02:00
|
|
|
hidden: bool = False
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
|
2020-02-18 20:27:04 +01:00
|
|
|
class ExtensionManager:
|
2020-09-05 08:00:44 +02:00
|
|
|
def __init__(self):
|
2021-08-28 11:16:59 +02:00
|
|
|
self._disabled: List[str] = settings.LNBITS_DISABLED_EXTENSIONS
|
2021-03-24 04:40:32 +01:00
|
|
|
self._extension_folders: List[str] = [
|
2021-08-28 11:16:59 +02:00
|
|
|
x[1] for x in os.walk(os.path.join(settings.LNBITS_PATH, "extensions"))
|
2021-03-24 04:40:32 +01:00
|
|
|
][0]
|
2020-02-18 20:27:04 +01:00
|
|
|
|
|
|
|
@property
|
2020-03-04 23:11:15 +01:00
|
|
|
def extensions(self) -> List[Extension]:
|
2020-02-18 20:27:04 +01:00
|
|
|
output = []
|
|
|
|
|
2021-07-05 11:02:36 +02:00
|
|
|
if "all" in self._disabled:
|
|
|
|
return output
|
|
|
|
|
2021-03-24 04:40:32 +01:00
|
|
|
for extension in [
|
|
|
|
ext for ext in self._extension_folders if ext not in self._disabled
|
|
|
|
]:
|
2020-02-18 20:27:04 +01:00
|
|
|
try:
|
2021-03-24 04:40:32 +01:00
|
|
|
with open(
|
2021-08-28 11:16:59 +02:00
|
|
|
os.path.join(settings.LNBITS_PATH, "extensions", extension, "config.json")
|
2021-03-24 04:40:32 +01:00
|
|
|
) as json_file:
|
2020-02-18 20:27:04 +01:00
|
|
|
config = json.load(json_file)
|
|
|
|
is_valid = True
|
|
|
|
except Exception:
|
|
|
|
config = {}
|
|
|
|
is_valid = False
|
|
|
|
|
2020-08-31 04:19:43 +02:00
|
|
|
output.append(
|
|
|
|
Extension(
|
|
|
|
extension,
|
|
|
|
is_valid,
|
|
|
|
config.get("name"),
|
|
|
|
config.get("short_description"),
|
|
|
|
config.get("icon"),
|
|
|
|
config.get("contributors"),
|
2021-03-28 03:10:24 +02:00
|
|
|
config.get("hidden") or False,
|
2020-08-31 04:19:43 +02:00
|
|
|
)
|
|
|
|
)
|
2020-02-18 20:27:04 +01:00
|
|
|
|
|
|
|
return output
|
|
|
|
|
2019-12-14 21:26:26 +01:00
|
|
|
|
2020-09-05 08:00:44 +02:00
|
|
|
def get_valid_extensions() -> List[Extension]:
|
2021-03-24 04:40:32 +01:00
|
|
|
return [
|
|
|
|
extension for extension in ExtensionManager().extensions if extension.is_valid
|
|
|
|
]
|
2020-09-05 08:00:44 +02:00
|
|
|
|
|
|
|
|
2020-04-16 17:10:53 +02:00
|
|
|
def urlsafe_short_hash() -> str:
|
|
|
|
return shortuuid.uuid()
|
2020-09-15 20:54:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_js_vendored(prefer_minified: bool = False) -> List[str]:
|
|
|
|
paths = get_vendored(".js", prefer_minified)
|
|
|
|
|
|
|
|
def sorter(key: str):
|
|
|
|
if "moment@" in key:
|
|
|
|
return 1
|
|
|
|
if "vue@" in key:
|
|
|
|
return 2
|
|
|
|
if "vue-router@" in key:
|
|
|
|
return 3
|
|
|
|
if "polyfills" in key:
|
|
|
|
return 4
|
|
|
|
return 9
|
|
|
|
|
|
|
|
return sorted(paths, key=sorter)
|
|
|
|
|
|
|
|
|
|
|
|
def get_css_vendored(prefer_minified: bool = False) -> List[str]:
|
2020-10-17 20:24:22 +02:00
|
|
|
paths = get_vendored(".css", prefer_minified)
|
|
|
|
|
|
|
|
def sorter(key: str):
|
|
|
|
if "quasar@" in key:
|
|
|
|
return 1
|
|
|
|
if "vue@" in key:
|
|
|
|
return 2
|
|
|
|
if "chart.js@" in key:
|
|
|
|
return 100
|
|
|
|
return 9
|
|
|
|
|
|
|
|
return sorted(paths, key=sorter)
|
2020-09-15 20:54:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_vendored(ext: str, prefer_minified: bool = False) -> List[str]:
|
|
|
|
paths: List[str] = []
|
2021-03-24 04:40:32 +01:00
|
|
|
for path in glob.glob(
|
2021-08-28 11:16:59 +02:00
|
|
|
os.path.join(settings.LNBITS_PATH, "static/vendor/**"), recursive=True
|
2021-03-24 04:40:32 +01:00
|
|
|
):
|
2020-09-15 20:54:05 +02:00
|
|
|
if path.endswith(".min" + ext):
|
|
|
|
# path is minified
|
|
|
|
unminified = path.replace(".min" + ext, ext)
|
|
|
|
if prefer_minified:
|
|
|
|
paths.append(path)
|
|
|
|
if unminified in paths:
|
|
|
|
paths.remove(unminified)
|
|
|
|
elif unminified not in paths:
|
|
|
|
paths.append(path)
|
|
|
|
|
|
|
|
elif path.endswith(ext):
|
|
|
|
# path is not minified
|
|
|
|
minified = path.replace(ext, ".min" + ext)
|
|
|
|
if not prefer_minified:
|
|
|
|
paths.append(path)
|
|
|
|
if minified in paths:
|
|
|
|
paths.remove(minified)
|
|
|
|
elif minified not in paths:
|
|
|
|
paths.append(path)
|
|
|
|
|
2020-10-09 03:10:17 +02:00
|
|
|
return sorted(paths)
|
2020-09-15 20:54:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
def url_for_vendored(abspath: str) -> str:
|
2021-08-28 11:16:59 +02:00
|
|
|
return "/" + os.path.relpath(abspath, settings.LNBITS_PATH)
|
2021-08-27 20:54:42 +02:00
|
|
|
|
|
|
|
def url_for(
|
|
|
|
endpoint: str,
|
|
|
|
external: Optional[bool] = False,
|
|
|
|
**params: Any,
|
|
|
|
) -> str:
|
|
|
|
base = g().base_url if external else ""
|
|
|
|
url_params = "?"
|
|
|
|
for key in params:
|
|
|
|
url_params += f"{key}={params[key]}&"
|
|
|
|
url = f"{base}{endpoint}{url_params}"
|
|
|
|
return url
|
|
|
|
|
|
|
|
def template_renderer() -> Jinja2Templates:
|
|
|
|
t = Jinja2Templates(
|
|
|
|
loader=jinja2.FileSystemLoader(["lnbits/templates", "lnbits/core/templates"]),
|
|
|
|
)
|
2021-08-28 11:16:59 +02:00
|
|
|
t.env.globals["SITE_TITLE"] = settings.LNBITS_SITE_TITLE
|
|
|
|
t.env.globals["SITE_TAGLINE"] = settings.LNBITS_SITE_TAGLINE
|
|
|
|
t.env.globals["SITE_DESCRIPTION"] = settings.LNBITS_SITE_DESCRIPTION
|
|
|
|
t.env.globals["LNBITS_THEME_OPTIONS"] = settings.LNBITS_THEME_OPTIONS
|
|
|
|
t.env.globals["LNBITS_VERSION"] = settings.LNBITS_COMMIT
|
2021-08-27 20:54:42 +02:00
|
|
|
t.env.globals["EXTENSIONS"] = get_valid_extensions()
|
|
|
|
|
2021-08-28 11:16:59 +02:00
|
|
|
if settings.DEBUG:
|
2021-08-27 20:54:42 +02:00
|
|
|
t.env.globals["VENDORED_JS"] = map(url_for_vendored, get_js_vendored())
|
|
|
|
t.env.globals["VENDORED_CSS"] = map(url_for_vendored, get_css_vendored())
|
|
|
|
else:
|
|
|
|
t.env.globals["VENDORED_JS"] = ["/static/bundle.js"]
|
|
|
|
t.env.globals["VENDORED_CSS"] = ["/static/bundle.css"]
|
|
|
|
|
|
|
|
return t
|