Fixed unicode problems, allow usage with Python3, start process of using Dict for...
authorMichael Petch <mpetch@capp-sysware.com>
Mon, 30 Dec 2013 19:41:51 +0000 (12:41 -0700)
committerMichael Petch <mpetch@capp-sysware.com>
Mon, 30 Dec 2013 19:41:51 +0000 (12:41 -0700)
extractxgdata.py
xgimport.py
xgstruct.py
xgutils.py
xgzarc.py

index 125b2b0..dc2c987 100755 (executable)
@@ -27,7 +27,7 @@ import argparse
 import xgimport
 import xgzarc
 import xgstruct
-
+import pprint
 
 def parseoptsegments(parser, segments):
 
@@ -91,18 +91,16 @@ if __name__ == '__main__':
                             break
                         if isinstance(rec, xgstruct.HeaderMatchEntry):
                             fileversion = rec.version
-#                            print fileversion
                         elif isinstance(rec, xgstruct.UnimplementedEntry):
                             continue
-                        print rec
-
+                        pprint.pprint (rec.__dict__)
                 elif segment.type == xgimport.Import.Segment.XG_ROLLOUTS:
                     segment.fd.seek(os.SEEK_SET, 0)
                     while True:
                         rec = xgstruct.RolloutFileRecord().fromstream(segment.fd)
                         if rec is None:
                             break
-                        print rec
+                        pprint.pprint (rec.__dict__)
 
         except (xgimport.Error, xgzarc.Error) as e:
-            print e.value
+            print (e.value)
index f9b8b11..f0ae771 100644 (file)
@@ -89,10 +89,10 @@ class Import(object):
         with open(self.filename, "rb") as xginfile:
             # Extract the uncompressed Game Data Header (GDH)
             # Note: MS Windows Vista feature
-            unpacked_data = _struct.unpack('<4ciiQi', xginfile.read(24))
-            magicStr = ''.join(unpacked_data[0:4][::-1])
+            unpacked_data = _struct.unpack('<4BiiQi', xginfile.read(24))
+            magicStr = bytearray(unpacked_data[0:4][::-1]).decode('ascii')
             hdrVer = unpacked_data[4]
-            if magicStr <> 'HMGR' or hdrVer <> 1:
+            if magicStr != 'HMGR' or hdrVer != 1:
                 raise Error("Not a game data format file", self.filename)
 
             hdrSize = unpacked_data[5]
@@ -136,8 +136,8 @@ class Import(object):
                 # number to ensure it is valid
                 if xg_filetype == Import.Segment.XG_GAMEFILE:
                     segment_file.seek(Import.Segment.XG_GAMEHDR_LEN)
-                    magicStr = ''.join(segment_file.read(4))
-                    if magicStr <> 'DMLI':
+                    magicStr = bytearray(segment_file.read(4)).decode('ascii')
+                    if magicStr != 'DMLI':
                         raise Error("Not a valid XG gamefile", self.filename)
 
                 yield xg_filesegment
index a3aca49..3615402 100755 (executable)
@@ -27,49 +27,43 @@ import struct as _struct
 import os as _os
 
 
-class TimeSettingRecord(object):
+class TimeSettingRecord(dict):
 
     SIZEOFREC = 32
 
-    def __init__(self):
-        self.clocktype = 0
-        self.pergame = False
-        self.time1 = 0
-        self.time2 = 0
-        self.penalty = 0
-        self.timeleft1 = 0
-        self.timeleft2 = 0
-        self.penaltymoney = 0
-        pass
-
-    def __str__(self):
-        return str(self.todict())
-
-    def __repr__(self):
-        return str(self.todict())
+    def __init__(self, **kw):
+        defaults = {
+            'ClockType': 0,
+            'PerGame': False,
+            'Time1': 0,
+            'Time2': 0,
+            'Penalty': 0,
+            'TimeLeft1': 0,
+            'TimeLeft2': 0,
+            'PenaltyMoney': 0,            
+            }
+        super(TimeSettingRecord, self).__init__(defaults, **kw)
+
+    def __setattr__(self, key, value):
+        self[key] = value
+
+    def __getattr__(self, key):
+        return self[key]
 
     def fromstream(self, stream):
         unpacked_data = _struct.unpack(
             '<lBxxxllllll',
             stream.read(self.SIZEOFREC))
-        self.clocktype = unpacked_data[0]
-        self.pergame = bool(unpacked_data[1])
-        self.time1 = unpacked_data[2]
-        self.time2 = unpacked_data[3]
-        self.penalty = unpacked_data[4]
-        self.timeleft1 = unpacked_data[5]
-        self.timeleft2 = unpacked_data[6]
-        self.penaltymoney = unpacked_data[7]
-
+        self.ClockType = unpacked_data[0]
+        self.PerGame = bool(unpacked_data[1])
+        self.Time1 = unpacked_data[2]
+        self.Time2 = unpacked_data[3]
+        self.Penalty = unpacked_data[4]
+        self.TimeLeft1 = unpacked_data[5]
+        self.TimeLeft2 = unpacked_data[6]
+        self.PenaltyMoney = unpacked_data[7]
         return self
 
