nixfiles/musicutil/utils/scan_for_music.py

24 lines
614 B
Python
Raw Normal View History

2022-02-04 13:32:22 +00:00
from pathlib import Path
from os.path import relpath
2022-02-04 13:35:01 +00:00
from ..types import File
from ..meta import supported_formats
2022-02-04 13:32:22 +00:00
2022-02-06 16:18:14 +00:00
2022-02-04 16:19:11 +00:00
def file_from_path(path: Path, src: str) -> File:
file = File()
file.path_to = str(path.parent)
file.path_from_src = relpath(
str(path.parent), src)
file.filename = path.stem
file.extension = path.suffix.replace(".", "")
return file
2022-02-04 13:54:10 +00:00
2022-02-06 16:18:14 +00:00
2022-02-04 13:32:22 +00:00
def scan_for_music(src: str) -> list[File]:
2022-02-04 13:54:10 +00:00
files: list[File] = []
for format in supported_formats:
for path in Path(src).rglob("*." + format):
2022-02-04 16:19:11 +00:00
files.append(file_from_path(path, src))
2022-02-04 13:54:10 +00:00
return files