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.

84 lines
1.9 KiB

  1. #!/usr/bin/env python
  2. # Copyright 2006 John-Mark Gurney <gurney_j@resnet.uroegon.edu>
  3. #
  4. # $Id$
  5. #
  6. import os.path
  7. import zipfile
  8. from DIDLLite import StorageFolder, Item, VideoItem, AudioItem, TextItem, ImageItem, Resource
  9. from FSStorage import FSObject, registerklassfun
  10. def isatpath(f, p):
  11. if f[:len(p)] != p:
  12. # First part of path doesn't match, it's not
  13. return False
  14. slash = f[len(p):].find('/')
  15. if slash != -1 and slash != len(f) - len(p) + 1:
  16. # Another path component, skip it, as long as it's not the char
  17. return False
  18. # otherwise, it is at this level
  19. endtrim = len(f)
  20. if slash:
  21. endtrip -= 1
  22. return f[len(p):endtrim]
  23. class ZipChildDir(StorageFolder):
  24. '''This is to represent a child dir of the zip file.'''
  25. class ZipFile(FSObject, StorageFolder):
  26. def __init__(self, *args, **kwargs):
  27. '''If a zip argument is passed it, use that as the zip archive.'''
  28. path = kwargs['path']
  29. del kwargs['path']
  30. StorageFolder.__init__(self, *args, **kwargs)
  31. FSObject.__init__(self, path)
  32. # mapping from path to objectID
  33. self.pathObjmap = {}
  34. def getdirents(path):
  35. '''Returns the list of entires for the path, '' is the root path.'''
  36. lst = self.zip.namelist()
  37. if path:
  38. path += '/'
  39. return filter(lambda x, p = path: isatpath(x, p), lst)
  40. def doUpdate(self):
  41. # open the zipfile as necessary.
  42. self.zip = zipfile.ZipFile(self.FSpath)
  43. allzipfiles = sets.Set(self.zip.namelist())
  44. children = sets.Set(getdirents(''))
  45. for i in self.pathObjmap.keys():
  46. if i not in children:
  47. # delete
  48. self.cd.delItem(self.pathObjmap[i])
  49. del self.pathObjmap[i]
  50. for i in children:
  51. if i in self.pathObjmap:
  52. continue
  53. # new object
  54. if i not in allzipfiles:
  55. # must be a dir
  56. else:
  57. ZipChild(self, i)
  58. def detectzipfile(path, fobj):
  59. try:
  60. z = zipfile.ZipFile(fobj)
  61. except:
  62. return None, None
  63. return ZipFile, {}
  64. registerklassfun(detectzipfile)