From 7e763094b3a2aa892eb873446a2de83e0fd8d445 Mon Sep 17 00:00:00 2001 From: "Paul Gauthier (aider)" Date: Thu, 22 Aug 2024 06:34:19 -0700 Subject: [PATCH] feat: Replace wordcount with find_original_update_blocks in testsr.py --- testsr.py | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/testsr.py b/testsr.py index ce22333a4..62af795ed 100644 --- a/testsr.py +++ b/testsr.py @@ -1,11 +1,7 @@ import re import sys - - -def wordcount(text): - """Count the number of words in the given text.""" - return len(text.split()) - +import json +from aider.coders.editblock_coder import find_original_update_blocks, DEFAULT_FENCE def process_markdown(filename): try: @@ -15,6 +11,7 @@ def process_markdown(filename): # Split the content into sections based on '####' headers sections = re.split(r"(?=####\s)", content) + results = [] for section in sections: if section.strip(): # Ignore empty sections # Extract the header (if present) @@ -22,19 +19,41 @@ def process_markdown(filename): # Get the content (everything after the header) content = "\n".join(section.split("\n")[1:]).strip() - # Count words - count = wordcount(content) + # Process the content with find_original_update_blocks + blocks = list(find_original_update_blocks(content, DEFAULT_FENCE)) - print(f"{header}: {count} words") + # Create a dictionary for this section + section_result = { + "header": header, + "blocks": [] + } + + for block in blocks: + if block[0] is None: # This is a shell command block + section_result["blocks"].append({ + "type": "shell", + "content": block[1] + }) + else: # This is a SEARCH/REPLACE block + section_result["blocks"].append({ + "type": "search_replace", + "filename": block[0], + "original": block[1], + "updated": block[2] + }) + + results.append(section_result) + + # Output the results as JSON + print(json.dumps(results, indent=2)) except FileNotFoundError: - print(f"Error: File '{filename}' not found.") + print(json.dumps({"error": f"File '{filename}' not found."})) except Exception as e: - print(f"An error occurred: {e}") - + print(json.dumps({"error": f"An error occurred: {str(e)}"})) if __name__ == "__main__": if len(sys.argv) != 2: - print("Usage: python testsr.py ") + print(json.dumps({"error": "Usage: python testsr.py "})) else: process_markdown(sys.argv[1])