Skip to content

Commit

Permalink
image: Clean up setting of Image.filename and Image.noclobber
Browse files Browse the repository at this point in the history
  • Loading branch information
keirf committed Oct 4, 2023
1 parent 0ebe619 commit 5b5a255
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 29 deletions.
7 changes: 4 additions & 3 deletions src/greaseweazle/image/a2r.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

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

Expand Down
7 changes: 4 additions & 3 deletions src/greaseweazle/image/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ 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
opts = ImageOpts() # empty

## 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

Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/greaseweazle/image/imd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]:
Expand Down
7 changes: 4 additions & 3 deletions src/greaseweazle/image/img.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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]:
Expand Down
4 changes: 3 additions & 1 deletion src/greaseweazle/image/kryoflux.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@ 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,
'''\
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
Expand Down
7 changes: 4 additions & 3 deletions src/greaseweazle/image/msa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]:
Expand Down
5 changes: 3 additions & 2 deletions src/greaseweazle/image/scp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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)):

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

0 comments on commit 5b5a255

Please sign in to comment.