Removing/replacing multi-line code sections with python
<p>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):</p>

<pre><code>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)
</code></pre>

<p>The thing is that the construct I'm parsing for and want to replace <em>could</em> contain other nested statements that also need to be removed as part of the <code>bar(...)</code> removal.</p>

<p>Here is what a sample, to be cleaned, code snippet would look like:</p>

<pre><code>annotation (
  foo1(k=3),
  bar(
    x=0.29,
    y=0,
    bar1(
    x=3, y=4),
    width=0.71,
    height=0.85),
  foo2(System(...))
</code></pre>

<p>I would think that someone might have solved something similar before :)</p>
