Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ default_language_version:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
rev: v6.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
Expand All @@ -21,14 +21,14 @@ repos:
- id: detect-private-key

- repo: https://github.com/astral-sh/uv-pre-commit
rev: '0.6.10'
rev: '0.8.23'
hooks:
- id: uv-lock
- id: uv-sync

# Run the Ruff linter.
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.11.2
rev: v0.13.3
hooks:
# Linter
- id: ruff
Expand All @@ -37,7 +37,7 @@ repos:
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.15.0
rev: v1.18.2
hooks:
- id: mypy

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test: $(TEST_FILE) $(TEST_TWO_FILE)
@mkvmerge -J $(TEST_FILE)
@echo "Running mkvmerge -J $(TEST_TWO_FILE)..."
@mkvmerge -J $(TEST_TWO_FILE)
pytest --cov=pymkv $(TEST_DIR) --cov-report=xml --junitxml=test-results/junit.xml
uv run pytest --cov=pymkv $(TEST_DIR) --cov-report=xml --junitxml=test-results/junit.xml

$(TEST_FILE):
@if [ ! -f $(TEST_FILE) ]; then \
Expand Down
10 changes: 5 additions & 5 deletions pymkv/MKVFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
checking_file_path,
get_file_info,
verify_mkvmerge,
verify_supported,
)

T = TypeVar("T")
Expand Down Expand Up @@ -123,10 +122,6 @@ def __init__(
raise FileNotFoundError(msg)

if file_path is not None:
if not verify_supported(file_path, mkvmerge_path=self.mkvmerge_path):
msg = f"The file '{file_path}' is not a valid Matroska file or is not supported."
raise ValueError(msg)
# add file title
file_path = checking_file_path(file_path)
try:
info_json = get_file_info(
Expand All @@ -142,6 +137,11 @@ def __init__(
e.cmd,
output=error_output,
) from e

if not info_json["container"]["supported"]:
msg = f"The file '{file_path}' is not a valid Matroska file or is not supported."
raise ValueError(msg)

if self.title is None and "title" in info_json["container"]["properties"]:
self.title = info_json["container"]["properties"]["title"]

Expand Down
6 changes: 4 additions & 2 deletions pymkv/MKVTrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def __init__( # noqa: PLR0913
tag_entries: int = 0,
compression: bool | None = None,
) -> None:
from pymkv.TypeTrack import get_track_extension
from pymkv.TypeTrack import get_track_extension # noqa: PLC0415

# track info
self._track_codec: str | None = None
Expand Down Expand Up @@ -215,6 +215,8 @@ def file_path(self, file_path: str) -> None:

This method checks if the provided file path is valid and supported by mkvmerge.
If the file is valid, it sets the file path and resets the track_id to 0.
If existing_info is already set, skip the verify_supported check since the file
was already verified.

Args:
file_path (str): The path to the file containing the track.
Expand All @@ -223,7 +225,7 @@ def file_path(self, file_path: str) -> None:
ValueError: If the file is not a valid Matroska file or is not supported.
"""
fp = checking_file_path(file_path)
if not verify_supported(fp, mkvmerge_path=self.mkvmerge_path):
if not self._info_json and not verify_supported(fp, mkvmerge_path=self.mkvmerge_path):
msg = f"The file '{file_path}' is not a valid Matroska file or is not supported."
raise ValueError(msg)
self._file_path = fp
Expand Down
24 changes: 17 additions & 7 deletions pymkv/Verifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import os
import subprocess as sp
from collections.abc import Iterable, Sequence
from functools import cache
from pathlib import Path
from re import match
from typing import Any
Expand Down Expand Up @@ -122,6 +123,18 @@ def get_file_info(
return json.loads(sp.check_output(cmds).decode()) # noqa: S603


@cache
def _verify_mkvmerge_cached(mkvmerge_path_tuple: tuple[str, ...]) -> bool:
"""Internal cached function to verify mkvmerge availability."""
try:
mkvmerge_command = list(mkvmerge_path_tuple)
mkvmerge_command.append("-V")
output = sp.check_output(mkvmerge_command).decode() # noqa: S603
except (sp.CalledProcessError, FileNotFoundError):
return False
return bool(match("mkvmerge.*", output))


def verify_mkvmerge(
mkvmerge_path: str | os.PathLike | Iterable[str] = "mkvmerge",
) -> bool:
Expand All @@ -137,13 +150,10 @@ def verify_mkvmerge(
bool
True if mkvmerge is available at the specified path, False otherwise.
"""
try:
mkvmerge_command = list(prepare_mkvtoolnix_path(mkvmerge_path))
mkvmerge_command.append("-V")
output = sp.check_output(mkvmerge_command).decode() # noqa: S603
except (sp.CalledProcessError, FileNotFoundError):
return False
return bool(match("mkvmerge.*", output))
mkvmerge_command = prepare_mkvtoolnix_path(mkvmerge_path)
if isinstance(mkvmerge_command, list):
mkvmerge_command = tuple(mkvmerge_command)
return _verify_mkvmerge_cached(mkvmerge_command)


def verify_matroska(
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dev = [
"mypy<2.0.0,>=1.14.1",
"ruff<1.0.0,>=0.9.0",
"sphinx-immaterial<0.14.0,>=0.12.4; python_version >= \"3.10\"",
"pyinstrument>=5.1.1",
]

[project.urls]
Expand Down
18 changes: 18 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from pathlib import Path

import pytest
from pyinstrument import Profiler

TESTS_ROOT = Path.cwd()


@pytest.fixture
Expand Down Expand Up @@ -67,3 +70,18 @@ def get_path_test_srt(get_base_path: Path) -> Path:
srt_path = get_base_path / "test_file.srt"
create_srt_file_with_random_text(srt_path)
return srt_path


@pytest.fixture(autouse=True)
def auto_profile(request: pytest.FixtureRequest) -> Generator[None, None, None]:
profile_root = TESTS_ROOT / ".profiles"
profiler = Profiler()
profiler.start()

yield

profiler.stop()
profile_root.mkdir(exist_ok=True)
test_name = request.node.name
results_file = profile_root / f"{test_name}.html"
profiler.write_html(results_file)
4 changes: 2 additions & 2 deletions tests/test_open_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ def test_file_not_found() -> None:
def test_file_not_support() -> None:
with pytest.raises(
ValueError,
match="The file 'tests/conftest.py' is not a valid Matroska file or is not supported.",
match=r"The file 'tests[/\\]conftest\.py' is not a valid Matroska file or is not supported\.",
):
MKVFile("tests/conftest.py")


def test_track_not_support() -> None:
with pytest.raises(
ValueError,
match="The file 'tests/conftest.py' is not a valid Matroska file or is not supported.",
match=r"The file 'tests[/\\]conftest\.py' is not a valid Matroska file or is not supported\.",
):
MKVTrack("tests/conftest.py")

Expand Down
Loading