I'm trying to remove multiple lines containing an obsoleted code fragment from various file with the help of python. I looked for some examples but could not really find what I was looking for. What I basically need is something that does in principle the following (contains non-python syntax):
def cleanCode(filepath):
"""Clean out the obsolete or superflous 'bar()' code."""
with open(filepath, 'r') as foo_file:
string = foo_file[index_of("bar("):]
depth = 0
for char in string:
if char == "(": depth += 1
if char == ")": depth -= 1
if depth == 0: last_index = current_char_position
with open(filepath,'w') as foo_file:
mo_file.write(string)
The thing is that the construct I'm parsing for and want to replace could contain other nested statements that also need to be removed as part of the bar(...)
removal.
Here is what a sample, to be cleaned, code snippet would look like:
annotation (
foo1(k=3),
bar(
x=0.29,
y=0,
bar1(
x=3, y=4),
width=0.71,
height=0.85),
foo2(System(...))
I would think that someone might have solved something similar before :)