2019-12-14 21:26:26 +01:00
|
|
|
import json
|
2020-02-18 20:27:04 +01:00
|
|
|
import os
|
2020-09-15 20:54:05 +02:00
|
|
|
import glob
|
2020-08-31 04:19:43 +02:00
|
|
|
import shortuuid # type: ignore
|
2019-12-14 21:26:26 +01:00
|
|
|
|
2020-03-04 23:11:15 +01:00
|
|
|
from typing import List, NamedTuple, Optional
|
2020-02-18 20:27:04 +01:00
|
|
|
|
2020-09-05 08:00:44 +02:00
|
|
|
from .settings import LNBITS_DISABLED_EXTENSIONS, LNBITS_PATH
|
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
|
|
|
|
|
|
|
|
|
2020-02-18 20:27:04 +01:00
|
|
|
class ExtensionManager:
|
2020-09-05 08:00:44 +02:00
|
|
|
def __init__(self):
|
|
|
|
self._disabled: List[str] = LNBITS_DISABLED_EXTENSIONS
|
2021-03-24 04:40:32 +01:00
|
|
|
self._extension_folders: List[str] = [
|
|
|
|
x[1] for x in os.walk(os.path.join(LNBITS_PATH, "extensions"))
|
|
|
|
][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-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(
|
|
|
|
os.path.join(LNBITS_PATH, "extensions", extension, "config.json")
|
|
|
|
) 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"),
|
|
|
|
)
|
|
|
|
)
|
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(
|
|
|
|
os.path.join(LNBITS_PATH, "static/vendor/**"), recursive=True
|
|
|
|
):
|
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:
|
|
|
|
return "/" + os.path.relpath(abspath, LNBITS_PATH)
|