eerimoq / bincopy

Mangling of various file formats that conveys binary information (Motorola S-Record, Intel HEX, TI-TXT, Verilog VMEM, ELF and binary files).
MIT License
109 stars 38 forks source link

Promote _Segments, _Segment, _Chunk to public API #35

Closed bessman closed 11 months ago

bessman commented 11 months ago

It would be nice to be able to use these types in annotations:

write_data(data: bincopy.Chunk) -> None:
    ...

instead of having to write:

write_data(data: bincopy._Segment._Chunk) -> None:
    ...

which is both harder on the eyes and makes various linters complain about accessing protected members.

eerimoq commented 11 months ago

Good suggestion (probably). Part of version 18.0.0.

eerimoq commented 11 months ago

In case this breaks peoples code, I could add aliases like _Segments = Segments. I think I'll do that actually.

bessman commented 11 months ago

Great! Here's another, related suggestion: Segment.chunks should yield smaller Segments instead of Chunks.

My use case is that I need to split a Segment into chunks for writing to a target device. Then I need to split those chunks into smaller chunks in order to calculate a checksum of the larger chunk. But Chunk doesn't have a .chunks method, so that doesn't work :(

Instead, I created a new Segment and chunked that:

Segment(chunk.address, chunk.address + len(chunk.data), chunk.data, 1).chunks()

But is there any reason chunks can't just yield Segments to begin with?

eerimoq commented 11 months ago

Not that I can think of, but was some time I used bincopy so I might have forgotten. If you have time you could submit a PR with the change and I can have a closer look (and maybe you encounter some problem).

bessman commented 11 months ago

Segment._word_size_bytes should have a public getter. It is useful to be able to get chunks of a certain number of bytes, for example when calculating a checksum. Since .chunks wants the size and alignment in words, it is necessary to know the word size to get a certain number of bytes per chunk. The Segment knows it internally, but it's only public on the BinFile, which may not be available in a subroutine.

eerimoq commented 11 months ago

Feel free to make it public on Segment as well.