diff --git a/damnit/ctxsupport/damnit_ctx.py b/damnit/ctxsupport/damnit_ctx.py index 288f63de..ae1befee 100644 --- a/damnit/ctxsupport/damnit_ctx.py +++ b/damnit/ctxsupport/damnit_ctx.py @@ -50,6 +50,8 @@ def __init__( def __call__(self, func): self.func = func self.name = func.__name__ + if self.title is None: + self.title = self.name return self def check(self): diff --git a/damnit/gui/main_window.py b/damnit/gui/main_window.py index 7fad4bd7..35a47a9c 100644 --- a/damnit/gui/main_window.py +++ b/damnit/gui/main_window.py @@ -50,6 +50,11 @@ class MainWindow(QtWidgets.QMainWindow): context_dir_changed = QtCore.pyqtSignal(str) save_context_finished = QtCore.pyqtSignal(bool) # True if saved + context_saved = QtCore.pyqtSignal() + check_context_file_timer = None + vars_ctx_size_mtime = None + editor_ctx_size_mtime = None + updating_vars = False db = None db_id = None @@ -88,6 +93,8 @@ def __init__(self, context_dir: Path = None, connect_to_kafka: bool = True): self._tab_widget.setEnabled(False) self.setCentralWidget(self._tab_widget) + self.context_saved.connect(self.launch_update_computed_vars) + self.table = None self.zulip_messenger = None @@ -260,17 +267,49 @@ def autoconfigure(self, path: Path): self.show_default_status_message() self.context_dir_changed.emit(str(path)) self.launch_update_computed_vars() + self.start_watching_context_file() def launch_update_computed_vars(self): + # Triggered when we open a proposal & when saving the context file log.debug("Launching subprocess to read variables from context file") + self.updating_vars = True proc = QtCore.QProcess(parent=self) # Show stdout & stderr with the parent process proc.setProcessChannelMode(QtCore.QProcess.ProcessChannelMode.ForwardedChannels) + proc.finished.connect(self.done_update_computed_vars) proc.finished.connect(proc.deleteLater) proc.setWorkingDirectory(str(self.context_dir)) proc.start(sys.executable, ['-m', 'damnit.cli', 'read-context']) proc.closeWriteChannel() + def done_update_computed_vars(self): + self.updating_vars = False + self.vars_ctx_size_mtime = self.get_context_size_mtime() + + def get_context_size_mtime(self): + st = self._context_path.stat() + return st.st_size, st.st_mtime + + def poll_context_file(self): + size_mtime = self.get_context_size_mtime() + if (self.vars_ctx_size_mtime != size_mtime) and not self.updating_vars: + log.info("Context file changed, updating computed variables") + self.launch_update_computed_vars() + + if (self.editor_ctx_size_mtime != size_mtime) and self._context_is_saved: + log.info("Context file changed, reloading editor") + self.reload_context() + + def start_watching_context_file(self): + if self.check_context_file_timer is not None: + self.check_context_file_timer.stop() + self.check_context_file_timer.deleteLater() + + self.check_context_file_timer = tmr = QtCore.QTimer(self) + tmr.setInterval(30_000) + tmr.timeout.connect(self.poll_context_file) + tmr.start() + def add_variable(self, name, title, variable_type, description="", before=None): n_static_cols = self.table_view.get_static_columns_count() before_pos = n_static_cols + 1 @@ -812,6 +851,7 @@ def test_context_result(self, test_result, output, checked_code): self.mark_context_saved() self._context_code_to_save = None self.save_context_finished.emit(saving) + self.context_saved.emit() if test_result == ContextTestResult.ERROR: self.set_error_widget_text(output) @@ -860,6 +900,7 @@ def mark_context_saved(self): self._tab_widget.tabBar().setTabTextColor(1, QtGui.QColor("black")) self._editor_status_message = str(self._context_path.resolve()) self.on_tab_changed(self._tab_widget.currentIndex()) + self.editor_ctx_size_mtime = self.get_context_size_mtime() def save_value(self, prop, run, name, value): if self.db is None: diff --git a/damnit/gui/table.py b/damnit/gui/table.py index 88226505..3a062aec 100644 --- a/damnit/gui/table.py +++ b/damnit/gui/table.py @@ -115,7 +115,11 @@ def set_column_visibility(self, name, visible, for_restore=False): deselected. The `for_restore` argument lets you specify which behaviour you want. """ - column_index = self.damnit_model.find_column(name, by_title=True) + try: + column_index = self.damnit_model.find_column(name, by_title=True) + except KeyError: + log.error("Could not find column %r to set visibility", name) + return self.setColumnHidden(column_index, not visible)