Tuesday, June 14, 2005

preprocess.py - a (portable) multi-language file preprocessor

This is a variation on the typical C preprocessor to extend it to multiple
languages. Languages for which it works include: C++, Python, Perl, Tcl, XML,
JavaScript, CSS, IDL, etc. Preprocess is usable both as a command line
app and as a Python module.

Here is how is works: All preprocessor statements are on their own line. A
preprocessor statement is a comment (as appropriate for the language of the
file being preprocessed). This way the preprocessor statements do not make an
unpreprocessed file syntactically incorrect. For example:

    preprocess -D FAV_COLOR=blue foo.py

will yield this transformation:

            print "hi there"
# #if FAV_COLOR == "blue"
print "My fav color is blue"
# #else
print "My fav color is red"
# #endif

            print "hi there"
print "My fav color is blue"

And similarly for XML files: See the websitewebsite

Download: Linux source and Windows source. . dunno yet what the difference would be, but it says so on the website :)