python line editor

A

Art

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
 
T

Tom B.

I'm having a hard time understanding your question,

what exactly would
/sFoo
:i"Bar"
:wq
do to a file, do you have a GUI available?

Have a look at sys.argv() and glob.glob() in the standard libraries they
might help.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,204
Messages
2,571,063
Members
47,670
Latest member
micheljon

Latest Threads

Top