[Zodb-checkins] CVS: ZEO/ZEO - ClientCache.py:1.18.6.5

Tim Peters tim.one@home.com
Sun, 27 Jan 2002 15:31:53 -0500


Update of /cvs-repository/ZEO/ZEO
In directory cvs.zope.org:/tmp/cvs-serv9214

Modified Files:
      Tag: Standby-branch
	ClientCache.py 
Log Message:
Added whitespace throughout so that (a) I can read this, and (b) can
insert debugging prints.


=== ZEO/ZEO/ClientCache.py 1.18.6.4 => 1.18.6.5 ===
 
         # Allocate locks:
-        l=allocate_lock()
-        self._acquire=l.acquire
-        self._release=l.release
+        L = allocate_lock()
+        self._acquire = L.acquire
+        self._release = L.release
 
         if client:
             # Create a persistent cache
             if var is None:
-                try: var=CLIENT_HOME
+                try:
+                    var = CLIENT_HOME
                 except:
-                    try: var=os.path.join(INSTANCE_HOME,'var')
-                    except: var=os.getcwd()
+                    try:
+                        var = os.path.join(INSTANCE_HOME, 'var')
+                    except:
+                        var = os.getcwd()
 
             # Get the list of cache file names
-            self._p=p=map(lambda i, p=storage, var=var, c=client:
-                          os.path.join(var,'c%s-%s-%s.zec' % (p, c, i)),
-                          (0,1))
+            self._p = p = map(lambda i, p=storage, var=var, c=client:
+                                os.path.join(var, 'c%s-%s-%s.zec' % (p, c, i)),
+                              (0, 1))
             # get the list of cache files
-            self._f=f=[None, None]
+            self._f = f = [None, None]
 
             # initialize cache serial numbers
             s=['\0\0\0\0\0\0\0\0', '\0\0\0\0\0\0\0\0']
-            for i in 0,1:
+            for i in 0, 1:
                 if os.path.exists(p[i]):
-                    fi=open(p[i],'r+b')
-                    if fi.read(4)==magic: # Minimal sanity
-                        fi.seek(0,2)
+                    fi = open(p[i],'r+b')
+                    if fi.read(4) == magic: # Minimal sanity
+                        fi.seek(0, 2)
                         if fi.tell() > 30:
                             fi.seek(22)
-                            s[i]=fi.read(8)
+                            s[i] = fi.read(8)
                     # If we found a non-zero serial, then use the file
-                    if s[i] != '\0\0\0\0\0\0\0\0': f[i]=fi
-                    fi=None
+                    if s[i] != '\0\0\0\0\0\0\0\0':
+                        f[i] = fi
+                    fi = None
 
             # Whoever has the larger serial is the current
-            if s[1] > s[0]: current=1
-            elif s[0] > s[1]: current=0
+            if s[1] > s[0]:
+                current = 1
+            elif s[0] > s[1]:
+                current = 0
             else:
                 if f[0] is None:
                     # We started, open the first cache file
-                    f[0]=open(p[0], 'w+b')
+                    f[0] = open(p[0], 'w+b')
                     f[0].write(magic)
-                current=0
-                f[1]=None
+                current = 0
+                f[1] = None
         else:
             self._f = f = [tempfile.TemporaryFile(suffix='.zec'), None]
-            # self._p file names 'None' signifies unnamed temp files.
+            # self._p file name 'None' signifies an unnamed temp file.
             self._p = p = [None, None]
             f[0].write(magic)
-            current=0
+            current = 0
 
         log("cache opened.  current = %s" % current)
 
-        self._limit=size/2
-        self._current=current
+        self._limit = size / 2
+        self._current = current
 
     def open(self):
         # XXX open is overloaded to perform two tasks for
@@ -224,16 +230,17 @@
         self._acquire()
         try:
             self._index=index={}
-            self._get=index.get
-            serial={}
-            f=self._f
-            current=self._current
+            self._get = index.get
+            serial = {}
+            f = self._f
+            current = self._current
             if f[not current] is not None:
                 read_index(index, serial, f[not current], not current)
-            self._pos=read_index(index, serial, f[current], current)
+            self._pos = read_index(index, serial, f[current], current)
 
             return serial.items()
