Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ classifiers = [
]
dependencies = [
"Jinja2>=3.1.1,<3.2.0",
"dakarabase>=1.4.2,<1.5.0",
"dakarabase>=2.0.0,<2.1.0",
"filetype>=1.2.0,<1.3.0",
"packaging>=21.3,<22.0",
"path>=16.4.0,<16.5.0",
"python-mpv-jsonipc>=1.1.13,<1.2.0",
"python-vlc>=3.0.18121,<3.1.0,!=3.0.12117",
"setuptools>=68",
Expand Down
6 changes: 3 additions & 3 deletions src/dakara_player/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
handle_config_incomplete = generate_exception_handler(
DakaraError,
"Config may be incomplete, please check '{}'".format(
directories.user_config_dir / CONFIG_FILE
directories.user_config_path / CONFIG_FILE
),
)
handle_parameter_error = generate_exception_handler(
ParameterError,
"Config may be incomplete, please check '{}'".format(
directories.user_config_dir / CONFIG_FILE
directories.user_config_path / CONFIG_FILE
),
)

Expand Down Expand Up @@ -123,7 +123,7 @@ def play(args):
with handle_config_not_found():
create_logger()
config = Config(CONFIG_PREFIX)
config.load_file(directories.user_config_dir / CONFIG_FILE)
config.load_file(directories.user_config_path / CONFIG_FILE)
config.check_mandatory_keys(["player", "server"])
config.set_debug(args.debug)
set_loglevel(config)
Expand Down
8 changes: 4 additions & 4 deletions src/dakara_player/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ def get_audio_files(filepath):
"""Get audio files with the same name as provided file.

Args:
filepath (path.Path): Path of the initial file.
filepath (pathlib.Path): Path of the initial file.

Returns:
list of path.Path: List of paths of audio files.
list of pathlib.Path: List of paths of audio files.
"""
# list files with similar stem
items = filepath.dirname().glob("{}.*".format(filepath.stem))
items = filepath.parent.glob(f"{filepath.stem}.*")
return [item for item in items if item != filepath and is_audio_file(item)]


def is_audio_file(file_path):
"""Detect if a file is audio file based on standard magic numbers.

Args:
file_path (path.Path): Path of the file to investigate.
file_path (pathlib.Path): Path of the file to investigate.

Returns:
bool: `True` if the file is an audio file, `False` otherwise.
Expand Down
28 changes: 15 additions & 13 deletions src/dakara_player/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import logging
from importlib.resources import path
from pathlib import Path
from shutil import copy

from dakara_base.exceptions import DakaraError
from path import Path

logger = logging.getLogger(__name__)

Expand All @@ -27,6 +28,7 @@ class BackgroundLoader:
- `something.png`

and you use the following configuration:
>>> from pathlib import Path
>>> loader = BackgroundLoader(
... destination=Path("/destination"),
... directory=Path("/directory/custom"),
Expand All @@ -42,24 +44,24 @@ class BackgroundLoader:
>>> loader.load()
>>> loader.backgrounds
{
"idle": "/destination/idle.png",
"transition": "/destination/transition.png",
"other": "/destination/something.png"
"idle": Path("/destination/idle.png"),
"transition": Path("/destination/transition.png"),
"other": Path("/destination/something.png")
}

Args:
destination (path.Path): Where to copy found background files.
destination (pathlib.Path): Where to copy found background files.
package (str): Package checked for backgrounds by default.
directory (path.Path): Custom directory checked for backgrounds.
directory (pathlib.Path): Custom directory checked for backgrounds.
filenames (dict): Dictionary of background filenames. The key is the
background name, the value the background file name.

Attributes:
backgrounds (dict): Dictionary of background file paths. The key is
the background name, the value the background file path.
destination (path.Path): Where to copy found background files.
destination (pathlib.Path): Where to copy found background files.
package (str): Package checked for backgrounds by default.
directory (path.Path): Custom directory checked for backgrounds.
directory (pathlib.Path): Custom directory checked for backgrounds.
filenames (dict): Dictionary of background filenames. The key is the
background name, the value the background file name.
"""
Expand All @@ -73,7 +75,7 @@ def __init__(
):
self.destination = destination
self.package = package
self.directory = directory or Path()
self.directory = directory
self.filenames = filenames or {}
self.backgrounds = {}

Expand All @@ -91,16 +93,16 @@ def get_background_path(self, background_name, file_name):
file_name (str): Name of the background file.

Returns:
path.Path: Absolute path to the background file.
pathlib.Path: Absolute path to the background file.
"""
# trying to load from custom directory
if self.directory:
if self.directory is not None:
file_path = self.directory / file_name
if file_path.exists():
logger.debug(
"Loading custom %s background file '%s'", background_name, file_name
)
return file_path.copy(self.destination)
return Path(copy(file_path, self.destination))

# trying to load from package by default
try:
Expand All @@ -111,7 +113,7 @@ def get_background_path(self, background_name, file_name):
file_name,
)
file_path = Path(file)
return file_path.copy(self.destination)
return Path(copy(file_path, self.destination))

except FileNotFoundError as error:
raise BackgroundNotFoundError(
Expand Down
63 changes: 32 additions & 31 deletions src/dakara_player/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import platform
from abc import ABC, abstractmethod
from importlib.resources import contents, path
from pathlib import Path
from shutil import copy

from dakara_base.exceptions import DakaraError
from path import Path

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -77,7 +78,7 @@ def get_font_name_list(self):
font_file_name_list = [
file
for file in contents(self.package)
if Path(file).ext.lower() in FONT_EXTENSIONS
if Path(file).suffix.lower() in FONT_EXTENSIONS
]
logger.debug("Found %i font(s) to load", len(font_file_name_list))

Expand All @@ -87,7 +88,7 @@ def get_font_path_iterator(self):
"""Give font paths in font package.

Yields:
path.Path: Absolute path to the font, from the package.
pathlib.Path: Absolute path to the font, from the package.
"""
for font_file_name in self.get_font_name_list():
with path(self.package, font_file_name) as font_file_path:
Expand Down Expand Up @@ -116,9 +117,9 @@ class FontLoaderLinux(FontLoader):

Attributes:
package (str): Package checked for font files.
font_loaded (dict of path.Path): List of loaded fonts. The key is the
font file name and the value is the path of the installed font in
user directory.
font_loaded (dict of pathlib.Path): List of loaded fonts. The key is
the font file name and the value is the path of the installed font
in user directory.
"""

GREETINGS = "Font loader for Linux selected"
Expand All @@ -132,71 +133,71 @@ def __init__(self, *args, **kwargs):
# create list of fonts
self.fonts_loaded = {}

def get_system_font_path_list(self):
def get_system_font_name_list(self):
"""Retrieve the list of system fonts.

Returns:
list of path.Path: List of font paths.
list of pathlib.Path: List of font paths.
"""
return list(self.FONT_DIR_SYSTEM.walkfiles())
return [path.name for path in self.FONT_DIR_SYSTEM.rglob("*")]

def get_user_font_path_list(self):
def get_user_font_name_list(self):
"""Retrieve the list of user fonts.

Returns:
list of path.Path: List of font paths.
list of pathlib.Path: List of font paths.
"""
return list(self.FONT_DIR_USER.expanduser().walkfiles())
return [path.name for path in self.FONT_DIR_USER.expanduser().rglob("*")]

def load(self):
"""Load the fonts."""
# ensure that the user font directory exists
self.FONT_DIR_USER.expanduser().mkdir_p()
self.FONT_DIR_USER.expanduser().mkdir(parents=True, exist_ok=True)

# get system and user font files
system_font_path_list = self.get_system_font_path_list()
user_font_path_list = self.get_user_font_path_list()
system_font_name_list = self.get_system_font_name_list()
user_font_name_list = self.get_user_font_name_list()

# load fonts
for font_file_path in self.get_font_path_iterator():
self.load_font(font_file_path, system_font_path_list, user_font_path_list)
self.load_font(font_file_path, system_font_name_list, user_font_name_list)

def load_font(self, font_file_path, system_font_path_list, user_font_path_list):
def load_font(self, font_file_path, system_font_name_list, user_font_name_list):
"""Load the provided font.

Args:
font_file_path (path.Path): Absolute path of the font to load.
system_font_path_list (list of path.Path): List of absolute paths
of system fonts.
user_font_path_list (list of path.Path): List of absolute paths of
user fonts.
font_file_path (pathlib.Path): Absolute path of the font to load.
system_font_name_list (list of pathlib.Path): List of system fonts
name.
user_font_name_list (list of pathlib.Path): List of user fonts
name.
"""
# get font file name
font_file_name = font_file_path.basename()
font_file_name = font_file_path.name

# check if the font is installed at system level
if any(font_file_name in path for path in system_font_path_list):
if font_file_name in system_font_name_list:
logger.debug("Font '%s' found in system directory", font_file_name)
return

# check if the font is installed at user level
if any(font_file_name in path for path in user_font_path_list):
if font_file_name in user_font_name_list:
logger.debug("Font '%s' found in user directory", font_file_name)
return

# check if the font exists as broken link at user level
# in this case remove it and continue execution
font_file_user_path = self.FONT_DIR_USER.expanduser() / font_file_name
if font_file_user_path.islink():
if font_file_user_path.is_symlink():
logger.debug(
"Dead symbolic link found for font '%s' in user directory, "
"removing it",
font_file_name,
)
font_file_user_path.unlink_p()
font_file_user_path.unlink(missing_ok=True)

# then, if the font is not installed, load by copying it
font_file_path.copy(font_file_user_path)
copy(font_file_path, font_file_user_path)

# register the font
self.fonts_loaded[font_file_name] = font_file_user_path
Expand Down Expand Up @@ -250,8 +251,8 @@ class FontLoaderWindows(FontLoader):

Attributes:
package (str): Package checked for font files.
font_loaded (dict of path.Path): List of loaded fonts. The key is the
font file name and the value is the path of font used at
font_loaded (dict of pathlib.Path): List of loaded fonts. The key is
the font file name and the value is the path of font used at
installation.
"""

Expand Down Expand Up @@ -279,7 +280,7 @@ def load_font(self, font_file_path):
"""Load the provided font.

Args:
font_file_path (path.Path): Absolute path of the font to load.
font_file_path (pathlib.Path): Absolute path of the font to load.
"""
success = ctypes.windll.gdi32.AddFontResourceW(font_file_path)
if success:
Expand Down
Loading