finalize changes for native apps.
This commit is contained in:
parent
e36552e336
commit
ef2094f140
5 changed files with 77 additions and 53 deletions
1
.vscode/settings.json
vendored
1
.vscode/settings.json
vendored
|
|
@ -52,6 +52,7 @@
|
||||||
"matroska",
|
"matroska",
|
||||||
"mbed",
|
"mbed",
|
||||||
"mccabe",
|
"mccabe",
|
||||||
|
"MEIPASS",
|
||||||
"Microformat",
|
"Microformat",
|
||||||
"microformats",
|
"microformats",
|
||||||
"mkvtoolsnix",
|
"mkvtoolsnix",
|
||||||
|
|
|
||||||
5
app.spec
5
app.spec
|
|
@ -31,6 +31,7 @@ hidden = [
|
||||||
"engineio",
|
"engineio",
|
||||||
"engineio.async_drivers.aiohttp",
|
"engineio.async_drivers.aiohttp",
|
||||||
"socketio.async_drivers.aiohttp",
|
"socketio.async_drivers.aiohttp",
|
||||||
|
"app"
|
||||||
]
|
]
|
||||||
|
|
||||||
hidden = [f.replace("-", "_") for f in hidden]
|
hidden = [f.replace("-", "_") for f in hidden]
|
||||||
|
|
@ -41,7 +42,7 @@ a = Analysis( # noqa: F821 # type: ignore
|
||||||
binaries=binaries,
|
binaries=binaries,
|
||||||
datas=[
|
datas=[
|
||||||
("ui/exported", "ui/exported"),
|
("ui/exported", "ui/exported"),
|
||||||
("app/migrations", "migrations"),
|
("app/", "app/"),
|
||||||
],
|
],
|
||||||
hiddenimports=hidden,
|
hiddenimports=hidden,
|
||||||
hookspath=[],
|
hookspath=[],
|
||||||
|
|
@ -62,7 +63,7 @@ exe = EXE( # type: ignore # noqa: F821
|
||||||
debug=False,
|
debug=False,
|
||||||
strip=False,
|
strip=False,
|
||||||
upx=True,
|
upx=True,
|
||||||
console=False,
|
console=True, # Turn on to True if you want a console window for debugging.
|
||||||
icon="ui/public/favicon.ico",
|
icon="ui/public/favicon.ico",
|
||||||
onefile=True,
|
onefile=True,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -94,8 +94,7 @@ def calc_download_path(base_path: str | Path, folder: str | None = None, create_
|
||||||
if not folder:
|
if not folder:
|
||||||
return str(base_path)
|
return str(base_path)
|
||||||
|
|
||||||
if folder.startswith("/"):
|
folder = folder.removeprefix("/")
|
||||||
folder = folder[1:]
|
|
||||||
|
|
||||||
realBasePath = base_path.resolve()
|
realBasePath = base_path.resolve()
|
||||||
download_path = Path(realBasePath).joinpath(folder).resolve(strict=False)
|
download_path = Path(realBasePath).joinpath(folder).resolve(strict=False)
|
||||||
|
|
@ -179,8 +178,7 @@ def extract_info(
|
||||||
else:
|
else:
|
||||||
log_wrapper.add_target(target=params["callback"], level=logging.ERROR, name="callback")
|
log_wrapper.add_target(target=params["callback"], level=logging.ERROR, name="callback")
|
||||||
|
|
||||||
if "callback" in params:
|
params.pop("callback", None)
|
||||||
del params["callback"]
|
|
||||||
|
|
||||||
if log_wrapper.has_targets():
|
if log_wrapper.has_targets():
|
||||||
if "logger" in params:
|
if "logger" in params:
|
||||||
|
|
|
||||||
115
app/native.py
115
app/native.py
|
|
@ -1,38 +1,50 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import queue
|
||||||
os.environ["PYTHONUTF8"] = "1"
|
import socket
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
import threading
|
||||||
|
import traceback
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
import platformdirs
|
||||||
|
|
||||||
|
sys.path.insert(0, os.path.join(getattr(sys, "_MEIPASS", os.path.abspath(os.path.dirname(__file__))), "app"))
|
||||||
|
|
||||||
|
APP_NAME = "YTPTube"
|
||||||
APP_ROOT = str((Path(__file__).parent / "..").resolve())
|
APP_ROOT = str((Path(__file__).parent / "..").resolve())
|
||||||
if APP_ROOT not in sys.path:
|
if APP_ROOT not in sys.path:
|
||||||
sys.path.insert(0, APP_ROOT)
|
sys.path.insert(0, APP_ROOT)
|
||||||
|
|
||||||
|
|
||||||
import json
|
|
||||||
import queue
|
|
||||||
import socket
|
|
||||||
import threading
|
|
||||||
|
|
||||||
import platformdirs
|
|
||||||
|
|
||||||
ready = threading.Event()
|
|
||||||
exception_holder = queue.Queue()
|
|
||||||
|
|
||||||
APP_NAME = "YTPTube"
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import webview # type: ignore
|
import webview # type: ignore
|
||||||
except ImportError as e:
|
except ImportError as e:
|
||||||
if "nt" == os.name:
|
pkgs = "pywebview[edgechromium]" if os.name == "nt" else "pywebview[qt]"
|
||||||
msg = "Please run 'uv pip install pywebview[edgechromium]' package to run YTPTube in native mode."
|
msg: str = f"Please run 'uv pip install {pkgs}' to run YTPTube in native mode."
|
||||||
else:
|
|
||||||
msg = "Please run 'uv pip install pywebview[qt]' package to run YTPTube in native mode."
|
|
||||||
raise ImportError(msg) from e
|
raise ImportError(msg) from e
|
||||||
|
|
||||||
|
|
||||||
|
def error_window(exc: Exception | str) -> None:
|
||||||
|
trace: str = "\n".join(traceback.format_exception(exc)) if isinstance(exc, Exception) else exc
|
||||||
|
webview.create_window(
|
||||||
|
f"{APP_NAME} - Error",
|
||||||
|
html=f"<h1 style='color:red;'>An error occurred</h1><pre>{trace}</pre>",
|
||||||
|
width=600,
|
||||||
|
height=400,
|
||||||
|
resizable=True,
|
||||||
|
)
|
||||||
|
webview.start(
|
||||||
|
gui="edgechromium" if os.name == "nt" else "qt",
|
||||||
|
debug=False,
|
||||||
|
storage_path=str(Path(os.getenv("YTP_TEMP_PATH", os.getcwd())) / "webview"),
|
||||||
|
private_mode=False,
|
||||||
|
)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
def set_env():
|
def set_env():
|
||||||
dct = {}
|
dct = {}
|
||||||
|
|
||||||
|
|
@ -52,32 +64,31 @@ def set_env():
|
||||||
os.environ.update(dct)
|
os.environ.update(dct)
|
||||||
|
|
||||||
|
|
||||||
def app_start(host: str, port: int) -> None:
|
def run_backend(host: str, port: int, ready_event: threading.Event, error_queue: queue.Queue):
|
||||||
import asyncio
|
|
||||||
|
|
||||||
from main import Main
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
loop = asyncio.get_running_loop()
|
try:
|
||||||
except RuntimeError:
|
loop = asyncio.get_running_loop()
|
||||||
loop = asyncio.new_event_loop()
|
except RuntimeError:
|
||||||
asyncio.set_event_loop(loop)
|
loop = asyncio.new_event_loop()
|
||||||
|
asyncio.set_event_loop(loop)
|
||||||
|
|
||||||
try:
|
from app.main import Main
|
||||||
Main(is_native=True).start(host, port, cb=lambda: ready.set())
|
|
||||||
|
Main(is_native=True).start(host, port, cb=lambda: ready_event.set())
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
exception_holder.put(e)
|
logging.exception(e)
|
||||||
ready.set()
|
error_queue.put(e)
|
||||||
|
ready_event.set()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
def main():
|
||||||
host = "127.0.0.1"
|
host = "127.0.0.1"
|
||||||
set_env()
|
set_env()
|
||||||
|
|
||||||
cfg_path: Path = Path(os.getenv("YTP_CONFIG_PATH")) / "webview.json"
|
cfg_path: Path = Path(os.getenv("YTP_CONFIG_PATH")) / "webview.json"
|
||||||
|
|
||||||
port = None
|
port = None
|
||||||
win_conf: dict[str, int] = {}
|
win_conf: dict[str, int] = {}
|
||||||
|
|
||||||
if cfg_path.exists():
|
if cfg_path.exists():
|
||||||
data = json.loads(cfg_path.read_text())
|
data = json.loads(cfg_path.read_text())
|
||||||
port = data.get("port")
|
port = data.get("port")
|
||||||
|
|
@ -91,18 +102,21 @@ if __name__ == "__main__":
|
||||||
port = s.getsockname()[1]
|
port = s.getsockname()[1]
|
||||||
cfg_path.write_text(json.dumps({"port": port}))
|
cfg_path.write_text(json.dumps({"port": port}))
|
||||||
|
|
||||||
threading.Thread(target=app_start, args=(host, port), daemon=True).start()
|
ready = threading.Event()
|
||||||
|
errors: queue.Queue = queue.Queue()
|
||||||
|
threading.Thread(target=run_backend, args=(host, port, ready, errors), daemon=False).start()
|
||||||
|
|
||||||
ready.wait()
|
ready.wait(timeout=5)
|
||||||
|
|
||||||
if not exception_holder.empty():
|
if not errors.empty():
|
||||||
raise exception_holder.get()
|
error_window(errors.get())
|
||||||
|
return
|
||||||
|
|
||||||
|
from app.library.version import APP_VERSION
|
||||||
|
|
||||||
|
gui = "edgechromium" if os.name == "nt" else "qt"
|
||||||
create_kwargs = {**win_conf, "resizable": True}
|
create_kwargs = {**win_conf, "resizable": True}
|
||||||
|
window = webview.create_window(f"{APP_NAME} - {APP_VERSION}", f"http://{host}:{port}", **create_kwargs)
|
||||||
webview.settings["ALLOW_DOWNLOADS"] = True
|
|
||||||
webview.settings["OPEN_DEVTOOLS_IN_DEBUG"] = False
|
|
||||||
window = webview.create_window(APP_NAME, f"http://{host}:{port}", **create_kwargs)
|
|
||||||
|
|
||||||
def save_geometry():
|
def save_geometry():
|
||||||
cfg = {
|
cfg = {
|
||||||
|
|
@ -116,13 +130,22 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
window.events.resized += lambda *_: save_geometry()
|
window.events.resized += lambda *_: save_geometry()
|
||||||
window.events.moved += lambda *_: save_geometry()
|
window.events.moved += lambda *_: save_geometry()
|
||||||
|
window.events.closing += lambda *_: os._exit(0)
|
||||||
|
|
||||||
gui = os.getenv("YTP_WV_GUI", None)
|
webview.settings["ALLOW_DOWNLOADS"] = True
|
||||||
gui = "edgechromium" if os.name == "nt" else "qt"
|
webview.settings["OPEN_DEVTOOLS_IN_DEBUG"] = False
|
||||||
|
|
||||||
webview.start(
|
webview.start(
|
||||||
gui=gui,
|
gui=gui,
|
||||||
debug=True,
|
debug=True,
|
||||||
storage_path=str(Path(os.getenv("YTP_TEMP_PATH", os.getcwd())) / "webview"),
|
storage_path=str(Path(os.getenv("YTP_TEMP_PATH", os.getcwd())) / "webview"),
|
||||||
private_mode=False,
|
private_mode=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
main()
|
||||||
|
except Exception as e:
|
||||||
|
logging.exception(e)
|
||||||
|
error_window(e)
|
||||||
|
os._exit(1)
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,8 @@
|
||||||
<span>Opts</span>
|
<span>Opts</span>
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="column is-12" v-if="!hasFormatInConfig && get_preset(form.preset)?.description">
|
<div class="column is-12"
|
||||||
|
v-if="!config.app.basic_mode && !hasFormatInConfig && get_preset(form.preset)?.description">
|
||||||
<div class="is-overflow-auto" style="max-height: 150px;">
|
<div class="is-overflow-auto" style="max-height: 150px;">
|
||||||
<div class="is-ellipsis is-clickable" @click="expand_description">
|
<div class="is-ellipsis is-clickable" @click="expand_description">
|
||||||
<span class="icon"><i class="fa-solid fa-info" /></span> {{ get_preset(form.preset)?.description }}
|
<span class="icon"><i class="fa-solid fa-info" /></span> {{ get_preset(form.preset)?.description }}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue