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.

73 lines
1.7 KiB

  1. #!/usr/bin/env python
  2. # Copyright 2006 John-Mark Gurney <jmg@funkthat.com>
  3. __version__ = '$Change$'
  4. # $Id$
  5. #
  6. # Convert file information into a DIDL class. Dynamicly generate a new class
  7. # from a base class and the DIDL class to be determined.
  8. #
  9. __all__ = [ 'mimetoclass', 'buildClassMT', 'getClassMT', ]
  10. import os.path
  11. import weakref
  12. from DIDLLite import VideoItem, AudioItem, TextItem, ImageItem
  13. from twisted.python import log
  14. from twisted.web import static
  15. mimedict = static.loadMimeTypes()
  16. classdict = weakref.WeakValueDictionary()
  17. mimetoclass = {
  18. 'application/ogg': AudioItem,
  19. 'video': VideoItem,
  20. 'audio': AudioItem,
  21. 'text': TextItem,
  22. 'image': ImageItem,
  23. }
  24. def getClassMT(name, mimetype = None, fp = None):
  25. '''Return a tuple of the DIDLLite class and mimetype responsible for the named/mimetyped/fpd file.'''
  26. if mimetype is None:
  27. fn, ext = os.path.splitext(name)
  28. ext = ext.lower()
  29. try:
  30. mimetype = mimedict[ext]
  31. except KeyError:
  32. log.msg('no mime-type for: %s' % name)
  33. return None, None
  34. ty = mimetype.split('/')[0]
  35. if mimetype in mimetoclass:
  36. klass = mimetoclass[mimetype]
  37. elif ty in mimetoclass:
  38. klass = mimetoclass[ty]
  39. else:
  40. # XXX - We could fall file -i on it
  41. log.msg('no item for mimetype: %s' % mimetype)
  42. return None, None
  43. return klass, mimetype
  44. def buildClassMT(baseklass, name, *args, **kwargs):
  45. klass, mt = getClassMT(name, *args, **kwargs)
  46. if klass is None:
  47. return None, None
  48. try:
  49. return classdict[(baseklass, klass)], mt
  50. except KeyError:
  51. pass
  52. class ret(baseklass, klass):
  53. pass
  54. ret.__name__ = '+'.join(['%s.%s' % (x.__module__, x.__name__) for x in (baseklass, klass)])
  55. classdict[(baseklass, klass)] = ret
  56. return ret, mt