home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / serpentine / xspf.py < prev    next >
Encoding:
Python Source  |  2006-08-23  |  4.1 KB  |  146 lines

  1. # LGPL License
  2. #
  3. # Copyright (C) 2005 Tiago Cogumbreiro <cogumbreiro@users.sf.net>
  4. #
  5. # This library is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Library General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 2 of the License, or (at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. # Library General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Library General Public
  16. # License along with this library; if not, write to the
  17. # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18. # Boston, MA 02111-1307, USA.
  19. #
  20. # Authors: Tiago Cogumbreiro <cogumbreiro@users.sf.net>
  21.  
  22. """
  23. This is a very simple utility module for retrieving basic XSPF playlist data.
  24. Basically it retrieves the playlist tracks' title, artist, location and duration.
  25. """
  26. from xml.dom import minidom
  27. from xml.xpath import Evaluate
  28. from xml.dom.minidom import getDOMImplementation
  29.  
  30. class _Field (object):
  31.     def __init__ (self, id = None, convert = None):
  32.         self.id = id
  33.         self.convert = convert
  34.         
  35.     data = None
  36.         
  37.     def toxml (self, doc, root):
  38.         if self.data is not None:
  39.             node = doc.createElement (self.id)
  40.             node.appendChild (doc.createTextNode (str (self.data)))
  41.             root.appendChild (node)
  42.  
  43. class _Struct (object):
  44.     _fields = {}
  45.     
  46.     def __init__ (self, **kw):
  47.         for key in kw:
  48.             assert key in self._fields
  49.             self._fields[key].data = kw[key]
  50.             
  51.     def __setattr__ (self, attr, value):
  52.         if attr in self._fields:
  53.             self._fields[attr].data = value
  54.         else:
  55.             self.__dict__[attr] = value
  56.     
  57.     def __getattr__ (self, attr):
  58.         if attr in self._fields:
  59.             return self._fields[attr].data
  60.         else:
  61.             raise AttributeError, "Instance has no attribute '%s'" % (attr)
  62.     
  63.     def toxml (self, doc, root):
  64.         
  65.         for key in self._fields:
  66.             self._fields[key].toxml(doc, root)
  67.     
  68.     def _parse_node (self, node):
  69.         for field in self._fields:
  70.             try:
  71.                 avail_fields = Evaluate ("%s"  % (field), node)
  72.                 if len(avail_fields) == 0:
  73.                     # No fields skip this one
  74.                     continue
  75.                 # Get the first field
  76.                 field_node = avail_fields[0]
  77.                 val = Evaluate ("string()", field_node).strip()
  78.                 convert = self._fields[field].convert
  79.                 if convert:
  80.                     val = convert (val)
  81.                 self._fields[field].data = val
  82.             except ValueError:
  83.                 pass
  84.  
  85. class Track (_Struct):
  86.     def __init__ (self, **kw):
  87.         self._fields = {"title": _Field("title"),
  88.                         "creator": _Field("creator"),
  89.                         "duration": _Field("duration", int),
  90.                         "location": _Field("location")}
  91.         _Struct.__init__ (self, **kw)
  92.                
  93.     def toxml (self, doc, root):
  94.         node = doc.createElement ("track")
  95.         root.appendChild (node)
  96.         return _Struct.toxml(self, doc, node)
  97.  
  98. class Playlist (_Struct):
  99.     def __init__ (self, **kw):
  100.         self._fields = {"title": _Field("title"),
  101.                         "creator": _Field("creator"),
  102.                         "duration": _Field("duration", int),
  103.                         "location": _Field("location")}
  104.         _Struct.__init__ (self, **kw)
  105.         self.tracks = []
  106.     
  107.     def toxml (self):
  108.         """Returns a xml.dom.Document representing the XSPF playlist"""
  109.         
  110.         DOM = getDOMImplementation ()
  111.         doc = DOM.createDocument (None, "playlist", None)
  112.         root = doc.documentElement
  113.         root.setAttribute ("version", "0")
  114.         
  115.         trackList = doc.createElement ("trackList")
  116.         root.appendChild (trackList)
  117.         
  118.         for t in self.tracks:
  119.             t.toxml (doc, trackList)
  120.         
  121.         return doc
  122.  
  123.     def parse (self, file_or_filename):
  124.         root = minidom.parse (file_or_filename)
  125.         # Iterate over tracks
  126.         for track_node in Evaluate ("/playlist/trackList/track", root):
  127.             t = Track()
  128.             # Get each field
  129.             #for field in t._fields:
  130.             #    try:
  131.             #        val = Evaluate ("string(%s)" % (field), track_node).strip()
  132.             #        convert = t._fields[field].convert
  133.             #        if convert:
  134.             #            val = convert (val)
  135.             #        setattr (t, field, val)
  136.             #    except ValueError:
  137.             #        pass
  138.             t._parse_node (track_node)
  139.             self.tracks.append(t)
  140.  
  141. if __name__ == '__main__':
  142.     import sys
  143.     p = Playlist ()
  144.     for arg in sys.argv[1:]:
  145.         print to_xml_string (p.parse_filename (arg))
  146.