def _install_config_watchers(flag_options: Dict[str, Any]) -> None: def on_config_changed(_path): load_config_options(flag_options) for filename in CONFIG_FILENAMES: if os.path.exists(filename): watch_file(filename, on_config_changed)
def test_watch_file(self, mock_event_watcher, mock_polling_watcher): """Test all possible outcomes of both `get_file_watcher_class` and `watch_file`, based on config.fileWatcherType and whether `watchdog_available` is true. """ subtest_params = [ (None, False, None), (None, True, None), ("poll", False, mock_polling_watcher), ("poll", True, mock_polling_watcher), ("watchdog", False, None), ("watchdog", True, mock_event_watcher), ("auto", False, mock_polling_watcher), ("auto", True, mock_event_watcher), ] for watcher_config, watchdog_available, file_watcher_class in subtest_params: test_name = f"config.fileWatcherType={watcher_config}, watcher_available={watchdog_available}" with self.subTest(test_name): with patch_config_options( {"server.fileWatcherType": watcher_config}), patch( "streamlit.watcher.file_watcher.watchdog_available", watchdog_available, ): # Test get_file_watcher_class() result self.assertEqual(file_watcher_class, get_file_watcher_class()) # Test watch_file(). If file_watcher_class is None, # nothing should happen. Otherwise, file_watcher_class # should be called with the watch_file params. on_file_changed = Mock() watch_file("some/file/path", on_file_changed) if file_watcher_class is not None: file_watcher_class.assert_called_with( "some/file/path", on_file_changed)
def _maybe_install_file_watcher(self) -> None: with self._lock: if self._file_watcher_installed: return watch_file(self._file_path, self._on_secrets_file_changed) # We set file_watcher_installed to True even if watch_file # returns False to avoid repeatedly trying to install it. self._file_watcher_installed = True
def _maybe_install_file_watcher(self) -> None: with self._lock: if self._file_watcher_installed: return # We force our watcher_type to 'poll' because Streamlit Sharing # stores `secrets.toml` in a virtual filesystem that is # incompatible with watchdog. watch_file( self._file_path, self._on_secrets_file_changed, watcher_type="poll", ) # We set file_watcher_installed to True even if watch_file # returns False to avoid repeatedly trying to install it. self._file_watcher_installed = True