-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlastimport.py
58 lines (45 loc) · 1.72 KB
/
lastimport.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -*-
"""Plugin providing a reference to the last successful import. Imported items'
ids are stored in a separate table in the database during import. Provides the
command `lastimport' to list them.
"""
from beets.plugins import BeetsPlugin
from beets.util import normpath
from beets.ui import Subcommand, print_
def import_hook(lib, paths):
"""Store imported items' ids in a database table"""
paths = [normpath(path) for path in paths]
with lib.transaction() as tx:
sql = """
DROP TABLE IF EXISTS last_import;
CREATE TABLE last_import (id INTEGER PRIMARY KEY);
"""
tx.script(sql)
params = ', '.join('?' * len(paths))
sql = """
INSERT INTO last_import
SELECT id FROM items WHERE path IN ({})
""".format(params)
tx.mutate(sql, paths)
def list_items(lib, opts, args):
"""Print a list of items from the last import
Output is formatted according to the `format_item' configuration option
except when the -p switch is used -- prints paths in this case.
"""
ids = []
with lib.transaction() as tx:
sql = 'SELECT id FROM last_import'
ids = [row['id'] for row in tx.query(sql)]
for item_id in ids:
for item in lib.items('id::^{}$'.format(item_id)):
print_(str(item))
class LastImportPlugin(BeetsPlugin):
"""Keep track of the last imported items"""
def __init__(self):
super(LastImportPlugin, self).__init__()
self.register_listener('import', import_hook)
def commands(self):
cmd = Subcommand('lastimport', help=u'list last imported items')
cmd.parser.add_path_option()
cmd.func = list_items
return [cmd]