refactor: replace global VERBOSE with instance variable in FileWatcher

This commit is contained in:
Paul Gauthier (aider) 2024-12-01 06:13:01 -08:00
parent 74108dd680
commit f36239712f

View file

@ -9,8 +9,6 @@ from watchfiles import watch
from aider.dump import dump # noqa from aider.dump import dump # noqa
VERBOSE = True
def is_source_file(path: Path) -> bool: def is_source_file(path: Path) -> bool:
""" """
@ -65,10 +63,11 @@ def load_gitignores(gitignore_paths: list[Path]) -> Optional[PathSpec]:
class FileWatcher: class FileWatcher:
"""Watches source files for changes and AI comments""" """Watches source files for changes and AI comments"""
def __init__(self, coder, encoding="utf-8", gitignores=None): def __init__(self, coder, encoding="utf-8", gitignores=None, verbose=False):
self.coder = coder self.coder = coder
self.encoding = encoding self.encoding = encoding
self.root = Path(coder.root) self.root = Path(coder.root)
self.verbose = verbose
self.stop_event = None self.stop_event = None
self.watcher_thread = None self.watcher_thread = None
self.changed_files = None self.changed_files = None
@ -87,7 +86,7 @@ class FileWatcher:
return False return False
rel_path = path_abs.relative_to(self.root) rel_path = path_abs.relative_to(self.root)
if VERBOSE: if self.verbose:
dump(rel_path) dump(rel_path)
if gitignore_spec and gitignore_spec.match_file(str(rel_path)): if gitignore_spec and gitignore_spec.match_file(str(rel_path)):
@ -98,7 +97,7 @@ class FileWatcher:
if not is_source_file(path_obj): if not is_source_file(path_obj):
return False return False
if VERBOSE: if self.verbose:
dump("ok", rel_path) dump("ok", rel_path)
# Check if file contains AI markers # Check if file contains AI markers
@ -107,11 +106,11 @@ class FileWatcher:
content = f.read() content = f.read()
res = bool(re.search(r"(?:#|//) *ai\b", content, re.IGNORECASE)) res = bool(re.search(r"(?:#|//) *ai\b", content, re.IGNORECASE))
if VERBOSE: if self.verbose:
dump(res) dump(res)
return res return res
except (IOError, UnicodeDecodeError) as err: except (IOError, UnicodeDecodeError) as err:
if VERBOSE: if self.verbose:
dump(err) dump(err)
return False return False
@ -136,14 +135,14 @@ class FileWatcher:
if comments := get_ai_comment(file, encoding=self.encoding): if comments := get_ai_comment(file, encoding=self.encoding):
result[file] = comments result[file] = comments
if VERBOSE: if self.verbose:
dump(result) dump(result)
if result: if result:
self.coder.abs_fnames.update(changed_files) self.coder.abs_fnames.update(changed_files)
self.coder.io.interrupt_input() self.coder.io.interrupt_input()
return return
except Exception as e: except Exception as e:
if VERBOSE: if self.verbose:
dump(f"File watcher error: {e}") dump(f"File watcher error: {e}")
raise e raise e