Debian Codegraph @

beetsplug/ihate.py

raw
1 """Warns you about things you hate (or even blocks import)."""
2
3 from __future__ import annotations
4
5 from typing import TYPE_CHECKING
6
7 from beets.importer import Action
8 from beets.library import Album, Item, parse_query_string
9 from beets.plugins import BeetsPlugin
10
11 if TYPE_CHECKING:
12 from collections.abc import Iterable
13
14 from beets.importer import ImportSession, ImportTask
15
16
17 __author__ = "[email protected]"
18 __version__ = "2.0"
19
20
21 class IHatePlugin(BeetsPlugin):
22 def __init__(self) -> None:
23 super().__init__()
24 self.register_listener(
25 "import_task_choice", self.import_task_choice_event
26 )
27 self.config.add({"warn": [], "skip": []})
28
29 @classmethod
30 def do_i_hate_this(
31 cls, task: ImportTask, action_patterns: Iterable[str]
32 ) -> bool:
33 """Process group of patterns (warn or skip) and returns True if
34 task is hated and not whitelisted.
35 """
36 if action_patterns:
37 for query_string in action_patterns:
38 query, _ = parse_query_string(
39 query_string, Album if task.is_album else Item
40 )
41 if any(query.match(item) for item in task.imported_items()):
42 return True
43 return False
44
45 def import_task_choice_event(
46 self, session: ImportSession, task: ImportTask
47 ) -> None:
48 skip_queries = self.config["skip"].as_str_seq()
49 warn_queries = self.config["warn"].as_str_seq()
50
51 if task.choice_flag == Action.APPLY:
52 if skip_queries or warn_queries:
53 self._log.debug("processing your hate")
54 if self.do_i_hate_this(task, skip_queries):
55 task.choice_flag = Action.SKIP
56 self._log.info("skipped: {}", task.source.desc)
57 return
58 if self.do_i_hate_this(task, warn_queries):
59 self._log.info("you may hate this: {}", task.source.desc)
60 else:
61 self._log.debug("nothing to do")
62 else:
63 self._log.debug("user made a decision, nothing to do")