add folder count to folder route

This commit is contained in:
geoffrey45 2023-03-25 05:26:01 +03:00
parent d43dcbff46
commit 79029ae346
8 changed files with 47 additions and 67 deletions

View File

@ -8,10 +8,8 @@ from pathlib import Path
from flask import Blueprint, request from flask import Blueprint, request
from app import settings from app import settings
from app.lib.folderslib import GetFilesAndDirs from app.lib.folderslib import GetFilesAndDirs, create_folder
from app.db.sqlite.settings import SettingsSQLMethods as db from app.db.sqlite.settings import SettingsSQLMethods as db
from app.models.folder import Folder
from app.utils.hashing import create_folder_hash
from app.utils.wintools import win_replace_slash, is_windows from app.utils.wintools import win_replace_slash, is_windows
api = Blueprint("folder", __name__, url_prefix="/") api = Blueprint("folder", __name__, url_prefix="/")
@ -44,14 +42,7 @@ def get_folder_tree():
return { return {
"folders": [ "folders": [
Folder( create_folder(str(f)) for f in folders
name=f.name if f.name != "" else str(f).replace("\\", "/"),
path=win_replace_slash(str(f)),
has_tracks=True,
is_sym=f.is_symlink(),
path_hash=create_folder_hash(*f.parts[1:]),
)
for f in folders
], ],
"tracks": [], "tracks": [],
} }

View File

@ -8,7 +8,7 @@ from app.db.sqlite.settings import SettingsSQLMethods as sdb
from app.utils.generators import get_random_str from app.utils.generators import get_random_str
from app.utils.threading import background from app.utils.threading import background
from app.store.store import FolderStore from app.store.folder import FolderStore
from app.store.albums import AlbumStore from app.store.albums import AlbumStore
from app.store.tracks import TrackStore from app.store.tracks import TrackStore
from app.store.artists import ArtistStore from app.store.artists import ArtistStore

View File

