2 # Copyright (C) 2010-2012 W. Trevor King <wking@drexel.edu>
4 # This file is part of Hooke.
6 # Hooke is free software: you can redistribute it and/or modify it under the
7 # terms of the GNU Lesser General Public License as published by the Free
8 # Software Foundation, either version 3 of the License, or (at your option) any
11 # Hooke is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16 # You should have received a copy of the GNU Lesser General Public License
17 # along with Hooke. If not, see <http://www.gnu.org/licenses/>.
19 """Upgrade version 0.1 playlists (XML) to the current Hooke playlist
24 import xml.dom.minidom
28 from hooke.playlist import FilePlaylist
31 class Converter (FilePlaylist):
32 def _restore_key(self, key):
33 """Restore keys encoded with :meth:`_clean_key`.
35 return key.replace(u'\u00B7', ' ')
37 def _from_xml_doc(self, doc, identify=True):
38 """Load a playlist from an :class:`xml.dom.minidom.Document`
41 root = doc.documentElement
42 for attribute,value in root.attributes.items():
43 attribute = self._restore_key(attribute)
44 if attribute == 'version':
45 assert value == '0.1', \
46 'Cannot read v%s playlist with a v%s reader' \
47 % (value, self.version)
48 elif attribute == 'index':
49 self._index = int(value)
51 self.info[attribute] = value
52 for curve_element in doc.getElementsByTagName('curve'):
53 path = curve_element.getAttribute('path')
54 info = dict([(self._restore_key(key), value)
55 for key,value in curve_element.attributes.items()])
57 self.append_curve_by_path(path, info, identify=identify)
58 self.jump(self._index) # ensure valid index
60 def from_string(self, string, identify=True):
61 u"""Load a playlist from a string.
66 >>> string = '''<?xml version="1.0" encoding="utf-8"?>
67 ... <playlist index="1" note="An example playlist" version="0.1">
68 ... <curve note="The first curve" path="../curve/one"/>
69 ... <curve attr\xb7with\xb7spaces="The second curve
with endlines" path="../curve/two"/>
72 >>> p = FilePlaylist(drivers=[],
73 ... path=os.path.join('path', 'to', 'my', 'playlist'))
74 >>> p.from_string(string, identify=False)
78 {u'note': u'An example playlist'}
83 >>> p[-1].info['attr with spaces']
84 u'The second curve\\nwith endlines'
86 doc = xml.dom.minidom.parseString(string)
87 self._from_xml_doc(doc, identify=identify)
89 def load(self, path=None, identify=True, hooke=None):
90 """Load a playlist from a file.
93 doc = xml.dom.minidom.parse(self.path)
94 self._from_xml_doc(doc, identify=identify)
95 #self._digest = self.digest()
97 curve.set_hooke(hooke)
100 if __name__ == '__main__':
101 if len(sys.argv) < 2:
102 print >> sys.stderr, 'usage: upgrade_playlist_0p1.py X.hkp [Y.hkp ...]'
105 for path in sys.argv[1:]:
106 p = Converter(drivers=None, path=path)
107 p.load(identify=False)