refactor to use io.write_text

This commit is contained in:
Paul Gauthier 2023-07-05 21:18:19 -07:00
parent 0ea52b412f
commit b46dffbb20
5 changed files with 16 additions and 15 deletions

View file

@ -924,7 +924,7 @@ class Coder:
if full_path in self.abs_fnames: if full_path in self.abs_fnames:
if not self.dry_run and write_content: if not self.dry_run and write_content:
Path(full_path).write_text(write_content) self.io.write_text(full_path, write_content)
return full_path return full_path
if not Path(full_path).exists(): if not Path(full_path).exists():
@ -950,7 +950,7 @@ class Coder:
self.repo.git.add(full_path) self.repo.git.add(full_path)
if not self.dry_run and write_content: if not self.dry_run and write_content:
Path(full_path).write_text(write_content) self.io.write_text(full_path, write_content)
return full_path return full_path

View file

@ -27,7 +27,10 @@ class EditBlockCoder(Coder):
if not full_path: if not full_path:
continue continue
content = self.io.read_text(full_path) content = self.io.read_text(full_path)
if do_replace(full_path, content, original, updated, self.dry_run): content = do_replace(full_path, content, original, updated)
if content:
if not self.dry_run:
self.io.write_text(full_path, content)
edited.add(path) edited.add(path)
continue continue
self.io.tool_error(f"Failed to apply edit to {path}") self.io.tool_error(f"Failed to apply edit to {path}")
@ -212,7 +215,7 @@ def strip_quoted_wrapping(res, fname=None):
return res return res
def do_replace(fname, content, before_text, after_text, dry_run=False): def do_replace(fname, content, before_text, after_text):
before_text = strip_quoted_wrapping(before_text, fname) before_text = strip_quoted_wrapping(before_text, fname)
after_text = strip_quoted_wrapping(after_text, fname) after_text = strip_quoted_wrapping(after_text, fname)
fname = Path(fname) fname = Path(fname)
@ -230,13 +233,8 @@ def do_replace(fname, content, before_text, after_text, dry_run=False):
new_content = content + after_text new_content = content + after_text
else: else:
new_content = replace_most_similar_chunk(content, before_text, after_text) new_content = replace_most_similar_chunk(content, before_text, after_text)
if not new_content:
return
if not dry_run: return new_content
fname.write_text(new_content)
return True
ORIGINAL = "<<<<<<< ORIGINAL" ORIGINAL = "<<<<<<< ORIGINAL"

View file

@ -136,7 +136,10 @@ class EditBlockFunctionCoder(Coder):
if not full_path: if not full_path:
continue continue
content = self.io.read_text(full_path) content = self.io.read_text(full_path)
if do_replace(full_path, content, original, updated, self.dry_run): content = do_replace(full_path, content, original, updated)
if content:
if not self.dry_run:
self.io.write_text(full_path, content)
edited.add(path) edited.add(path)
continue continue
self.io.tool_error(f"Failed to apply edit to {path}") self.io.tool_error(f"Failed to apply edit to {path}")

View file

@ -66,7 +66,7 @@ class WholeFileCoder(Coder):
edited.add(fname) edited.add(fname)
if not self.dry_run: if not self.dry_run:
new_lines = "".join(new_lines) new_lines = "".join(new_lines)
full_path.write_text(new_lines) self.io.write_text(full_path, new_lines)
fname = None fname = None
new_lines = [] new_lines = []
@ -125,6 +125,6 @@ class WholeFileCoder(Coder):
edited.add(fname) edited.add(fname)
if not self.dry_run: if not self.dry_run:
new_lines = "".join(new_lines) new_lines = "".join(new_lines)
Path(full_path).write_text(new_lines) self.io.write_text(full_path, new_lines)
return edited return edited

View file

@ -137,14 +137,14 @@ class InputOutput:
def read_text(self, filename): def read_text(self, filename):
try: try:
with open(filename, "r", encoding=self.encoding) as f: with open(str(filename), "r", encoding=self.encoding) as f:
return f.read() return f.read()
except (FileNotFoundError, UnicodeError) as e: except (FileNotFoundError, UnicodeError) as e:
self.tool_error(f"{filename}: {e}") self.tool_error(f"{filename}: {e}")
return return
def write_text(self, filename, content): def write_text(self, filename, content):
with open(filename, "w", encoding=self.encoding) as f: with open(str(filename), "w", encoding=self.encoding) as f:
f.write(content) f.write(content)
def get_input(self, root, rel_fnames, addable_rel_fnames, commands): def get_input(self, root, rel_fnames, addable_rel_fnames, commands):