@ -1,15 +1,44 @@
import os import os
from concurrent.futures import ThreadPoolExecutor from pathlib import Path
from app.logger import log
from app.models import Folder, Track from app.models import Folder, Track
from app.settings import SUPPORTED_FILES from app.settings import SUPPORTED_FILES
from app.logger import log
from app.utils.wintools import win_replace_slash from app.utils.wintools import win_replace_slash
from app.store.store import FolderStore
from app.store.tracks import TrackStore from app.store.tracks import TrackStore
def create_folder(path: str, count=0) -> Folder:
"""
Creates a folder object from a path.
"""
folder = Path(path)
return Folder(
name=folder.name,
path=win_replace_slash(str(folder)),
is_sym=folder.is_symlink(),
count=count
)
def get_folders(paths: list[str]):
"""
Filters out folders that don't have any tracks and
returns a list of folder objects.
"""
count_dict = {path: 0 for path in paths}
for track in TrackStore.tracks:
for path in paths:
if track.filepath.startswith(path):
count_dict[path] += 1
folders = [{"path": path, "count": count_dict[path]} for path in paths]
return [create_folder(f['path'], f['count']) for f in folders if f['count'] > 0]
class GetFilesAndDirs: class GetFilesAndDirs:
""" """
Get files and folders from a directory. Get files and folders from a directory.
@ -51,17 +80,6 @@ class GetFilesAndDirs:
files = [f["path"] for f in files_] files = [f["path"] for f in files_]
tracks = TrackStore.get_tracks_by_filepaths(files) tracks = TrackStore.get_tracks_by_filepaths(files)
folders = get_folders(dirs)
# TODO: Remove this threadpool and modify the get_folder store return tracks, folders
# method to accept a list of paths.
with ThreadPoolExecutor() as pool:
iterable = pool.map(FolderStore.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

@ -12,7 +12,7 @@ from app.logger import log
from app.models import Album, Artist, Track from app.models import Album, Artist, Track
from app.utils.filesystem import run_fast_scandir from app.utils.filesystem import run_fast_scandir
from app.store.store import FolderStore from app.store.folder import FolderStore
from app.store.albums import AlbumStore from app.store.albums import AlbumStore
from app.store.tracks import TrackStore from app.store.tracks import TrackStore
from app.store.artists import ArtistStore from app.store.artists import ArtistStore

View File

@ -17,7 +17,7 @@ from app.db.sqlite.tracks import SQLiteManager
from app.db.sqlite.tracks import SQLiteTrackMethods as db from app.db.sqlite.tracks import SQLiteTrackMethods as db
from app.db.sqlite.settings import SettingsSQLMethods as sdb from app.db.sqlite.settings import SettingsSQLMethods as sdb
from app.store.store import FolderStore from app.store.folder import FolderStore
from app.store.tracks import TrackStore from app.store.tracks import TrackStore
from app.store.albums import AlbumStore from app.store.albums import AlbumStore
from app.store.artists import ArtistStore from app.store.artists import ArtistStore

View File

@ -5,6 +5,5 @@ from dataclasses import dataclass
class Folder: class Folder:
name: str name: str
path: str path: str
has_tracks: bool
is_sym: bool = False is_sym: bool = False
path_hash: str = "" count: int = 0

View File

@ -1,7 +1,7 @@
""" """
Prepares the server for use. Prepares the server for use.
""" """
from app.store.store import FolderStore from app.store.folder import FolderStore
from app.setup.files import create_config_dir from app.setup.files import create_config_dir
from app.setup.sqlite import setup_sqlite, run_migrations from app.setup.sqlite import setup_sqlite, run_migrations

View File

@ -2,13 +2,13 @@
In memory store. In memory store.
""" """
from pathlib import Path from pathlib import Path
from tqdm import tqdm from tqdm import tqdm
from app.models import Folder from app.models import Folder
from app.utils.bisection import UseBisection from app.utils.bisection import UseBisection
from app.utils.hashing import create_folder_hash from app.utils.hashing import create_folder_hash
from app.utils.wintools import win_replace_slash
from app.lib import folderslib
from .tracks import TrackStore from .tracks import TrackStore
@ -44,21 +44,6 @@ class FolderStore:
path_hash = create_folder_hash(*Path(path).parts[1:]) path_hash = create_folder_hash(*Path(path).parts[1:])
return path_hash in folder_hashes return path_hash in folder_hashes
@staticmethod
def create_folder(path: str) -> Folder:
"""
Creates a folder object from a path.
"""
folder = Path(path)
return Folder(
name=folder.name,
path=win_replace_slash(str(folder)),
is_sym=folder.is_symlink(),
has_tracks=True,
path_hash=create_folder_hash(*folder.parts[1:]),
)
@classmethod @classmethod
def add_folder(cls, path: str): def add_folder(cls, path: str):
""" """
@ -68,7 +53,7 @@ class FolderStore:
if cls.check_has_tracks(path): if cls.check_has_tracks(path):
return return
folder = cls.create_folder(path) folder = folderslib.create_folder(path)
cls.folders.append(folder) cls.folders.append(folder)
@classmethod @classmethod
@ -109,7 +94,7 @@ class FolderStore:
pass pass
for path in tqdm(valid_folders, desc="Processing folders"): for path in tqdm(valid_folders, desc="Processing folders"):
folder = cls.create_folder(str(path)) folder = folderslib.create_folder(str(path))
cls.folders.append(folder) cls.folders.append(folder)
@ -130,21 +115,8 @@ class FolderStore:
if not has_tracks: if not has_tracks:
return None return None
folder = cls.create_folder(path) folder = folderslib.create_folder(path)
cls.folders.append(folder) cls.folders.append(folder)
return folder return folder
@classmethod # TODO: Remove this file. it's no longer needed.
def get_folders_count(cls, paths: list[str]) -> list[dict[str, int]]:
count_dict = {path: 0 for path in paths}
for track in TrackStore.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