1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
3 # This file is part of Hooke.
5 # Hooke is free software: you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation, either
8 # version 3 of the License, or (at your option) any later version.
10 # Hooke is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with Hooke. If not, see
17 # <http://www.gnu.org/licenses/>.
19 """The `playlist` module provides a :class:`Playlist` and its subclass
20 :class:`FilePlaylist` for manipulating lists of
21 :class:`hooke.curve.Curve`\s.
27 import xml.dom.minidom
29 from . import curve as curve
32 class NoteIndexList (list):
33 """A list that keeps track of a "current" item and additional notes.
35 :attr:`index` (i.e. "bookmark") is the index of the currently
36 current curve. Also keep a :class:`dict` of additional information
39 def __init__(self, name=None):
40 super(NoteIndexList, self).__init__()
46 return '<%s %s>' % (self.__class__.__name__, self.name)
48 def _setup_item(self, item):
49 """Perform any required initialization before returning an item.
56 item = self[self._index]
57 self._setup_item(item)
60 def jump(self, index):
64 self._index = index % len(self)
67 self.jump(self._index + 1)
70 self.jump(self._index - 1)
72 def filter(self, keeper_fn=lambda item:True, *args, **kwargs):
73 c = copy.deepcopy(self)
74 for item in reversed(c):
76 if keeper_fn(item, *args, **kwargs) != True:
78 try: # attempt to maintain the same current item
79 c._index = c.index(self.current())
84 class Playlist (NoteIndexList):
85 """A :class:`NoteIndexList` of :class:`hooke.curve.Curve`\s.
87 Keeps a list of :attr:`drivers` for loading curves.
89 def __init__(self, drivers, name=None):
90 super(Playlist, self).__init__(name=name)
91 self.drivers = drivers
92 self._loaded = [] # List of loaded curves, see :meth:`._setup_item`.
93 self._max_loaded = 100 # curves to hold in memory simultaneously.
95 def append_curve_by_path(self, path, info=None, identify=True):
97 path = os.path.join(os.path.dirname(self.path), path)
98 path = os.path.normpath(path)
99 c = curve.Curve(path, info=info)
101 c.identify(self.drivers)
105 def _setup_item(self, curve):
106 if curve != None and curve not in self._loaded:
107 if curve not in self:
109 if curve.driver == None:
110 c.identify(self.drivers)
111 if curve.data == None:
113 self._loaded.append(curve)
114 if len(self._loaded) > self._max_loaded:
115 oldest = self._loaded.pop(0)
118 class FilePlaylist (Playlist):
121 def __init__(self, drivers, name=None, path=None):
122 super(FilePlaylist, self).__init__(drivers, name)
125 self._ignored_keys = [
126 'experiment', # class instance, not very exciting.
129 def set_path(self, path):
131 if not path.endswith('.hkp'):
134 if self.name == None:
135 self.name = os.path.basename(path)
138 return self.digest() == self._digest
141 r"""Compute the sha1 digest of the flattened playlist
147 >>> root_path = os.path.sep + 'path'
148 >>> p = FilePlaylist(drivers=[],
149 ... path=os.path.join(root_path, 'to','playlist'))
150 >>> p.info['note'] = 'An example playlist'
151 >>> c = curve.Curve(os.path.join(root_path, 'to', 'curve', 'one'))
152 >>> c.info['note'] = 'The first curve'
154 >>> c = curve.Curve(os.path.join(root_path, 'to', 'curve', 'two'))
155 >>> c.info['note'] = 'The second curve'
158 '\\\x14\x87\x88*q\xf8\xaa\xa7\x84f\x82\xa1S>\xfd3+\xd0o'
160 string = self.flatten()
161 return hashlib.sha1(string).digest()
163 def flatten(self, absolute_paths=False):
164 """Create a string representation of the playlist.
166 A playlist is an XML document with the following syntax::
168 <?xml version="1.0" encoding="utf-8"?>
169 <playlist attribute="value">
170 <curve path="/my/file/path/"/ attribute="value" ...>
174 Relative paths are interpreted relative to the location of the
180 >>> root_path = os.path.sep + 'path'
181 >>> p = FilePlaylist(drivers=[],
182 ... path=os.path.join(root_path, 'to','playlist'))
183 >>> p.info['note'] = 'An example playlist'
184 >>> c = curve.Curve(os.path.join(root_path, 'to', 'curve', 'one'))
185 >>> c.info['note'] = 'The first curve'
187 >>> c = curve.Curve(os.path.join(root_path, 'to', 'curve', 'two'))
188 >>> c.info['note'] = 'The second curve'
190 >>> print p.flatten() # doctest: +NORMALIZE_WHITESPACE +REPORT_UDIFF
191 <?xml version="1.0" encoding="utf-8"?>
192 <playlist index="0" note="An example playlist" version="0.1">
193 <curve note="The first curve" path="curve/one"/>
194 <curve note="The second curve" path="curve/two"/>
197 >>> print p.flatten(absolute_paths=True) # doctest: +NORMALIZE_WHITESPACE +REPORT_UDIFF
198 <?xml version="1.0" encoding="utf-8"?>
199 <playlist index="0" note="An example playlist" version="0.1">
200 <curve note="The first curve" path="/path/to/curve/one"/>
201 <curve note="The second curve" path="/path/to/curve/two"/>
205 implementation = xml.dom.minidom.getDOMImplementation()
206 # create the document DOM object and the root element
207 doc = implementation.createDocument(None, 'playlist', None)
208 root = doc.documentElement
209 root.setAttribute('version', self.version) # store playlist version
210 root.setAttribute('index', str(self._index))
211 for key,value in self.info.items(): # save info variables
212 root.setAttribute(key, str(value))
213 for curve in self: # save curves and their attributes
214 curve_element = doc.createElement('curve')
215 root.appendChild(curve_element)
216 path = os.path.abspath(os.path.expanduser(curve.path))
217 if absolute_paths == False:
218 path = os.path.relpath(
222 os.path.expanduser(self.path))))
223 curve_element.setAttribute('path', path)
224 for key,value in curve.info.items():
225 if key in self._ignored_keys:
227 curve_element.setAttribute(key, str(value))
228 string = doc.toprettyxml(encoding='utf-8')
229 root.unlink() # break circular references for garbage collection
232 def _from_xml_doc(self, doc, identify=True):
233 """Load a playlist from an :class:`xml.dom.minidom.Document`
236 root = doc.documentElement
237 for attribute,value in root.attributes.items():
238 if attribute == 'version':
239 assert value == self.version, \
240 'Cannot read v%s playlist with a v%s reader' \
241 % (value, self.version)
242 elif attribute == 'index':
243 self._index = int(value)
245 self.info[attribute] = value
246 for curve_element in doc.getElementsByTagName('curve'):
247 path = curve_element.getAttribute('path')
248 info = dict(curve_element.attributes.items())
250 self.append_curve_by_path(path, info, identify=identify)
251 self.jump(self._index) # ensure valid index
253 def from_string(self, string, identify=True):
254 """Load a playlist from a string.
259 >>> string = '''<?xml version="1.0" encoding="utf-8"?>
260 ... <playlist index="1" note="An example playlist" version="0.1">
261 ... <curve note="The first curve" path="../curve/one"/>
262 ... <curve note="The second curve" path="../curve/two"/>
265 >>> p = FilePlaylist(drivers=[],
266 ... path=os.path.join('path', 'to', 'my', 'playlist'))
267 >>> p.from_string(string, identify=False)
271 {u'note': u'An example playlist'}
277 doc = xml.dom.minidom.parseString(string)
278 self._from_xml_doc(doc, identify=identify)
280 def load(self, path=None, identify=True):
281 """Load a playlist from a file.
284 doc = xml.dom.minidom.parse(self.path)
285 self._from_xml_doc(doc, identify=identify)
286 self._digest = self.digest()
288 def save(self, path=None):
289 """Saves the playlist in a XML file.
292 f = file(self.path, 'w')
293 f.write(self.flatten())