Skip to content

Commit

Permalink
refactor: move ls_dirs to utils.fs
Browse files Browse the repository at this point in the history
  • Loading branch information
loqusion committed Apr 25, 2024
1 parent 1e4e3cb commit a5777f9
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 48 deletions.
3 changes: 1 addition & 2 deletions src/hyprshade/cli/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import click

from hyprshade.shader.core import PureShader, Shader

from .utils import ls_dirs
from hyprshade.utils.fs import ls_dirs


@click.command(short_help="List available screen shaders")
Expand Down
12 changes: 3 additions & 9 deletions src/hyprshade/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
from typing import TYPE_CHECKING, Any, Final, Literal, TypeVar, overload

import click
from more_itertools import flatten, unique_justseen
from more_itertools import unique_justseen

from hyprshade.config.core import Config
from hyprshade.shader.core import Shader
from hyprshade.utils.fs import scandir_recursive
from hyprshade.utils.fs import ls_dirs
from hyprshade.utils.path import stripped_basename
from hyprshade.utils.xdg import user_config_dir

if TYPE_CHECKING:
from collections.abc import Callable, Iterable, Iterator
from os import PathLike
from collections.abc import Callable, Iterator


T = TypeVar("T", str, int, float, bool, click.ParamType)
Expand Down Expand Up @@ -63,11 +62,6 @@ def get_script_path() -> str: # pragma: no cover
return os.path.realpath(sys.argv[0], strict=True)


def ls_dirs(dirs: Iterable[str | PathLike[str]]) -> Iterator[str]:
all_files = flatten(scandir_recursive(d, max_depth=5) for d in dirs)
return (f.path for f in sorted(all_files, key=lambda f: f.name))


class ShaderParamType(click.ParamType):
name: Final = "shader"

Expand Down
10 changes: 9 additions & 1 deletion src/hyprshade/utils/fs.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from __future__ import annotations

import os
from os import PathLike
from typing import TYPE_CHECKING, AnyStr

from more_itertools import flatten

if TYPE_CHECKING:
from collections.abc import Iterator
from collections.abc import Iterable, Iterator

from _typeshed import GenericPath

Expand All @@ -25,3 +28,8 @@ def scandir_recursive(

while dir_stack:
yield from scandir_recursive(dir_stack.pop(), max_depth=max_depth - 1)


def ls_dirs(dirs: Iterable[str | PathLike[str]]) -> Iterator[str]:
all_files = flatten(scandir_recursive(d, max_depth=5) for d in dirs)
return (f.path for f in sorted(all_files, key=lambda f: f.name))
35 changes: 0 additions & 35 deletions tests/cli/utils/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,10 @@
from pathlib import Path

import pytest

from hyprshade.cli.ls import ls_dirs
from hyprshade.cli.utils import ContextObject, write_systemd_user_unit
from tests.conftest import Isolation
from tests.types import ConfigFactory


class TestLsDirs:
def test_empty(self):
assert list(ls_dirs([])) == []

def test_single(self, tmp_path: Path):
(tmp_file := tmp_path / "foo").touch()
assert list(ls_dirs([tmp_path])) == [str(tmp_file)]

def test_multiple(self, tmp_path: Path):
(tmp_file1 := tmp_path / "bar").touch()
(tmp_file2 := tmp_path / "foo").touch()
assert list(ls_dirs([tmp_path])) == list(map(str, [tmp_file1, tmp_file2]))

def test_multiple_dirs(self, tmp_path_factory: pytest.TempPathFactory):
paths = []
for name in ["foo", "bar", "baz"]:
tmp_path = tmp_path_factory.mktemp(name)
(tmp_path / f"qux{name}").touch()
paths.append(tmp_path)

assert list(ls_dirs(paths)) == list(
map(
str,
[
paths[1] / "quxbar",
paths[2] / "quxbaz",
paths[0] / "quxfoo",
],
)
)


def test_write_systemd_user_unit(isolation: Isolation):
write_systemd_user_unit("service", "foo")
assert (
Expand Down
34 changes: 33 additions & 1 deletion tests/utils/test_fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from hyprshade.utils.fs import scandir_recursive
from hyprshade.utils.fs import ls_dirs, scandir_recursive


class TestScandirRecursive:
Expand Down Expand Up @@ -61,3 +61,35 @@ def test_negative_max_depth(self, tmp_path: Path):
def test_invalid_path(self, tmp_path: Path):
with pytest.raises(FileNotFoundError):
list(scandir_recursive(tmp_path / "doesnotexist", max_depth=0))


class TestLsDirs:
def test_empty(self):
assert list(ls_dirs([])) == []

def test_single(self, tmp_path: Path):
(tmp_file := tmp_path / "foo").touch()
assert list(ls_dirs([tmp_path])) == [str(tmp_file)]

def test_multiple(self, tmp_path: Path):
(tmp_file1 := tmp_path / "bar").touch()
(tmp_file2 := tmp_path / "foo").touch()
assert list(ls_dirs([tmp_path])) == list(map(str, [tmp_file1, tmp_file2]))

def test_multiple_dirs(self, tmp_path_factory: pytest.TempPathFactory):
paths = []
for name in ["foo", "bar", "baz"]:
tmp_path = tmp_path_factory.mktemp(name)
(tmp_path / f"qux{name}").touch()
paths.append(tmp_path)

assert list(ls_dirs(paths)) == list(
map(
str,
[
paths[1] / "quxbar",
paths[2] / "quxbaz",
paths[0] / "quxfoo",
],
)
)

0 comments on commit a5777f9

Please sign in to comment.