Implemented conditionally included applet resource for standalone mode

Update excel-password-remover.yml, png-chunk-analyser.yml, and 6 more files...
This commit is contained in:
2025-02-26 21:24:16 +01:00
parent 65db2dea5a
commit a76ab59543
8 changed files with 131 additions and 34 deletions

View File

@@ -1,8 +1,9 @@
applets: applets:
- id: "excel-password-remover" - id: "excel-password-remover"
resources: resources:
scripts: scripts:
- "excel-password-remover.mjs" - "https://cdn.nibblepoker.lu/JSZip/v3.10.1/jszip.min.js"
- "standalone://jszip.min.js"
- "applet://excel-password-remover.mjs"
stylesheets: stylesheets:
- "excel-password-remover.css" - "applet://excel-password-remover.css"

View File

@@ -1,8 +1,7 @@
applets: applets:
- id: "png-chunk-analyser" - id: "png-chunk-analyser"
resources: resources:
scripts: scripts:
- "png-chunk-analyser.mjs" - "applet://png-chunk-analyser.mjs"
stylesheets: stylesheets:
- "png-chunk-analyser.css" - "applet://png-chunk-analyser.css"

View File

@@ -1,8 +1,7 @@
applets: applets:
- id: "png-optimizer" - id: "png-optimizer"
resources: resources:
scripts: scripts:
- "png-optimizer.mjs" - "applet://png-optimizer.mjs"
stylesheets: stylesheets:
- "png-optimizer.css" - "applet://png-optimizer.css"

View File

@@ -1,8 +1,7 @@
applets: applets:
- id: "web-usb-test" - id: "web-usb-test"
resources: resources:
scripts: scripts:
- "web-usb-test.mjs" - "applet://web-usb-test.mjs"
stylesheets: stylesheets:
- "web-usb-test.css" - "applet://web-usb-test.css"

View File

@@ -1,8 +1,7 @@
applets: applets:
- id: "uuid-generator" - id: "uuid-generator"
resources: resources:
scripts: scripts:
- "uuid-generator.mjs" - "applet://uuid-generator.mjs"
stylesheets: stylesheets:
- "uuid-generator.css" - "applet://uuid-generator.css"

View File

@@ -1,4 +1,3 @@
const excelFileRegex = /^.*\.xls[xm]$/gi; const excelFileRegex = /^.*\.xls[xm]$/gi;
const excelWorksheetRegex = /^xl\/worksheets\/.*.xml$/gi; const excelWorksheetRegex = /^xl\/worksheets\/.*.xml$/gi;

View File

@@ -1,5 +1,7 @@
from dataclasses import dataclass, field from dataclasses import dataclass, field
from enum import Enum
from typing import Optional from typing import Optional
from urllib.parse import ParseResult, urlparse
from locked_dict.locked_dict import LockedDict from locked_dict.locked_dict import LockedDict
@@ -70,10 +72,59 @@ class ContentMetadata:
self.general = ContentGeneralMetadata(**self.general) self.general = ContentGeneralMetadata(**self.general)
class ContentResourceType(Enum):
UNKNOWN = []
REMOTE = ["http", "https"]
APPLET = ["applet"]
STANDALONE = ["standalone", "stand"]
class ContentResourceDefinition:
raw_uri: str
resource_type: ContentResourceType
resource_uri: ParseResult
def __init__(self, raw_uri):
self.raw_uri = raw_uri
self.resource_uri = urlparse(self.raw_uri)
self.resource_type = ContentResourceType.UNKNOWN
for content_resource_type in ContentResourceType:
if self.resource_uri.scheme in content_resource_type.value:
self.resource_type = content_resource_type
def can_be_standalone(self):
return self.resource_type in [ContentResourceType.STANDALONE, ContentResourceType.APPLET]
def is_standalone(self):
return self.resource_type == ContentResourceType.STANDALONE
def is_remote(self):
return self.resource_type == ContentResourceType.REMOTE
def is_applet(self):
return self.resource_type == ContentResourceType.APPLET
def get_clean_path(self):
return self.raw_uri.replace(f"{self.resource_uri.scheme}://", "")
def __repr__(self):
return (f"ContentResourceDefinition(raw_uri: {self.raw_uri}, "
f"resource_type: {self.resource_type}, "
f"resource_uri: {self.resource_uri})")
@dataclass @dataclass
class ContentResource: class ContentResource:
scripts: list[str] = field(default_factory=list) scripts: list[ContentResourceDefinition] = field(default_factory=list)
stylesheets: list[str] = field(default_factory=list) stylesheets: list[ContentResourceDefinition] = field(default_factory=list)
def __post_init__(self):
self.scripts: list[str]
self.stylesheets: list[str]
self.scripts = [ContentResourceDefinition(x) for x in self.scripts]
self.stylesheets = [ContentResourceDefinition(x) for x in self.stylesheets]
@dataclass @dataclass
@@ -84,6 +135,7 @@ class ContentApplet:
def __post_init__(self): def __post_init__(self):
self.resources: dict self.resources: dict
self.resources = ContentResource(**self.resources) self.resources = ContentResource(**self.resources)
# print(self.resources)
@dataclass @dataclass

