Skip to content

Commit

Permalink
Remove unused function list_score_paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef-Friedrich committed Jan 23, 2024
1 parent d2b18dd commit d0ff6f0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 53 deletions.
2 changes: 1 addition & 1 deletion mscxyz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@

Style = mscxyz.style.Style

list_score_paths = utils.list_score_paths
list_files = utils.list_files
"""list_score_paths"""
34 changes: 0 additions & 34 deletions mscxyz/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,6 @@
INCH = 25.4


def list_score_paths(
path: str, extension: ListExtension = "both", glob: Optional[str] = None
) -> list[str]:
"""List all scores in path.
:param path: The path so search for score files.
:param extension: Possible values: “both”, “mscz” or “mscx”.
:param glob: A glob string, see fnmatch
"""
if not glob:
if extension == "both":
glob = "*.msc[xz]"
elif extension in ("mscx", "mscz"):
glob = f"*.{extension}"
else:
raise ValueError(
"Possible values for the argument “extension” "
"are: “both”, “mscx”, “mscz”"
)
if os.path.isfile(path):
if fnmatch.fnmatch(path, glob):
return [path]
else:
return []
out: List[str] = []
for root, _, scores in os.walk(path):
for score in scores:
if fnmatch.fnmatch(score, glob):
scores_path = os.path.join(root, score)
out.append(scores_path)
out.sort()
return out


def list_files(
src: str | list[str], extension: ListExtension = "both", glob: Optional[str] = None
) -> Generator[Path, None, None]:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Meta,
Score,
Style,
list_score_paths,
list_files,
)


Expand All @@ -28,4 +28,4 @@ def test_class_style() -> None:


def test_function_list_scores() -> None:
assert list_score_paths.__name__ == "list_score_paths"
assert list_files.__name__ == "list_files"
12 changes: 6 additions & 6 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest
from lxml.etree import _Element

from mscxyz import Score, list_score_paths
from mscxyz import Score, list_files


def test_instantiate_a_score_object(score_file: str) -> None:
Expand All @@ -32,18 +32,18 @@ def print_elements(element: _Element, level: int) -> None:


def test_list_score_paths(nested_dir: Path) -> None:
score_paths = list_score_paths(path=str(nested_dir), extension="mscz")
score_paths = list(list_files(src=str(nested_dir), extension="mscz"))
for score_path in score_paths:
score = Score(score_path)
assert score.path.exists()
assert score.extension == "mscz"

assert len(score_paths) == 4

assert "level1/level2/level3/score3.mscz" in score_paths[0]
assert "level1/level2/score2.mscz" in score_paths[1]
assert "level1/score1.mscz" in score_paths[2]
assert "score0.mscz" in score_paths[3]
assert "level1/level2/level3/score3.mscz" in str(score_paths[0])
assert "level1/level2/score2.mscz" in str(score_paths[1])
assert "level1/score1.mscz" in str(score_paths[2])
assert "score0.mscz" in str(score_paths[3])


@pytest.mark.skip("Will be fixed later")
Expand Down
28 changes: 18 additions & 10 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

import os
import tempfile
from typing import Optional
from pathlib import Path
from typing import Generator, Optional
from unittest import mock

import pytest
Expand All @@ -23,12 +24,19 @@ class TestFunctions:
def _list_scores(
path: str, extension: ListExtension = "both", glob: Optional[str] = None
) -> list[str]:
with mock.patch("os.walk") as mockwalk:
with mock.patch("os.walk") as mockwalk, mock.patch(
"pathlib.Path.is_dir"
) as is_dir:
is_dir.return_value = True
mockwalk.return_value = [
("/a", ("bar",), ("lorem.mscx",)),
("/a/b", (), ("impsum.mscz", "dolor.mscx", "sit.txt")),
]
return utils.list_score_paths(path, extension, glob)
scores: list[str] = []
for score in utils.list_files(path, extension, glob):
scores.append(str(score))
scores.sort()
return scores

@pytest.mark.skip("TODO: Fix this test")
@mock.patch("mscxyz.score.Score")
Expand All @@ -42,7 +50,7 @@ def test_without_extension(self) -> None:

def test_extension_both(self) -> None:
result: list[str] = self._list_scores("/test", extension="both")
assert result == ["/a/b/dolor.mscx", "/a/b/impsum.mscz", "/a/lorem.mscx"]
assert result == ['/a/b/dolor.mscx', '/a/b/impsum.mscz', '/a/lorem.mscx']

def test_extension_mscx(self) -> None:
result: list[str] = self._list_scores("/test", extension="mscx")
Expand All @@ -57,20 +65,20 @@ def test_raises_exception(self) -> None:
self._list_scores("/test", extension="lol") # type: ignore

def test_isfile(self) -> None:
with mock.patch("os.path.isfile") as mock_isfile:
with mock.patch("pathlib.Path.is_file") as mock_isfile:
mock_isfile.return_value = True
result = utils.list_score_paths("/a/b/lorem.mscx")
assert result == ["/a/b/lorem.mscx"]
result = list(utils.list_files("/a/b/lorem.mscx"))
assert str(result[0]) == "/a/b/lorem.mscx"

def test_isfile_no_match(self) -> None:
with mock.patch("os.path.isfile") as mock_isfile:
with mock.patch("pathlib.Path.is_file") as mock_isfile:
mock_isfile.return_value = True
result: list[str] = utils.list_score_paths("/a/b/lorem.lol")
result = list(utils.list_files("/a/b/lorem.lol"))
assert result == []

def test_arg_glob_txt(self) -> None:
result: list[str] = self._list_scores("/test", glob="*.txt")
assert result == ["/a/b/sit.txt"]
assert str(result[0]) == "/a/b/sit.txt"

def test_arg_glob_lol(self) -> None:
result: list[str] = self._list_scores("/test", glob="*.lol")
Expand Down

0 comments on commit d0ff6f0

Please sign in to comment.