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 modify it
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 # 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.
31 from yaml.representer import RepresenterError
33 from . import curve as curve
34 from .util.itertools import reverse_enumerate
37 class NoteIndexList (list):
38 """A list that keeps track of a "current" item and additional notes.
40 :attr:`index` (i.e. "bookmark") is the index of the currently
41 current curve. Also keep a :class:`dict` of additional information
44 def __init__(self, name=None):
45 super(NoteIndexList, self).__init__()
49 self._set_ignored_attrs()
52 return str(self.__unicode__())
54 def __unicode__(self):
55 return u'<%s %s>' % (self.__class__.__name__, self.name)
60 def _set_ignored_attrs(self):
61 self._ignored_attrs = ['_ignored_attrs', '_default_attrs']
62 self._default_attrs = {
66 def __getstate__(self):
67 state = dict(self.__dict__)
68 for key in self._ignored_attrs:
71 for key,value in self._default_attrs.items():
72 if key in state and state[key] == value:
74 assert 'items' not in state
76 self._assert_clean_state(self, state)
77 for item in self: # save curves and their attributes
78 item_state = self._item_getstate(item)
79 self._assert_clean_state(item, item_state)
80 state['items'].append(item_state)
83 def __setstate__(self, state):
84 self._set_ignored_attrs()
85 for key,value in self._default_attrs.items():
86 setattr(self, key, value)
87 for key,value in state.items():
90 setattr(self, key, value)
91 for item_state in state['items']:
92 self.append(self._item_setstate(item_state))
94 def _item_getstate(self, item):
97 def _item_setstate(self, state):
100 def _assert_clean_state(self, owner, state):
101 for k,v in state.items():
102 if k == 'drivers': # HACK. Need better driver serialization.
106 except RepresenterError, e:
107 raise NotImplementedError(
108 'cannot convert %s.%s = %s (%s) to YAML\n%s'
109 % (owner.__class__.__name__, k, v, type(v), e))
111 def _setup_item(self, item):
112 """Perform any required initialization before returning an item.
116 def index(self, value=None, *args, **kwargs):
117 """Extend `list.index`, returning the current index if `value`
122 return super(NoteIndexList, self).index(value, *args, **kwargs)
127 item = self[self._index]
128 self._setup_item(item)
131 def jump(self, index):
135 self._index = index % len(self)
138 self.jump(self._index + 1)
141 self.jump(self._index - 1)
143 def items(self, reverse=False):
144 """Iterate through `self` calling `_setup_item` on each item
149 Updates :attr:`_index` during the iteration so
150 :func:`~hooke.plugin.curve.current_curve_callback` works as
151 expected in :class:`~hooke.command.Command`\s called from
152 :class:`~hooke.plugin.playlist.ApplyCommand`. After the
153 iteration completes, :attr:`_index` is restored to its
159 items = reverse_enumerate(self)
161 items = enumerate(self)
164 self._setup_item(item)
168 def filter(self, keeper_fn=lambda item:True, load_curves=True,
170 c = copy.deepcopy(self)
171 if load_curves == True:
172 items = c.items(reverse=True)
176 if keeper_fn(item, *args, **kwargs) != True:
178 try: # attempt to maintain the same current item
179 c._index = c.index(self.current())
185 class Playlist (NoteIndexList):
186 """A :class:`NoteIndexList` of :class:`hooke.curve.Curve`\s.
188 Keeps a list of :attr:`drivers` for loading curves.
190 def __init__(self, drivers, name=None):
191 super(Playlist, self).__init__(name=name)
192 self.drivers = drivers
193 self._max_loaded = 100 # curves to hold in memory simultaneously.
195 def _set_ignored_attrs(self):
196 super(Playlist, self)._set_ignored_attrs()
197 self._ignored_attrs.extend([
198 '_item_ignored_attrs', '_item_default_attrs',
200 self._item_ignored_attrs = ['data']
201 self._item_default_attrs = {
207 self._loaded = [] # List of loaded curves, see :meth:`._setup_item`.
209 def _item_getstate(self, item):
210 assert isinstance(item, curve.Curve), type(item)
211 state = item.__getstate__()
212 for key in self._item_ignored_attrs:
215 for key,value in self._item_default_attrs.items():
216 if key in state and state[key] == value:
220 def _item_setstate(self, state):
221 for key,value in self._item_default_attrs.items():
224 item = curve.Curve(path=None)
225 item.__setstate__(state)
228 def append_curve_by_path(self, path, info=None, identify=True, hooke=None):
229 path = os.path.normpath(path)
230 c = curve.Curve(path, info=info)
233 c.identify(self.drivers)
237 def _setup_item(self, curve):
238 if curve != None and curve not in self._loaded:
239 if curve not in self:
241 if curve.driver == None:
242 c.identify(self.drivers)
243 if curve.data == None:
245 self._loaded.append(curve)
246 if len(self._loaded) > self._max_loaded:
247 oldest = self._loaded.pop(0)
251 class FilePlaylist (Playlist):
252 """A file-backed :class:`Playlist`.
256 def __init__(self, drivers, name=None, path=None):
257 super(FilePlaylist, self).__init__(drivers, name)
258 self.path = self._base_path = None
260 self._relative_curve_paths = True
262 def _set_ignored_attrs(self):
263 super(FilePlaylist, self)._set_ignored_attrs()
264 self._ignored_attrs.append('_digest')
267 def __getstate__(self):
268 state = super(FilePlaylist, self).__getstate__()
269 assert 'version' not in state, state
270 state['version'] = self.version
273 def __setstate__(self, state):
274 assert('version') in state, state
275 version = state.pop('version')
276 assert version == FilePlaylist.version, (
277 'invalid version %s (%s) != %s (%s)'
278 % (version, type(version),
279 FilePlaylist.version, type(FilePlaylist.version)))
280 super(FilePlaylist, self).__setstate__(state)
282 def _item_getstate(self, item):
283 state = super(FilePlaylist, self)._item_getstate(item)
284 if state.get('path', None) != None:
285 path = os.path.abspath(os.path.expanduser(state['path']))
286 if self._relative_curve_paths == True:
287 path = os.path.relpath(path, self._base_path)
291 def _item_setstate(self, state):
292 item = super(FilePlaylist, self)._item_setstate(state)
294 item.set_path(os.path.join(self._base_path, state['path']))
297 def set_path(self, path):
299 if self._base_path == None:
300 self._base_path = os.getcwd()
302 if not path.endswith('.hkp'):
305 self._base_path = os.path.dirname(os.path.abspath(
306 os.path.expanduser(self.path)))
307 if self.name == None:
308 self.name = os.path.basename(path)
310 def append_curve_by_path(self, path, *args, **kwargs):
311 if self._base_path != None:
312 path = os.path.join(self._base_path, path)
313 super(FilePlaylist, self).append_curve_by_path(path, *args, **kwargs)
316 return self.digest() == self._digest
319 r"""Compute the sha1 digest of the flattened playlist
325 >>> root_path = os.path.sep + 'path'
326 >>> p = FilePlaylist(drivers=[],
327 ... path=os.path.join(root_path, 'to','playlist'))
328 >>> p.info['note'] = 'An example playlist'
329 >>> c = curve.Curve(os.path.join(root_path, 'to', 'curve', 'one'))
330 >>> c.info['note'] = 'The first curve'
332 >>> c = curve.Curve(os.path.join(root_path, 'to', 'curve', 'two'))
333 >>> c.info['note'] = 'The second curve'
336 '\xa1\x1ax\xb1|\x84uA\xe4\x1d\xbf`\x004|\x82\xc2\xdd\xc1\x9e'
338 string = self.flatten()
339 return hashlib.sha1(string).digest()
342 """Create a string representation of the playlist.
344 A playlist is a YAML document with the following minimal syntax::
348 - path: picoforce.000
349 - path: picoforce.001
351 Relative paths are interpreted relative to the location of the
357 >>> from .engine import CommandMessage
359 >>> root_path = os.path.sep + 'path'
360 >>> p = FilePlaylist(drivers=[],
361 ... path=os.path.join(root_path, 'to','playlist'))
362 >>> p.info['note'] = 'An example playlist'
363 >>> c = curve.Curve(os.path.join(root_path, 'to', 'curve', 'one'))
364 >>> c.info['note'] = 'The first curve'
366 >>> c = curve.Curve(os.path.join(root_path, 'to', 'curve', 'two'))
367 >>> c.info['attr with spaces'] = 'The second curve\\nwith endlines'
368 >>> c.command_stack.extend([
369 ... CommandMessage('command A', {'arg 0':0, 'arg 1':'X'}),
370 ... CommandMessage('command B', {'arg 0':1, 'arg 1':'Y'}),
373 >>> print p.flatten() # doctest: +REPORT_UDIFF
374 # Hooke playlist version 0.2
378 _relative_curve_paths: true
380 info: {note: An example playlist}
382 - info: {note: The first curve}
385 - command_stack: !!python/object/new:hooke.command_stack.CommandStack
387 - !!python/object:hooke.engine.CommandMessage
388 arguments: {arg 0: 0, arg 1: X}
390 - !!python/object:hooke.engine.CommandMessage
391 arguments: {arg 0: 1, arg 1: Y}
393 info: {attr with spaces: 'The second curve
399 path: /path/to/playlist.hkp
402 >>> p._relative_curve_paths = False
403 >>> print p.flatten() # doctest: +REPORT_UDIFF
404 # Hooke playlist version 0.2
408 _relative_curve_paths: false
410 info: {note: An example playlist}
412 - info: {note: The first curve}
414 path: /path/to/curve/one
415 - command_stack: !!python/object/new:hooke.command_stack.CommandStack
417 - !!python/object:hooke.engine.CommandMessage
418 arguments: {arg 0: 0, arg 1: X}
420 - !!python/object:hooke.engine.CommandMessage
421 arguments: {arg 0: 1, arg 1: Y}
423 info: {attr with spaces: 'The second curve
427 path: /path/to/curve/two
429 path: /path/to/playlist.hkp
433 yaml_string = yaml.dump(self.__getstate__(), allow_unicode=True)
434 return ('# Hooke playlist version %s\n' % self.version) + yaml_string
436 def from_string(self, string):
437 u"""Load a playlist from a string.
444 >>> string = '''# Hooke playlist version 0.2
447 ... - path: picoforce.000
448 ... - path: picoforce.001
450 >>> p = FilePlaylist(drivers=[],
451 ... path=os.path.join('/path', 'to', 'my', 'playlist'))
452 >>> p.from_string(string)
455 /path/to/my/picoforce.000
456 /path/to/my/picoforce.001
458 More complicated example.
460 >>> string = '''# Hooke playlist version 0.2
461 ... _base_path: /path/to
465 ... _relative_curve_paths: true
466 ... info: {note: An example playlist}
468 ... - info: {note: The first curve}
470 ... - command_stack: !!python/object/new:hooke.command_stack.CommandStack
472 ... - !!python/object:hooke.engine.CommandMessage
473 ... arguments: {arg 0: 0, arg 1: X}
474 ... command: command A
475 ... - !!python/object:hooke.engine.CommandMessage
476 ... arguments: {arg 0: 1, arg 1: Y}
477 ... command: command B
478 ... info: {attr with spaces: 'The second curve
483 ... name: playlist.hkp
484 ... path: /path/to/playlist.hkp
487 >>> p = FilePlaylist(drivers=[],
488 ... path=os.path.join('path', 'to', 'my', 'playlist'))
489 >>> p.from_string(string)
493 {'note': 'An example playlist'}
495 ... print curve.name, curve.path
496 one /path/to/curve/one
497 two /path/to/curve/two
498 >>> p[-1].info['attr with spaces']
499 'The second curve\\nwith endlines'
500 >>> type(p[-1].command_stack)
501 <class 'hooke.command_stack.CommandStack'>
502 >>> p[-1].command_stack # doctest: +NORMALIZE_WHITESPACE
503 [<CommandMessage command A {arg 0: 0, arg 1: X}>,
504 <CommandMessage command B {arg 0: 1, arg 1: Y}>]
506 state = yaml.load(string)
507 self.__setstate__(state)
509 def save(self, path=None, makedirs=True):
510 """Saves the playlist to a YAML file.
513 dirname = os.path.dirname(self.path) or '.'
514 if makedirs == True and not os.path.isdir(dirname):
516 with open(self.path, 'w') as f:
517 f.write(self.flatten())
518 self._digest = self.digest()
520 def load(self, path=None, identify=True, hooke=None):
521 """Load a playlist from a file.
524 with open(self.path, 'r') as f:
526 self.from_string(text)
527 self._digest = self.digest()
529 curve.set_hooke(hooke)
531 curve.identify(self.drivers)
534 class Playlists (NoteIndexList):
535 """A :class:`NoteIndexList` of :class:`FilePlaylist`\s.
537 def __init__(self, *arg, **kwargs):
538 super(Playlists, self).__init__(*arg, **kwargs)
540 def _item_getstate(self, item):
541 assert isinstance(item, FilePlaylist), type(item)
542 return item.__getstate__()
544 def _item_setstate(self, state):
545 item = FilePlaylist(drivers=[])
546 item.__setstate__(state)