Adjust NoteIndexList._index in .index() if it falls off the end of the list.
[hooke.git] / hooke / playlist.py
index c89298e67a486fe355f3db59636e2f5a289e49ab..52d8df1352e6077ae77651512a8eab1305ba3718 100644 (file)
@@ -88,6 +88,8 @@ class NoteIndexList (list):
         is `None`.
         """
         if value == None:
+            if self._index >= len(self):  # perhaps items have been popped
+                self._index = len(self) - 1
             return self._index
         return super(NoteIndexList, self).index(value, *args, **kwargs)
 
@@ -213,7 +215,7 @@ class Playlist (NoteIndexList):
             pass
 
 
-def playlist_path(path):
+def playlist_path(path, expand=False):
     """Normalize playlist path extensions.
 
     Examples
@@ -229,6 +231,8 @@ def playlist_path(path):
         return None
     if not path.endswith('.hkp'):
         path += '.hkp'
+    if expand:
+        path = os.path.abspath(os.path.expanduser(path))
     return path
 
 
@@ -302,10 +306,9 @@ class FilePlaylist (Playlist):
             if self._base_path == None:
                 self._base_path = os.getcwd()
         else:
-            path = playlist_path(path)
+            path = playlist_path(path, expand=True)
             self.path = path
-            self._base_path = os.path.dirname(os.path.abspath(
-                os.path.expanduser(self.path)))
+            self._base_path = os.path.dirname(self.path)
             if self.name == None:
                 self.name = os.path.basename(path)
         if self._base_path != orig_base_path:
@@ -575,7 +578,7 @@ def from_string(string):
 def load(path=None, drivers=None, identify=True, hooke=None):
     """Load a playlist from a file.
     """
-    path = os.path.expanduser(playlist_path(path))
+    path = playlist_path(path, expand=True)
     with open(path, 'r') as f:
         text = f.read()
     playlist = from_string(text)