mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-05 04:53:01 +00:00
refactor most things to use the database directly
This commit is contained in:
@@ -1,15 +1,11 @@
|
||||
from dataclasses import dataclass
|
||||
from os import scandir
|
||||
from time import time
|
||||
from typing import List
|
||||
from typing import Set
|
||||
from typing import Tuple
|
||||
|
||||
from app import api
|
||||
from app import helpers
|
||||
from app.models import Folder
|
||||
from app.models import Track
|
||||
from tqdm import tqdm
|
||||
|
||||
from app import instances
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -18,20 +14,12 @@ class Dir:
|
||||
is_sym: bool
|
||||
|
||||
|
||||
def get_valid_folders() -> None:
|
||||
for track in api.TRACKS:
|
||||
api.VALID_FOLDERS.add(track.folder)
|
||||
|
||||
|
||||
def get_folder_track_count(foldername: str) -> int:
|
||||
"""
|
||||
Returns the number of files associated with a folder.
|
||||
"""
|
||||
count = 0
|
||||
for track in api.TRACKS:
|
||||
if foldername in track.folder:
|
||||
count += 1
|
||||
return count
|
||||
tracks = instances.tracks_instance.find_tracks_inside_path_regex(foldername)
|
||||
return len(tracks)
|
||||
|
||||
|
||||
def create_folder(dir: Dir) -> Folder:
|
||||
@@ -46,51 +34,6 @@ def create_folder(dir: Dir) -> Folder:
|
||||
return Folder(folder)
|
||||
|
||||
|
||||
def create_all_folders() -> Set[Folder]:
|
||||
folders: List[Folder] = []
|
||||
|
||||
for foldername in tqdm(api.VALID_FOLDERS, desc="Creating folders"):
|
||||
folder = create_folder(foldername)
|
||||
folders.append(folder)
|
||||
|
||||
return folders
|
||||
|
||||
|
||||
def get_subdirs(foldername: str) -> List[Folder]:
|
||||
"""
|
||||
Finds and Creates Folder objects for each sub-directory string in the foldername passed.
|
||||
"""
|
||||
subdirs = set()
|
||||
|
||||
for folder in api.VALID_FOLDERS:
|
||||
if foldername in folder:
|
||||
str0 = folder.replace(foldername, "")
|
||||
|
||||
try:
|
||||
str1 = str0.split("/")[1]
|
||||
except IndexError:
|
||||
str1 = None
|
||||
|
||||
if str1 is not None:
|
||||
subdirs.add(foldername + "/" + str1)
|
||||
return [create_folder(dir) for dir in subdirs]
|
||||
|
||||
|
||||
@helpers.background
|
||||
def run_scandir():
|
||||
"""
|
||||
Initiates the creation of all folder objects for each folder with a track in it.
|
||||
|
||||
Runs in a background thread after every 5 minutes.
|
||||
It calls the
|
||||
"""
|
||||
get_valid_folders()
|
||||
# folders_ = create_all_folders()
|
||||
"""Create all the folder objects before clearing api.FOLDERS"""
|
||||
|
||||
# api.FOLDERS = folders_
|
||||
|
||||
|
||||
class getFnF:
|
||||
"""
|
||||
Get files and folders from a directory.
|
||||
@@ -99,15 +42,6 @@ class getFnF:
|
||||
def __init__(self, path: str) -> None:
|
||||
self.path = path
|
||||
|
||||
@classmethod
|
||||
def get_tracks(cls, files: List[str]) -> List[Track]:
|
||||
"""
|
||||
Returns a list of Track objects for each file in the given list.
|
||||
"""
|
||||
tracks = helpers.UseBisection(api.TRACKS, "filepath", files)()
|
||||
tracks = filter(lambda t: t is not None, tracks)
|
||||
return list(tracks)
|
||||
|
||||
def __call__(self) -> Tuple[Track, Folder]:
|
||||
try:
|
||||
all = scandir(self.path)
|
||||
@@ -125,9 +59,11 @@ class getFnF:
|
||||
dirs.append(Dir(**dir))
|
||||
elif entry.is_file() and entry.name.endswith((".mp3", ".flac")):
|
||||
files.append(entry.path)
|
||||
tracks = self.get_tracks(files)
|
||||
tracks = instances.tracks_instance.find_songs_by_folder(self.path)
|
||||
tracks = [Track(track) for track in tracks]
|
||||
|
||||
folders = [create_folder(dir) for dir in dirs]
|
||||
|
||||
folders = filter(lambda f: f.trackcount > 0, folders)
|
||||
|
||||
return tracks, folders
|
||||
|
||||
Reference in New Issue
Block a user