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
66 changes: 51 additions & 15 deletions src/dakara_player/background.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Manage background images for media players."""

import logging
from importlib.resources import path
from importlib.resources import as_file, files
from pathlib import Path
from shutil import copy

Expand Down Expand Up @@ -97,29 +97,65 @@ def get_background_path(self, background_name, file_name):
"""
# trying to load from custom 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 Path(copy(file_path, self.destination))
try:
return self.copy_custom_background(background_name, file_name)

except FileNotFoundError:
pass

# trying to load from package by default
try:
with path(self.package, file_name) as file:
logger.debug(
"Loading default %s background file '%s'",
background_name,
file_name,
)
file_path = Path(file)
return Path(copy(file_path, self.destination))
return self.copy_default_background(background_name, file_name)

except FileNotFoundError as error:
raise BackgroundNotFoundError(
f"No {background_name} background file found for '{file_name}'"
) from error

def copy_custom_background(self, background_name, file_name):
"""Copy a custom background.

Args:
background_name (str): Name of the background.
file_name (str): Name of the background file.

Returns:
pathlib.Path: Absolute path to the background file.

Raises:
FileNotFoundError: If the custom file does not exist (by
`shuti.copy`).
"""
file_path = self.directory / file_name
output_path = Path(copy(file_path, self.destination))
logger.debug(
"Loading custom %s background file '%s'", background_name, file_name
)
return output_path

def copy_default_background(self, background_name, file_name):
"""Copy a default background.

Args:
background_name (str): Name of the background.
file_name (str): Name of the background file.

Returns:
pathlib.Path: Absolute path to the background file.

Raises:
FileNotFoundError: If the custom file does not exist (by
`shuti.copy`).
"""
with as_file(files(self.package).joinpath(file_name)) as file_path:
output_path = Path(copy(file_path, self.destination))
logger.debug(
"Loading default %s background file '%s'",
background_name,
file_name,
)
return output_path


class BackgroundNotFoundError(DakaraError, FileNotFoundError):
"""Error raised when a background cannot be found"""
14 changes: 8 additions & 6 deletions src/dakara_player/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
import platform
from abc import ABC, abstractmethod
from importlib.resources import contents, path
from importlib.resources import as_file, files
from pathlib import Path
from shutil import copy

Expand Down Expand Up @@ -76,9 +76,9 @@ def get_font_name_list(self):
"""
logger.debug("Scanning fonts directory")
font_file_name_list = [
file
for file in contents(self.package)
if Path(file).suffix.lower() in FONT_EXTENSIONS
file.name
for file in files(self.package).iterdir()
if file.is_file() and file.suffix.lower() in FONT_EXTENSIONS
]
logger.debug("Found %i font(s) to load", len(font_file_name_list))

Expand All @@ -91,8 +91,10 @@ def get_font_path_iterator(self):
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:
yield Path(font_file_path)
with as_file(
files(self.package).joinpath(font_file_name)
) as font_file_path:
yield font_file_path


class FontLoaderLinux(FontLoader):
Expand Down
4 changes: 2 additions & 2 deletions src/dakara_player/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import json
import logging
from importlib.resources import path
from importlib.resources import as_file, files
from pathlib import Path

from dakara_base.exceptions import DakaraError
Expand Down Expand Up @@ -88,7 +88,7 @@ def load(self):

def load_icon_map(self):
"""Load the icon map."""
with path("dakara_player.resources", ICON_MAP_FILE) as file:
with as_file(files("dakara_player.resources").joinpath(ICON_MAP_FILE)) as file:
self.icon_map = json.loads(file.read_text())

def load_templates(self):
Expand Down
29 changes: 12 additions & 17 deletions src/dakara_player/user_resources.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,43 @@
"""Manage the user resource directory and files."""

import logging
from distutils.util import strtobool
from importlib.resources import contents, path
from importlib.resources import files
from shutil import copy

from dakara_base.directory import directories
from dakara_base.utils import strtobool

logger = logging.getLogger(__name__)


def copy_resource(resource, destination, force):
def copy_resource(package, destination, force):
"""Copy the content of one resource directory.

