0e6988c660bee65bb2a96cf9fea0ebcfd4335088
[hooke.git] / contrib / upgrade_playlist_0p1.py
1 #!/usr/bin/python
2 # Copyright (C) 2010-2012 W. Trevor King <wking@tremily.us>
3 #
4 # This file is part of Hooke.
5 #
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
9 # later version.
10 #
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
14 # details.
15 #
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/>.
18
19 """Upgrade version 0.1 playlists (XML) to the current Hooke playlist
20 file format.
21 """
22
23 import sys
24 import xml.dom.minidom
25
26 import yaml
27
28 from hooke.playlist import FilePlaylist
29
30
31 class Converter (FilePlaylist):
32     def _restore_key(self, key):
33         """Restore keys encoded with :meth:`_clean_key`.
34         """
35         return key.replace(u'\u00B7', ' ')
36
37     def _from_xml_doc(self, doc, identify=True):
38         """Load a playlist from an :class:`xml.dom.minidom.Document`
39         instance.
40         """
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)
50             else:
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()])
56             info.pop('path')
57             self.append_curve_by_path(path, info, identify=identify)
58         self.jump(self._index) # ensure valid index
59
60     def from_string(self, string, identify=True):
61         u"""Load a playlist from a string.
62
63         Examples
64         --------
65
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&#xA;with endlines" path="../curve/two"/>
70         ... </playlist>
71         ... '''
72         >>> p = FilePlaylist(drivers=[],
73         ...                  path=os.path.join('path', 'to', 'my', 'playlist'))
74         >>> p.from_string(string, identify=False)
75         >>> p._index
76         1
77         >>> p.info
78         {u'note': u'An example playlist'}
79         >>> for curve in p:
80         ...     print curve.path
81         path/to/curve/one
82         path/to/curve/two
83         >>> p[-1].info['attr with spaces']
84         u'The second curve\\nwith endlines'
85         """
86         doc = xml.dom.minidom.parseString(string)
87         self._from_xml_doc(doc, identify=identify)
88
89     def load(self, path=None, identify=True, hooke=None):
90         """Load a playlist from a file.
91         """
92         self.set_path(path)
93         doc = xml.dom.minidom.parse(self.path)
94         self._from_xml_doc(doc, identify=identify)
95         #self._digest = self.digest()
96         for curve in self:
97             curve.set_hooke(hooke)
98
99
100 if __name__ == '__main__':
101     if len(sys.argv) < 2:
102         print >> sys.stderr, 'usage: upgrade_playlist_0p1.py X.hkp [Y.hkp ...]'
103         sys.exit(1)
104
105     for path in sys.argv[1:]:
106         p = Converter(drivers=None, path=path)
107         p.load(identify=False)
108         p.save()