Debian Codegraph @

beetsplug/loadext.py

raw
1 """Load SQLite extensions."""
2
3 from __future__ import annotations
4
5 import sqlite3
6 from typing import TYPE_CHECKING
7
8 from beets.dbcore import Database
9 from beets.plugins import BeetsPlugin
10
11 if TYPE_CHECKING:
12 from beets.library import Library
13
14
15 class LoadExtPlugin(BeetsPlugin):
16 def __init__(self) -> None:
17 super().__init__()
18
19 if not Database.supports_extensions:
20 self._log.warning(
21 "loadext is enabled but the current SQLite "
22 "installation does not support extensions"
23 )
24 return
25
26 self.register_listener("library_opened", self.library_opened)
27
28 def library_opened(self, lib: Library) -> None:
29 for v in self.config.sequence():
30 ext = v.as_filename()
31
32 self._log.debug("loading extension {}", ext)
33
34 try:
35 lib.load_extension(ext)
36 except sqlite3.OperationalError as e:
37 self._log.error("failed to load extension {}: {}", ext, e)