python line editor
Art
abrady at prontomail.com
Tue Aug 24 19:11:57 EDT 2004
Hello all,
this is possibly a not well-formed question, but are there any python
modules out there to do vi/ex/ed style script editing?
i.e. the kind where you pass the commands thru stdin. ie:
/sFoo
:i"Bar"
:wq
or something like that.
I'm porting some software right now so I have thousands of files to do
replaces on. I want to do everything in Python, but I find myself
writing a bunch of functions that look like this:
for line in fileinput.input( os.path.join( dir,
file ), inplace=1, backup="~"):
m = xbrex.match( line )
if m:
resdef = ""
#is this an ndef?
if m.group(1):
resdef = "#if !defined(_XBOX) &&
!defined(_PS2)"
else:
resdef = "#if defined(_XBOX) ||
defined(_PS2)"
#fixup the line
line = line.replace(m.group(0), resdef)
# write the line
sys.stdout.write(line)
Basically my problem is that I have to operate on files on a line by
line basis using this method. This case works fine, but there are
cases where I would like multi-line changes, basically mini-editor
support.
I know that this could be done with code i.e. if( startFound ): ...,
but I don't want to have to track a bunch of flags. . .
I know I could slurp the whole file into a string, but that still
doesn't have the simplicity of the ed syntax.
Since I'm basically asking for editor functionality, an alternative
would be to do this with ed and scripts, or elisp, but I'd like to try
python for this first.
Any suggestions?
Best regards,
Aaron
More information about the Python-list
mailing list