Adjust playlist curve setup to load curves after YAML restore.
[hooke.git] / hooke / playlist.py
index a4dd0abcf525858587c8e6bb24d4020fb0c20554..41132a106c48bbc80774bb2d850cf8c742922108 100644 (file)
@@ -91,11 +91,12 @@ class NoteIndexList (list):
             return self._index
         return super(NoteIndexList, self).index(value, *args, **kwargs)
 
-    def current(self):
+    def current(self, load=True):
         if len(self) == 0:
             return None
         item = self[self._index]
-        self._setup_item(item)
+        if load == True:
+            self._setup_item(item)
         return item
 
     def jump(self, index):
@@ -160,14 +161,13 @@ class Playlist (NoteIndexList):
     def __init__(self, drivers, name=None):
         super(Playlist, self).__init__(name=name)
         self.drivers = drivers
-        self._max_loaded = 100 # curves to hold in memory simultaneously.
 
     def _set_default_attrs(self):
         super(Playlist, self)._set_default_attrs()
         self._default_attrs['drivers'] = []
         # List of loaded curves, see :meth:`._setup_item`.
         self._default_attrs['_loaded'] = []
-        self._default_attrs['_max_loaded'] = 100
+        self._default_attrs['_max_loaded'] = 100  # curves to hold in memory simultaneously.
 
     def __setstate__(self, state):
         super(Playlist, self).__setstate__(state)
@@ -194,13 +194,22 @@ class Playlist (NoteIndexList):
                 self.append(curve)
             if curve.driver == None:
                 c.identify(self.drivers)
-            if curve.data == None:
+            if curve.data == None or max([d.size for d in curve.data]) == 0:
                 curve.load()
             self._loaded.append(curve)
             if len(self._loaded) > self._max_loaded:
                 oldest = self._loaded.pop(0)
                 oldest.unload()
 
+    def unload(self, curve):
+        "Inverse of .`_setup_item`."
+        curve.unload()
+        try:
+            self._loaded.remove(curve)
+        except ValueError:
+            pass
+
+
 def playlist_path(path):
     """Normalize playlist path extensions.
 
@@ -395,11 +404,13 @@ class FilePlaylist (Playlist):
             - !!python/object:hooke.engine.CommandMessage
               arguments: {arg 0: 0, arg 1: X}
               command: command A
+              explicit_user_call: true
             - !!python/object:hooke.engine.CommandMessage
               arguments:
                 arg 0: 1
                 curve: *id001
               command: command B
+              explicit_user_call: true
           info: {attr with spaces: 'The second curve
         <BLANKLINE>
               with endlines'}
@@ -427,11 +438,13 @@ class FilePlaylist (Playlist):
             - !!python/object:hooke.engine.CommandMessage
               arguments: {arg 0: 0, arg 1: X}
               command: command A
+              explicit_user_call: true
             - !!python/object:hooke.engine.CommandMessage
               arguments:
                 arg 0: 1
                 curve: *id001
               command: command B
+              explicit_user_call: true
           info: {attr with spaces: 'The second curve
         <BLANKLINE>
               with endlines'}
@@ -450,8 +463,10 @@ class FilePlaylist (Playlist):
         self._relative_curve_paths = self.relative_curve_paths
         self.update_curve_paths()
         self._relative_curve_paths = rcp
-
+        digest = self._digest
+        self._digest = None  # don't save the digest (recursive file).
         yaml_string = yaml.dump(self, allow_unicode=True)
+        self._digest = digest
         self.update_curve_paths()
         return ('# Hooke playlist version %s\n' % self.version) + yaml_string
 
@@ -557,12 +572,11 @@ def from_string(string):
 def load(path=None, drivers=None, identify=True, hooke=None):
     """Load a playlist from a file.
     """
-    path = playlist_path(path)
+    path = os.path.expanduser(playlist_path(path))
     with open(path, 'r') as f:
         text = f.read()
     playlist = from_string(text)
     playlist.set_path(path)
-    print type(playlist)
     playlist._digest = playlist.digest()
     if drivers != None:
         playlist.drivers = drivers