Python cleanups

This commit is contained in:
Damien Elmes 2025-06-15 13:30:07 +07:00
parent fb7c816124
commit cefb1a2997
6 changed files with 27 additions and 14 deletions

View file

@ -165,3 +165,5 @@ ignore_missing_imports = True
ignore_missing_imports = True ignore_missing_imports = True
[mypy-pip_system_certs.*] [mypy-pip_system_certs.*]
ignore_missing_imports = True ignore_missing_imports = True
[mypy-anki_audio]
ignore_missing_imports = True

View file

@ -3,9 +3,9 @@
from __future__ import annotations from __future__ import annotations
import os
import atexit import atexit
import logging import logging
import os
import sys import sys
from collections.abc import Callable from collections.abc import Callable
from typing import TYPE_CHECKING, Any, Union, cast from typing import TYPE_CHECKING, Any, Union, cast
@ -46,7 +46,6 @@ import builtins
import cProfile import cProfile
import getpass import getpass
import locale import locale
import os
import tempfile import tempfile
import traceback import traceback
from pathlib import Path from pathlib import Path

View file

@ -14,9 +14,7 @@ import aqt.utils
class _MacOSHelper: class _MacOSHelper:
def __init__(self) -> None: def __init__(self) -> None:
path = os.path.join( path = os.path.join(aqt.utils.aqt_data_folder(), "lib", "libankihelper.dylib")
aqt.utils.aqt_data_folder(), "lib", "libankihelper.dylib"
)
self._dll = CDLL(path) self._dll = CDLL(path)
self._dll.system_is_dark.restype = c_bool self._dll.system_is_dark.restype = c_bool

View file

@ -4,13 +4,17 @@
"""Helpers for the packaged version of Anki.""" """Helpers for the packaged version of Anki."""
from __future__ import annotations from __future__ import annotations
from pathlib import Path
import subprocess import subprocess
from pathlib import Path
from anki.utils import is_mac from anki.utils import is_mac
# pylint: disable=unused-import
def first_run_setup() -> None: def first_run_setup() -> None:
"""Code run the first time after install/upgrade. """Code run the first time after install/upgrade.
Currently, we just import our main libraries and invoke Currently, we just import our main libraries and invoke
mpv/lame on macOS, which is slow on the first run, and doing mpv/lame on macOS, which is slow on the first run, and doing
it this way shows progress being made. it this way shows progress being made.
@ -18,32 +22,41 @@ def first_run_setup() -> None:
if not is_mac: if not is_mac:
return return
def _dot(): def _dot():
print(".", flush=True, end="") print(".", flush=True, end="")
_dot() _dot()
import anki.collection import anki.collection
_dot() _dot()
import PyQt6.sip import PyQt6.sip
_dot() _dot()
import PyQt6.QtCore import PyQt6.QtCore
_dot() _dot()
import PyQt6.QtGui import PyQt6.QtGui
_dot() _dot()
import PyQt6.QtNetwork import PyQt6.QtNetwork
_dot() _dot()
import PyQt6.QtQuick import PyQt6.QtQuick
_dot() _dot()
import PyQt6.QtWebChannel import PyQt6.QtWebChannel
_dot() _dot()
import PyQt6.QtWebEngineCore import PyQt6.QtWebEngineCore
_dot() _dot()
import PyQt6.QtWebEngineWidgets import PyQt6.QtWebEngineWidgets
_dot() _dot()
import anki_audio
import PyQt6.QtWidgets import PyQt6.QtWidgets
import anki_audio
audio_pkg_path = Path(anki_audio.__file__).parent audio_pkg_path = Path(anki_audio.__file__).parent
# Invoke mpv and lame # Invoke mpv and lame
@ -51,6 +64,6 @@ def first_run_setup() -> None:
for cmd_name in ["mpv", "lame"]: for cmd_name in ["mpv", "lame"]:
_dot() _dot()
cmd[0] = audio_pkg_path / cmd_name cmd[0] = audio_pkg_path / cmd_name
subprocess.run(cmd, check=True, capture_output=True) subprocess.run([str(cmd[0]), str(cmd[1])], check=True, capture_output=True)
print() print()

View file

@ -283,19 +283,20 @@ def _packagedCmd(cmd: list[str]) -> tuple[Any, dict[str, str]]:
if is_win or is_mac: if is_win or is_mac:
try: try:
import anki_audio import anki_audio
audio_pkg_path = Path(anki_audio.__file__).parent audio_pkg_path = Path(anki_audio.__file__).parent
if is_win: if is_win:
packaged_path = audio_pkg_path / (cmd[0] + ".exe") packaged_path = audio_pkg_path / (cmd[0] + ".exe")
else: # is_mac else: # is_mac
packaged_path = audio_pkg_path / cmd[0] packaged_path = audio_pkg_path / cmd[0]
if packaged_path.exists(): if packaged_path.exists():
cmd[0] = str(packaged_path) cmd[0] = str(packaged_path)
return cmd, env return cmd, env
except ImportError: except ImportError:
# anki-audio not available, fall back to old behavior # anki-audio not available, fall back to old behavior
pass pass
packaged_path = Path(sys.prefix) / cmd[0] packaged_path = Path(sys.prefix) / cmd[0]
if packaged_path.exists(): if packaged_path.exists():
cmd[0] = str(packaged_path) cmd[0] = str(packaged_path)

View file

@ -19,7 +19,7 @@ for arch in architectures:
target = f"{arch}-apple-macos11" target = f"{arch}-apple-macos11"
temp_out = out_dir / f"temp_{arch}.dylib" temp_out = out_dir / f"temp_{arch}.dylib"
temp_files.append(temp_out) temp_files.append(temp_out)
args = [ args = [
"swiftc", "swiftc",
"-target", "-target",