View File

@@ -9,15 +9,39 @@ def render_applet_head(applet_data: ContentApplet, is_standalone: bool = False)
applet_style_html = "" applet_style_html = ""
for applet_style in applet_data.resources.stylesheets: for applet_style in applet_data.resources.stylesheets:
rsc_path = None
if is_standalone: if is_standalone:
with open(os.path.join("./static/resources/NibblePoker/applets/", applet_data.id, applet_style)) as applet_style_file: if not applet_style.can_be_standalone():
applet_style_html += "<style>" + applet_style_file.read() + "</style>" continue
if applet_style.is_applet():
rsc_path = os.path.join(
"./static/resources/NibblePoker/applets/",
applet_data.id,
applet_style.get_clean_path())
print(applet_style)
elif applet_style.is_standalone():
rsc_path = os.path.join(
"./static/resources/Standalone/",
applet_style.get_clean_path())
if rsc_path is not None:
with open(rsc_path) as applet_style_file:
applet_style_html += "<style>" + applet_style_file.read() + "</style>"
else: else:
applet_style_html += ("<link rel='stylesheet' href='" + if applet_style.is_applet():
url_for( rsc_path = url_for(
"static", "static",
filename="/resources/NibblePoker/applets/" + applet_data.id + "/" + applet_style) + filename="/resources/NibblePoker/applets/" +
"'>") applet_data.id + "/" +
applet_style.get_clean_path())
elif applet_style.is_remote():
rsc_path = applet_style.raw_uri
if rsc_path is not None:
applet_style_html += f"<link rel='stylesheet' href='{rsc_path}'>"
return applet_style_html return applet_style_html
@@ -26,16 +50,41 @@ def render_applet_scripts(applet_data: ContentApplet, is_standalone: bool = Fals
applet_script_html = "" applet_script_html = ""
for applet_script in applet_data.resources.scripts: for applet_script in applet_data.resources.scripts:
rsc_path = None
if is_standalone: if is_standalone:
with open(os.path.join("./static/resources/NibblePoker/applets/", applet_data.id, applet_script)) as applet_script_file: if not applet_script.can_be_standalone():
applet_script_html += "<script" + (" type='module'>" if applet_script.endswith(".mjs") else ">") continue
applet_script_html += applet_script_file.read()
applet_script_html += "</script>" if applet_script.is_applet():
rsc_path = os.path.join(
"./static/resources/NibblePoker/applets/",
applet_data.id,
applet_script.get_clean_path())
elif applet_script.is_standalone():
rsc_path = os.path.join(
"./static/resources/Standalone/",
applet_script.get_clean_path())
if rsc_path is not None:
with open(rsc_path) as applet_script_file:
applet_script_html += "<script" +(" type='module'>" if applet_script.raw_uri.endswith(".mjs") else ">")
applet_script_html += applet_script_file.read()
applet_script_html += "</script>"
else: else:
applet_script_html += ("<script src='" + if applet_script.is_applet():
url_for( rsc_path = url_for(
"static", "static",
filename="/resources/NibblePoker/applets/" + applet_data.id + "/" + applet_script) + filename="/resources/NibblePoker/applets/" +
"'" + (" type='module'" if applet_script.endswith(".mjs") else "") + "></script>") applet_data.id + "/" +
applet_script.get_clean_path())
elif applet_script.is_remote():
rsc_path = applet_script.raw_uri
if rsc_path is not None:
applet_script_html += f"<script src='{rsc_path}'{
" type='module'" if applet_script.raw_uri.endswith(".mjs") else ""
}></script>"
return applet_script_html return applet_script_html