- implementation = xml.dom.minidom.getDOMImplementation()
- # create the document DOM object and the root element
- doc = implementation.createDocument(None, 'playlist', None)
- root = doc.documentElement
- root.setAttribute('version', self.version) # store playlist version
- root.setAttribute('index', str(self._index))
- for key,value in self.info.items(): # save info variables
- root.setAttribute(key, str(value))
- for curve in self: # save curves and their attributes
- curve_element = doc.createElement('curve')
- root.appendChild(curve_element)
- path = os.path.abspath(os.path.expanduser(curve.path))
- if absolute_paths == False:
- path = os.path.relpath(
- path,
- os.path.abspath(os.path.expanduser(self.path)))
- curve_element.setAttribute('path', path)
- for key,value in curve.info.items():
- curve_element.setAttribute(key, str(value))
- string = doc.toprettyxml(encoding='utf-8')
- root.unlink() # break circular references for garbage collection
- return string
-
- def _from_xml_doc(self, doc):
- """Load a playlist from an :class:`xml.dom.minidom.Document`
- instance.
- """
- root = doc.documentElement
- for attribute,value in root.attributes.items():
- if attribute == 'version':
- assert value == self.version, \
- 'Cannot read v%s playlist with a v%s reader' \
- % (value, self.version)
- elif attribute == 'index':
- self._index = int(value)
- else:
- self.info[attribute] = value
- for curve_element in doc.getElementsByTagName('curve'):
- path = curve_element.getAttribute('path')
- info = dict(curve_element.attributes.items())
- info.pop('path')
- self.append_curve_by_path(path, info, identify=False)
- self.jump(self._index) # ensure valid index