From d4d960372d120140e8a97fb6085d3135122077d0 Mon Sep 17 00:00:00 2001 From: Pascal Marco Caversaccio Date: Thu, 15 Aug 2024 13:17:57 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=E2=80=8D=E2=99=82=EF=B8=8F=20This?= =?UTF-8?q?=20requires=20hard=20skills=20looool?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Pascal Marco Caversaccio --- scripts/insert_venom_pragma.py | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/scripts/insert_venom_pragma.py b/scripts/insert_venom_pragma.py index 4f700bba..ae74b59d 100644 --- a/scripts/insert_venom_pragma.py +++ b/scripts/insert_venom_pragma.py @@ -1,29 +1,24 @@ import os -def add_venom_pragma(filepath): - with open(filepath, "r") as file: - lines = file.readlines() - - # Insert `pragma experimental-codegen` in the second line to activate the `venom` backend. - if len(lines) >= 2: +def insert_venom_pragma(filepath): + with open(filepath, "r+") as f: + lines = f.readlines() + if len(lines) < 2: + lines.append("") + # Insert `pragma experimental-codegen` on the second line to activate the `venom` backend. lines.insert(1, "# pragma experimental-codegen\n") - else: - lines.append("# pragma experimental-codegen\n") - - # Write the modified lines back to the file. - with open(filepath, "w") as file: - file.writelines(lines) + # Move the file pointer back to the beginning of the file. + f.seek(0) + f.writelines(lines) def process_directory(directory): for root, _, files in os.walk(directory): for file in files: - if file.endswith(".vy") or file.endswith(".vyi"): - filepath = os.path.join(root, file) - add_venom_pragma(filepath) + if file.endswith((".vy", ".vyi")): + insert_venom_pragma(os.path.join(root, file)) if __name__ == "__main__": - src_directory = "src/snekmate" - process_directory(src_directory) + process_directory("src/snekmate")