def integrate_to_file(what, filename, start_line, end_line):
try:<EOL><INDENT>with open(filename) as f:<EOL><INDENT>lines = f.readlines()<EOL><DEDENT><DEDENT>except IOError:<EOL><INDENT>lines = []<EOL><DEDENT>tmp_file = tempfile.NamedTemporaryFile(delete=False)<EOL>lines.reverse()<EOL>while lines:<EOL><INDENT>line = lines.pop()<EOL>if line == start_line:<EOL><INDENT>break<EOL><DEDENT>tmp_file.write(line)<EOL><DEDENT>tmp_file.write(start_line)<EOL>tmp_file.write(what)<EOL>tmp_file.write(end_line)<EOL>while lines:<EOL><INDENT>line = lines.pop()<EOL>if line == end_line:<EOL><INDENT>break<EOL><DEDENT><DEDENT>tmp_file.writelines(lines)<EOL>tmp_file.close()<EOL>os.rename(tmp_file.name, filename)<EOL>
WARNING this is working every second run.. so serious bug
    Integrate content into a file withing "line marks"