Skip to content

Commit 6541d7e

Browse files
committed
refactor(cli): improve DevModeChangeHandler code quality
- Extract duplicate logic into _handle_event helper method - Use tuple syntax for str.endswith() with multiple extensions - Move import os to module level following PEP 8
1 parent b00d2dd commit 6541d7e

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/google/adk/cli/cli.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from datetime import datetime
1818
from pathlib import Path
19+
import os
1920
import threading
2021
from typing import Optional
2122
from typing import Union
@@ -58,19 +59,19 @@ def __init__(self):
5859
self.reload_needed = threading.Event()
5960
self.last_modified_file = None
6061

61-
def on_modified(self, event: FileSystemEvent):
62+
def _handle_event(self, event: FileSystemEvent):
63+
"""Handle file system events for .py and .yaml files."""
6264
if event.is_directory:
6365
return
64-
if event.src_path.endswith('.py') or event.src_path.endswith('.yaml'):
66+
if event.src_path.endswith(('.py', '.yaml')):
6567
self.last_modified_file = event.src_path
6668
self.reload_needed.set()
6769

70+
def on_modified(self, event: FileSystemEvent):
71+
self._handle_event(event)
72+
6873
def on_created(self, event: FileSystemEvent):
69-
if event.is_directory:
70-
return
71-
if event.src_path.endswith('.py') or event.src_path.endswith('.yaml'):
72-
self.last_modified_file = event.src_path
73-
self.reload_needed.set()
74+
self._handle_event(event)
7475

7576
def check_and_reset(self) -> tuple[bool, Optional[str]]:
7677
"""Check if reload is needed and reset the flag."""
@@ -256,8 +257,6 @@ async def run_cli(
256257
observer: Optional[Observer] = None
257258
change_handler: Optional[DevModeChangeHandler] = None
258259
if dev_mode:
259-
import os
260-
261260
agent_path = os.path.join(agent_parent_dir, agent_folder_name)
262261
change_handler = DevModeChangeHandler()
263262
observer = Observer()

0 commit comments

Comments
 (0)