diff --git a/src/greaseweazle/image/a2r.py b/src/greaseweazle/image/a2r.py index 5a9fdfc0..4fccf8b6 100644 --- a/src/greaseweazle/image/a2r.py +++ b/src/greaseweazle/image/a2r.py @@ -74,9 +74,10 @@ class A2R(Image): read_only = True - def __init__(self) -> None: + def __init__(self, name: str) -> None: self.to_track: Dict[Tuple[int,int], A2RTrack] = dict() - + self.filename = name + def process_rwcp(self, dat: bytes) -> None: @@ -117,7 +118,7 @@ def from_file(cls, name: str, _fmt) -> Image: 'A2R: Invalid signature') dat = dat[8:] - a2r = cls() + a2r = cls(name) # Extract the RWCP chunk(s). while len(dat) > 8: diff --git a/src/greaseweazle/image/caps.py b/src/greaseweazle/image/caps.py index 1c470cb5..09687383 100644 --- a/src/greaseweazle/image/caps.py +++ b/src/greaseweazle/image/caps.py @@ -165,7 +165,8 @@ class CAPS(Image): read_only = True - def __init__(self) -> None: + def __init__(self, name: str) -> None: + self.filename = name self.lib = get_libcaps() def __del__(self) -> None: @@ -186,7 +187,7 @@ def get_track(self, cyl: int, head: int) -> Optional[MasterTrack]: @classmethod def from_file(cls, name: str, _fmt) -> Image: - caps = cls() + caps = cls(name) errprefix = f'CAPS: {cls.imagetype}' caps.iid = caps.lib.CAPSAddImage() diff --git a/src/greaseweazle/image/edsk.py b/src/greaseweazle/image/edsk.py index a95bbc05..783857d5 100644 --- a/src/greaseweazle/image/edsk.py +++ b/src/greaseweazle/image/edsk.py @@ -162,9 +162,10 @@ def verify_track(self, flux): class EDSK(Image): - def __init__(self) -> None: + def __init__(self, name: str) -> None: self.to_track: Dict[Tuple[int,int], Union[ibm.IBMTrack,EDSKTrack]] = dict() + self.filename = name # Find all weak ranges in the given sector data copies. @staticmethod @@ -292,7 +293,7 @@ def from_file(cls, name: str, _fmt) -> Image: with open(name, "rb") as f: dat = f.read() - edsk = cls() + edsk = cls(name) sig, creator, ncyls, nsides, track_sz = struct.unpack( '<34s14s2BH', dat[:52]) @@ -353,7 +354,7 @@ def from_file(cls, name: str, _fmt) -> Image: num_copies = (3 if data_size == 49152 else data_size // native_size) data_size //= num_copies - weak = cls().find_weak_ranges(sec_data, data_size) + weak = EDSK.find_weak_ranges(sec_data, data_size) sec_data = sec_data[:data_size] sectors.append((c,h,r,n,errs,sec_data)) # IDAM @@ -434,9 +435,9 @@ def from_file(cls, name: str, _fmt) -> Image: ngap3 += 1 # Special track handlers - special_track = cls()._build_8k_track(sectors) + special_track = EDSK._build_8k_track(sectors) if special_track is None: - special_track = cls()._build_kbi19_track(sectors) + special_track = EDSK._build_kbi19_track(sectors) if special_track is not None: track = special_track break diff --git a/src/greaseweazle/image/hfe.py b/src/greaseweazle/image/hfe.py index 48946cc9..d53b3a16 100644 --- a/src/greaseweazle/image/hfe.py +++ b/src/greaseweazle/image/hfe.py @@ -160,8 +160,9 @@ class HFE(Image): opts: HFEOpts - def __init__(self) -> None: + def __init__(self, name: str) -> None: self.opts = HFEOpts() + self.filename = name # Each track is (bitlen, rawbytes). # rawbytes is a bytes() object in little-endian bit order. self.to_track: Dict[Tuple[int,int], HFETrack] = dict() @@ -184,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() + hfe = cls(name) hfe.opts.bitrate = bitrate hfe.opts.version = version diff --git a/src/greaseweazle/image/image.py b/src/greaseweazle/image/image.py index eef7a4bf..3dfe55c7 100644 --- a/src/greaseweazle/image/image.py +++ b/src/greaseweazle/image/image.py @@ -35,6 +35,8 @@ def w_set(self, filename: str, opt: str, val: str) -> None: class Image: + filename: str + noclobber = False default_format: Optional[str] = None read_only = False write_on_ctrl_c = False @@ -42,7 +44,7 @@ class Image: ## Context manager for image objects created using .to_file() - def __enter__(self): + def __enter__(self) -> Image: self.file = open(self.filename, ('wb','xb')[self.noclobber]) return self @@ -65,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() - obj.filename = name + obj = cls(name) obj.fmt = fmt obj.noclobber = noclobber return obj diff --git a/src/greaseweazle/image/imd.py b/src/greaseweazle/image/imd.py index b470f5ca..00d40a96 100644 --- a/src/greaseweazle/image/imd.py +++ b/src/greaseweazle/image/imd.py @@ -24,10 +24,9 @@ class IMDMode: class IMD(Image): - def __init__(self, name: str, noclobber=False): + def __init__(self, name: str): self.to_track: Dict[Tuple[int,int],ibm.IBMTrack_Fixed] = dict() self.filename = name - self.noclobber = noclobber @classmethod @@ -116,7 +115,9 @@ def from_file(cls, name: str, _fmt) -> Image: @classmethod def to_file(cls, name: str, fmt, noclobber: bool) -> Image: - return cls(name, noclobber=noclobber) + obj = cls(name) + obj.noclobber = noclobber + return obj def get_track(self, cyl: int, side: int) -> Optional[ibm.IBMTrack_Fixed]: diff --git a/src/greaseweazle/image/img.py b/src/greaseweazle/image/img.py index b1beba3a..2f8e96e5 100644 --- a/src/greaseweazle/image/img.py +++ b/src/greaseweazle/image/img.py @@ -18,13 +18,12 @@ class IMG(Image): sequential = False min_cyls: Optional[int] = None - def __init__(self, name: str, fmt, noclobber=False): + def __init__(self, name: str, fmt): self.to_track: Dict[Tuple[int,int],codec.Codec] = dict() error.check(fmt is not None, """\ Sector image requires a disk format to be specified""") self.filename = name self.fmt: codec.DiskDef = fmt - self.noclobber = noclobber def track_list(self): @@ -65,7 +64,9 @@ def from_file(cls, name: str, fmt: Optional[codec.DiskDef]) -> Image: 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__)) - return cls(name, fmt, noclobber=noclobber) + obj = cls(name, fmt) + obj.noclobber = noclobber + return obj def get_track(self, cyl: int, side: int) -> Optional[codec.Codec]: diff --git a/src/greaseweazle/image/kryoflux.py b/src/greaseweazle/image/kryoflux.py index b1664e24..6a3635b9 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): + def __init__(self, name: str) -> None: m = re.search(r'\d{2}\.[01]\.raw$', name, flags=re.IGNORECASE) error.check( m is not None, @@ -41,7 +41,9 @@ def __init__(self, name): Bad Kryoflux image name pattern '%s' Name pattern must be path/to/nameNN.N.raw (N is a digit)''' % name) + assert m is not None # mypy self.basename = name[:m.start()] + self.filename = name @classmethod diff --git a/src/greaseweazle/image/msa.py b/src/greaseweazle/image/msa.py index 988f0f69..e28477df 100644 --- a/src/greaseweazle/image/msa.py +++ b/src/greaseweazle/image/msa.py @@ -15,11 +15,10 @@ class MSA(Image): - def __init__(self, name: str, fmt=None, noclobber=False): + def __init__(self, name: str, fmt=None): self.to_track: Dict[Tuple[int,int],ibm.IBMTrack_Fixed] = dict() self.filename = name self.fmt = fmt - self.noclobber = noclobber @classmethod @@ -92,7 +91,9 @@ def from_file(cls, name: str, _fmt) -> Image: @classmethod def to_file(cls, name: str, fmt, noclobber: bool) -> Image: - return cls(name, fmt, noclobber) + obj = cls(name, fmt) + obj.noclobber = noclobber + return obj def get_track(self, cyl: int, side: int) -> Optional[ibm.IBMTrack_Fixed]: diff --git a/src/greaseweazle/image/scp.py b/src/greaseweazle/image/scp.py index 64140962..3c524aec 100644 --- a/src/greaseweazle/image/scp.py +++ b/src/greaseweazle/image/scp.py @@ -111,11 +111,12 @@ class SCP(Image): opts: SCPOpts - def __init__(self) -> None: + def __init__(self, name: str) -> 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]: @@ -180,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() + scp = cls(name) for trknr in range(len(trk_offs)): diff --git a/src/greaseweazle/image/td0.py b/src/greaseweazle/image/td0.py index aca86ce2..8ff3a546 100644 --- a/src/greaseweazle/image/td0.py +++ b/src/greaseweazle/image/td0.py @@ -22,8 +22,9 @@ class TD0(Image): read_only = True - def __init__(self) -> None: + def __init__(self, name: str) -> None: self.to_track: Dict[Tuple[int,int],ibm.IBMTrack_Fixed] = dict() + self.filename = name @classmethod @@ -32,7 +33,7 @@ def from_file(cls, name: str, _fmt) -> Image: with open(name, "rb") as f: dat = f.read() - td0 = cls() + td0 = cls(name) # Check and strip the header sig, td_ver, data_rate, stepping, n_sides, crc = struct.unpack(