Minor code cleanup
authorMichael Petch <mpetch@capp-sysware.com>
Fri, 10 Jan 2014 04:59:06 +0000 (21:59 -0700)
committerMichael Petch <mpetch@capp-sysware.com>
Fri, 10 Jan 2014 04:59:06 +0000 (21:59 -0700)
xgimport.py
xgstruct.py
xgzarc.py

index c8dbeea..96321b5 100644 (file)
@@ -95,7 +95,7 @@ class Import(object):
             if gdfheader is None:
                 raise Error("Not a game data format file", self.filename)
             
-            # Extract the Game Format Header
+            # Extract the Game Format Header to a temporary file
             with Import.Segment(type=Import.Segment.GDF_HDR) as segment:
                 xginfile.seek(0)
                 block = xginfile.read(gdfheader.HeaderSize)
@@ -103,10 +103,10 @@ class Import(object):
                 segment.file.flush()
                 yield segment
 
-            # Extract the uncompressed thumbnail JPEG from the GDH
+            # Extract the uncompressed thumbnail JPEG from the GDF hdr
             if (gdfheader.ThumbnailSize > 0):
                 with Import.Segment(type=Import.Segment.GDF_IMAGE) as segment:
-                    xginfile.seek(gdfheader.ThumbnailOffset, 1)
+                    xginfile.seek(gdfheader.ThumbnailOffset, _os.SEEK_CUR)
                     imgbuf = xginfile.read(gdfheader.ThumbnailSize)
                     segment.file.write(imgbuf)
                     segment.file.flush()
@@ -128,8 +128,8 @@ class Import(object):
                 xg_filesegment.filename = seg_filename
                 xg_filesegment.fd = segment_file
 
-                # If we are looking at the game info file then check the magic
-                # number to ensure it is valid
+                # If we are looking at the game info file then check 
+                # the magic number to ensure it is valid
                 if xg_filetype == Import.Segment.XG_GAMEFILE:
                     segment_file.seek(Import.Segment.XG_GAMEHDR_LEN)
                     magicStr = bytearray(segment_file.read(4)).decode('ascii')
index 9b52f73..43c8b94 100644 (file)
@@ -41,7 +41,7 @@ class GameDataFormatHdrRecord(dict):
             'HeaderSize': 0,              # size of the header
             'ThumbnailOffset': 0,         # location of the thumbnail (jpg)
             'ThumbnailSize': 0,           # size in bye of the thumbnail
-            'GameGUID': [],               # game id (GUID)
+            'GameGUID': None,             # game id (GUID)
             'GameName': None,             # Unicode game name
             'SaveName': None,             # Unicode save name
             'LevelName': None,            # Unicode level name
@@ -58,8 +58,8 @@ class GameDataFormatHdrRecord(dict):
     def fromstream(self, stream):
         try:
             unpacked_data = \
-                _struct.unpack('<4BiiQiLHHBB6s1024H1024H1024H1024H', 
-                stream.read(self.SIZEOFREC))
+                    _struct.unpack('<4BiiQiLHHBB6s1024H1024H1024H1024H', 
+                    stream.read(self.SIZEOFREC))
         except:
             return None
 
@@ -71,11 +71,13 @@ class GameDataFormatHdrRecord(dict):
         self.HeaderSize = unpacked_data[5]
         self.ThumbnailOffset = unpacked_data[6]
         self.ThumbnailSize = unpacked_data[7]
-        guidp1,guidp2,guidp3,guidp4,guidp5 = unpacked_data[8:13]
+    
+        # Convert Delphi 4 component GUID to the 6 components 
+        # of a Python GUID.
+        guidp1, guidp2, guidp3, guidp4, guidp5 = unpacked_data[8:13]
         guidp6 = int(_binascii.b2a_hex(unpacked_data[13]), 16)
-        self.GameGUID = str(_uuid.UUID(fields=(guidp1,guidp2,guidp3,
-                            guidp4,guidp5,guidp6)))
+        self.GameGUID = str(_uuid.UUID(fields=(guidp1, guidp2, guidp3,
+                            guidp4, guidp5, guidp6)))
 
         self.GameName = _xgutils.utf16intarraytostr(unpacked_data[14:1038])
         self.SaveName = _xgutils.utf16intarraytostr(unpacked_data[1038:2062])
@@ -439,7 +441,7 @@ class FooterGameEntry(dict):
             'Winner': 0,                    # who win +1=player1, -1 player 2
             'PointsWon': 0,                 # point scored
             'Termination': 0,               # 0=Drop 1=single 2=gammon 3=Backgamon 
-                                            # (0,1,2) +100=Resign  (0,1,2)+1000 settle
+                                            # (0,1,2)+100=Resign  (0,1,2)+1000 settle
             'ErrResign': 0.0,               # error made by resigning (-1000 if not analyze)
             'ErrTakeResign': 0.0,           # error made by accepting the resign (-1000 if not analyze)
             'Eval': None,                   # evaluation of the final position
index 3ddf8c3..780c4a4 100644 (file)
--- a/xgzarc.py
+++ b/xgzarc.py
@@ -195,27 +195,26 @@ class ZlibArchive(object):
             filerecords = []
             curstreampos = self.stream.tell()
 
-            self.stream.seek(-ArchiveRecord.SIZEOFREC, 2)
+            self.stream.seek(-ArchiveRecord.SIZEOFREC, _os.SEEK_END)
             self.endofarcdata = self.stream.tell()
             self.arcrec.fromstream(self.stream)
-            # print self.arcrec.todict()
 
             # Position ourselves at the beginning of the archive file index
             self.stream.seek(-ArchiveRecord.SIZEOFREC -
-                             self.arcrec.registrysize, 2)
+                             self.arcrec.registrysize, _os.SEEK_END)
             self.startofarcdata = self.stream.tell() - self.arcrec.archivesize
 
             # Compute the CRC32 of all the archive data including file index
             streamcrc = _xgutils.streamcrc32(
-                self.stream,
-                startpos=self.startofarcdata,
-                numbytes=(self.endofarcdata - self.startofarcdata))
+                    self.stream,
+                    startpos=self.startofarcdata,
+                    numbytes=(self.endofarcdata - self.startofarcdata))
             if streamcrc != self.arcrec.crc:
                 raise Error("Archive CRC check failed - file corrupt")
 
             # Decompress the index into a temporary file
             idx_filename = self.__extractsegment(
-                iscompressed=self.arcrec.compressedregistry)
+                    iscompressed=self.arcrec.compressedregistry)
             if idx_filename is None:
                 raise Error("Error extracting archive index")