diff --git a/src/greaseweazle/image/dcp.py b/src/greaseweazle/image/dcp.py index 00f87095..6c77a190 100644 --- a/src/greaseweazle/image/dcp.py +++ b/src/greaseweazle/image/dcp.py @@ -1,4 +1,4 @@ -# greaseweazle/image/dim.py +# greaseweazle/image/dcp.py # # Written & released by Keir Fraser # @@ -8,24 +8,27 @@ import struct from greaseweazle import error -from greaseweazle.image.img import IMG +from greaseweazle.image.img import IMG_AutoFormat from greaseweazle.codec import codec from .image import Image -class DCP(IMG): +class DCP(IMG_AutoFormat): read_only = True + @staticmethod + def format_from_file(name: str) -> codec.DiskDef: + fmt = codec.get_diskdef('pc98.2hd') + assert fmt is not None # mypy + return fmt + def from_bytes(self, dat: bytes) -> None: header = dat[:162] pos = 162 - format_str = 'pc98.2hd' - fmt = codec.get_diskdef(format_str) - assert fmt is not None # mypy + fmt = self.fmt - for t in self.track_list(): - cyl, head = t.cyl, t.head + for cyl, head in self.track_list(): if self.sides_swapped: head ^= 1 track = fmt.mk_track(cyl, head) @@ -38,7 +41,6 @@ def from_bytes(self, dat: bytes) -> None: self.to_track[cyl,head] = track elif header[cyl * 2 + head] != 0: raise error.Fatal("DCP: Corrupt header.") - self.format_str = format_str # Local variables: # python-indent: 4 diff --git a/src/greaseweazle/image/dim.py b/src/greaseweazle/image/dim.py index ad637246..5b11b764 100644 --- a/src/greaseweazle/image/dim.py +++ b/src/greaseweazle/image/dim.py @@ -8,17 +8,20 @@ import struct from greaseweazle import error -from greaseweazle.image.img import IMG +from greaseweazle.image.img import IMG_AutoFormat from greaseweazle.codec import codec from .image import Image -class DIM(IMG): - default_format = None +class DIM(IMG_AutoFormat): + read_only = True - def from_bytes(self, dat: bytes) -> None: + @staticmethod + def format_from_file(name: str) -> codec.DiskDef: + + with open(name, "rb") as f: + header = f.read(256) - header = dat[:256] error.check(header[0xAB:0xB8] == b"DIFC HEADER ", "DIM: Not a DIM file.") media_byte, = struct.unpack('B255x', header) @@ -28,10 +31,16 @@ def from_bytes(self, dat: bytes) -> None: format_str = 'pc98.2hs' else: raise error.Fatal("DIM: Unsupported format.") + fmt = codec.get_diskdef(format_str) assert fmt is not None # mypy + return fmt + + def from_bytes(self, dat: bytes) -> None: pos = 256 + fmt = self.fmt + for t in fmt.tracks: cyl, head = t.cyl, t.head if self.sides_swapped: @@ -40,7 +49,6 @@ def from_bytes(self, dat: bytes) -> None: if track is not None: pos += track.set_img_track(dat[pos:]) self.to_track[cyl,head] = track - self.format_str = format_str # Local variables: # python-indent: 4 diff --git a/src/greaseweazle/image/img.py b/src/greaseweazle/image/img.py index 622de239..071cfc62 100644 --- a/src/greaseweazle/image/img.py +++ b/src/greaseweazle/image/img.py @@ -10,7 +10,7 @@ from greaseweazle import error from greaseweazle.codec import codec from greaseweazle.flux import HasFlux -from .image import Image +from .image import Image, OptDict class IMG(Image): @@ -91,6 +91,20 @@ def get_image(self) -> bytes: return tdat +class IMG_AutoFormat(IMG): + + @staticmethod + def format_from_file(name: str) -> codec.DiskDef: + raise NotImplementedError + + @classmethod + def from_file(cls, name: str, fmt: Optional[codec.DiskDef], + opts: OptDict) -> Image: + error.check(fmt is None, + f'{cls.__name__}: Format cannot be overridden') + fmt = cls.format_from_file(name) + return super().from_file(name, fmt, opts) + # Local variables: # python-indent: 4 # End: diff --git a/src/greaseweazle/tools/write.py b/src/greaseweazle/tools/write.py index ad6f8a27..d917b677 100644 --- a/src/greaseweazle/tools/write.py +++ b/src/greaseweazle/tools/write.py @@ -235,10 +235,6 @@ def main(argv) -> None: print("Writing " + str(args.tracks)) if args.precomp is not None: print(args.precomp) - if hasattr(image, 'format_str'): - print("Image format " + image.format_str) - error.check(args.format is None, - 'Cannot override image format with --format') if args.format: print("Format " + args.format) try: