I want to modify quicktime files, so I'm working with quicktime.py but it only parses information. It doesn't know how to write/modify things.
In C, struct records are actually very powerful - you get 4 things for the cost of 1 readable definition:
- Define names for each variable.
- Define serializable types and order for each variable (let's ignore machine specific shenanigans for this discussion).
- Pack (write)
- Unpack ('parse')
In python, the struct
module can do numbers 2-4 but you need to do extra work to make python define names for both packing and unpacking based on 1 definition (DRY).
OTOH ctypes
is able to do 1-4 (3-4 aren't exactly in the stdlib but they're easier to patch in using this) and ctypes supports nesting.
I understand that if more complex parsing/serializing is needed, specific code will be written. But still it seems to me that I should be able to explain to python what a file looks like and it could do the rest (pack/unpack). Problem is that ctypes is advertised as a "foreign function library" so it's not "supposed" to do this. Another issue is ctypes probably won't work well with a HUGE file where you just want to seek to wherever and change a few bits, though I haven't tested this.
Here's the question: what's the DRY way to read and modify binary formats in python?