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: 6 additions & 2 deletions pymkv/MKVFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
from pymkv.MKVTrack import MKVTrack
from pymkv.Timestamp import Timestamp
from pymkv.utils import prepare_mkvtoolnix_path
from pymkv.Verifications import checking_file_path, verify_mkvmerge
from pymkv.Verifications import checking_file_path, verify_mkvmerge, verify_supported


class MKVFile:
Expand Down Expand Up @@ -104,10 +104,14 @@ def __init__( # noqa: C901, PLR0912
self.attachments = []
self._number_file = 0

if not verify_mkvmerge(mkvmerge_path=mkvmerge_path):
if not verify_mkvmerge(mkvmerge_path=self.mkvmerge_path):
msg = "mkvmerge is not at the specified path, add it there or changed mkvmerge_path property"
raise FileNotFoundError(msg)

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)

if file_path is not None:
# add file title
file_path = checking_file_path(file_path)
Expand Down
6 changes: 3 additions & 3 deletions pymkv/MKVTrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from pymkv.ISO639_2 import is_iso639_2
from pymkv.TypeTrack import get_track_extension
from pymkv.utils import prepare_mkvtoolnix_path
from pymkv.Verifications import verify_supported
from pymkv.Verifications import checking_file_path, verify_supported


class MKVTrack:
Expand Down Expand Up @@ -189,9 +189,9 @@ def file_path(self) -> str:

@file_path.setter
def file_path(self, file_path: str) -> None:
file_path = str(Path(file_path).expanduser())
file_path = checking_file_path(file_path)
if not verify_supported(file_path, mkvmerge_path=self.mkvmerge_path):
msg = '"{}" is not a supported file'
msg = f"The file '{file_path}' is not a valid Matroska file or is not supported."
raise ValueError(msg)
self._file_path = file_path
self.track_id = 0
Expand Down
4 changes: 2 additions & 2 deletions pymkv/Verifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def verify_mkvmerge(mkvmerge_path: str | list | os.PathLike | None = "mkvmerge")
return bool(match("mkvmerge.*", output))


def verify_matroska(file_path: str | os.PathLike, mkvmerge_path: str = "mkvmerge") -> bool:
def verify_matroska(file_path: str | os.PathLike, mkvmerge_path: str | list | os.PathLike | None = "mkvmerge") -> bool:
"""
Parameters
----------
Expand Down Expand Up @@ -93,7 +93,7 @@ def verify_matroska(file_path: str | os.PathLike, mkvmerge_path: str = "mkvmerge
msg = "mkvmerge is not at the specified path, add it there or change the mkvmerge_path property"
raise FileNotFoundError(msg)
try:
info_json = json.loads(
info_json: dict = json.loads(
sp.check_output(
[*prepare_mkvtoolnix_path(mkvmerge_path), "-J", checking_file_path(file_path)], # noqa: S603
).decode(),
Expand Down
18 changes: 17 additions & 1 deletion tests/test_open_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from pymkv import MKVFile
from pymkv import MKVFile, MKVTrack


def test_open_file(get_path_test_file: Path) -> None:
Expand All @@ -22,3 +22,19 @@ def test_mux_file(get_base_path: Path, get_path_test_file: Path) -> None:
def test_file_not_found() -> None:
with pytest.raises(FileNotFoundError):
MKVFile("file-zero.mkv")


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.",
):
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.",
):
MKVTrack("tests/conftest.py")
19 changes: 19 additions & 0 deletions tests/test_verify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from pathlib import Path

import pytest

from pymkv import verify_matroska


def test_verify_matroska_true(get_path_test_file: Path) -> None:
assert verify_matroska(get_path_test_file) is True


def test_verify_matroska_error(get_path_test_file: Path) -> None:
with pytest.raises(FileNotFoundError):
verify_matroska(get_path_test_file, "mkvmerge-test")


def test_verify_matroska_false(get_path_test_file: Path) -> None:
with pytest.raises(KeyError):
verify_matroska("tests/conftest.py")