Skip to content

Commit

Permalink
walk files sorted naturally
Browse files Browse the repository at this point in the history
  • Loading branch information
tfeldmann committed Feb 23, 2024
1 parent 749db29 commit 64335dd
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 8 deletions.
6 changes: 5 additions & 1 deletion organize/walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path
from typing import Iterable, Iterator, List, Literal, NamedTuple, Optional, Set

from natsort import os_sorted
from pydantic import Field
from pydantic.dataclasses import dataclass

Expand Down Expand Up @@ -57,7 +58,10 @@ def scandir(top: str, collectfiles: bool = True) -> ScandirResult:
result.dirs.append(entry)
elif collectfiles:
result.nondirs.append(entry)
return result
return ScandirResult(
dirs=os_sorted(result.dirs, key=lambda x: x.name),
nondirs=os_sorted(result.nondirs, key=lambda x: x.name),
)


class DirActions(NamedTuple):
Expand Down
17 changes: 16 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ docx2txt = "^0.8"
ExifRead = "2.3.2" # Pinned: https://github.com/tfeldmann/organize/issues/267
Jinja2 = "^3.1.2"
macos-tags = { version = "^1.5.1", markers = "sys_platform == 'darwin'" }
natsort = "^8.4.0"
pdfminer-six = "^20231228"
platformdirs = "^4.0.0"
pydantic = "^2.3.0"
Expand Down
2 changes: 1 addition & 1 deletion tests/core/test_filter_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_filter_mode(fs, testoutput, filter_mode, expected_msgs):
@pytest.mark.parametrize(
"filter_mode, expected_msgs",
(
("any", ["foo", "baz", "x"]),
("any", ["baz", "foo", "x"]),
("all", ["x"]),
("none", []),
),
Expand Down
4 changes: 2 additions & 2 deletions tests/core/test_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_multiple_pathes(fs, testoutput):
- echo: '{path.name}'
"""
).execute(simulate=False, output=testoutput)
assert testoutput.messages == ["foo.txt", "bar.txt"] * 2
assert testoutput.messages == ["bar.txt", "foo.txt"] * 2


def test_multiple_pathes_single_location(fs, testoutput):
Expand All @@ -66,7 +66,7 @@ def test_multiple_pathes_single_location(fs, testoutput):
- echo: '{path.name}'
"""
).execute(simulate=False, output=testoutput)
assert testoutput.messages == ["foo.txt", "bar.txt"] * 2
assert testoutput.messages == ["bar.txt", "foo.txt", "bar.txt", "foo.txt"]


def test_multiple_dirs(fs, testoutput):
Expand Down
25 changes: 25 additions & 0 deletions tests/core/test_walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,28 @@ def test_exclude_dirs(fs):
"test",
)
assert len(list(Walker(exclude_dirs=["subC"]).files("/test"))) == 4


def test_order(fs):
make_files(
{
"2024": {"004": "", "001": "", "003": "", "002": ""},
"1989": {"D": "", "C": "", "A": "", "B": ""},
"2000": {"B": {"2": "", "1": ""}, "A": {"1": "", "2": ""}},
},
"test",
)
assert list(Walker().files("/test")) == [
Path("/test/1989/A"),
Path("/test/1989/B"),
Path("/test/1989/C"),
Path("/test/1989/D"),
Path("/test/2000/A/1"),
Path("/test/2000/A/2"),
Path("/test/2000/B/1"),
Path("/test/2000/B/2"),
Path("/test/2024/001"),
Path("/test/2024/002"),
Path("/test/2024/003"),
Path("/test/2024/004"),
]
2 changes: 1 addition & 1 deletion tests/filters/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_filename_move(fs, testoutput):
"""
Config.from_string(config).execute(simulate=False, output=testoutput)
assert testoutput.messages == [
"Found JPG file: test",
"Found JPG file: asd",
"Found JPG file: camel",
"Found JPG file: test",
]
2 changes: 1 addition & 1 deletion tests/filters/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ def test_python_dict(fs, testoutput):
"""
Config.from_string(config).execute(simulate=False, output=testoutput)
assert testoutput.messages == [
"100 foo",
"200 bar",
"300 baz",
"100 foo",
]


Expand Down
2 changes: 1 addition & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ def test_api(fs, testoutput):
]
)
config.execute(simulate=False, output=testoutput)
assert testoutput.messages == ["FOOFOO", "BAR", "BAZ"]
assert testoutput.messages == ["BAR", "BAZ", "FOOFOO"]

0 comments on commit 64335dd

Please sign in to comment.