-    def todict(self):
-        return {'clocktype': self.clocktype, 'pergame': self.pergame,
-                'time1': self.time1, 'time2': self.time2,
-                'penalty': self.penalty, 'timeleft1': self.timeleft1,
-                'timeleft2': self.timeleft2,
-                'penaltymoney': self.penaltymoney}
-
 
 class EvalLevelRecord(object):
 
@@ -160,7 +154,7 @@ class EngineStructBestMoveRecord(object):
 
         self.eval = ()
         for row in range(32):
-            unpacked_data = _struct.unpack('7f', stream.read(28))
+            unpacked_data = _struct.unpack('<7f', stream.read(28))
             self.eval += (unpacked_data,)
 
         unpacked_data = _struct.unpack('<Bbbb', stream.read(4))
@@ -510,13 +504,13 @@ class MissingEntry(object):
                 'missingpoints': self.missingpoints}
  
 
-class FooterMatchEntry(object):
+class FooterMatchEntry(dict):
 
     SIZEOFREC = 2560
 
     def __init__(self):
-        self.name = 'MatchFooter'
-        self.entrytype = GameFileRecord.ENTRYTYPE_FOOTERMATCH
+        name = 'MatchFooter'
+        entrytype = GameFileRecord.ENTRYTYPE_FOOTERMATCH
         self.score1m = 0
         self.score2m = 0
         self.winnerm = 0
@@ -527,7 +521,7 @@ class FooterMatchEntry(object):
         self.datem = 0.0
 
     def __str__(self):
-        return str(self.todict())
+        return str(self.__dict__)
 
     def __repr__(self):
         return str(self)
@@ -964,6 +958,7 @@ class RolloutContextEntry(object):
                                        'flBBBxlBxxxxxxx37d37d37d37d37d37d37l' \
                                        'ff7f7fffl7fllllllBBxxffBxxxlBxHH',
                                        stream.read(2174))
+
         self.truncated = unpacked_data[0] # bool
         self.errorlimited = unpacked_data[1] # bool
         self.truncate = unpacked_data[2]
@@ -1079,8 +1074,9 @@ class RolloutFileRecord(object):
 
     def fromstream(self, stream):
         # If we are at EOF then return
-        if stream.read(1) == '':
+        if len(stream.read(1)) < 1:
             return None
+
         stream.seek(-1, _os.SEEK_CUR)
         startpos = stream.tell()
 
index 0de2eab..46884a3 100644 (file)
@@ -64,9 +64,9 @@ def utf16intarraytostr(intarray, encoding):
     for intval in intarray:
         if intval == 0:
             break
-        newstr += [unichr(intval).encode(encoding)]
+        newstr += [intval]
 
-    return ''.join(newstr)
+    return ''.join(bytearray(newstr).decode(encoding))
 
 
 def delphidatetimeconv(delphi_datetime):
index bf94caa..518bf7d 100644 (file)
--- a/xgzarc.py
+++ b/xgzarc.py
@@ -149,7 +149,7 @@ class ZlibArchive(object):
                     tmpfile.write(stream)
 
                     # Read until we have uncompressed a complete segment
-                    while decomp.unused_data == '':
+                    while len(decomp.unused_data) == 0:
                         block = self.stream.read(self.__MAXBUFSIZE)
                         if len(block) > 0:
                             try:
@@ -208,7 +208,7 @@ class ZlibArchive(object):
                 self.stream,
                 startpos=self.startofarcdata,
                 numbytes=(self.endofarcdata - self.startofarcdata))
-            if streamcrc <> self.arcrec.crc:
+            if streamcrc != self.arcrec.crc:
                 raise Error("Archive CRC check failed - file corrupt")
 
             # Decompress the index into a temporary file
@@ -238,7 +238,6 @@ class ZlibArchive(object):
     def getarchivefile(self, filerec):
         # Do processing on the temporary file
         self.stream.seek(filerec.start + self.startofarcdata)
-
         tmpfilename = self.__extractsegment(iscompressed=filerec.compressed,
                                             numbytes=filerec.csize)
         if tmpfilename is None:
@@ -247,10 +246,9 @@ class ZlibArchive(object):
 
         # Compute the CRC32 on the uncompressed file
         streamcrc = _xgutils.streamcrc32(tmpfile)
-        if streamcrc <> filerec.crc:
+        if streamcrc != filerec.crc:
             raise Error("File CRC check failed - file corrupt")
 
-        tmpfile.flush()
         return tmpfile, tmpfilename
 
     def setblocksize(self, blksize):