mirror of
https://github.com/robmcmullen/atrcopy.git
synced 2025-01-15 11:29:53 +00:00
Refs #1: added gzip container as reference
This commit is contained in:
parent
664963cb58
commit
714493b597
@ -58,13 +58,16 @@ def find_diskimage(filename):
|
|||||||
with open(filename, "rb") as fh:
|
with open(filename, "rb") as fh:
|
||||||
if options.verbose:
|
if options.verbose:
|
||||||
print("Loading file %s" % filename)
|
print("Loading file %s" % filename)
|
||||||
rawdata = SegmentData(fh.read())
|
data = to_numpy(fh.read())
|
||||||
parser = None
|
parser = None
|
||||||
try:
|
try:
|
||||||
unpacked = guess_container(rawdata.data)
|
container = guess_container(data, options.verbose)
|
||||||
|
if container is not None:
|
||||||
|
data = container.unpacked
|
||||||
except errors.UnsupportedContainer as e:
|
except errors.UnsupportedContainer as e:
|
||||||
print(f"{filename}: {e}")
|
print(f"{filename}: {e}")
|
||||||
return None
|
return None
|
||||||
|
rawdata = SegmentData(data)
|
||||||
for mime in mime_parse_order:
|
for mime in mime_parse_order:
|
||||||
if options.verbose:
|
if options.verbose:
|
||||||
print("Trying MIME type %s" % mime)
|
print("Trying MIME type %s" % mime)
|
||||||
|
@ -1,12 +1,32 @@
|
|||||||
|
import gzip
|
||||||
|
import io
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
from . import errors
|
from . import errors
|
||||||
from .segments import SegmentData
|
from .segments import SegmentData
|
||||||
|
from .utils import to_numpy
|
||||||
|
|
||||||
|
|
||||||
class DiskImageContainer:
|
class DiskImageContainer:
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
self.unpacked = self.unpack_raw_data(data)
|
self.unpacked = self.__unpack_raw_data(data)
|
||||||
|
|
||||||
def unpack_raw_data(self, data):
|
def __unpack_raw_data(self, data):
|
||||||
|
raw = data.tobytes()
|
||||||
|
unpacked = self.unpack_bytes(raw)
|
||||||
|
return to_numpy(unpacked)
|
||||||
|
|
||||||
|
def unpack_bytes(self, byte_data):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class GZipContainer(DiskImageContainer):
|
||||||
|
def unpack_bytes(self, byte_data):
|
||||||
|
try:
|
||||||
|
buf = io.BytesIO(byte_data)
|
||||||
|
with gzip.GzipFile(mode='rb', fileobj=buf) as f:
|
||||||
|
unpacked = f.read()
|
||||||
|
except OSError as e:
|
||||||
|
raise errors.InvalidContainer(e)
|
||||||
|
return unpacked
|
||||||
|
@ -21,7 +21,7 @@ class DCMContainer(DiskImageContainer):
|
|||||||
self.index += 1
|
self.index += 1
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def unpack_raw_data(self, data):
|
def unpack_bytes(self, data):
|
||||||
self.index = 0
|
self.index = 0
|
||||||
self.count = len(data)
|
self.count = len(data)
|
||||||
self.raw = data
|
self.raw = data
|
||||||
|
@ -10,6 +10,7 @@ from .dos33 import Dos33DiskImage, ProdosDiskImage, Dos33BinFile
|
|||||||
from .standard_delivery import StandardDeliveryImage
|
from .standard_delivery import StandardDeliveryImage
|
||||||
from . import errors
|
from . import errors
|
||||||
from .magic import guess_detail_for_mime
|
from .magic import guess_detail_for_mime
|
||||||
|
from .container import GZipContainer
|
||||||
from .dcm import DCMContainer
|
from .dcm import DCMContainer
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
@ -159,17 +160,22 @@ class ProdosSegmentParser(SegmentParser):
|
|||||||
|
|
||||||
|
|
||||||
known_containers = [
|
known_containers = [
|
||||||
|
GZipContainer,
|
||||||
DCMContainer,
|
DCMContainer,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def guess_container(r):
|
def guess_container(r, verbose=False):
|
||||||
for c in known_containers:
|
for c in known_containers:
|
||||||
|
if verbose:
|
||||||
|
log.info(f"trying container {c}")
|
||||||
try:
|
try:
|
||||||
found = c(r)
|
found = c(r)
|
||||||
except errors.InvalidContainer:
|
except errors.InvalidContainer as e:
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
|
if verbose:
|
||||||
|
log.info(f"found container {c}")
|
||||||
return found
|
return found
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user