Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add broken test for issue 305 #306

Merged
merged 4 commits into from
Apr 4, 2024
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
14 changes: 7 additions & 7 deletions kloppy/_providers/tracab.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
from kloppy.domain import TrackingDataset
from kloppy.infra.serializers.tracking.tracab.tracab_dat import (
TRACABDatDeserializer,
TRACABInputs,
)
from kloppy.infra.serializers.tracking.tracab.tracab_json import (
TRACABJSONDeserializer,
TRACABInputs,
)
from kloppy.io import FileLike, open_as_file
from kloppy.io import FileLike, open_as_file, get_file_extension


def load(
Expand Down Expand Up @@ -47,13 +46,14 @@ def identify_deserializer(
meta_data: FileLike,
raw_data: FileLike,
) -> Union[Type[TRACABDatDeserializer], Type[TRACABJSONDeserializer]]:
deserializer = None
if "xml" in meta_data.name and "dat" in raw_data.name:
meta_data_extension = get_file_extension(meta_data)
raw_data_extension = get_file_extension(raw_data)

if meta_data_extension == ".xml" and raw_data_extension == ".dat":
deserializer = TRACABDatDeserializer
if "json" in meta_data.name and "json" in raw_data.name:
elif meta_data_extension == ".json" and raw_data_extension == ".json":
deserializer = TRACABJSONDeserializer

if deserializer is None:
else:
raise ValueError(
"Tracab file format could not be recognized, please specify"
)
Expand Down
11 changes: 11 additions & 0 deletions kloppy/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ def create(cls, input_: "FileLike", **kwargs):
FileLike = Union[str, PurePath, bytes, IO[bytes], Source]


def get_file_extension(f: FileLike) -> str:
if isinstance(f, str):
return os.path.splitext(f)[1]
elif isinstance(f, PurePath):
return os.path.splitext(f.name)[1]
elif isinstance(f, Source):
return get_file_extension(f.data)
else:
raise Exception("Could not determine extension")


def get_local_cache_stream(url: str, cache_dir: str) -> Tuple[BinaryIO, bool]:
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
Expand Down
45 changes: 45 additions & 0 deletions kloppy/tests/issues/test_issue_305.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from pathlib import Path

import pytest

from kloppy import tracab


@pytest.fixture(scope="session")
def json_meta_data(base_dir: Path) -> Path:
return base_dir / "files" / "tracab_meta.json"


@pytest.fixture(scope="session")
def json_raw_data(base_dir: Path) -> Path:
return base_dir / "files" / "tracab_raw.json"


@pytest.fixture(scope="session")
def xml_meta_data(base_dir: Path) -> Path:
return base_dir / "files" / "tracab_meta.xml"


@pytest.fixture(scope="session")
def dat_raw_data(base_dir: Path) -> Path:
return base_dir / "files" / "tracab_raw.dat"


class TestIssue305:
def test_str_path_xml(self, xml_meta_data: Path, dat_raw_data: Path):
dataset = tracab.load(
meta_data=str(xml_meta_data),
raw_data=str(dat_raw_data),
coordinates="tracab",
only_alive=False,
)
assert dataset

def test_str_path_json(self, json_meta_data: Path, json_raw_data: Path):
dataset = tracab.load(
meta_data=str(json_meta_data),
raw_data=str(json_raw_data),
coordinates="tracab",
only_alive=False,
)
assert dataset