Ran update_copyright.py, updating all the copyright blurbs and adding AUTHORS.
[hooke.git] / hooke / playlist.py
index de58e9f57cac874962ff0dc1d2bca28e708b7bfe..fc4ea9a7d72649ceb2354fcbece4750da829c7d4 100644 (file)
@@ -1,4 +1,22 @@
-"""The playlist module provides :class:`Playlist` its subclass
+# Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
+#
+# This file is part of Hooke.
+#
+# Hooke is free software: you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation, either
+# version 3 of the License, or (at your option) any later version.
+#
+# Hooke is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with Hooke.  If not, see
+# <http://www.gnu.org/licenses/>.
+
+"""The `playlist` module provides a :class:`Playlist` and its subclass
 :class:`FilePlaylist` for manipulating lists of
 :class:`hooke.curve.Curve`\s.
 """
@@ -10,36 +28,25 @@ import xml.dom.minidom
 
 from . import curve as curve
 
-class Playlist (list):
-    """A list of :class:`hooke.curve.Curve`\s.
 
-    Keeps a list of :attr:`drivers` for loading curves, the
-    :attr:`index` (i.e. "bookmark") of the currently active curve, and
-    a :class:`dict` of additional informtion (:attr:`info`).
+class NoteIndexList (list):
+    """A list that keeps track of a "current" item and additional notes.
+
+    :attr:`index` (i.e. "bookmark") is the index of the currently
+    current curve.  Also keep a :class:`dict` of additional information
+    (:attr:`info`).
     """
-    def __init__(self, drivers, name=None):
-        super(Playlist, self).__init__()
-        self.drivers = drivers
+    def __init__(self, name=None):
+        super(NoteIndexList, self).__init__()
         self.name = name
         self.info = {}
         self._index = 0
 
-    def append_curve_by_path(self, path, info=None, identify=True):
-        if self.path != None:
-            path = os.path.join(self.path, path)
-        path = os.path.normpath(path)
-        c = curve.Curve(path, info=info)
-        if identify == True:
-            c.identify(self.drivers)
-        self.append(c)
-        return c
-
-    def active_curve(self):
+    def current(self):
+        if len(self) == 0:
+            return None
         return self[self._index]
 
-    def has_curves(self):
-        return len(self) > 0
-
     def jump(self, index):
         if len(self) == 0:
             self._index = 0
@@ -52,27 +59,52 @@ class Playlist (list):
     def previous(self):
         self.jump(self._index - 1)
 
-    def filter(self, keeper_fn=lambda curve:True):
-        playlist = copy.deepcopy(self)
-        for curve in reversed(playlist.curves):
-            if keeper_fn(curve) != True:
-                playlist.curves.remove(curve)
-        try: # attempt to maintain the same active curve
-            playlist._index = playlist.index(self.active_curve())
+    def filter(self, keeper_fn=lambda item:True):
+        c = copy.deepcopy(self)
+        for item in reversed(c):
+            if keeper_fn(item) != True:
+                c.remove(item)
+        try: # attempt to maintain the same current item
+            c._index = c.index(self.current())
         except ValueError:
-            playlist._index = 0
-        return playlist
+            c._index = 0
+        return c
+
+class Playlist (NoteIndexList):
+    """A :class:`NoteIndexList` of :class:`hooke.curve.Curve`\s.
+
+    Keeps a list of :attr:`drivers` for loading curves.
+    """
+    def __init__(self, drivers, name=None):
+        super(Playlist, self).__init__(name=name)
+        self.drivers = drivers
+
+    def append_curve_by_path(self, path, info=None, identify=True):
+        if self.path != None:
+            path = os.path.join(self.path, path)
+        path = os.path.normpath(path)
+        c = curve.Curve(path, info=info)
+        if identify == True:
+            c.identify(self.drivers)
+        self.append(c)
+        return c
 
 class FilePlaylist (Playlist):
     version = '0.1'
 
     def __init__(self, drivers, name=None, path=None):
-        if name == None and path != None:
-            name = os.path.basename(path)
         super(FilePlaylist, self).__init__(drivers, name)
-        self.path = path
+        self.set_path(path)
         self._digest = None
 
+    def set_path(self, path):
+        if path != None:
+            if not path.endswith('.hkp'):
+                path += '.hkp'
+            self.path = path
+            if self.name == None:
+                self.name = os.path.basename(path)
+
     def is_saved(self):
         return self.digest() == self._digest
 
@@ -210,21 +242,20 @@ class FilePlaylist (Playlist):
         path/to/curve/two
         """
         doc = xml.dom.minidom.parseString(string)
-        return self._from_xml_doc(doc)
+        self._from_xml_doc(doc)
 
     def load(self, path=None):
         """Load a playlist from a file.
         """
-        if path != None:
-            self.path = path
-        if self.name == None:
-            self.name = os.path.basename(self.path)
-        doc = xml.dom.minidom.parse(path)
-        return self._from_xml_doc(doc)
+        self.set_path(path)
+        doc = xml.dom.minidom.parse(self.path)
+        self._from_xml_doc(doc)
+        self._digest = self.digest()
 
-    def save(self, path):
+    def save(self, path=None):
         """Saves the playlist in a XML file.
         """
-        f = file(path, 'w')
+        self.set_path(path)
+        f = file(self.path, 'w')
         f.write(self.flatten())
         f.close()