[Python-Dev] Proposed new module for the Python library

Eric S. Raymond esr@thyrsus.com
Thu, 2 Aug 2001 22:36:32 -0400

There's a kind of boilerplate code I've written way too often.  The most
recent time, I got fed up and wrote a candidate library module so I'd
never have to do it again.

Here's the header comment:

ccframe -- framework code for building compiler-like programs.

There is a common `compiler-like' pattern in Unix scripts which is useful
for translation utilities of all sorts.  A program following this pattern
behaves as a filter when no argument files are specified on the command
line, but otherwise transforms each file individually into a corresponding
output file.

This module provides framework and glue code to make such programs easy
to write.  You supply a function to massage the file data; depending
on which entry point you use, it can take input and output file pointers,
or it can take a string consisting of the entire file's data and return
a replacement, or it can take in succession strings consisting of each
of the file's lines and return a translated line for each.

Argument files are transformed in left to right order on the command line.
A filename consisting of a dash is interpreted as a directive to read from
standard input (this can be useful in pipelines).

Replacement of each file is atomic and doesn't occur until the
translation of that file has completed.  Any tempfiles are removed
automatically on any exception thrown by the translation function,
and the exception is then passed upwards.

The entry points return 0 on success, 1 to signal a failed file open, and
2 to signal a failed tempfile open or rename.  Error messages are emitted
to stderr.

Design comments?  Critiques?  Code on request.

I'm already considering throwing exceptions on open and rename errors
instead of complaining to stderr and returning an error status.  That
would be more Pythonic, though slightly less convenient in the
commonest cases.
		<a href="http://www.tuxedo.org/~esr/">Eric S. Raymond</a>

The two pillars of `political correctness' are, 
  a) willful ignorance, and
  b) a steadfast refusal to face the truth
	-- George MacDonald Fraser