Skip to content

Commit

Permalink
Fix and simplify reading and seeking in olecf/thumbs
Browse files Browse the repository at this point in the history
pylinting to fix various bugs and code smells
  • Loading branch information
MosesofEgypt committed Jan 30, 2025
1 parent 3759292 commit d4d91e8
Show file tree
Hide file tree
Showing 22 changed files with 364 additions and 548 deletions.
4 changes: 1 addition & 3 deletions supyr_struct/blocks/array_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ def __setitem__(self, index, new_value):
# a Block, set its parent attribute to this Block.
if isinstance(new_value, Block):
new_value.parent = self

desc = object.__getattribute__(self, 'desc')
elif isinstance(index, slice):
# if this is an array, dont worry about
# the descriptor since its list indexes
Expand Down Expand Up @@ -773,7 +771,7 @@ class PArrayBlock(ArrayBlock):
node it describes to be stored as well as a
reference to whatever Block it is parented to
'''
__slots__ = ('STEPTREE')
__slots__ = ('STEPTREE', )

def __init__(self, desc, parent=None, steptree=None,
init_attrs=None, **kwargs):
Expand Down
23 changes: 10 additions & 13 deletions supyr_struct/blocks/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,7 @@ def __sizeof__(self, seenset=None):
return 0

seenset.add(id(self))
bytes_total = object.__sizeof__(self)

desc = object.__getattribute__(self, 'desc')

return bytes_total
return object.__sizeof__(self)

def __binsize__(self, node, substruct=False):
'''You must override this method'''
Expand Down Expand Up @@ -312,16 +308,19 @@ def get_desc(self, desc_key, attr_name=None):
"descriptor of '%s'.") %
(desc_key, desc.get('NAME')))

def get_root(node):
def get_root(self):
'''Navigates up the given node and returns the root node.'''
# rather than name the function argument 'self' it's slightly
# faster to just name it 'root' and not have to do 'root = self'
try:
while node.parent:
node = node.parent
while self.parent:
self = self.parent # pylint: disable=W0642 # SHUTUP PYLINT
# for the sake of minimal operations and
# keeping the method signature clean, we
# will be evil and redefine self
except AttributeError:
pass
return node
return self

def get_neighbor(self, path, node=None):
'''
Expand Down Expand Up @@ -680,12 +679,10 @@ def serialize(self, **kwargs):
else:
parent_tag = self.get_root()

if "calc_pointers" in kwargs:
calc_pointers = kwargs["calc_pointers"]
calc_pointers = kwargs.get("calc_pointers", True)
if isinstance(parent_tag, supyr_struct.tag.Tag):
calc_pointers = parent_tag.calc_pointers
else:
calc_pointers = True
parent_tag = None

# convert string attr_indexes to ints
Expand Down Expand Up @@ -820,7 +817,7 @@ def parent(self, new_val):
except TypeError:
# some object types don't support __weakref__ so we have to
# wrap them in something that our getter will still work with
new_val = lambda val=new_val: val
new_val = lambda val=new_val: val # pylint: disable=C3001

# we just need to set self._parent to the new wrapped value.
# we want to do this as fast as possible, so we're going to
Expand Down
12 changes: 5 additions & 7 deletions supyr_struct/blocks/data_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,12 @@ def __sizeof__(self, seenset=None):

seenset.add(id(self))
data = self.data
if isinstance(data, Block):
bytes_total = object.__sizeof__(self) + data.__sizeof__(seenset)
else:
bytes_total = object.__sizeof__(self) + getsizeof(data)

desc = object.__getattribute__(self, 'desc')

return bytes_total
return object.__sizeof__(self) + (
data.__sizeof__(seenset)
if isinstance(data, Block) else
getsizeof(data)
)

def __copy__(self):
'''
Expand Down
6 changes: 1 addition & 5 deletions supyr_struct/blocks/list_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,6 @@ def __sizeof__(self, seenset=None):
seenset.add(id(self))
bytes_total = list.__sizeof__(self)

desc = object.__getattribute__(self, 'desc')

for i in range(len(self)):
item = list.__getitem__(self, i)
if not id(item) in seenset:
Expand Down Expand Up @@ -873,7 +871,7 @@ class PListBlock(ListBlock):
node it describes to be stored as well as a
reference to whatever Block it is parented to.
'''
__slots__ = ('STEPTREE')
__slots__ = ('STEPTREE', )

def __init__(self, desc, parent=None, steptree=None,
init_attrs=None, **kwargs):
Expand Down Expand Up @@ -925,8 +923,6 @@ def __sizeof__(self, seenset=None):
seenset.add(id(steptree))
bytes_total += getsizeof(steptree)

desc = object.__getattribute__(self, 'desc')

for i in range(len(self)):
item = list.__getitem__(self, i)
if not id(item) in seenset:
Expand Down
2 changes: 0 additions & 2 deletions supyr_struct/blocks/union_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,6 @@ def __sizeof__(self, seenset=None):
seenset.add(id(self))
bytes_total = object.__sizeof__(self) + getsizeof(self.u_node)

desc = object.__getattribute__(self, 'desc')

return bytes_total

def __binsize__(self, node, substruct=False):
Expand Down
5 changes: 4 additions & 1 deletion supyr_struct/blocks/void_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def __init__(self, desc=None, parent=None, **kwargs):
object.__setattr__(self, "desc", desc)
self.parent = parent

def set_size(self, new_value, attr_index=None, **context):
pass

