FmtRW

PicRW is dead, long live FmtRW!

What?

(please forgive the following unstructured madness...)

This Python package lets you define a binary file-format, and then read and write data in this format. It is a spin-off from PicRW below.

Why?

I wanted a fairly easy way to read and write JPEG and TIFF files, and specially their meta-info, like photographer, caption and such. While there are packages for reading this I couldnt find any that could write. I also wanted to learn Python. And then I got greedy, I wanted more...

How?

I didnt want to define yet another language, so everything is in Python. Using small building blocks of objects you define the format in a recursive hierarchial way. You can easily define new classes if you lack any. You can also insert small snippets of code that will be executed while reading or writing data.

As a programmer I love acronyms (ACR) so many things in the code are a three letter name (TLN). How else than producing new exotic acronyms (NEA) can a computer-consultatnt keep his job (KHJ)?

Format

A format is an object from a subclass to the class Fmt. The easiest way to get a format object is to call a constructor like Const({},{'val':'\0'}), which would then match the string '\0'. The first parameter will most certainly be {} for the moment, and the second parameter is a dictionary whith the objects attributes.

Once you have an object like this you can call its read function which if succesful will fill its val attribute reading data from a file, or, if its val attribute is properly filled in, you can call its write function and have its data written to a file.

Currently we have the following types of fmt's to play whith:

FmtAttributeUseDefaultDumped
FmtA superclass
supthe super-fmt this fmt belongs toNone
nththe index this fmt has in its super-fmts val-array, or -1-1Y
depthe depth for this fmt in the fmt-tree0Y
posthe position in the file for this fmt0Y
ardaction to be executed before readNone
rdaaction to be executed after readNone
awraction to be executed before writeNoneY
wraaction to be executed after writeNoneY
namthe name for this fmt''Y
usranything, its up to the user to make use of this slotNoneY
FmtE(Fmt)A superclass, an 'empty' fmt
valtype depends on subclass''Y
FmtB(Fmt)A superclass, a Buff-like fmt
valread value, type depends on subclass''Y
Fmt1(Fmt)A superclass, a fmt with one sub-fmt
valthe sub-fmt readNoneY
FmtN(Fmt)A superclass, a fmt with many sub-fmts
valthe sub-fmts read[]Y
Or(Fmt1)The first working format in fmt is used
fmtan array of possible formats of the data[]
And(FmtN)All of the formats in fmt are used sequetially
fmtan array of the formats of the data[]
For(FmtN)The format in fmt is repeated lim times
fmtthe format of the dataNone
limthe number of repititions as count or [min,max][0,sys.maxint]Y
Until(FmtN)The format in fmt is repeated until the format in tst succeds
fmtthe format of the dataNone
tstthe format for the testNone
kepappend the data to val that match tst?TrueY
Buff(FmtB)A string of siz bytes
sizthe number of bytes to read0Y
valthe bytes readNoneY
Tuff(FmtB)A string of bytes until the format in tst succeds (terminated buff)
tstthe format for the test[Const,{'fmt':'\0'}]
valthe bytes readNoneY
Struct(FmtB)Using the struct module
fmta format string as in module struct'>H'Y
valthe data read[]Y
Varlen(FmtB)Used in Midi
valthe number read0Y
Const(FmtB)The data must match this
valthe string of bytes to matchNoneY
Detour(Fmt1)Make a detour to det and do the format in fmt, then return
detthe adress to seek to0Y
fmtthe format of the dataNone
Seek(FmtE)Make a detour to val, don't return
valthe adress to seek to0Y
Empty(FmtE)
Eof(FmtE)Test for Eof
Tell(FmtE)Test for val
valthe adress to compare tell whith0Y

The red attributes are set by makeFmt. The green attributes are set by read and write.

A format can also be a string that will be evaluated.

Each Fmt has two functions to make it easier to navigate upwards to a sup-fmt: iup(i) which goes up i steps. nup(n) which goes up until a sup whith nam == n is found.

Examples

There are currently three examples of format-descriptions: Tiff, Jpeg and Midi, they all come in one python-module each. To use Midi.py and run it on a file called x.mid, just type: ./Midi.py x.mid > x.mid.py and you should get a script in x.mid.py. If you then run the script, you should get py.x.mid

PicRW

PicRW is more or less dead, use FmtRW instead While i was busy programming PicRW i realized that I should separate the format-description from the code, so take a look at FmtRW instead, it's currently slower, but much more fun.

A Python module that reads and writes JPEG and TIFF images. When the module is run as a script the module will read an image from file and emit a Python script that when run will generate the image file again.

If you have a file x.jpg do this at the prompt: ./PicRW.py -g x.jpg and you will get a file called x.jpg.py Edit it and then do this at the prompt: ./x.jpg.py and you will get a file called py.x.jpg

Some help on the options: ./PicRW.py -h

Have fun!

Download them at:

SourceForge.net Logo

updated 2004-06-06