Skip to content

Commit

Permalink
preparing to tackle Issue #23
Browse files Browse the repository at this point in the history
  • Loading branch information
snake-biscuits committed May 19, 2022
1 parent 91d2230 commit 33d2a4d
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions bsp_tool/lumps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ class RawBspLump:
offset: int # position in file where lump begins
_changes: Dict[int, bytes]
# ^ {index: new_byte}
# NOTE: bytes objects don't support item assignment
# -- but we want to allow light hex editting with slice overrides
_length: int # number of indexable entries

def __init__(self, file: io.BufferedReader, lump_header: Any):
Expand Down Expand Up @@ -130,17 +132,14 @@ def __iadd__(self, other_bytes: bytes):
self._length += len(other_bytes)
self[start:] = other_bytes

def __setitem__(self, index: Union[int, slice], value: Any):
def __setitem__(self, index: slice, value: Any):
"""remapping slices is allowed, but only slices"""
if not isinstance(index, slice):
raise TypeError(f"list indices must be integers or slices, not {type(index)}")
_slice = _remap_slice(index, self._length)
# TODO: allow slice assignment to act like insert/extend
if isinstance(index, int):
index = _remap_negative_index(index, self._length)
for i, v in zip(range(_slice.start, _slice.stop, _slice.step), value):
self._changes[index] = value
elif isinstance(index, slice):
_slice = _remap_slice(index, self._length)
for i, v in zip(range(_slice.start, _slice.stop, _slice.step), value):
self[i] = v
else:
raise TypeError(f"list indices must be integers or slices, not {type(index)}")

def __iter__(self):
return iter([self[i] for i in range(self._length)])
Expand Down

0 comments on commit 33d2a4d

Please sign in to comment.