From 26489987376f1dc0b541217ca93cc344b7984129 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Fri, 7 Nov 2008 00:38:57 -0800 Subject: [PATCH] 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] --- README | 4 ++++ ZipStorage.py | 48 ++++++++++++++++++++++++++++++++++-------------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/README b/README index ac97441..aad1d51 100644 --- a/README +++ b/README @@ -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 diff --git a/ZipStorage.py b/ZipStorage.py index 445691a..649268a 100644 --- a/ZipStorage.py +++ b/ZipStorage.py @@ -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: