A Python UPnP Media Server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.7 KiB

  1. #!/usr/bin/env python
  2. # Copyright 2009 John-Mark Gurney <jmg@funkthat.com>
  3. __version__ = '$Change$'
  4. # $Id$
  5. import FileDIDL
  6. import xml.dom.minidom
  7. from DIDLLite import StorageFolder, Item, Resource, ResourceList
  8. from FSStorage import FSObject, registerklassfun
  9. def getElementText(elm, strip=True):
  10. s = ''.join(x.nodeValue for x in elm.childNodes)
  11. if strip:
  12. s = s.strip()
  13. return s
  14. class ItemObject(FSObject, Item):
  15. def __init__(self, *args, **kwargs):
  16. path = kwargs.pop('path')
  17. dom = kwargs.pop('dom')
  18. FSObject.__init__(self, path)
  19. Item.__init__(self, *args, **kwargs)
  20. self.checkUpdate(dom=dom)
  21. def doUpdate(self, dom=None):
  22. if dom is None:
  23. dom = xml.dom.minidom.parse(open(self.FSpath))
  24. obj = dom.getElementsByTagName('Object')[0]
  25. mt = getElementText(obj.getElementsByTagName('mimetype')[0])
  26. self.res = ResourceList()
  27. for i in obj.getElementsByTagName('res'):
  28. r = Resource(getElementText(i.getElementsByTagName(
  29. 'url')[0]), 'http-get:*:%s:*' % mt)
  30. size = i.getElementsByTagName('size')
  31. if size:
  32. r.size = int(getElementText(size[0]))
  33. self.res.append(r)
  34. super(ItemObject, self).doUpdate()
  35. def canHandle(fobj):
  36. # XXX - create schema and validate
  37. dom = xml.dom.minidom.parse(fobj)
  38. #print 'ch:', `dom`
  39. obj = dom.getElementsByTagName('Object')[0]
  40. mt = getElementText(obj.getElementsByTagName('mimetype')[0])
  41. #print 'ch:', `obj`, `mt`
  42. return dom, mt
  43. def detectitem(path, fobj):
  44. #print 'di:', `path`, `fobj`
  45. try:
  46. dom, mt = canHandle(fobj)
  47. except:
  48. #import traceback
  49. #traceback.print_exc()
  50. return None, None
  51. klass, mt = FileDIDL.buildClassMT(ItemObject, path, mimetype=mt)
  52. return klass, { 'path': path, 'dom': dom }
  53. registerklassfun(detectitem)