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
30 from .compat import minidom as minidom # dynamically patch xml.sax.minidom
33 class NoteIndexList (list):
34 """A list that keeps track of a "current" item and additional notes.
36 :attr:`index` (i.e. "bookmark") is the index of the currently
37 current curve. Also keep a :class:`dict` of additional information
40 def __init__(self, name=None):
41 super(NoteIndexList, self).__init__()
47 return '<%s %s>' % (self.__class__.__name__, self.name)
49 def _setup_item(self, item):
50 """Perform any required initialization before returning an item.
57 item = self[self._index]
58 self._setup_item(item)
61 def jump(self, index):
65 self._index = index % len(self)
68 self.jump(self._index + 1)
71 self.jump(self._index - 1)
73 def filter(self, keeper_fn=lambda item:True, *args, **kwargs):
74 c = copy.deepcopy(self)
75 for item in reversed(c):
77 if keeper_fn(item, *args, **kwargs) != True:
79 try: # attempt to maintain the same current item
80 c._index = c.index(self.current())
85 class Playlist (NoteIndexList):
86 """A :class:`NoteIndexList` of :class:`hooke.curve.Curve`\s.
88 Keeps a list of :attr:`drivers` for loading curves.
90 def __init__(self, drivers, name=None):
91 super(Playlist, self).__init__(name=name)
92 self.drivers = drivers
93 self._loaded = [] # List of loaded curves, see :meth:`._setup_item`.
94 self._max_loaded = 100 # curves to hold in memory simultaneously.
96 def append_curve_by_path(self, path, info=None, identify=True):
98 path = os.path.join(os.path.dirname(self.path), path)
99 path = os.path.normpath(path)
100 c = curve.Curve(path, info=info)
102 c.identify(self.drivers)
106 def _setup_item(self, curve):
107 if curve != None and curve not in self._loaded:
108 if curve not in self:
110 if curve.driver == None:
111 c.identify(self.drivers)
112 if curve.data == None:
114 self._loaded.append(curve)
115 if len(self._loaded) > self._max_loaded:
116 oldest = self._loaded.pop(0)
119 class FilePlaylist (Playlist):
122 def __init__(self, drivers, name=None, path=None):
123 super(FilePlaylist, self).__init__(drivers, name)
126 self._ignored_keys = [
127 'experiment', # class instance, not very exciting.
130 def set_path(self, path):
132 if not path.endswith('.hkp'):
135 if self.name == None:
136 self.name = os.path.basename(path)
139 return self.digest() == self._digest
142 r"""Compute the sha1 digest of the flattened playlist
148 >>> root_path = os.path.sep + 'path'
149 >>> p = FilePlaylist(drivers=[],
150 ... path=os.path.join(root_path, 'to','playlist'))
151 >>> p.info['note'] = 'An example playlist'
152 >>> c = curve.Curve(os.path.join(root_path, 'to', 'curve', 'one'))
153 >>> c.info['note'] = 'The first curve'
155 >>> c = curve.Curve(os.path.join(root_path, 'to', 'curve', 'two'))
156 >>> c.info['note'] = 'The second curve'
159 '\\\x14\x87\x88*q\xf8\xaa\xa7\x84f\x82\xa1S>\xfd3+\xd0o'
161 string = self.flatten()
162 return hashlib.sha1(string).digest()
164 def flatten(self, absolute_paths=False):
165 """Create a string representation of the playlist.
167 A playlist is an XML document with the following syntax::
169 <?xml version="1.0" encoding="utf-8"?>
170 <playlist attribute="value">
171 <curve path="/my/file/path/"/ attribute="value" ...>
175 Relative paths are interpreted relative to the location of the
181 >>> root_path = os.path.sep + 'path'
182 >>> p = FilePlaylist(drivers=[],
183 ... path=os.path.join(root_path, 'to','playlist'))
184 >>> p.info['note'] = 'An example playlist'
185 >>> c = curve.Curve(os.path.join(root_path, 'to', 'curve', 'one'))
186 >>> c.info['note'] = 'The first curve'
188 >>> c = curve.Curve(os.path.join(root_path, 'to', 'curve', 'two'))
189 >>> c.info['note'] = 'The second curve\\nwith endlines'
191 >>> print p.flatten() # doctest: +NORMALIZE_WHITESPACE +REPORT_UDIFF
192 <?xml version="1.0" encoding="utf-8"?>
193 <playlist index="0" note="An example playlist" version="0.1">
194 <curve note="The first curve" path="curve/one"/>
195 <curve note="The second curve
with endlines" path="curve/two"/>
198 >>> print p.flatten(absolute_paths=True) # doctest: +NORMALIZE_WHITESPACE +REPORT_UDIFF
199 <?xml version="1.0" encoding="utf-8"?>
200 <playlist index="0" note="An example playlist" version="0.1">
201 <curve note="The first curve" path="/path/to/curve/one"/>
202 <curve note="The second curve
with endlines" path="/path/to/curve/two"/>
206 implementation = xml.dom.minidom.getDOMImplementation()
207 # create the document DOM object and the root element
208 doc = implementation.createDocument(None, 'playlist', None)
209 root = doc.documentElement
210 root.setAttribute('version', self.version) # store playlist version
211 root.setAttribute('index', str(self._index))
212 for key,value in self.info.items(): # save info variables
213 root.setAttribute(key, str(value))
214 for curve in self: # save curves and their attributes
215 curve_element = doc.createElement('curve')
216 root.appendChild(curve_element)
217 path = os.path.abspath(os.path.expanduser(curve.path))
218 if absolute_paths == False:
219 path = os.path.relpath(
223 os.path.expanduser(self.path))))
224 curve_element.setAttribute('path', path)
225 for key,value in curve.info.items():
226 if key in self._ignored_keys:
228 curve_element.setAttribute(key,str(value))
229 string = doc.toprettyxml(encoding='utf-8')
230 root.unlink() # break circular references for garbage collection
233 def _from_xml_doc(self, doc, identify=True):
234 """Load a playlist from an :class:`xml.dom.minidom.Document`
237 root = doc.documentElement
238 for attribute,value in root.attributes.items():
239 if attribute == 'version':
240 assert value == self.version, \
241 'Cannot read v%s playlist with a v%s reader' \
242 % (value, self.version)
243 elif attribute == 'index':
244 self._index = int(value)
246 self.info[attribute] = value
247 for curve_element in doc.getElementsByTagName('curve'):
248 path = curve_element.getAttribute('path')
249 info = dict(curve_element.attributes.items())
251 self.append_curve_by_path(path, info, identify=identify)
252 self.jump(self._index) # ensure valid index
254 def from_string(self, string, identify=True):
255 """Load a playlist from a string.
260 >>> string = '''<?xml version="1.0" encoding="utf-8"?>
261 ... <playlist index="1" note="An example playlist" version="0.1">
262 ... <curve note="The first curve" path="../curve/one"/>
263 ... <curve note="The second curve
with endlines" path="../curve/two"/>
266 >>> p = FilePlaylist(drivers=[],
267 ... path=os.path.join('path', 'to', 'my', 'playlist'))
268 >>> p.from_string(string, identify=False)
272 {u'note': u'An example playlist'}
277 >>> p[-1].info['note']
278 u'The second curve\\nwith endlines'
280 doc = xml.dom.minidom.parseString(string)
281 self._from_xml_doc(doc, identify=identify)
283 def load(self, path=None, identify=True):
284 """Load a playlist from a file.
287 doc = xml.dom.minidom.parse(self.path)
288 self._from_xml_doc(doc, identify=identify)
289 self._digest = self.digest()
291 def save(self, path=None):
292 """Saves the playlist in a XML file.
295 f = file(self.path, 'w')
296 f.write(self.flatten())