1# -*- coding: utf-8 -*-
2# This file is part of beets.
3# Copyright 2019, Jack Wilsdon <jack.wilsdon@gmail.com>
4#
5# Permission is hereby granted, free of charge, to any person obtaining
6# a copy of this software and associated documentation files (the
7# "Software"), to deal in the Software without restriction, including
8# without limitation the rights to use, copy, modify, merge, publish,
9# distribute, sublicense, and/or sell copies of the Software, and to
10# permit persons to whom the Software is furnished to do so, subject to
11# the following conditions:
12#
13# The above copyright notice and this permission notice shall be
14# included in all copies or substantial portions of the Software.
15
16"""Load SQLite extensions.
17"""
18
19from __future__ import division, absolute_import, print_function
20
21from beets.dbcore import Database
22from beets.plugins import BeetsPlugin
23import sqlite3
24
25
26class LoadExtPlugin(BeetsPlugin):
27    def __init__(self):
28        super(LoadExtPlugin, self).__init__()
29
30        if not Database.supports_extensions:
31            self._log.warn('loadext is enabled but the current SQLite '
32                           'installation does not support extensions')
33            return
34
35        self.register_listener('library_opened', self.library_opened)
36
37    def library_opened(self, lib):
38        for v in self.config:
39            ext = v.as_filename()
40
41            self._log.debug(u'loading extension {}', ext)
42
43            try:
44                lib.load_extension(ext)
45            except sqlite3.OperationalError as e:
46                self._log.error(u'failed to load extension {}: {}', ext, e)
47