Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __copy_file(self, dest_path, metadata):
# Copy files
resource_path = os.path.join(_common.RSRC, b'full.mp3')
shutil.copy(resource_path, dest_path)
medium = MediaFile(dest_path)
# Set metadata
for attr in metadata:
setattr(medium, attr, metadata[attr])
medium.save()
def test_write_extended_tag_from_item(self):
plugin = BeetsPlugin()
plugin.add_media_field('customtag', field_extension)
try:
mf = self._mediafile_fixture('empty')
self.assertIsNone(mf.customtag)
item = Item(path=mf.path, customtag=u'Gb')
item.write()
mf = mediafile.MediaFile(mf.path)
self.assertEqual(mf.customtag, u'Gb')
finally:
delattr(mediafile.MediaFile, 'customtag')
Item._media_fields.remove('customtag')
def test_delete_tag(self):
mediafile = self._mediafile_fixture('full')
keys = self.full_initial_tags.keys()
for key in set(keys) - set(['art', 'month', 'day']):
self.assertIsNotNone(getattr(mediafile, key))
for key in keys:
delattr(mediafile, key)
mediafile.save()
mediafile = MediaFile(mediafile.path)
for key in keys:
self.assertIsNone(getattr(mediafile, key))
def test_write_genre_list(self):
mediafile = self._mediafile_fixture('empty')
mediafile.genres = [u'one', u'two']
mediafile.save()
mediafile = MediaFile(mediafile.path)
assertCountEqual(self, mediafile.genres, [u'one', u'two'])
def test_embed_art_from_album(self):
self._setup_data()
album = self.add_album_fixture()
item = album.items()[0]
album.artpath = self.small_artpath
album.store()
self.run_command('embedart', '-y')
mediafile = MediaFile(syspath(item.path))
self.assertEqual(mediafile.images[0].data, self.image_data)
def test_write_dates(self):
mediafile = self._mediafile_fixture('full')
mediafile.date = datetime.date(2001, 1, 2)
mediafile.original_date = datetime.date(1999, 12, 30)
mediafile.save()
mediafile = MediaFile(mediafile.path)
self.assertEqual(mediafile.year, 2001)
self.assertEqual(mediafile.month, 1)
self.assertEqual(mediafile.day, 2)
self.assertEqual(mediafile.date, datetime.date(2001, 1, 2))
self.assertEqual(mediafile.original_year, 1999)
self.assertEqual(mediafile.original_month, 12)
self.assertEqual(mediafile.original_day, 30)
self.assertEqual(mediafile.original_date, datetime.date(1999, 12, 30))
def test_unicode_label_in_m4a(self):
self.mf.label = u'foo\xe8bar'
self.mf.save()
new_mf = mediafile.MediaFile(self.path)
self.assertEqual(new_mf.label, u'foo\xe8bar')
def read(self, read_path=None):
"""Read the metadata from the associated file.
If `read_path` is specified, read metadata from that file
instead. Updates all the properties in `_media_fields`
from the media file.
Raises a `ReadError` if the file could not be read.
"""
if read_path is None:
read_path = self.path
else:
read_path = normpath(read_path)
try:
mediafile = MediaFile(syspath(read_path))
except UnreadableFileError as exc:
raise ReadError(read_path, exc)
for key in self._media_fields:
value = getattr(mediafile, key)
if isinstance(value, six.integer_types):
if value.bit_length() > 63:
value = 0
self[key] = value
# Database's mtime should now reflect the on-disk value.
if read_path == self.path:
self.mtime = self.current_mtime()
self.path = read_path
def add_or_update(self, path):
"""
Add or update a libray track in the catalog.
It's important to note, that if a track has both changed paths and
changed metadata, the path and file checksum will no longer match, thus
the track will be added as a *new* item in the catalog.
"""
media = mediafile.MediaFile(path)
track = mediafile_to_track(media, self.library_path)
# Update artwork cache
if track.artwork_hash:
ensure_artwork_cache(prefix=self.artwork_path, media=media, track=track)
# Get ID of the track with matching path or MD5
track_filter = sqlalchemy.or_(
db.Track.file_path == track.file_path, db.Track.file_hash == track.file_hash
)
try:
old_track = self.db_session.query(db.Track).filter(track_filter).one()
track.id = old_track.id
except sqlalchemy.orm.exc.NoResultFound:
pass
self._log.error(u'could not open file to scrub: {0}',
exc)
return
images = mf.images
# Remove all tags.
self._scrub(item.path)
# Restore tags, if enabled.
if restore:
self._log.debug(u'writing new tags after scrub')
item.try_write()
if images:
self._log.debug(u'restoring art')
try:
mf = mediafile.MediaFile(util.syspath(item.path),
config['id3v23'].get(bool))
mf.images = images
mf.save()
except mediafile.UnreadableFileError as exc:
self._log.error(u'could not write tags: {0}', exc)