Adrian Sampson avatar Adrian Sampson committed bd47091

mbsync: use SingletonQuery for item updates

Comments (0)

Files changed (2)

         row = self.rowiter.next()  # May raise StopIteration.
         return Item(row)
 
+def get_query(val, album=False):
+    """Takes a value which may be None, a query string, a query string
+    list, or a Query object, and returns a suitable Query object. album
+    determines whether the query is to match items or albums.
+    """
+    if album:
+        default_fields = ALBUM_DEFAULT_FIELDS
+        all_keys = ALBUM_KEYS
+    else:
+        default_fields = ITEM_DEFAULT_FIELDS
+        all_keys = ITEM_KEYS
+
+    # Convert a single string into a list of space-separated
+    # criteria.
+    if isinstance(val, basestring):
+        val = val.split()
+
+    if val is None:
+        return TrueQuery()
+    elif isinstance(val, list) or isinstance(val, tuple):
+        return AndQuery.from_strings(val, default_fields, all_keys)
+    elif isinstance(val, Query):
+        return val
+    else:
+        raise ValueError('query must be None or have type Query or str')
+
+
 
 # An abstract library.
 
         raise NotImplementedError
 
 
-    # Helpers.
-
-    @classmethod
-    def _get_query(cls, val=None, album=False):
-        """Takes a value which may be None, a query string, a query
-        string list, or a Query object, and returns a suitable Query
-        object. album determines whether the query is to match items
-        or albums.
-        """
-        if album:
-            default_fields = ALBUM_DEFAULT_FIELDS
-            all_keys = ALBUM_KEYS
-        else:
-            default_fields = ITEM_DEFAULT_FIELDS
-            all_keys = ITEM_KEYS
-
-        # Convert a single string into a list of space-separated
-        # criteria.
-        if isinstance(val, basestring):
-            val = val.split()
-
-        if val is None:
-            return TrueQuery()
-        elif isinstance(val, list) or isinstance(val, tuple):
-            return AndQuery.from_strings(val, default_fields, all_keys)
-        elif isinstance(val, Query):
-            return val
-        elif not isinstance(val, Query):
-            raise ValueError('query must be None or have type Query or str')
-
-
     # Basic operations.
 
     def add(self, item, copy=False):
     # Querying.
 
     def albums(self, query=None, artist=None):
-        query = self._get_query(query, True)
+        query = get_query(query, True)
         if artist is not None:
             # "Add" the artist to the query.
             query = AndQuery((query, MatchQuery('albumartist', artist)))
         return [Album(self, dict(res)) for res in rows]
 
     def items(self, query=None, artist=None, album=None, title=None):
-        queries = [self._get_query(query, False)]
+        queries = [get_query(query, False)]
         if artist is not None:
             queries.append(MatchQuery('artist', artist))
         if album is not None:

beetsplug/mbsync.py

 # The above copyright notice and this permission notice shall be
 # included in all copies or substantial portions of the Software.
 
-"""Update local library from MusicBrainz
+"""Update library's tags using MusicBrainz.
 """
 import logging
 
     write = opts.write
 
     with lib.transaction():
-        singletons = lib.items(ui.decargs(args + ['singleton']))
-        albums = lib.albums(ui.decargs(args))
-
-        for s in singletons:
+        # Process matching singletons.
+        singletons_query = library.get_query(ui.decargs(args), False)
+        singletons_query.subqueries.append(library.SingletonQuery(True))
+        for s in lib.items(singletons_query):
             if not s.mb_trackid:
                 log.info(u'Skipping singleton {0}: has no mb_trackid'
                          .format(s.title))
             autotag.apply_item_metadata(s, match.info)
             _print_and_apply_changes(lib, s, move, pretend, write)
 
-        for a in albums:
+        # Process matching albums.
+        for a in lib.albums(ui.decargs(args)):
             if not a.mb_albumid:
                 log.info(u'Skipping album {0}: has no mb_albumid'.format(a.id))
                 continue
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.