From b4ea155b24f417ecb7706356f2a80332a8b1b41d Mon Sep 17 00:00:00 2001 From: kris Date: Sun, 16 Apr 2017 22:56:04 +0100 Subject: [PATCH] Add a utils.HexDump library Be a bit more robust about dealing with data corruption --- src/apple2disk/dos33disk.py | 19 ++++++++++++------- src/apple2disk/utils.py | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 src/apple2disk/utils.py diff --git a/src/apple2disk/dos33disk.py b/src/apple2disk/dos33disk.py index 0014b05..26d05ac 100644 --- a/src/apple2disk/dos33disk.py +++ b/src/apple2disk/dos33disk.py @@ -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 - fds = FileDataSector.fromSector(self.ReadSector(t, s), entry.FileName()) + 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: - self.parsed_contents = parser(contents) - else: - self.parsed_contents = None \ No newline at end of file + try: + self.parsed_contents = parser(contents) + except Exception: + print "Failed to parse file %s" % self.catalog_entry + print utils.HexDump(contents.tobytes()) \ No newline at end of file diff --git a/src/apple2disk/utils.py b/src/apple2disk/utils.py new file mode 100644 index 0000000..f50c801 --- /dev/null +++ b/src/apple2disk/utils.py @@ -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 = []