-        finally: self._release()
+        finally:
+            self._release()
 
     def close(self):
         for f in self._f:
@@ -251,51 +258,60 @@
     def invalidate(self, oid, version):
         self._acquire()
         try:
-            p=self._get(oid, None)
-            if p is None: return None
-            f=self._f[p < 0]
-            ap=abs(p)
+            p = self._get(oid, None)
+            if p is None:
+                return None
+            f = self._f[p < 0]
+            ap = abs(p)
             f.seek(ap)
-            h=f.read(8)
-            if h != oid: return
-            f.seek(8,1) # Dang, we shouldn't have to do this. Bad Solaris & NT
+            h = f.read(8)
+            if h != oid:
+                return
+            f.seek(8, 1) # Dang, we shouldn't have to do this. Bad Solaris & NT
             if version:
                 f.write('n')
             else:
                 del self._index[oid]
                 f.write('i')
-        finally: self._release()
+        finally:
+            self._release()
 
     def load(self, oid, version):
         self._acquire()
         try:
-            p=self._get(oid, None)
-            if p is None: return None
-            f=self._f[p < 0]
-            ap=abs(p)
-            seek=f.seek
-            read=f.read
+            p = self._get(oid, None)
+            if p is None:
+                return None
+            f = self._f[p < 0]
+            ap = abs(p)
+            seek = f.seek
+            read = f.read
             seek(ap)
-            h=read(27)
+            h = read(27)
             if len(h)==27 and h[8] in 'nv' and h[:8]==oid:
                 tlen, vlen, dlen = unpack(">iHi", h[9:19])
-            else: tlen=-1
+            else:
+                tlen = -1
             if tlen <= 0 or vlen < 0 or dlen < 0 or vlen+dlen > tlen:
                 del self._index[oid]
                 return None
 
             if h[8]=='n':
-                if version: return None
+                if version:
+                    return None
                 if not dlen:
                     del self._index[oid]
                     return None
 
             if not vlen or not version:
-                if dlen: return read(dlen), h[19:]
-                else: return None
+                if dlen:
+                    return read(dlen), h[19:]
+                else:
+                    return None
 
-            if dlen: seek(dlen, 1)
-            v=read(vlen)
+            if dlen:
+                seek(dlen, 1)
+            v = read(vlen)
             if version != v:
                 if dlen:
                     seek(-dlen-vlen, 1)
@@ -303,24 +319,25 @@
                 else:
                     return None
 
-            dlen=unpack(">i", read(4))[0]
+            dlen = unpack(">i", read(4))[0]
             return read(dlen), read(8)
-        finally: self._release()
+        finally:
+            self._release()
 
     def update(self, oid, serial, version, data):
         self._acquire()
         try:
             if version:
                 # We need to find and include non-version data
-                p=self._get(oid, None)
+                p = self._get(oid, None)
                 if p is None:
                     return self._store(oid, '', '', version, data, serial)
-                f=self._f[p < 0]
-                ap=abs(p)
-                seek=f.seek
-                read=f.read
+                f = self._f[p < 0]
+                ap = abs(p)
+                seek = f.seek
+                read = f.read
                 seek(ap)
-                h=read(27)
+                h = read(27)
                 if len(h)==27 and h[8] in 'nv' and h[:8]==oid:
                     tlen, vlen, dlen = unpack(">iHi", h[9:19])
                 else:
@@ -330,8 +347,8 @@
                     return self._store(oid, '', '', version, data, serial)
 
                 if dlen:
-                    p=read(dlen)
-                    s=h[19:]
+                    p = read(dlen)
+                    s = h[19:]
                 else:
                     return self._store(oid, '', '', version, data, serial)
 
@@ -339,41 +356,47 @@
             else:
                 # Simple case, just store new data:
                 self._store(oid, data, serial, '', None, None)
-        finally: self._release()
+        finally:
+            self._release()
 
     def modifiedInVersion(self, oid):
         self._acquire()
         try:
-            p=self._get(oid, None)
-            if p is None: return None
-            f=self._f[p < 0]
-            ap=abs(p)
-            seek=f.seek
-            read=f.read
+            p = self._get(oid, None)
+            if p is None:
+                return None
+            f = self._f[p < 0]
+            ap = abs(p)
+            seek = f.seek
+            read = f.read
             seek(ap)
