Added dummy diskimage to allow writing assembled files to local disk

This commit is contained in:
Rob McMullen 2018-07-17 18:47:40 -07:00
parent e4ce309bd3
commit dff073208f
2 changed files with 66 additions and 21 deletions

View File

@ -23,6 +23,7 @@ from .cartridge import A8CartHeader, AtariCartImage
from .parsers import SegmentParser, DefaultSegmentParser, guess_parser_for_mime, guess_parser_for_system, guess_container, iter_parsers, iter_known_segment_parsers, mime_parse_order, parsers_for_filename from .parsers import SegmentParser, DefaultSegmentParser, guess_parser_for_mime, guess_parser_for_system, guess_container, iter_parsers, iter_known_segment_parsers, mime_parse_order, parsers_for_filename
from .magic import guess_detail_for_mime from .magic import guess_detail_for_mime
from .utils import to_numpy, text_to_int from .utils import to_numpy, text_to_int
from .dummy import LocalFilesystem
def process(image, dirent, options): def process(image, dirent, options):
@ -54,27 +55,30 @@ def process(image, dirent, options):
def find_diskimage(filename): def find_diskimage(filename):
with open(filename, "rb") as fh: if filename == ".":
if options.verbose: parser = LocalFilesystem()
print("Loading file %s" % filename) else:
data = to_numpy(fh.read()) with open(filename, "rb") as fh:
parser = None if options.verbose:
container = guess_container(data, options.verbose) print("Loading file %s" % filename)
if container is not None: data = to_numpy(fh.read())
data = container.unpacked parser = None
rawdata = SegmentData(data) container = guess_container(data, options.verbose)
for mime in mime_parse_order: if container is not None:
if options.verbose: data = container.unpacked
print("Trying MIME type %s" % mime) rawdata = SegmentData(data)
parser = guess_parser_for_mime(mime, rawdata, options.verbose) for mime in mime_parse_order:
if parser is None: if options.verbose:
continue print("Trying MIME type %s" % mime)
if options.verbose: parser = guess_parser_for_mime(mime, rawdata, options.verbose)
print("Found parser %s" % parser.menu_name) if parser is None:
mime2 = guess_detail_for_mime(mime, rawdata, parser) continue
if mime != mime2 and options.verbose: if options.verbose:
print("Signature match: %s" % mime2) print("Found parser %s" % parser.menu_name)
break mime2 = guess_detail_for_mime(mime, rawdata, parser)
if mime != mime2 and options.verbose:
print("Signature match: %s" % mime2)
break
if parser is None: if parser is None:
raise errors.UnsupportedDiskImage("Unknown disk image type") raise errors.UnsupportedDiskImage("Unknown disk image type")
else: else:

41
atrcopy/dummy.py Normal file
View File

@ -0,0 +1,41 @@
import os
import numpy as np
from . import errors
from .segments import SegmentData, EmptySegment, ObjSegment
from .diskimages import DiskImageBase
from .utils import to_numpy
import logging
log = logging.getLogger(__name__)
class LocalFilesystemImage(DiskImageBase):
def __init__(self, path):
self.path = path
def __str__(self, path="."):
return f"Local filesystem output to: {self.path}"
def save(self, filename=""):
# This is to save the disk image containing the files on the disk image
# to the local disk, which doesn't make sense when the disk image is
# the filesystem.
pass
def find_dirent(self, name):
path = os.path.join(self.path, name)
if os.path.exists(path):
return True
raise errors.FileNotFound("%s not found on disk" % str(name))
def write_file(self, name, filetype, data):
path = os.path.join(self.path, name)
with open(path, "wb") as fh:
fh.write(data)
class LocalFilesystem():
def __init__(self, path="."):
self.image = LocalFilesystemImage(path)