mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-20 12:24:59 +00:00
style: Run linter on clean_metadata.py
This commit is contained in:
parent
58f4db4e52
commit
ebfce5b0f2
1 changed files with 24 additions and 20 deletions
|
@ -2,13 +2,12 @@
|
||||||
|
|
||||||
import difflib
|
import difflib
|
||||||
import json
|
import json
|
||||||
from pathlib import Path
|
|
||||||
import re
|
import re
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import json5
|
import json5
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def find_block_lines(lines, key_to_remove):
|
def find_block_lines(lines, key_to_remove):
|
||||||
"""Finds the start and end line indices for a top-level key's block."""
|
"""Finds the start and end line indices for a top-level key's block."""
|
||||||
start_line_idx = -1
|
start_line_idx = -1
|
||||||
|
@ -36,16 +35,18 @@ def find_block_lines(lines, key_to_remove):
|
||||||
j += 1
|
j += 1
|
||||||
continue
|
continue
|
||||||
if stripped_next_line.startswith("{"):
|
if stripped_next_line.startswith("{"):
|
||||||
start_line_idx = i # Start from the key definition line
|
start_line_idx = i # Start from the key definition line
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
potential_start = -1 # False alarm
|
potential_start = -1 # False alarm
|
||||||
break
|
break
|
||||||
if start_line_idx != -1:
|
if start_line_idx != -1:
|
||||||
break
|
break
|
||||||
|
|
||||||
if start_line_idx == -1:
|
if start_line_idx == -1:
|
||||||
print(f"Warning: Could not reliably find start line for '{key_to_remove}'. Skipping removal.")
|
print(
|
||||||
|
f"Warning: Could not reliably find start line for '{key_to_remove}'. Skipping removal."
|
||||||
|
)
|
||||||
return None, None # Key block start not found clearly
|
return None, None # Key block start not found clearly
|
||||||
|
|
||||||
brace_level = 0
|
brace_level = 0
|
||||||
|
@ -62,20 +63,20 @@ def find_block_lines(lines, key_to_remove):
|
||||||
for char_idx, char in enumerate(line):
|
for char_idx, char in enumerate(line):
|
||||||
# Rudimentary string detection
|
# Rudimentary string detection
|
||||||
if char == '"':
|
if char == '"':
|
||||||
# Check if preceded by an odd number of backslashes (escaped quote)
|
# Check if preceded by an odd number of backslashes (escaped quote)
|
||||||
backslashes = 0
|
backslashes = 0
|
||||||
temp_idx = char_idx - 1
|
temp_idx = char_idx - 1
|
||||||
while temp_idx >= 0 and line[temp_idx] == '\\':
|
while temp_idx >= 0 and line[temp_idx] == "\\":
|
||||||
backslashes += 1
|
backslashes += 1
|
||||||
temp_idx -= 1
|
temp_idx -= 1
|
||||||
if backslashes % 2 == 0:
|
if backslashes % 2 == 0:
|
||||||
in_string = not in_string
|
in_string = not in_string
|
||||||
|
|
||||||
if not in_string:
|
if not in_string:
|
||||||
if char == '{':
|
if char == "{":
|
||||||
brace_level += 1
|
brace_level += 1
|
||||||
block_started = True # Mark that we've entered the block
|
block_started = True # Mark that we've entered the block
|
||||||
elif char == '}':
|
elif char == "}":
|
||||||
brace_level -= 1
|
brace_level -= 1
|
||||||
|
|
||||||
# Check if the block ends *after* processing the entire line
|
# Check if the block ends *after* processing the entire line
|
||||||
|
@ -84,8 +85,11 @@ def find_block_lines(lines, key_to_remove):
|
||||||
break
|
break
|
||||||
|
|
||||||
if end_line_idx == -1:
|
if end_line_idx == -1:
|
||||||
print(f"Warning: Could not find end of block for '{key_to_remove}' starting at line {start_line_idx+1}. Skipping removal.")
|
print(
|
||||||
return None, None # Block end not found
|
f"Warning: Could not find end of block for '{key_to_remove}' starting at line"
|
||||||
|
f" {start_line_idx+1}. Skipping removal."
|
||||||
|
)
|
||||||
|
return None, None # Block end not found
|
||||||
|
|
||||||
return start_line_idx, end_line_idx
|
return start_line_idx, end_line_idx
|
||||||
|
|
||||||
|
@ -96,7 +100,7 @@ def remove_block_surgically(file_path, key_to_remove):
|
||||||
# Read with universal newlines, but keep track for writing
|
# Read with universal newlines, but keep track for writing
|
||||||
with open(file_path, "r") as f:
|
with open(file_path, "r") as f:
|
||||||
content = f.read()
|
content = f.read()
|
||||||
lines = content.splitlines(keepends=True) # Keep original line endings
|
lines = content.splitlines(keepends=True) # Keep original line endings
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error reading {file_path} for removal: {e}")
|
print(f"Error reading {file_path} for removal: {e}")
|
||||||
return False
|
return False
|
||||||
|
@ -104,7 +108,7 @@ def remove_block_surgically(file_path, key_to_remove):
|
||||||
start_idx, end_idx = find_block_lines(lines, key_to_remove)
|
start_idx, end_idx = find_block_lines(lines, key_to_remove)
|
||||||
|
|
||||||
if start_idx is None or end_idx is None:
|
if start_idx is None or end_idx is None:
|
||||||
return False # Error message already printed by find_block_lines
|
return False # Error message already printed by find_block_lines
|
||||||
|
|
||||||
# Prepare the lines to be written, excluding the identified block
|
# Prepare the lines to be written, excluding the identified block
|
||||||
output_lines = lines[:start_idx] + lines[end_idx + 1 :]
|
output_lines = lines[:start_idx] + lines[end_idx + 1 :]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue