Skip to content

Commit

Permalink
fix: correctly reload custom kaitai KSY after it is edited
Browse files Browse the repository at this point in the history
the custom ksy pollutes hobbit's internal python modules and won't reload if the same module name is there. this fix is a stopgap until the python script runs are capable of detecting and cleaning any imported modules.
  • Loading branch information
hello-adam authored Aug 24, 2021
2 parents 10426de + cc54677 commit e675f67
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/hobbits-plugins/analyzers/KaitaiStruct/scripts/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ def process_value(value, section):
else:
value = f"0x{binascii.hexlify(value).decode()}"
section['value'] = value
section['type'] = f"bytes[{len(value)}]"

elif isinstance(value, str):
if len(value) > 15:
value = f"{value[:12]}..."
section['value'] = value
section['type'] = f"str[{len(value)}]"

elif isinstance(value, float):
section['value'] = value
Expand Down Expand Up @@ -121,6 +123,7 @@ def parse_struct(struct, sections, prefix="", parent_offset = 0, base_io=None, b
idx_offset = struct._debug[name]['arr'][idx]['start'] + base_offset

if isinstance(value_item, KaitaiStruct):
section['type'] = f"{type(value_item).__name__}[{len(value)}]"
sections.append(idx_section)
parse_struct(value_item, sections, idx_label, idx_offset, base_io, base_offset)
else:
Expand All @@ -131,6 +134,7 @@ def parse_struct(struct, sections, prefix="", parent_offset = 0, base_io=None, b
"parent": idx_section["parent"],
}
if process_value(value_item, value_item_section):
section['type'] = f"{value_item_section.get('type', 'array')}[{len(value)}]"
sections.append(value_item_section)

elif process_value(value, section):
Expand All @@ -151,6 +155,10 @@ def parse_data(input_filename, output_filename, action_progress):
sys.path.append(os.path.dirname(scripts[0]))
package_name = os.path.splitext(module_file)[0]
class_name = package_name.capitalize()
try:
del sys.modules[package_name]
except KeyError:
pass
struct_module = importlib.__import__(package_name, fromlist=[class_name])
Struct = getattr(struct_module, class_name)

Expand Down

0 comments on commit e675f67

Please sign in to comment.