2

I have several java code file and I have to add a public class level variable in each of them. There are so many files that I need to write a python 2.2 script to do the same.

package xx.xx.xx;

import java.util.Properties;

public class MyClass extends YourClass{

    public myMethod() throws MyException {

    }
}

expected output

package xx.xx.xx;

import java.util.Properties;

public class MyClass extends YourClass{

    public static final String CO_ID = "XXXXX" 

    public myMethod() throws MyMException {

    }
}

I know file.find('{') give me the index of the first occurrence of { but I need push rest of the code down and insert my public member in each file.

3
  • 2
    Sounds to me like any decent editor should be able to handle that with a simple regex replace. Why use Python for this? Commented Mar 1, 2013 at 11:26
  • This operation is part of many other operations which happens to every java source file. So I need a piece of python code which can be placed inside the existing python code doing all other operation There are more than 400 files, you want me to open all of them one by one ? Commented Mar 1, 2013 at 11:30
  • One more problem , it does not work when class file starts with final public final class MyClass extends YourClass{ Commented Mar 1, 2013 at 17:25

2 Answers 2

5

Python has a convenient tool for this: the fileinput module:

import fileinput
import sys
import re

newline = '''\
{l}
    public static final String CO_ID = "XXXXX"
'''

filename = '/path/to/file.java'
for line in fileinput.input([filename], inplace=True, backup='.bak'):
    if re.match(r'public class', line):
        sys.stdout.write(newline.format(l=line))
    else:
        sys.stdout.write(line)

  • inplace=True, alters the file "in-place". Actually a temporary file is created, and then moved to the original location.
  • backup='.bak' tells fileinput.input to create a backup of the original file.
  • sys.stdout.write is used instead of print since print adds an extra new line while sys.stdout.write does not.
Sign up to request clarification or add additional context in comments.

5 Comments

Can you simplify the code a bit more? I already know how to traverse files recursively in a directory, just give example of putting the string in one file. Note I am in a very old version Python 2.2
If you are using Python2.2, I'm not even sure you have the fileinput module. This entire answer may not be any use to you... Hold on... you do have fileinput!
I've simplified the code to show only the use of fileinput.
Fantastic , Works like a charm in Python 3.3 But error out in 2.2 :( File "insert.py", line 13, in ? sys.stdout.write(newline.format(l=line)) AttributeError: 'str' object has no attribute 'format' Any workaround for formatting ?
Oops, I forgot that str.format was introduced in Python2.6. It looks like you've already found the answer (using % string interpolation) below.
1

Ubuntu's code works but I have modified for Python 2.2 Python 2.2 compatible code

import fileinput
import sys
import re

newline = '''\
%s
    public static final String CO_ID = "XXXXX"
'''

filename = '/path/to/file.java'
for line in fileinput.input([filename], inplace=True, backup='.bak'):
    if re.match(r'public class', line):
        sys.stdout.write(newline%line)
    else:
        sys.stdout.write(line)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.