-            h=read(27)
+            h = read(27)
             if len(h)==27 and h[8] in 'nv' and h[:8]==oid:
                 tlen, vlen, dlen = unpack(">iHi", h[9:19])
-            else: tlen=-1
+            else:
+                tlen = -1
             if tlen <= 0 or vlen < 0 or dlen < 0 or vlen+dlen > tlen:
                 del self._index[oid]
                 return None
 
-            if h[8]=='n': return None
+            if h[8] == 'n':
+                return None
 
-            if not vlen: return ''
+            if not vlen:
+                return ''
             seek(dlen, 1)
             return read(vlen)
-        finally: self._release()
+        finally:
+            self._release()
 
     def checkSize(self, size):
         self._acquire()
         try:
             # Make sure we aren't going to exceed the target size.
             # If we are, then flip the cache.
-            if self._pos+size > self._limit:
-                current=not self._current
-                self._current=current
+            if self._pos + size > self._limit:
+                current = not self._current
+                self._current = current
                 if self._p[current] is not None:
                     # Persistent cache file:
                     # Note that due to permission madness, waaa,
@@ -381,15 +404,18 @@
                     # we open the new one. Waaaaaaaaaa.
                     if self._f[current] is not None:
                         self._f[current].close()
-                        try: os.remove(self._p[current])
-                        except: pass
-                    self._f[current]=open(self._p[current],'w+b')
+                        try:
+                            os.remove(self._p[current])
+                        except:
+                            pass
+                    self._f[current] = open(self._p[current],'w+b')
                 else:
                     # Temporary cache file:
                     self._f[current] = tempfile.TemporaryFile(suffix='.zec')
                 self._f[current].write(magic)
-                self._pos=pos=4
-        finally: self._release()
+                self._pos = pos = 4
+        finally:
+            self._release()
 
 
     def store(self, oid, p, s, version, pv, sv):
@@ -432,39 +458,46 @@
         self._pos += tlen
 
 def read_index(index, serial, f, current):
-    seek=f.seek
-    read=f.read
-    pos=4
-    seek(0,2)
-    size=f.tell()
+    seek = f.seek
+    read = f.read
+    pos = 4
+    seek(0, 2)
+    size = f.tell()
 
     while 1:
         f.seek(pos)
-        h=read(27)
+        h = read(27)
 
         if len(h)==27 and h[8] in 'vni':
             tlen, vlen, dlen = unpack(">iHi", h[9:19])
-        else: tlen=-1
-        if tlen <= 0 or vlen < 0 or dlen < 0 or vlen+dlen > tlen:
+        else:
+            tlen = -1
+        if tlen <= 0 or vlen < 0 or dlen < 0 or vlen + dlen > tlen:
             break
 
-        oid=h[:8]
+        oid = h[:8]
 
         if h[8]=='v' and vlen:
             seek(dlen+vlen, 1)
-            vdlen=read(4)
-            if len(vdlen) != 4: break
-            vdlen=unpack(">i", vdlen)[0]
-            if vlen+dlen+42+vdlen > tlen: break
+            vdlen = read(4)
+            if len(vdlen) != 4:
+                break
+            vdlen = unpack(">i", vdlen)[0]
+            if vlen+dlen+42+vdlen > tlen:
+                break
             seek(vdlen, 1)
-            vs=read(8)
-            if read(4) != h[9:13]: break
-        else: vs=None
+            vs = read(8)
+            if read(4) != h[9:13]:
+                break
+        else:
+            vs = None
 
         if h[8] in 'vn':
-            if current: index[oid]=-pos
-            else: index[oid]=pos
-            serial[oid]=h[-8:], vs
+            if current:
+                index[oid] = -pos
+            else:
+                index[oid] = pos
+            serial[oid] = h[-8:], vs
         else:
             if serial.has_key(oid):
                 # We have a record for this oid, but it was invalidated!
@@ -472,10 +505,12 @@
                 del index[oid]
 
 
-        pos=pos+tlen
+        pos = pos + tlen
 
     f.seek(pos)
-    try: f.truncate()
-    except: pass
+    try:
+        f.truncate()
+    except:
+        pass
 
     return pos