From 13fede597f6c839c5c7d7a2a7fc5c8a07bb31174 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Thu, 8 Oct 2015 10:40:18 -0700 Subject: [PATCH] Fix my fix for #1586: iterate over views --- beets/__init__.py | 2 +- beets/util/confit.py | 26 ++++++++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/beets/__init__.py b/beets/__init__.py index 867ead368..fbd4e10c3 100644 --- a/beets/__init__.py +++ b/beets/__init__.py @@ -33,7 +33,7 @@ class IncludeLazyConfig(confit.LazyConfig): super(IncludeLazyConfig, self).read(user, defaults) try: - for view in self['include'].all_contents(): + for view in self['include']: filename = view.as_filename() if os.path.isfile(filename): self.set_file(filename) diff --git a/beets/util/confit.py b/beets/util/confit.py index 4c5b30cb6..2614f7ed7 100644 --- a/beets/util/confit.py +++ b/beets/util/confit.py @@ -215,10 +215,28 @@ class ConfigView(object): return '<{}: {}>'.format(self.__class__.__name__, self.name) def __iter__(self): - # Prevent list(config) from using __getitem__ and entering an - # infinite loop. - raise TypeError(u"{!r} object is not " - u"iterable".format(self.__class__.__name__)) + """Iterate over the keys of a dictionary view or the *subviews* + of a list view. + """ + # Try getting the keys, if this is a dictionary view. + try: + keys = self.keys() + for key in keys: + yield key + + except ConfigTypeError: + # Otherwise, try iterating over a list. + collection = self.get() + if not isinstance(collection, (list, tuple)): + raise ConfigTypeError( + '{0} must be a dictionary or a list, not {1}'.format( + self.name, type(collection).__name__ + ) + ) + + # Yield all the indices in the list. + for index in range(len(collection)): + yield self[index] def __getitem__(self, key): """Get a subview of this view."""