Browse Source

support different path seps, and also skip directories if they

have the isdir attr and it's true..

[git-p4: depot-paths = "//depot/": change = 1223]
replace/b43bf02ddeddd088c0e6b94974ca1a46562eb3db
John-Mark Gurney 16 years ago
parent
commit
2648998737
2 changed files with 38 additions and 14 deletions
  1. +4
    -0
      README
  2. +34
    -14
      ZipStorage.py

+ 4
- 0
README View File

@@ -97,6 +97,10 @@ v0.x:
so that I can browse the shows I have recorded.
Fix transcoding of MPEG-2, use mp2 audio instead of mp3.
Sync up the programs in mpegts.
Skip directories in ZipStorage for zip and rar archives.
Support both / and \ as path separators in ZipStorage. RarFile uses
a \ while tar uses /. We count the number of each and use the most
common.

v0.3:
Include some patches for twisted in the distro, in the directory


+ 34
- 14
ZipStorage.py View File

@@ -32,11 +32,14 @@ from twisted.web import http
from twisted.web import server
from twisted.web import resource

def inserthierdict(d, name, obj):
def inserthierdict(d, name, obj, sep):
if not name:
return
i = name.find('/')
if i == -1:

if sep is not None:
i = name.find(sep)

if sep is None or i == -1:
d[name] = obj
return

@@ -44,15 +47,18 @@ def inserthierdict(d, name, obj):
rname = name[i + 1:]
# remaining path components
try:
inserthierdict(d[dname], rname, obj)
inserthierdict(d[dname], rname, obj, sep)
except KeyError:
d[dname] = {}
inserthierdict(d[dname], rname, obj)
inserthierdict(d[dname], rname, obj, sep)

def buildNameHier(names, objs):
def buildNameHier(names, objs, sep):
ret = {}
for n, o in itertools.izip(names, objs):
inserthierdict(ret, n, o)
#Skip directories in a TarFile or RarFile
if hasattr(o, 'isdir') and o.isdir():
continue
inserthierdict(ret, n, o, sep)

return ret

@@ -176,7 +182,8 @@ class ZipChildDir(ZipItem, StorageFolder):

def __init__(self, *args, **kwargs):
self.hier = kwargs['hier']
del kwargs['hier']
self.sep = kwargs['sep']
del kwargs['hier'], kwargs['sep']
ZipItem.__init__(self, *args, **kwargs)
del kwargs['zf'], kwargs['zo'], kwargs['name']
StorageFolder.__init__(self, *args, **kwargs)
@@ -194,17 +201,19 @@ class ZipChildDir(ZipItem, StorageFolder):
self.cd.delItem(self.pathObjmap[i])
del self.pathObjmap[i]

cursep = self.sep
for i in children:
if i in self.pathObjmap:
continue

# new object
pathname = os.path.join(self.name, i)
pathname = cursep.join((self.name, i))
if isinstance(self.hier[i], dict):
# must be a dir
self.pathObjmap[i] = self.cd.addItem(self.id,
ZipChildDir, i, zf = self.zf, zo = self,
name = pathname, hier = self.hier[i])
ZipChildDir, i, zf=self.zf, zo=self,
name=pathname, hier=self.hier[i],
sep=cursep)
else:
klass, mt = FileDIDL.buildClassMT(ZipFile, i)
if klass is None:
@@ -257,6 +266,8 @@ def genZipFile(path):
raise

class ZipObject(FSObject, StorageFolder):
seps = [ '/', '\\' ]

def __init__(self, *args, **kwargs):
'''If a zip argument is passed it, use that as the zip archive.'''
path = kwargs['path']
@@ -271,7 +282,16 @@ class ZipObject(FSObject, StorageFolder):
def doUpdate(self):
# open the zipfile as necessary.
self.zip = genZipFile(self.FSpath)
hier = buildNameHier(self.zip.namelist(), self.zip.infolist())
nl = self.zip.namelist()
cnt = 0
cursep = None
for i in self.seps:
newsum = sum([ j.count(i) for j in nl ])
if newsum > cnt:
cursep = i
cnt = newsum
self.sep = cursep
hier = buildNameHier(nl, self.zip.infolist(), cursep)

doupdate = False
children = sets.Set(hier.keys())
@@ -290,8 +310,8 @@ class ZipObject(FSObject, StorageFolder):
if isinstance(hier[i], dict):
# must be a dir
self.pathObjmap[i] = self.cd.addItem(self.id,
ZipChildDir, i, zf = self.zip, zo = self,
name = i, hier = hier[i])
ZipChildDir, i, zf=self.zip, zo=self,
name=i, hier=hier[i], sep=cursep)
else:
klass, mt = FileDIDL.buildClassMT(ZipFile, i)
if klass is None:


Loading…
Cancel
Save