Add a utils.HexDump library

Be a bit more robust about dealing with data corruption
This commit is contained in:
kris 2017-04-16 22:56:04 +01:00
parent b97c404514
commit b4ea155b24
2 changed files with 29 additions and 7 deletions

View File

@ -1,9 +1,7 @@
import applesoft
import bitstring
import disk as disklib
import string
PRINTABLE = set(string.letters + string.digits + string.punctuation + ' ')
import utils
class FileType(object):
def __init__(self, short_type, long_type, parser=None):
@ -227,7 +225,11 @@ class Dos33Disk(disklib.Disk):
#print "XXX found a sparse sector?"
continue
(t, s) = ts
try:
fds = FileDataSector.fromSector(self.ReadSector(t, s), entry.FileName())
except disklib.IOError, e:
print "%s: Failed to read File data sector: %s" % (entry, e)
continue
contents.append(fds.data)
return File(entry, contents)
@ -276,8 +278,11 @@ class File(object):
self.catalog_entry = catalog_entry
self.contents = contents
self.parsed_contents = None
parser = catalog_entry.file_type.parser
if parser:
try:
self.parsed_contents = parser(contents)
else:
self.parsed_contents = None
except Exception:
print "Failed to parse file %s" % self.catalog_entry
print utils.HexDump(contents.tobytes())

17
src/apple2disk/utils.py Normal file
View File

@ -0,0 +1,17 @@
import string
PRINTABLE = set(string.letters + string.digits + string.punctuation + ' ')
def HexDump(data):
line = []
for idx, b in enumerate(data):
if idx % 8 == 0:
print '$%02x: ' % idx,
print "%02x" % ord(b),
if b in PRINTABLE:
line.append(b)
else:
line.append('.')
if (idx + 1) % 8 == 0:
print " %s" % ''.join(line)
line = []