Args:
resource (str): Resource to copy.
package (str): Package of resources to copy.
destination (pathlib.Path): Directory where to copy the resource.
force (bool): If the destination exists and this flag is set to `True`,
overwrite the destination.
"""
if not force and destination.exists():
try:
result = strtobool(
input(
f"Directory {destination} already exists, "
"overwrite it with its content? [y/N] "
)
result = strtobool(
input(
f"Directory {destination} already exists, "
"overwrite it with its content? [y/N] "
)

except ValueError:
result = False
)

if not result:
return

destination.mkdir(parents=True, exist_ok=True)

for file_name in contents(resource):
for resource in files(package).iterdir():
# ignore Python files
if file_name.startswith("__"):
if resource.name.startswith("__"):
continue

with path(resource, file_name) as file:
copy(file, destination)
copy(resource, destination)


def create_resource_files(force=False):
Expand Down
19 changes: 7 additions & 12 deletions tests/integration/base.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
from importlib.resources import as_file, files
from pathlib import Path
from queue import Empty
from shutil import copy
from tempfile import TemporaryDirectory
from time import sleep
from unittest import TestCase

try:
from importlib.resources import path

except ImportError:
from importlib_resources import path


class TestCasePoller(TestCase):
"""Test class that can poll the state of tested player."""
Expand Down Expand Up @@ -94,24 +89,24 @@ def setUp(self):
self.kara_folder_path = Path(self.kara_folder.name).resolve()

# create subtitle
with path("tests.resources", "song1.ass") as file:
with as_file(files("tests.resources").joinpath("song1.ass")) as file:
self.subtitle1_path = Path(copy(file, self.kara_folder_path))

with path("tests.resources", "song2.ass") as file:
with as_file(files("tests.resources").joinpath("song2.ass")) as file:
self.subtitle2_path = Path(copy(file, self.kara_folder_path))

# create song
with path("tests.resources", "song1.mkv") as file:
with as_file(files("tests.resources").joinpath("song1.mkv")) as file:
self.song1_path = Path(copy(file, self.kara_folder_path))

with path("tests.resources", "song2.mkv") as file:
with as_file(files("tests.resources").joinpath("song2.mkv")) as file:
self.song2_path = Path(copy(file, self.kara_folder_path))

with path("tests.resources", "song3.avi") as file:
with as_file(files("tests.resources").joinpath("song3.avi")) as file:
self.song3_path = Path(copy(file, self.kara_folder_path))

# create audio
with path("tests.resources", "song2.mp3") as file:
with as_file(files("tests.resources").joinpath("song2.mp3")) as file:
self.audio2_path = Path(copy(file, self.kara_folder_path))

# create playlist entry
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/test_audio.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from importlib.resources import path
from importlib.resources import as_file, files
from pathlib import Path
from unittest import TestCase
from unittest.mock import patch
Expand All @@ -9,17 +9,17 @@
class IsAudioFileTestCase(TestCase):
def test_mp3(self):
"""Test to detect a MP3 file."""
with path("tests.resources", "song2.mp3") as file:
with as_file(files("tests.resources").joinpath("song2.mp3")) as file:
self.assertTrue(is_audio_file(file))

def test_ass(self):
"""Test to not detect an ASS file."""
with path("tests.resources", "song2.ass") as file:
with as_file(files("tests.resources").joinpath("song2.ass")) as file:
self.assertFalse(is_audio_file(file))

def test_mkv(self):
"""Test to not detect a MKV file."""
with path("tests.resources", "song2.mkv") as file:
with as_file(files("tests.resources").joinpath("song2.mkv")) as file:
self.assertFalse(is_audio_file(file))


Expand Down
Loading