From 43f95c0da9b41391acefe366a1b413d680c4da38 Mon Sep 17 00:00:00 2001 From: Keir Fraser Date: Wed, 4 Oct 2023 15:41:04 +0100 Subject: [PATCH] image: Adjustments to allow all subclasses to use Image.to_file() --- src/greaseweazle/image/a2r.py | 4 ++-- src/greaseweazle/image/caps.py | 4 ++-- src/greaseweazle/image/d88.py | 4 ++-- src/greaseweazle/image/edsk.py | 4 ++-- src/greaseweazle/image/hfe.py | 4 ++-- src/greaseweazle/image/image.py | 3 +-- src/greaseweazle/image/imd.py | 11 ++--------- src/greaseweazle/image/img.py | 9 --------- src/greaseweazle/image/kryoflux.py | 10 ++-------- src/greaseweazle/image/msa.py | 7 ------- src/greaseweazle/image/scp.py | 6 +++--- src/greaseweazle/image/td0.py | 4 ++-- 12 files changed, 20 insertions(+), 50 deletions(-) diff --git a/src/greaseweazle/image/a2r.py b/src/greaseweazle/image/a2r.py index 4fccf8b6..d42ed9ee 100644 --- a/src/greaseweazle/image/a2r.py +++ b/src/greaseweazle/image/a2r.py @@ -74,7 +74,7 @@ class A2R(Image): read_only = True - def __init__(self, name: str) -> None: + def __init__(self, name: str, _fmt) -> None: self.to_track: Dict[Tuple[int,int], A2RTrack] = dict() self.filename = name @@ -118,7 +118,7 @@ def from_file(cls, name: str, _fmt) -> Image: 'A2R: Invalid signature') dat = dat[8:] - a2r = cls(name) + a2r = cls(name, _fmt) # Extract the RWCP chunk(s). while len(dat) > 8: diff --git a/src/greaseweazle/image/caps.py b/src/greaseweazle/image/caps.py index 09687383..2ac7b228 100644 --- a/src/greaseweazle/image/caps.py +++ b/src/greaseweazle/image/caps.py @@ -165,7 +165,7 @@ class CAPS(Image): read_only = True - def __init__(self, name: str) -> None: + def __init__(self, name: str, _fmt) -> None: self.filename = name self.lib = get_libcaps() @@ -187,7 +187,7 @@ def get_track(self, cyl: int, head: int) -> Optional[MasterTrack]: @classmethod def from_file(cls, name: str, _fmt) -> Image: - caps = cls(name) + caps = cls(name, _fmt) errprefix = f'CAPS: {cls.imagetype}' caps.iid = caps.lib.CAPSAddImage() diff --git a/src/greaseweazle/image/d88.py b/src/greaseweazle/image/d88.py index 4fe71597..e30fe966 100644 --- a/src/greaseweazle/image/d88.py +++ b/src/greaseweazle/image/d88.py @@ -43,7 +43,7 @@ class D88(Image): read_only = True - def __init__(self, name: str): + def __init__(self, name: str, _fmt): self.opts = D88Opts() self.to_track: List[TrackDict] = list() self.filename = name @@ -171,7 +171,7 @@ def disk_from_file(f, disk_offset: int) -> TrackDict: @classmethod def from_file(cls, name: str, _fmt) -> Image: - d88 = cls(name) + d88 = cls(name, _fmt) with open(name, "rb") as f: f.seek(0, os.SEEK_END) diff --git a/src/greaseweazle/image/edsk.py b/src/greaseweazle/image/edsk.py index 783857d5..4f0b5762 100644 --- a/src/greaseweazle/image/edsk.py +++ b/src/greaseweazle/image/edsk.py @@ -162,7 +162,7 @@ def verify_track(self, flux): class EDSK(Image): - def __init__(self, name: str) -> None: + def __init__(self, name: str, _fmt) -> None: self.to_track: Dict[Tuple[int,int], Union[ibm.IBMTrack,EDSKTrack]] = dict() self.filename = name @@ -293,7 +293,7 @@ def from_file(cls, name: str, _fmt) -> Image: with open(name, "rb") as f: dat = f.read() - edsk = cls(name) + edsk = cls(name, _fmt) sig, creator, ncyls, nsides, track_sz = struct.unpack( '<34s14s2BH', dat[:52]) diff --git a/src/greaseweazle/image/hfe.py b/src/greaseweazle/image/hfe.py index d53b3a16..cb4e9f1d 100644 --- a/src/greaseweazle/image/hfe.py +++ b/src/greaseweazle/image/hfe.py @@ -160,7 +160,7 @@ class HFE(Image): opts: HFEOpts - def __init__(self, name: str) -> None: + def __init__(self, name: str, _fmt) -> None: self.opts = HFEOpts() self.filename = name # Each track is (bitlen, rawbytes). @@ -185,7 +185,7 @@ def from_file(cls, name: str, _fmt): error.check(0 < n_cyl, "HFE: Invalid #cyls") error.check(0 < n_side < 3, "HFE: Invalid #sides") - hfe = cls(name) + hfe = cls(name, _fmt) hfe.opts.bitrate = bitrate hfe.opts.version = version diff --git a/src/greaseweazle/image/image.py b/src/greaseweazle/image/image.py index 3dfe55c7..768b9100 100644 --- a/src/greaseweazle/image/image.py +++ b/src/greaseweazle/image/image.py @@ -67,8 +67,7 @@ def __exit__(self, type, value, tb): def to_file(cls, name, fmt, noclobber): error.check(not cls.read_only, "%s: Cannot create %s image files" % (name, cls.__name__)) - obj = cls(name) - obj.fmt = fmt + obj = cls(name, fmt) obj.noclobber = noclobber return obj diff --git a/src/greaseweazle/image/imd.py b/src/greaseweazle/image/imd.py index 00d40a96..95dd5d62 100644 --- a/src/greaseweazle/image/imd.py +++ b/src/greaseweazle/image/imd.py @@ -24,7 +24,7 @@ class IMDMode: class IMD(Image): - def __init__(self, name: str): + def __init__(self, name: str, _fmt): self.to_track: Dict[Tuple[int,int],ibm.IBMTrack_Fixed] = dict() self.filename = name @@ -35,7 +35,7 @@ def from_file(cls, name: str, _fmt) -> Image: with open(name, "rb") as f: dat = f.read() - imd = cls(name) + imd = cls(name, _fmt) # Check and strip the header sig, = struct.unpack('4s', dat[:4]) @@ -113,13 +113,6 @@ def from_file(cls, name: str, _fmt) -> Image: return imd - @classmethod - def to_file(cls, name: str, fmt, noclobber: bool) -> Image: - obj = cls(name) - obj.noclobber = noclobber - return obj - - def get_track(self, cyl: int, side: int) -> Optional[ibm.IBMTrack_Fixed]: if (cyl,side) not in self.to_track: return None diff --git a/src/greaseweazle/image/img.py b/src/greaseweazle/image/img.py index 2f8e96e5..211d9cc6 100644 --- a/src/greaseweazle/image/img.py +++ b/src/greaseweazle/image/img.py @@ -60,15 +60,6 @@ def from_file(cls, name: str, fmt: Optional[codec.DiskDef]) -> Image: return img - @classmethod - def to_file(cls, name: str, fmt, noclobber: bool) -> Image: - error.check(not cls.read_only, - "%s: Cannot create %s image files" % (name, cls.__name__)) - obj = cls(name, fmt) - obj.noclobber = noclobber - return obj - - def get_track(self, cyl: int, side: int) -> Optional[codec.Codec]: if (cyl,side) not in self.to_track: return None diff --git a/src/greaseweazle/image/kryoflux.py b/src/greaseweazle/image/kryoflux.py index 6a3635b9..38c8f86b 100644 --- a/src/greaseweazle/image/kryoflux.py +++ b/src/greaseweazle/image/kryoflux.py @@ -33,7 +33,7 @@ class OOB: class KryoFlux(Image): - def __init__(self, name: str) -> None: + def __init__(self, name: str, _fmt) -> None: m = re.search(r'\d{2}\.[01]\.raw$', name, flags=re.IGNORECASE) error.check( m is not None, @@ -46,18 +46,12 @@ def __init__(self, name: str) -> None: self.filename = name - @classmethod - def to_file(cls, name, fmt, noclobber): - kf = cls(name) - kf.noclobber = noclobber - return kf - @classmethod def from_file(cls, name, _fmt): # Check that the specified raw file actually exists. with open(name, 'rb') as _: pass - return cls(name) + return cls(name, _fmt) def get_track(self, cyl, side): diff --git a/src/greaseweazle/image/msa.py b/src/greaseweazle/image/msa.py index e28477df..0ebd90fd 100644 --- a/src/greaseweazle/image/msa.py +++ b/src/greaseweazle/image/msa.py @@ -89,13 +89,6 @@ def from_file(cls, name: str, _fmt) -> Image: return msa - @classmethod - def to_file(cls, name: str, fmt, noclobber: bool) -> Image: - obj = cls(name, fmt) - obj.noclobber = noclobber - return obj - - def get_track(self, cyl: int, side: int) -> Optional[ibm.IBMTrack_Fixed]: if (cyl,side) not in self.to_track: return None diff --git a/src/greaseweazle/image/scp.py b/src/greaseweazle/image/scp.py index 3c524aec..9ac28f5b 100644 --- a/src/greaseweazle/image/scp.py +++ b/src/greaseweazle/image/scp.py @@ -111,14 +111,14 @@ class SCP(Image): opts: SCPOpts - def __init__(self, name: str) -> None: + def __init__(self, name: str, _fmt) -> None: self.opts = SCPOpts() self.nr_revs: Optional[int] = None self.to_track: Dict[int, SCPTrack] = dict() self.index_cued = True self.filename = name - + def side_count(self) -> List[int]: s = [0,0] # non-empty tracks on each side for tnr in self.to_track: @@ -181,7 +181,7 @@ def from_file(cls, name: str, _fmt) -> Image: splices = struct.unpack('<168I', dat[pos+4:pos+169*4]) pos += chk_len - scp = cls(name) + scp = cls(name, _fmt) for trknr in range(len(trk_offs)): diff --git a/src/greaseweazle/image/td0.py b/src/greaseweazle/image/td0.py index 8ff3a546..b8894562 100644 --- a/src/greaseweazle/image/td0.py +++ b/src/greaseweazle/image/td0.py @@ -22,7 +22,7 @@ class TD0(Image): read_only = True - def __init__(self, name: str) -> None: + def __init__(self, name: str, _fmt) -> None: self.to_track: Dict[Tuple[int,int],ibm.IBMTrack_Fixed] = dict() self.filename = name @@ -33,7 +33,7 @@ def from_file(cls, name: str, _fmt) -> Image: with open(name, "rb") as f: dat = f.read() - td0 = cls(name) + td0 = cls(name, _fmt) # Check and strip the header sig, td_ver, data_rate, stepping, n_sides, crc = struct.unpack(