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
[mypy-pip_system_certs.*]
ignore_missing_imports = True
[mypy-anki_audio]
ignore_missing_imports = True

View file

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

View file

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

View file

@ -4,13 +4,17 @@
"""Helpers for the packaged version of Anki."""
from __future__ import annotations
from pathlib import Path
import subprocess
from pathlib import Path
from anki.utils import is_mac
# pylint: disable=unused-import
def first_run_setup() -> None:
"""Code run the first time after install/upgrade.
Currently, we just import our main libraries and invoke
mpv/lame on macOS, which is slow on the first run, and doing
it this way shows progress being made.
@ -18,32 +22,41 @@ def first_run_setup() -> None:
if not is_mac:
return
def _dot():
print(".", flush=True, end="")
_dot()
import anki.collection
_dot()
import PyQt6.sip
_dot()
import PyQt6.QtCore
_dot()
import PyQt6.QtGui
_dot()
import PyQt6.QtNetwork
_dot()
import PyQt6.QtQuick
_dot()
import PyQt6.QtWebChannel
_dot()
import PyQt6.QtWebEngineCore
_dot()
import PyQt6.QtWebEngineWidgets
_dot()
import anki_audio
import PyQt6.QtWidgets
import anki_audio
audio_pkg_path = Path(anki_audio.__file__).parent
# Invoke mpv and lame
@ -51,6 +64,6 @@ def first_run_setup() -> None:
for cmd_name in ["mpv", "lame"]:
_dot()
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:
try:
import anki_audio
audio_pkg_path = Path(anki_audio.__file__).parent
if is_win:
packaged_path = audio_pkg_path / (cmd[0] + ".exe")
else: # is_mac
packaged_path = audio_pkg_path / cmd[0]
if packaged_path.exists():
cmd[0] = str(packaged_path)
return cmd, env
except ImportError:
# anki-audio not available, fall back to old behavior
pass
packaged_path = Path(sys.prefix) / cmd[0]
if packaged_path.exists():
cmd[0] = str(packaged_path)

View file

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