Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate database change event send on Library.add #5561

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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: 2 additions & 1 deletion beets/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,9 @@ def remove(self):
plugins.send("database_change", lib=self._db, model=self)

def add(self, lib=None):
# super().add() calls self.store(), which sends `database_change`,
# so don't do it here
super().add(lib)
plugins.send("database_change", lib=self._db, model=self)

def __format__(self, spec):
if not spec:
Expand Down
3 changes: 2 additions & 1 deletion beets/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import beets
from beets import logging
from beets.types import EventType

PLUGIN_NAMESPACE = "beetsplug"

Expand Down Expand Up @@ -204,14 +205,14 @@
_raw_listeners = None
listeners = None

def register_listener(self, event, func):
def register_listener(self, event: EventType, func):
"""Add a function as a listener for the specified event."""
wrapped_func = self._set_log_level_and_params(logging.WARNING, func)

cls = self.__class__
if cls.listeners is None or cls._raw_listeners is None:
cls._raw_listeners = defaultdict(list)

Check failure on line 214 in beets/plugins.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Need type annotation for "_raw_listeners"
cls.listeners = defaultdict(list)

Check failure on line 215 in beets/plugins.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Need type annotation for "listeners"
if func not in cls._raw_listeners[event]:
cls._raw_listeners[event].append(func)
cls.listeners[event].append(wrapped_func)
Expand Down Expand Up @@ -290,7 +291,7 @@
)


_instances = {}

Check failure on line 294 in beets/plugins.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Need type annotation for "_instances" (hint: "_instances: dict[<type>, <type>] = ...")


def find_plugins():
Expand Down
33 changes: 33 additions & 0 deletions beets/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from typing import Literal

EventType = Literal[
"pluginload",
"import",
"album_imported",
"album_removed",
"item_copied",
"item_imported",
"before_item_moved",
"item_moved",
"item_linked",
"item_hardlinked",
"item_reflinked",
"item_removed",
"write",
"after_write",
"import_task_created",
"import_task_start",
"import_task_apply",
"import_task_before_choice",
"import_task_choice",
"import_task_files",
"library_opened",
"database_change",
"cli_exit",
"import_begin",
"trackinfo_received",
"albuminfo_received",
"before_choose_candidate",
"mb_track_extract",
"mb_album_extract",
]
4 changes: 3 additions & 1 deletion beets/util/hidden.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
from typing import Union


def is_hidden(path: Union[bytes, Path]) -> bool:
def is_hidden(path: Union[bytes, str, Path]) -> bool:
"""
Determine whether the given path is treated as a 'hidden file' by the OS.
"""

if isinstance(path, bytes):
path = Path(os.fsdecode(path))
elif isinstance(path, str):
path = Path(path)

# TODO: Avoid doing a platform check on every invocation of the function.
# TODO: Stop supporting 'bytes' inputs once 'pathlib' is fully integrated.
Expand Down
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Bug fixes:
:bug:`5265`
:bug:`5371`
:bug:`4715`
* Fix an issue where calling `Library.add` would cause the `database_change`
event to be sent twice, not once.
:bug:`5560`

For packagers:

Expand Down
26 changes: 26 additions & 0 deletions test/test_database_change.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os

import beets
import beets.logging as blog
from beets.test import _common
from beets.test.helper import BeetsTestCase, capture_log


class DatabaseChangeTestBase(BeetsTestCase):
def test_item_added_one_database_change(self):
self.item = _common.item()
self.item.path = beets.util.normpath(
os.path.join(
self.temp_dir,
b"a",
b"b.mp3",
)
)
self.item.album = "a"
self.item.title = "b"

blog.getLogger("beets").set_global_level(blog.DEBUG)
with capture_log() as logs:
self.lib.add(self.item)

assert logs.count("Sending event: database_change") == 1
Loading