import sys import os def replace_func(c, f, n): s = f"def {f}(" i = c.find(s) if i == -1: return c ls = c[:i].splitlines() if ls and ls[-1].strip().startswith("@"): i = c.rfind("@", 0, i) nx = c.find("\ndef ", i + 1) if nx == -1: nx = c.find("\nclass ", i + 1) if nx == -1: nx = len(c) return c[:i] + n.strip() + "\n\n" + c[nx:] v = "core/views.py" with open(v, "r") as f: c = f.read() with open("core/filter_helper.py", "r") as f: h = f.read().strip() if "get_filtered_voter_queryset" not in c: c = c.replace("def voter_advanced_search", h + "\n\n" + "def voter_advanced_search", 1) with open("core/views_new.py", "r") as f: v_n = f.read().strip() with open("core/bulk_sms_new.py", "r") as f: s_n = f.read().strip() with open("core/bulk_email_new.py", "r") as f: e_n = f.read().strip() with open("core/export_new.py", "r") as f: x_n = f.read().strip() c = replace_func(c, "voter_advanced_search", v_n) c = replace_func(c, "bulk_send_sms", s_n) c = replace_func(c, "voter_bulk_send_email", e_n) c = replace_func(c, "export_voters_csv", x_n) with open(v, "w") as f: f.write(c)