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.

72 lines
1.7 KiB

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