midstar / pycstruct

A python library for reading and writing binary data similar to what is done in C language structs
MIT License
24 stars 4 forks source link

Descriptive struct #21

Open vallsv opened 3 years ago

vallsv commented 3 years ago

When i saw your library, i was searching something to define cstrucs as descriptive way.

With the last python typing annotation there is probably way to provide something like that:

class Foo(pycstruct.Struct):
    foo: pycstruct.uint32
    foo2: pycstruct.uint16
    foo3: pycstruct.array[pycstruct.float64, 32]

...or...

class Foo:
    foo: typing.Annotated[int, pycstruct.type("uint32")]
    foo2: typing.Annotated[int, pycstruct.type("uint16")]
    foo3: typing.Annotated[float, pycstruct.type("float64, length=32")]

...or...

class Foo:
    @pycstruct.tpye("uint32")
    foo: int
    @pycstruct.tpye("uint16")
    foo2: int
    @pycstruct.tpye("uint16", length=32)
    foo3: float

No idea if that stuff could work or not.

I would be very happy to use such thing. Do you think it could interest you?

midstar commented 3 years ago

Hi,

Thank you for your suggestion. It would probably be possible to implement any of these alternatives in a separate file (like annotations.py). You are welcome with a suggestion in a pull request.

However, it seems that most users are utilizing the cparser and standard C header files (or strings) to define there structs, bitfields etc.

/Joel

vallsv commented 3 years ago

Yes you are right. But i was thinking about getting ride of castxml for pure python lib. Just because it it's extra dependency which is more difficult to install.

There is probably an opportunity to provide a tool to convert .h file to a pure python file using your lib. As result any project could remove the castxml dependency easily if needed.

I will maybe play around if i have some time.

midstar commented 3 years ago

It would be great if pycstruct had a .h parser that did not depend on castxml.

castxml is extremly powerful and builds on a real compiler which supports the complete standard with all complex corner cases. Thus it would not be feasible to implement such a powerful parser.

However, it should be possible to implement the basic cases for enum, struct/bitfield and union. But it gets quite complex when the source includes typedefs, macros, includes etc.