1 """The playlist module provides :class:`Playlist` its subclass
2 :class:`FilePlaylist` for manipulating lists of
3 :class:`hooke.curve.Curve`\s.
11 from . import curve as curve
14 class NoteIndexList (list):
15 """A list that keeps track of a "current" item and additional notes.
17 :attr:`index` (i.e. "bookmark") is the index of the currently
18 current curve. Also keep a :class:`dict` of additional information
21 def __init__(self, name=None):
22 super(NoteIndexList, self).__init__()
30 return self[self._index]
32 def jump(self, index):
36 self._index = index % len(self)
39 self.jump(self._index + 1)
42 self.jump(self._index - 1)
44 def filter(self, keeper_fn=lambda item:True):
45 c = copy.deepcopy(self)
46 for item in reversed(c):
47 if keeper_fn(item) != True:
49 try: # attempt to maintain the same current item
50 c._index = c.index(self.current())
55 class Playlist (NoteIndexList):
56 """A :class:`NoteIndexList` of :class:`hooke.curve.Curve`\s.
58 Keeps a list of :attr:`drivers` for loading curves.
60 def __init__(self, drivers, name=None):
61 super(Playlist, self).__init__(name=name)
62 self.drivers = drivers
64 def append_curve_by_path(self, path, info=None, identify=True):
66 path = os.path.join(self.path, path)
67 path = os.path.normpath(path)
68 c = curve.Curve(path, info=info)
70 c.identify(self.drivers)
74 class FilePlaylist (Playlist):
77 def __init__(self, drivers, name=None, path=None):
78 super(FilePlaylist, self).__init__(drivers, name)
82 def set_path(self, path):
83 if not path.endswith('.hkp'):
85 if name == None and path != None:
86 name = os.path.basename(path)
89 return self.digest() == self._digest
92 r"""Compute the sha1 digest of the flattened playlist
98 >>> root_path = os.path.sep + 'path'
99 >>> p = FilePlaylist(drivers=[],
100 ... path=os.path.join(root_path, 'to','playlist'))
101 >>> p.info['note'] = 'An example playlist'
102 >>> c = curve.Curve(os.path.join(root_path, 'to', 'curve', 'one'))
103 >>> c.info['note'] = 'The first curve'
105 >>> c = curve.Curve(os.path.join(root_path, 'to', 'curve', 'two'))
106 >>> c.info['note'] = 'The second curve'
109 "\xa1\x99\x8a\x99\xed\xad\x13'\xa7w\x12\x00\x07Z\xb3\xd0zN\xa2\xe1"
111 string = self.flatten()
112 return hashlib.sha1(string).digest()
114 def flatten(self, absolute_paths=False):
115 """Create a string representation of the playlist.
117 A playlist is an XML document with the following syntax::
119 <?xml version="1.0" encoding="utf-8"?>
120 <playlist attribute="value">
121 <curve path="/my/file/path/"/ attribute="value" ...>
125 Relative paths are interpreted relative to the location of the
131 >>> root_path = os.path.sep + 'path'
132 >>> p = FilePlaylist(drivers=[],
133 ... path=os.path.join(root_path, 'to','playlist'))
134 >>> p.info['note'] = 'An example playlist'
135 >>> c = curve.Curve(os.path.join(root_path, 'to', 'curve', 'one'))
136 >>> c.info['note'] = 'The first curve'
138 >>> c = curve.Curve(os.path.join(root_path, 'to', 'curve', 'two'))
139 >>> c.info['note'] = 'The second curve'
141 >>> print p.flatten() # doctest: +NORMALIZE_WHITESPACE +REPORT_UDIFF
142 <?xml version="1.0" encoding="utf-8"?>
143 <playlist index="0" note="An example playlist" version="0.1">
144 <curve note="The first curve" path="../curve/one"/>
145 <curve note="The second curve" path="../curve/two"/>
148 >>> print p.flatten(absolute_paths=True) # doctest: +NORMALIZE_WHITESPACE +REPORT_UDIFF
149 <?xml version="1.0" encoding="utf-8"?>
150 <playlist index="0" note="An example playlist" version="0.1">
151 <curve note="The first curve" path="/path/to/curve/one"/>
152 <curve note="The second curve" path="/path/to/curve/two"/>
156 implementation = xml.dom.minidom.getDOMImplementation()
157 # create the document DOM object and the root element
158 doc = implementation.createDocument(None, 'playlist', None)
159 root = doc.documentElement
160 root.setAttribute('version', self.version) # store playlist version
161 root.setAttribute('index', str(self._index))
162 for key,value in self.info.items(): # save info variables
163 root.setAttribute(key, str(value))
164 for curve in self: # save curves and their attributes
165 curve_element = doc.createElement('curve')
166 root.appendChild(curve_element)
167 path = os.path.abspath(os.path.expanduser(curve.path))
168 if absolute_paths == False:
169 path = os.path.relpath(
171 os.path.abspath(os.path.expanduser(self.path)))
172 curve_element.setAttribute('path', path)
173 for key,value in curve.info.items():
174 curve_element.setAttribute(key, str(value))
175 string = doc.toprettyxml(encoding='utf-8')
176 root.unlink() # break circular references for garbage collection
179 def _from_xml_doc(self, doc):
180 """Load a playlist from an :class:`xml.dom.minidom.Document`
183 root = doc.documentElement
184 for attribute,value in root.attributes.items():
185 if attribute == 'version':
186 assert value == self.version, \
187 'Cannot read v%s playlist with a v%s reader' \
188 % (value, self.version)
189 elif attribute == 'index':
190 self._index = int(value)
192 self.info[attribute] = value
193 for curve_element in doc.getElementsByTagName('curve'):
194 path = curve_element.getAttribute('path')
195 info = dict(curve_element.attributes.items())
197 self.append_curve_by_path(path, info, identify=False)
198 self.jump(self._index) # ensure valid index
200 def from_string(self, string):
201 """Load a playlist from a string.
206 >>> string = '''<?xml version="1.0" encoding="utf-8"?>
207 ... <playlist index="1" note="An example playlist" version="0.1">
208 ... <curve note="The first curve" path="../curve/one"/>
209 ... <curve note="The second curve" path="../curve/two"/>
212 >>> p = FilePlaylist(drivers=[],
213 ... path=os.path.join('path', 'to','playlist'))
214 >>> p.from_string(string)
218 {u'note': u'An example playlist'}
224 doc = xml.dom.minidom.parseString(string)
225 return self._from_xml_doc(doc)
227 def load(self, path=None):
228 """Load a playlist from a file.
231 doc = xml.dom.minidom.parse(self.path)
232 return self._from_xml_doc(doc)
234 def save(self, path=None):
235 """Saves the playlist in a XML file.
238 f = file(self.path, 'w')
239 f.write(self.flatten())