add a store method to count the number of tracks in a folder

~ Courtesy of chatgpt3.5
+ add a few a TODO
This commit is contained in:
geoffrey45 2023-03-21 22:15:31 +03:00
parent a5d61e8919
commit 32bb3d37e1
4 changed files with 25 additions and 4 deletions

View File

@ -71,9 +71,7 @@ def get_album_tracks_and_info():
album.check_is_single(tracks)
if album.is_single:
album.is_single = True
else:
if not album.is_single:
album.check_type()
album.is_favorite = check_is_fav(albumhash, FavType.album)

View File

@ -235,6 +235,7 @@ class Store:
"""
Returns a folder object by its path.
"""
# TODO: Modify this method to accept a list of paths, sorting is computationally expensive.
folders = sorted(cls.folders, key=lambda x: x.path)
folder = UseBisection(folders, "path", [path])()[0]
@ -250,6 +251,21 @@ class Store:
cls.folders.append(folder)
return folder
@classmethod
def get_folders_count(cls, paths: list[str]) -> list[dict[str, int]]:
count_dict = {path: 0 for path in paths}
for track in cls.tracks:
for path in paths:
if track.filepath.startswith(path):
count_dict[path] += 1
result = [{"path": path, "count": count_dict[path]} for path in paths]
# TODO: Modify this method to return Folder objects with
# track count mapped. Keep an eye on function complexity.
return result
@classmethod
def get_tracks_by_filepaths(cls, paths: list[str]) -> list[Track]:
"""

View File

@ -1,5 +1,6 @@
import os
from concurrent.futures import ThreadPoolExecutor
from pprint import pprint
from app.db.store import Store
from app.models import Folder, Track
@ -50,10 +51,16 @@ class GetFilesAndDirs:
tracks = Store.get_tracks_by_filepaths(files)
# TODO: Remove this threadpool and modify the get_folder store
# method to accept a list of paths.
with ThreadPoolExecutor() as pool:
iterable = pool.map(Store.get_folder, dirs)
folders = [i for i in iterable if i is not None]
folders = filter(lambda f: f.has_tracks, folders)
folders_with_count_dict = Store.get_folders_count(dirs)
pprint(folders_with_count_dict)
# TODO: Map folder count to folder object
return tracks, folders # type: ignore

View File

@ -110,7 +110,7 @@ class Album:
"""
if (
len(tracks) == 1
and tracks[0].title == self.title
and tracks[0].title.lower() == self.title.lower()
# and tracks[0].track == 1
# and tracks[0].disc == 1