Skip to content

Commit

Permalink
Now support next track, major fix to player crash in different direct…
Browse files Browse the repository at this point in the history
…ory with current playlist.
  • Loading branch information
jupiterbjy committed Dec 8, 2021
1 parent 1fa8220 commit 8b62b53
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 200 deletions.
77 changes: 0 additions & 77 deletions CUIAudioPlayer/CompatibilityPatch.py

This file was deleted.

55 changes: 25 additions & 30 deletions CUIAudioPlayer/FileWalker.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# TODO: separate audio related logic from pathwrapper via subclass
class PathWrapper:
primary_formats = set("." + key.lower() for key in sf.available_formats().keys())
secondary_formats = {".m4a", ".mp3"} if PY_DUB_ENABLED else {}
secondary_formats = {".m4a", ".mp3"} if PY_DUB_ENABLED else set()
supported_formats = primary_formats | secondary_formats
supported_formats = supported_formats | set(key.upper() for key in supported_formats)

Expand All @@ -33,42 +33,43 @@ def __init__(self, path: str = "./"):
self.folder_list: List[pathlib.Path] = []

def list_audio(self) -> Generator[pathlib.Path, None, None]:
return (path_obj for path_obj in self.list_file() if path_obj.suffix in self.supported_formats)
yield from (path_obj for path_obj in self.list_file() if path_obj.suffix in self.supported_formats)

def list_folder(self) -> Generator[pathlib.Path, None, None]:
"""
First element will be current folder location. either use next() or list()[1:] to skip it.
"""

def generator():
yield self.current_path.parent
for item in self.current_path.glob("*/"):
if item.is_dir():
yield item

return generator()
yield self.current_path.parent
yield from (path_ for path_ in self.current_path.iterdir() if path_.is_dir())

def list_file(self) -> Generator[pathlib.Path, None, None]:
"""
Can't use glob as it match folders such as .git, using pathlib.Path object instead.
"""

return (item for item in self.current_path.glob("*/") if item.is_file())
yield from (item for item in self.current_path.iterdir() if item.is_file())

def step_in(self, directory: Union[str, pathlib.Path]):
def step_in(self, directory_idx: int):
"""
Relative / Absolute paths supported.
"""

self.current_path = self.current_path.joinpath(directory)
try:
self.current_path = self.folder_list[directory_idx]
except IndexError as err:
raise NotADirectoryError(f"Directory index {directory_idx} does not exist!") from err

self.refresh_list()
return self.current_path

def step_out(self, depth=1):
if depth <= 0:
def step_out(self):

if self.current_path == self.current_path.parent:
return self.current_path

self.current_path = self.current_path.parents[depth - 1]
self.current_path = self.current_path.parent

self.refresh_list()
return self.current_path

Expand All @@ -81,18 +82,12 @@ def refresh_list(self):

def fetch_meta(self):
# This might have to deal the cases such as path changing before generator fires up.
def generator():
for file_dir in self.list_audio():
yield TinyTag.get(file_dir)

return generator()
for file_dir in self.list_audio():
yield TinyTag.get(file_dir)

def fetch_tag_data(self):
def generator():
for file_dir in self.list_audio():
yield TinyTag.get(file_dir)

return generator()
for file_dir in self.list_audio():
yield TinyTag.get(file_dir)

def __len__(self):
return len(self.folder_list) + len(self.audio_file_list)
Expand All @@ -108,9 +103,9 @@ def __getitem__(self, item: int):
return self.audio_file_list[item - len(self.folder_list)]

def index(self, target: Union[str, pathlib.Path]):
path_ = pathlib.Path(target)
try:
return len(self.folder_list) + self.audio_file_list.index(target)
except ValueError:
# assuming it's pure string directory.
path_converted = pathlib.Path(target)
return len(self.folder_list) + self.audio_file_list.index(path_converted)
return len(self.folder_list) + self.audio_file_list.index(path_)
except ValueError as err:
raise IndexError(f"Cannot find given target '{path_.as_posix()}'!") from err

7 changes: 6 additions & 1 deletion CUIAudioPlayer/LoggingConfigurator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import logging
import inspect

from loguru import logger
assert logger
# all below are now deprecated

LOG_DETAILED_CALLER = True
# This will log where the function is from.
Expand All @@ -23,6 +26,7 @@
def get_caller_stack_name(depth=1):
"""
Gets the name of caller.
:param depth: determine which scope to inspect, for nested usage.
"""
return inspect.stack()[depth][3]
Expand Down Expand Up @@ -94,7 +98,8 @@ def inner(msg, *args, **kwargs):
return inner


logger = CallerLoggedLogger()
# logger = CallerLoggedLogger()


# def logging_decorator(func):
# async def inner()
Expand Down
Loading

0 comments on commit 8b62b53

Please sign in to comment.