2019-12-14 21:26:26 +01:00
|
|
|
import json
|
2020-02-18 20:27:04 +01:00
|
|
|
import os
|
2019-12-14 21:26:26 +01:00
|
|
|
import sqlite3
|
|
|
|
|
2020-02-18 20:27:04 +01:00
|
|
|
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
|
|
|
|
|
2019-12-14 21:26:26 +01:00
|
|
|
|
|
|
|
class MegaEncoder(json.JSONEncoder):
|
2020-02-10 13:29:20 +01:00
|
|
|
def default(self, obj):
|
|
|
|
if isinstance(obj, sqlite3.Row):
|
|
|
|
return {k: obj[k] for k in obj.keys()}
|
2020-02-05 12:47:17 +01:00
|
|
|
return obj
|
2019-12-14 21:26:26 +01:00
|
|
|
|
|
|
|
|
2020-01-31 21:07:05 +01:00
|
|
|
def megajson(obj):
|
|
|
|
return json.dumps(obj, cls=MegaEncoder)
|