def __copy__(self):
'''
Creates a copy of this Block which references
Expand Down Expand Up @@ -125,4 +128,4 @@ def get_size(self, attr_index=None, **context):

def parse(self, **kwargs):
'''VoidBlocks have nothing to parse. Does nothing.'''
pass
pass # pylint: disable=W0107 # removing pass here would look horrible
22 changes: 9 additions & 13 deletions supyr_struct/blocks/while_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ def extend(self, new_attrs, **kwargs):
elif isinstance(new_attrs, int):
# if this Block is an array and "new_attr" is an int it means
# that we are supposed to append this many of the SUB_STRUCT
for i in range(new_attrs):
self.append(**kwargs)
[self.append(**kwargs) for _ in range(new_attrs)]
else:
raise TypeError("Argument type for 'extend' must be an " +
"instance of ListBlock or int, not %s" %
Expand Down Expand Up @@ -264,24 +263,21 @@ def set_size(self, new_value=None, attr_index=None, **context):
self_desc.get('NAME', UNNAMED),
f_type, f_type.size))

# if a new size wasnt provided then it needs to be calculated
newsize = desc['TYPE'].sizecalc(
node, parent=self, attr_index=attr_index, **context
) if new_value is None else new_value

if isinstance(size, int):
# Because literal descriptor sizes are supposed to be static
# (unless you're changing the structure), we don't even try to
# change the size if the new size is less than the current one.
if new_value is None and newsize <= size:
if newsize <= size:
return
raise DescEditError("Changing a size statically defined in a " +
"descriptor is not supported through " +
"set_size. Make a new descriptor instead.")

# if a new size wasnt provided then it needs to be calculated
if new_value is not None:
newsize = new_value
else:
newsize = desc['TYPE'].sizecalc(node, parent=self,
attr_index=attr_index, **context)

if isinstance(size, str):
elif isinstance(size, str):
# set size by traversing the tag structure
# along the path specified by the string
self.set_neighbor(size, newsize, node)
Expand Down Expand Up @@ -463,7 +459,7 @@ class PWhileBlock(WhileBlock):
See supyr_struct.blocks.while_block.WhileBlock.__doc__ for more help.
'''
__slots__ = ('STEPTREE')
__slots__ = ('STEPTREE', )

__init__ = PArrayBlock.__init__

Expand Down
15 changes: 6 additions & 9 deletions supyr_struct/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __exit__(self, except_type, except_value, traceback):
if self._close_rawdata:
self._rawdata.close()
except AttributeError:
return
pass


def get_rawdata(**kwargs):
Expand Down Expand Up @@ -121,7 +121,7 @@ class Buffer():
def __init__(self, *args):
# Dummy __init__ that makes sure there is always a self._pos.
# Accepts args like *args to account for child objects.
self._pos = 0
self._pos = 0 # pylint: disable=E0237

def read(self, count=None):
'''
Expand Down Expand Up @@ -194,14 +194,11 @@ def peek(self, count=None, offset=None):
Reads and returns 'count' number of bytes without
changing the current read/write pointer position.
'''
if offset is None:
pos = self._pos
else:
pos = offset
pos = self._pos if offset is None else offset
try:
if pos + count < len(self):
return self[pos:pos + count]
return self[pos:pos + len(self)]
len_self = len(self)
peek_end = pos + count
return self[pos: len_self if peek_end >= len_self else peek_end]
except TypeError:
pass

Expand Down
2 changes: 1 addition & 1 deletion supyr_struct/defs/audio/wav.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def read_write_id3_data_size(
# are ignored, so a 257 bytes long tag is represented as $00 00 02 01.
if writebuffer is not None:
buffer.write(bytes(
(size >> (7 * (3 - i))) & 0x7F
(val >> (7 * (3 - i))) & 0x7F
for i, val in enumerate([parent.frame_data_size] * 4)
))
else:
Expand Down
14 changes: 8 additions & 6 deletions supyr_struct/defs/block_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,6 @@ def make_desc(self, *desc_entries, **desc):
for key in tuple(desc.keys()):
if key not in desc_keywords:
del desc[key]
continue
elif isinstance(desc[key], BlockDef):
# if the entry in desc is a BlockDef, it
# needs to be replaced with its descriptor.
Expand All @@ -505,8 +504,6 @@ def make_subdefs(self, replace_subdefs=False):
Converts all the entries in self.subdefs into BlockDefs and
tries to make BlockDefs for all the entries in the descriptor.
'''
desc = self.descriptor

sub_kwargs = {'align_mode': self.align_mode, 'endian': self.endian}

# make sure all the subdefs are BlockDefs
Expand All @@ -515,10 +512,11 @@ def make_subdefs(self, replace_subdefs=False):
if not isinstance(d, BlockDef):
self.subdefs[i] = BlockDef(str(i), descriptor=d, **sub_kwargs)

# DO NOT REMOVE THE RETURN!!!!!
# DO NOT UNCOMMENT!!!!!
# The below code was causing a 300% memory bloat and making library
# startup take much longer. Only enable if a solution is found.
return
'''
desc = self.descriptor
# try to make all descriptors in this Blockdef into their own BlockDefs
for i in desc:
Expand All @@ -533,6 +531,7 @@ def make_subdefs(self, replace_subdefs=False):
**sub_kwargs)
except Exception:
pass
'''

def sanitize(self, desc=None):
'''
Expand Down Expand Up @@ -662,7 +661,10 @@ def set_entry_count(self, src_dict, key=None):
int_count += 1
src_dict[ENTRIES] = int_count

def str_to_name(self, string, reserved_names=reserved_desc_names, **kwargs):
def str_to_name(self, string, reserved_names=None, **kwargs):
if reserved_names is None:
reserved_names = reserved_desc_names

e_str = ""
try:
if not isinstance(string, str):
Expand Down
Loading

0 comments on commit d4d91e8

Please sign in to comment.