def test_delete_item_with_file(self): web.app.config['READONLY'] = False # Create an item with a file ipath = os.path.join(self.temp_dir, b'testfile2.mp3') shutil.copy(os.path.join(_common.RSRC, b'full.mp3'), ipath) self.assertTrue(os.path.exists(ipath)) item_id = self.lib.add(Item.from_path(ipath)) # Check we can find the temporary item we just created response = self.client.get('/item/' + str(item_id)) res_json = json.loads(response.data.decode('utf-8')) self.assertEqual(response.status_code, 200) self.assertEqual(res_json['id'], item_id) # Delete item by id, with file response = self.client.delete('/item/' + str(item_id) + '?delete') res_json = json.loads(response.data.decode('utf-8')) self.assertEqual(response.status_code, 200) # Check the item has gone response = self.client.get('/item/' + str(item_id)) self.assertEqual(response.status_code, 404) # Check the file has gone self.assertFalse(os.path.exists(ipath))
def addItemFixture(self, basename='ok.ogg'): src = os.path.join(self.fixture_dir, basename) dst = os.path.join(self.libdir, basename) shutil.copy(src, dst) item = Item.from_path(dst) item.add(self.lib) return item
def test_get_single_item_by_path(self): data_path = os.path.join(_common.RSRC, b'full.mp3') self.lib.add(Item.from_path(data_path)) response = self.client.get('/item/path/' + data_path.decode('utf-8')) res_json = json.loads(response.data.decode('utf-8')) self.assertEqual(response.status_code, 200) self.assertEqual(res_json['title'], 'full')
def test_get_single_item_by_path(self): data_path = os.path.join(_common.RSRC, b'full.mp3') self.lib.add(Item.from_path(data_path)) response = self.client.get('/item/path/' + data_path.decode('utf-8')) res_json = json.loads(response.data.decode('utf-8')) self.assertEqual(response.status_code, 200) self.assertEqual(res_json['title'], u'full')
def setUp(self): self.setup_beets() self.load_plugins('edit') # Create some mediafiles, and store them for comparison. self._create_import_dir(3) self.items_orig = [Item.from_path(f.path) for f in self.media_files] self.matcher = AutotagStub().install() self.matcher.matching = AutotagStub.GOOD self.config['import']['timid'] = True
def setupFixtureLibrary(self): self.import_dir = os.path.join(self.temp_dir, 'import') for file in os.listdir(self.fixture_dir): src = os.path.join(self.fixture_dir, file) dst = os.path.join(self.libdir, file) shutil.copy(src, dst) item = Item.from_path(dst) item.add(self.lib) check.set_checksum(item)
def add_item_fixtures(self, ext='mp3', count=1): items = [] paths = glob(os.path.join(_common.RSRC, '*.' + ext)) for path in paths[0:count]: item = Item.from_path(str(path)) item.add(self.lib) item.move(copy=True) item.store() items.append(item) return items
def pre_process_folder(folder): p = Path(folder) assert p.isdir(), "Got non directory input, breaking" paths = _collect_files_from_folder(p) items = [Item.from_path(str(f)) for f in paths] tempdir = tempfile.mkdtemp(prefix="temp_") for item in items: filename = os.path.basename(item.path) item.move_file(os.path.join(tempdir, filename), copy=True) yield AlbumTask(items)
def test_album_art(self): self.config['zero']['fields'] = ['images'] path = self.create_mediafile_fixture(images=['jpg']) item = Item.from_path(path) self.load_plugins('zero') item.write() mf = MediaFile(syspath(path)) self.assertEqual(0, len(mf.images))
def pre_process_folder(folder): p = Path(folder) assert p.isdir(), "Got non directory input, breaking" paths = _collect_files_from_folder(p) items = [Item.from_path(str(f)) for f in paths] tempdir = tempfile.mkdtemp(prefix='temp_') for item in items: filename = os.path.basename(item.path) item.move_file(os.path.join(tempdir, filename), copy=True) yield AlbumTask(items)
def emitter(): fields = list(mediafile.MediaFile.readable_fields()) fields.remove('images') mf = mediafile.MediaFile(syspath(path)) tags = {} for field in fields: tags[field] = getattr(mf, field) tags['art'] = mf.art is not None # create a temporary Item to take advantage of __format__ item = Item.from_path(syspath(path)) return tags, item
def test_read_flexible_attribute_from_file(self): plugin = BeetsPlugin() plugin.add_media_field('initialkey', field_extension) mediafile = self._mediafile_fixture('empty') mediafile.update({'initialkey': 'F#'}) item = Item.from_path(mediafile.path) self.assertEqual(item['initialkey'], 'F#') delattr(MediaFile, 'initialkey') Item.media_fields.remove('initialkey')
def test_read_flexible_attribute_from_file(self): plugin = BeetsPlugin() plugin.add_media_field('customtag', field_extension) mediafile = self._mediafile_fixture('empty') mediafile.update({'customtag': u'F#'}) mediafile.save() item = Item.from_path(mediafile.path) self.assertEqual(item['customtag'], u'F#') delattr(MediaFile, 'customtag') Item._media_fields.remove('customtag')
def test_album_art(self): path = self.create_mediafile_fixture(images=['jpg']) item = Item.from_path(path) mediafile = MediaFile(item.path) self.assertNotEqual(0, len(mediafile.images)) config['zero'] = {'fields': ['images']} self.load_plugins('zero') item.write() mediafile = MediaFile(item.path) self.assertEqual(0, len(mediafile.images))
def test_album_art(self): path = self.create_mediafile_fixture(images=['jpg']) item = Item.from_path(path) mediafile = MediaFile(syspath(item.path)) self.assertNotEqual(0, len(mediafile.images)) config['zero'] = {'fields': [u'images']} self.load_plugins('zero') item.write() mediafile = MediaFile(syspath(item.path)) self.assertEqual(0, len(mediafile.images))
def add_item_fixtures(self, ext='mp3', count=1): """Add a number of items with files to the database. """ items = [] path = os.path.join(_common.RSRC, 'full.' + ext) for i in range(count): item = Item.from_path(str(path)) item.album = u'\xc3\xa4lbum {0}'.format(i) # Check unicode paths item.title = u't\xc3\x8ftle {0}'.format(i) item.add(self.lib) item.move(copy=True) item.store() items.append(item) return items
def add_album_fixture(self, track_count=1, ext='mp3'): """Add an album with files to the database. """ items = [] path = os.path.join(_common.RSRC, 'full.' + ext) for i in range(track_count): item = Item.from_path(str(path)) item.album = u'\u00e4lbum' # Check unicode paths item.title = u't\u00eftle {0}'.format(i) item.add(self.lib) item.move(copy=True) item.store() items.append(item) return self.lib.add_album(items)
def add_track(self, **kwargs): values = { 'title': 'track 1', 'artist': 'artist 1', 'album': 'album 1', } values.update(kwargs) item = Item.from_path(os.path.join(self.fixture_dir, 'min.mp3')) item.add(self.lib) item.update(values) item.move(copy=True) item.write() return item
def add_album_fixture(self, track_count=1, ext='mp3'): """Add an album with files to the database. """ items = [] path = os.path.join(_common.RSRC, 'full.' + ext) for i in range(track_count): item = Item.from_path(bytes(path)) item.album = u'\u00e4lbum' # Check unicode paths item.title = u't\u00eftle {0}'.format(i) item.add(self.lib) item.move(copy=True) item.store() items.append(item) return self.lib.add_album(items)
def setupLibrary(self, file_count): """Add an album to the library with ``file_count`` items. """ album = Album(id=1) album.add(self.lib) fixture_glob = os.path.join(_common.RSRC, '*.mp3') for src in glob(fixture_glob)[0:file_count]: dst = os.path.join(self.libdir, os.path.basename(src)) shutil.copy(src, dst) item = Item.from_path(dst) item.album_id = 1 item.add(self.lib) self._reset_replaygain(item)
def add_item_fixtures(self, ext='mp3', count=1): """Add a number of items with files to the database. """ # TODO base this on `add_item()` items = [] path = os.path.join(_common.RSRC, util.bytestring_path('full.' + ext)) for i in range(count): item = Item.from_path(path) item.album = u'\u00e4lbum {0}'.format(i) # Check unicode paths item.title = u't\u00eftle {0}'.format(i) item.add(self.lib) item.move(copy=True) item.store() items.append(item) return items
def add_track(self, **kwargs): values = { 'title': 'track 1', 'artist': 'artist 1', 'album': 'album 1', } values.update(kwargs) item = Item.from_path(os.path.join(self.fixture_dir, bytestring_path('min.mp3'))) item.add(self.lib) item.update(values) _move_item(item) item.write() return item
def add_track(self, **kwargs): values = { 'title': 'track 1', 'artist': 'artist 1', 'album': 'album 1', } values.update(kwargs) item = Item.from_path( os.path.join(self.fixture_dir, bytestring_path('min.mp3'))) item.add(self.lib) item.update(values) _move_item(item) item.write() return item
def add_album_fixture(self, track_count=1, ext='mp3'): """Add an album with files to the database. """ items = [] path = os.path.join(_common.RSRC, util.bytestring_path('full.' + ext)) for i in range(track_count): item = Item.from_path(path) item.album = u'\u00e4lbum' # Check unicode paths item.title = u't\u00eftle {0}'.format(i) # mtime needs to be set last since other assignments reset it. item.mtime = 12345 item.add(self.lib) item.move(operation=MoveOperation.COPY) item.store() items.append(item) return self.lib.add_album(items)
def add_item_fixtures(self, ext='mp3', count=1): """Add a number of items with files to the database. """ # TODO base this on `add_item()` items = [] path = os.path.join(_common.RSRC, util.bytestring_path('full.' + ext)) for i in range(count): item = Item.from_path(path) item.album = f'\u00e4lbum {i}' # Check unicode paths item.title = f't\u00eftle {i}' # mtime needs to be set last since other assignments reset it. item.mtime = 12345 item.add(self.lib) item.move(operation=MoveOperation.COPY) item.store() items.append(item) return items
def add_album(self, **kwargs): values = { 'title': 'track 1', 'artist': 'artist 1', 'album': 'album 1', 'format': 'mp3', } values.update(kwargs) ext = values.pop('format').lower() item = Item.from_path(os.path.join(self.fixture_dir, 'min.' + ext)) item.add(self.lib) item.update(values) item.move(copy=True) item.write() album = self.lib.add_album([item]) album.albumartist = item.artist album.store() return album
def test_delete_replaygain_tag(self): path = self.create_mediafile_fixture() item = Item.from_path(path) item.rg_track_peak = 0.0 item.write() mediafile = MediaFile(syspath(item.path)) self.assertIsNotNone(mediafile.rg_track_peak) self.assertIsNotNone(mediafile.rg_track_gain) config['zero'] = { 'fields': ['rg_track_peak', 'rg_track_gain'], } self.load_plugins('zero') item.write() mediafile = MediaFile(syspath(item.path)) self.assertIsNone(mediafile.rg_track_peak) self.assertIsNone(mediafile.rg_track_gain)
def test_delete_replaygain_tag(self): path = self.create_mediafile_fixture() item = Item.from_path(path) item.rg_track_peak = 0.0 item.write() mediafile = MediaFile(item.path) self.assertIsNotNone(mediafile.rg_track_peak) self.assertIsNotNone(mediafile.rg_track_gain) config['zero'] = { 'fields': ['rg_track_peak', 'rg_track_gain'], } self.load_plugins('zero') item.write() mediafile = MediaFile(item.path) self.assertIsNone(mediafile.rg_track_peak) self.assertIsNone(mediafile.rg_track_gain)
def _merge_albums(self, objs): """Merge Album objs by copying missing items from albums in the tail to the head album. Return same number of albums, with the head album modified.""" ids = [i.mb_trackid for i in objs[0].items()] for o in objs[1:]: for i in o.items(): if i.mb_trackid not in ids: missing = Item.from_path(i.path) missing.album_id = objs[0].id missing.add(i._db) self._log.debug( u'item {0} missing from album {1}:' u' merging from {2} into {3}', missing, objs[0], displayable_path(o.path), displayable_path(missing.destination())) missing.move(copy=True) return objs
def emitter(included_keys): if included_keys == '*': fields = tag_fields() else: fields = included_keys if 'images' in fields: # We can't serialize the image data. fields.remove('images') mf = mediafile.MediaFile(syspath(path)) tags = {} for field in fields: if field == 'art': tags[field] = mf.art is not None else: tags[field] = getattr(mf, field, None) # create a temporary Item to take advantage of __format__ item = Item.from_path(syspath(path)) return tags, item
def _merge_albums(self, objs): """Merge Album objs by copying missing items from albums in the tail to the head album. Return same number of albums, with the head album modified.""" ids = [i.mb_trackid for i in objs[0].items()] for o in objs[1:]: for i in o.items(): if i.mb_trackid not in ids: missing = Item.from_path(i.path) missing.album_id = objs[0].id missing.add(i._db) self._log.debug(u'item {0} missing from album {1}:' u' merging from {2} into {3}', missing, objs[0], displayable_path(o.path), displayable_path(missing.destination())) missing.move(copy=True) return objs
def identify_album_from_multiple_paths(paths_list_or_beets_items_list=None, expected_artist=None, expected_album=None, expected_release_id=None): items = paths_list_or_beets_items_list if isinstance(paths_list_or_beets_items_list[0], six.string_types): items = [Item.from_path(x) for x in paths_list_or_beets_items_list if is_media_file(x)] logger.debug("Called with {} items, expected id {}".format(len(items), expected_release_id)) artist_name, album_name, album_recommendation_list, recommendation = \ tag_album(items, search_artist=expected_artist, search_album=expected_album, search_ids=[expected_release_id] if expected_release_id else []) if recommendation is Recommendation.none: return None, None distance, album_info, track_mapping, extra_items, extra_tracks = album_recommendation_list[0] logger.debug('Successfully matched album {} !'.format(album_info.album_id)) logger.info("Successfully tagged album {album_id}, releasegroup {rgid}".format(album_id=album_info.album_id, rgid=album_info.releasegroup_id)) return album_info, track_mapping
def create_track(self, hgid, hid, tmpfname, path): """ Creates the data corresponding to the track in impala associated with the holding uuid based on metadata gleaned from beets. If it fails, raises an exception. """ try: # In the event the item isn't a music track, we need to prevent it # from getting added to impala but keep it in moss. i = Item.from_path(tmpfname) except: return # Create a stack and format if they don't already exist self._create_default_objects() # Create the holding group if it doesn't already exist self._create_holding_group(hgid, i) # Create the holding if it doesn't already exist self._create_holding(hgid, hid, i) # Create the track self._create_track(hid, path, i)