From 3f27866400453ea44c4c5f32b99832d394c697e2 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Sat, 5 Dec 2009 23:43:59 -0800 Subject: [PATCH] add a ZipInfo wrapper class that adds a __cmp__ method to see if two files are the same... we check name (maybe we should drop that) date_time, size and CRC... make the two createObjects have the same structure... [git-p4: depot-paths = "//depot/": change = 1409] --- ZipStorage.py | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/ZipStorage.py b/ZipStorage.py index 8d2deb1..2246f67 100644 --- a/ZipStorage.py +++ b/ZipStorage.py @@ -62,6 +62,35 @@ def buildNameHier(names, objs, sep): return ret +def zipinfocmp(za, zb): + for i in [ 'filename', 'date_time', 'file_size', 'CRC' ]: + r = cmp(getattr(za, i), getattr(zb, i)) + if r: + return r + + return 0 + +class ZIWrap(object): + __slots__ = [ '_zi' ] + + def __init__(self, zi): + self._zi = zi + + __cmp__ = zipinfocmp + + def __getattr__(self, n): + return getattr(self._zi, n) + + def __setattr__(self, n, k): + if n == '_zi': + object.__setattr__(self, n, k) + return + + return setattr(self._zi, n, k) + + def __delattr__(sefl, n): + return delattr(self._zi, n) + class ZipFileTransfer(pb.Viewable): def __init__(self, zf, name, request): self.zf = zf @@ -202,7 +231,6 @@ class ZipChildDir(ZipItem, StorageFolder): kwargs['hier'] = self.hier[i] else: klass, mt = FileDIDL.buildClassMT(ZipFile, i) - kwargs['name'] = pathname kwargs['mimetype'] = mt return klass, i, (), kwargs @@ -256,6 +284,7 @@ class ZipObject(FSObject, StorageFolder): def genChildren(self): # open the zipfile as necessary. + # XXX - this might leave too many zip files around self.zip = genZipFile(self.FSpath) nl = self.zip.namelist() @@ -268,7 +297,8 @@ class ZipObject(FSObject, StorageFolder): cnt = newsum self.sep = cursep - hier = buildNameHier(nl, self.zip.infolist(), cursep) + hier = buildNameHier(nl, [ ZIWrap(x) for x in + self.zip.infolist() ], cursep) return hier @@ -279,9 +309,9 @@ class ZipObject(FSObject, StorageFolder): kwargs = { 'zf': self.zip, 'zo': self, 'name': i } if isinstance(arg, dict): # must be a dir - kwargs['hier'] = arg - kwargs['sep'] = self.sep klass = ZipChildDir + kwargs['sep'] = self.sep + kwargs['hier'] = arg else: klass, mt = FileDIDL.buildClassMT(ZipFile, i) kwargs['mimetype'] = mt