mirror of
https://github.com/fergalmoran/picard.git
synced 2026-05-27 05:16:10 +00:00
Move iter_files_from_objects to picard.util
This commit is contained in:
@@ -45,7 +45,6 @@
|
||||
import argparse
|
||||
from collections import defaultdict
|
||||
from functools import partial
|
||||
from itertools import chain
|
||||
import logging
|
||||
import os.path
|
||||
import platform
|
||||
@@ -111,7 +110,7 @@ from picard.util import (
|
||||
decode_filename,
|
||||
encode_filename,
|
||||
is_hidden,
|
||||
iter_unique,
|
||||
iter_files_from_objects,
|
||||
mbid_validate,
|
||||
normpath,
|
||||
process_events_iter,
|
||||
@@ -582,7 +581,7 @@ class Tagger(QtWidgets.QApplication):
|
||||
|
||||
def copy_files(self, objects):
|
||||
mimeData = QtCore.QMimeData()
|
||||
mimeData.setUrls([QtCore.QUrl.fromLocalFile(f.filename) for f in self.iter_files_from_objects(objects)])
|
||||
mimeData.setUrls([QtCore.QUrl.fromLocalFile(f.filename) for f in iter_files_from_objects(objects)])
|
||||
self.clipboard().setMimeData(mimeData)
|
||||
|
||||
def paste_files(self, target):
|
||||
@@ -645,17 +644,15 @@ class Tagger(QtWidgets.QApplication):
|
||||
item.filename if isinstance(item, File) else '')
|
||||
|
||||
def get_files_from_objects(self, objects, save=False):
|
||||
"""Return list of files from list of albums, clusters, tracks or files."""
|
||||
return list(self.iter_files_from_objects(objects, save=save))
|
||||
"""Return list of unique files from list of albums, clusters, tracks or files.
|
||||
|
||||
@staticmethod
|
||||
def iter_files_from_objects(objects, save=False):
|
||||
"""Creates an iterator over all unique files from list of albums, clusters, tracks or files."""
|
||||
return iter_unique(chain(*(obj.iterfiles(save) for obj in objects)))
|
||||
Note: Consider using picard.util.iter_files_from_objects instead, which returns an iterator.
|
||||
"""
|
||||
return list(iter_files_from_objects(objects, save=save))
|
||||
|
||||
def save(self, objects):
|
||||
"""Save the specified objects."""
|
||||
for file in self.iter_files_from_objects(objects, save=True):
|
||||
for file in iter_files_from_objects(objects, save=True):
|
||||
file.save()
|
||||
|
||||
def load_album(self, album_id, discid=None):
|
||||
@@ -805,7 +802,7 @@ class Tagger(QtWidgets.QApplication):
|
||||
"""Analyze the file(s)."""
|
||||
if not self.use_acoustid:
|
||||
return
|
||||
for file in self.iter_files_from_objects(objs):
|
||||
for file in iter_files_from_objects(objs):
|
||||
if file.can_analyze():
|
||||
file.set_pending()
|
||||
self._acoustid.analyze(file, partial(file._lookup_finished, File.LOOKUP_ACOUSTID))
|
||||
@@ -818,7 +815,7 @@ class Tagger(QtWidgets.QApplication):
|
||||
def finished(file, result):
|
||||
file.clear_pending()
|
||||
|
||||
for file in self.iter_files_from_objects(objs):
|
||||
for file in iter_files_from_objects(objs):
|
||||
file.set_pending()
|
||||
self._acoustid.fingerprint(file, partial(finished, file))
|
||||
|
||||
|
||||
@@ -73,6 +73,7 @@ from picard.plugin import ExtensionPoint
|
||||
from picard.track import Track
|
||||
from picard.util import (
|
||||
icontheme,
|
||||
iter_files_from_objects,
|
||||
iter_unique,
|
||||
restore_method,
|
||||
thread,
|
||||
@@ -1044,7 +1045,7 @@ class MainWindow(QtWidgets.QMainWindow, PreserveGeometry):
|
||||
return QtCore.QUrl.fromLocalFile(url)
|
||||
|
||||
def play_file(self):
|
||||
for file in self.tagger.iter_files_from_objects(self.selected_objects):
|
||||
for file in iter_files_from_objects(self.selected_objects):
|
||||
QtGui.QDesktopServices.openUrl(self._openUrl(file.filename))
|
||||
|
||||
def _on_player_error(self, error, msg):
|
||||
@@ -1053,7 +1054,7 @@ class MainWindow(QtWidgets.QMainWindow, PreserveGeometry):
|
||||
def open_folder(self):
|
||||
folders = iter_unique(
|
||||
os.path.dirname(f.filename) for f
|
||||
in self.tagger.iter_files_from_objects(self.selected_objects))
|
||||
in iter_files_from_objects(self.selected_objects))
|
||||
for folder in folders:
|
||||
QtGui.QDesktopServices.openUrl(self._openUrl(folder))
|
||||
|
||||
|
||||
@@ -37,6 +37,7 @@ from picard.const.sys import IS_MACOS
|
||||
from picard.util import (
|
||||
format_time,
|
||||
icontheme,
|
||||
iter_files_from_objects,
|
||||
)
|
||||
|
||||
from picard.ui.widgets import (
|
||||
@@ -133,7 +134,7 @@ class Player(QtCore.QObject):
|
||||
playlist = QtMultimedia.QMediaPlaylist(self)
|
||||
playlist.setPlaybackMode(QtMultimedia.QMediaPlaylist.Sequential)
|
||||
playlist.addMedia([QtMultimedia.QMediaContent(QtCore.QUrl.fromLocalFile(file.filename))
|
||||
for file in self.tagger.iter_files_from_objects(self._selected_objects)])
|
||||
for file in iter_files_from_objects(self._selected_objects)])
|
||||
self._player.setPlaylist(playlist)
|
||||
self._player.play()
|
||||
|
||||
|
||||
@@ -41,6 +41,7 @@ import builtins
|
||||
from collections import namedtuple
|
||||
from collections.abc import Mapping
|
||||
import html
|
||||
from itertools import chain
|
||||
import json
|
||||
import ntpath
|
||||
from operator import attrgetter
|
||||
@@ -112,6 +113,11 @@ def process_events_iter(iterable, interval=0.1):
|
||||
QtCore.QCoreApplication.processEvents()
|
||||
|
||||
|
||||
def iter_files_from_objects(objects, save=False):
|
||||
"""Creates an iterator over all unique files from list of albums, clusters, tracks or files."""
|
||||
return iter_unique(chain(*(obj.iterfiles(save) for obj in objects)))
|
||||
|
||||
|
||||
_io_encoding = sys.getfilesystemencoding()
|
||||
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ from collections import namedtuple
|
||||
from collections.abc import Iterator
|
||||
import os.path
|
||||
import unittest
|
||||
from unittest.mock import Mock
|
||||
|
||||
from test.picardtestcase import PicardTestCase
|
||||
|
||||
@@ -43,6 +44,7 @@ from picard.util import (
|
||||
find_best_match,
|
||||
imageinfo,
|
||||
is_absolute_path,
|
||||
iter_files_from_objects,
|
||||
iter_unique,
|
||||
limited_join,
|
||||
sort_by_similarity,
|
||||
@@ -455,6 +457,21 @@ class LimitedJoin(PicardTestCase):
|
||||
self.assertEqual(result, expected)
|
||||
|
||||
|
||||
class IterFilesFromObjectsTest(PicardTestCase):
|
||||
|
||||
def test_iterate_only_unique(self):
|
||||
f1 = Mock()
|
||||
f2 = Mock()
|
||||
f3 = Mock()
|
||||
obj1 = Mock()
|
||||
obj1.iterfiles = Mock(return_value=[f1, f2])
|
||||
obj2 = Mock()
|
||||
obj2.iterfiles = Mock(return_value=[f2, f3])
|
||||
result = iter_files_from_objects([obj1, obj2])
|
||||
self.assertTrue(isinstance(result, Iterator))
|
||||
self.assertEqual([f1, f2, f3], list(result))
|
||||
|
||||
|
||||
class IterUniqifyTest(PicardTestCase):
|
||||
|
||||
def test_unique(self):
|
||||
|
||||
Reference in New Issue
Block a user