Skip to content

Commit

Permalink
image: Adjustments to allow all subclasses to use Image.to_file()
Browse files Browse the repository at this point in the history
  • Loading branch information
keirf committed Oct 4, 2023
1 parent 5b5a255 commit 43f95c0
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/greaseweazle/image/a2r.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/greaseweazle/image/caps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/greaseweazle/image/d88.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/greaseweazle/image/edsk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down
4 changes: 2 additions & 2 deletions src/greaseweazle/image/hfe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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

Expand Down
3 changes: 1 addition & 2 deletions src/greaseweazle/image/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 2 additions & 9 deletions src/greaseweazle/image/imd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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])
Expand Down Expand Up @@ -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
Expand Down
9 changes: 0 additions & 9 deletions src/greaseweazle/image/img.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 2 additions & 8 deletions src/greaseweazle/image/kryoflux.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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):
Expand Down
7 changes: 0 additions & 7 deletions src/greaseweazle/image/msa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/greaseweazle/image/scp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)):

Expand Down
4 changes: 2 additions & 2 deletions src/greaseweazle/image/td0.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(
Expand Down

0 comments on commit 43f95c0

Please sign in to comment.