view zipfile266.diff @ 76:0e5ae28e0b2b

Points are now weighted on a test context basis In particular, this has allowed for simple extensions to the format of testconf to award points to whole test groups without at the same time compromising the future ability of giving partial score for correct but slow solutions. Specifically, the groupweight configuration variable has been added and normally has the format {groupindex: points} where groupindex is the group's index in the tests configuration variable. The backwards incompatible change is that test contexts are no longer guaranteed to learn the score awarded or the maximum possible score for every test case and may instead be notified about them in batches. In other news, the pointmap and groupweight configuration variables can (now) be given as sequences in addition to mappings. (Technically, the distinction currently made is dict versus everything else.) Items of a sequence pointmap/groupweight correspond directly to the test cases/ groups defined in the tests configuration variable; in particular, when groups are used, tests=[1],[2,3];pointmap={1:1,2:2,3:3} can now be written as pointmap=tests=[1],[2,3]. Missing items are handled in the same way in which they are handled when the variable is a mapping. Note that the items of groupweight correspond to whole test groups rather than individual test cases. In other news again, the wording of problem total lines has been changed from '<unweighted> points; weighted score: <weighted>' to '<weighted> points (<unweighted> before weighting)', and group total lines now properly report fractional numbers of points (this is a bug fix).
author Oleg Oshmyan <chortos@inbox.lv>
date Sat, 08 Jan 2011 16:03:35 +0200
parents 4ea7133ac25c
children
line wrap: on
line source

--- /usr/lib/python2.6/zipfile.py	2010-07-05 14:48:38.000000000 +0300
+++ zipfile.py	2010-11-25 01:39:22.749743303 +0200
@@ -1,6 +1,7 @@
 """
 Read and write ZIP files.
 """
+# Improved by Chortos-2 in 2009 and 2010 (added bzip2 support)
 import struct, os, time, sys, shutil
 import binascii, cStringIO, stat
 
@@ -11,8 +12,13 @@
     zlib = None
     crc32 = binascii.crc32
 
+try:
+    import bz2 # We may need its compression method
+except ImportError:
+    bz2 = None
+
 __all__ = ["BadZipfile", "error", "ZIP_STORED", "ZIP_DEFLATED", "is_zipfile",
-           "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile" ]
+           "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile", "ZIP_BZIP2" ]
 
 class BadZipfile(Exception):
     pass
@@ -33,6 +39,7 @@
 # constants for Zip file compression methods
 ZIP_STORED = 0
 ZIP_DEFLATED = 8
+ZIP_BZIP2 = 12
 # Other ZIP compression methods not supported
 
 # Below are some formats and associated data for reading/writing headers using
@@ -467,6 +474,9 @@
         self.compreadsize = 64*1024
         if self.compress_type == ZIP_DEFLATED:
             self.dc = zlib.decompressobj(-15)
+        elif self.compress_type == ZIP_BZIP2:
+            self.dc = bz2.BZ2Decompressor()
+            self.compreadsize = 900000
 
     def set_univ_newlines(self, univ_newlines):
         self.univ_newlines = univ_newlines
@@ -578,7 +588,7 @@
             if self.compress_type == ZIP_STORED:
                 lr = len(self.readbuffer)
                 bytesToRead = min(bytesToRead, size - lr)
-            elif self.compress_type == ZIP_DEFLATED:
+            else:
                 if len(self.readbuffer) > size:
                     # the user has requested fewer bytes than we've already
                     # pulled through the decompressor; don't read any more
@@ -608,14 +618,17 @@
                     newdata = ''.join(map(self.decrypter, newdata))
 
                 # decompress newly read data if necessary
-                if newdata and self.compress_type == ZIP_DEFLATED:
+                if newdata and self.compress_type != ZIP_STORED:
                     newdata = self.dc.decompress(newdata)
-                    self.rawbuffer = self.dc.unconsumed_tail
+                    self.rawbuffer = self.dc.unconsumed_tail if self.compress_type == ZIP_DEFLATED else ''
                     if self.eof and len(self.rawbuffer) == 0:
                         # we're out of raw bytes (both from the file and
                         # the local buffer); flush just to make sure the
                         # decompressor is done
-                        newdata += self.dc.flush()
+                        try:
+                            newdata += self.dc.flush()
+                        except AttributeError:
+                            pass
                         # prevent decompressor from being used again
                         self.dc = None
 
@@ -641,7 +654,8 @@
     file: Either the path to the file, or a file-like object.
           If it is a path, the file will be opened and closed by ZipFile.
     mode: The mode can be either read "r", write "w" or append "a".
-    compression: ZIP_STORED (no compression) or ZIP_DEFLATED (requires zlib).
+    compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib),
+                 or ZIP_BZIP2 (requires bz2).
     allowZip64: if True ZipFile will create files with ZIP64 extensions when
                 needed, otherwise it will raise an exception when this would
                 be necessary.
@@ -661,6 +675,10 @@
             if not zlib:
                 raise RuntimeError,\
                       "Compression requires the (missing) zlib module"
+        elif compression == ZIP_BZIP2:
+            if not bz2:
+                raise RuntimeError,\
+                      "Compression requires the (missing) bz2 module"
         else:
             raise RuntimeError, "That compression method is not supported"
 
@@ -987,7 +1005,10 @@
         if zinfo.compress_type == ZIP_DEFLATED and not zlib:
             raise RuntimeError, \
                   "Compression requires the (missing) zlib module"
-        if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED):
+        if zinfo.compress_type == ZIP_BZIP2 and not bz2:
+            raise RuntimeError, \
+                  "Compression requires the (missing) bz2 module"
+        if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED, ZIP_BZIP2):
             raise RuntimeError, \
                   "That compression method is not supported"
         if zinfo.file_size > ZIP64_LIMIT:
@@ -1048,6 +1069,8 @@
         if zinfo.compress_type == ZIP_DEFLATED:
             cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
                  zlib.DEFLATED, -15)
+        elif zinfo.compress_type == ZIP_BZIP2:
+            cmpr = bz2.BZ2Compressor()
         else:
             cmpr = None
         while 1:
@@ -1105,6 +1128,10 @@
                  zlib.DEFLATED, -15)
             bytes = co.compress(bytes) + co.flush()
             zinfo.compress_size = len(bytes)    # Compressed size
+        elif zinfo.compress_type == ZIP_BZIP2:
+            co = bz2.BZ2Compressor()
+            bytes = co.compress(bytes) + co.flush()
+            zinfo.compress_size = len(bytes)    # Compressed size
         else:
             zinfo.compress_size = zinfo.file_size
         zinfo.header_offset = self.fp.tell()    # Start of header bytes