Skip to content

Commit 6941504

Browse files
author
geigerzaehler
committed
Merge pull request #652 from geigerzaehler/item-write
Add path argument to item.write()
2 parents ee2cf0d + 4970c1d commit 6941504

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

beets/library.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,17 +414,23 @@ def read(self, read_path=None):
414414

415415
self.path = read_path
416416

417-
def write(self):
418-
"""Write the item's metadata to the associated file.
417+
def write(self, path=None):
418+
"""Write the item's metadata to a media file.
419+
420+
``path`` defaults to the item's path property.
419421
420422
Can raise either a `ReadError` or a `WriteError`.
421423
"""
424+
if path is None:
425+
path = self.path
426+
else:
427+
path = normpath(path)
422428
try:
423-
f = MediaFile(syspath(self.path))
429+
f = MediaFile(syspath(path))
424430
except (OSError, IOError) as exc:
425431
raise ReadError(self.path, exc)
426432

427-
plugins.send('write', item=self)
433+
plugins.send('write', item=self, path=path)
428434

429435
for key in ITEM_KEYS_WRITABLE:
430436
setattr(f, key, self[key])

beets/plugins.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import logging
1818
import traceback
1919
from collections import defaultdict
20+
import inspect
2021

2122
import beets
2223

@@ -339,4 +340,8 @@ def send(event, **arguments):
339340
Returns a list of return values from the handlers.
340341
"""
341342
log.debug('Sending event: %s' % event)
342-
return [handler(**arguments) for handler in event_handlers()[event]]
343+
for handler in event_handlers()[event]:
344+
# Don't break legacy plugins if we want to pass more arguments
345+
argspec = inspect.getargspec(handler).args
346+
args = dict((k, v) for k, v in arguments.items() if k in argspec)
347+
handler(**args)

beetsplug/convert.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,12 @@ def convert_item(dest_dir, keep_new, path_formats):
174174
encode(item.path, dest)
175175

176176
# Write tags from the database to the converted file.
177-
if not keep_new:
178-
item.path = dest
179-
item.write()
177+
item.write(path=dest)
180178

181-
# If we're keeping the transcoded file, read it again (after
182-
# writing) to get new bitrate, duration, etc.
183179
if keep_new:
180+
# If we're keeping the transcoded file, read it again (after
181+
# writing) to get new bitrate, duration, etc.
182+
item.path = dest
184183
item.read()
185184
item.store() # Store new path and audio data.
186185

test/test_library.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from beets import util
3030
from beets import plugins
3131
from beets import config
32+
from beets.mediafile import MediaFile
3233

3334
TEMP_LIB = os.path.join(_common.RSRC, 'test_copy.blb')
3435

@@ -958,6 +959,20 @@ def test_no_write_permission(self):
958959
self.i.path = path
959960
self.assertRaises(beets.library.WriteError, self.i.write)
960961

962+
def test_write_with_custom_path(self):
963+
custom_path = os.path.join(self.temp_dir, 'file.mp3')
964+
self.i.path = os.path.join(self.temp_dir, 'item_file.mp3')
965+
shutil.copy(os.path.join(_common.RSRC, 'empty.mp3'), custom_path)
966+
shutil.copy(os.path.join(_common.RSRC, 'empty.mp3'), self.i.path)
967+
968+
self.i['artist'] = 'new artist'
969+
self.assertNotEqual(MediaFile(custom_path).artist, 'new artist')
970+
self.assertNotEqual(MediaFile(self.i.path).artist, 'new artist')
971+
972+
self.i.write(custom_path)
973+
self.assertEqual(MediaFile(custom_path).artist, 'new artist')
974+
self.assertNotEqual(MediaFile(self.i.path).artist, 'new artist')
975+
961976

962977
def suite():
963978
return unittest.TestLoader().loadTestsFromName(__name__)

0 commit comments

Comments
 (0)