Skip to content

Commit cd91725

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 0496f30 commit cd91725

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
@@ -15,6 +15,7 @@
1515
from __future__ import annotations
1616

1717
from datetime import datetime
18+
import os
1819
import threading
1920
from typing import Optional
2021
from typing import Union
@@ -55,19 +56,19 @@ def __init__(self):
5556
self.reload_needed = threading.Event()
5657
self.last_modified_file = None
5758

58-
def on_modified(self, event: FileSystemEvent):
59+
def _handle_event(self, event: FileSystemEvent):
60+
"""Handle file system events for .py and .yaml files."""
5961
if event.is_directory:
6062
return
61-
if event.src_path.endswith('.py') or event.src_path.endswith('.yaml'):
63+
if event.src_path.endswith(('.py', '.yaml')):
6264
self.last_modified_file = event.src_path
6365
self.reload_needed.set()
6466

67+
def on_modified(self, event: FileSystemEvent):
68+
self._handle_event(event)
69+
6570
def on_created(self, event: FileSystemEvent):
66-
if event.is_directory:
67-
return
68-
if event.src_path.endswith('.py') or event.src_path.endswith('.yaml'):
69-
self.last_modified_file = event.src_path
70-
self.reload_needed.set()
71+
self._handle_event(event)
7172

7273
def check_and_reset(self) -> tuple[bool, Optional[str]]:
7374
"""Check if reload is needed and reset the flag."""
@@ -245,8 +246,6 @@ async def run_cli(
245246
observer: Optional[Observer] = None
246247
change_handler: Optional[DevModeChangeHandler] = None
247248
if dev_mode:
248-
import os
249-
250249
agent_path = os.path.join(agent_parent_dir, agent_folder_name)
251250
change_handler = DevModeChangeHandler()
252251
observer = Observer()

0 commit comments

Comments
 (0)