Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watch context file for changes saved separately #304

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions damnit/ctxsupport/damnit_ctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
41 changes: 41 additions & 0 deletions damnit/gui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@

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
Expand Down Expand Up @@ -88,6 +93,8 @@
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
Expand Down Expand Up @@ -260,17 +267,49 @@
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()

Check warning on line 297 in damnit/gui/main_window.py

View check run for this annotation

Codecov / codecov/patch

damnit/gui/main_window.py#L294-L297

Added lines #L294 - L297 were not covered by tests

if (self.editor_ctx_size_mtime != size_mtime) and self._context_is_saved:
log.info("Context file changed, reloading editor")
self.reload_context()

Check warning on line 301 in damnit/gui/main_window.py

View check run for this annotation

Codecov / codecov/patch

damnit/gui/main_window.py#L299-L301

Added lines #L299 - L301 were not covered by tests

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
Expand Down Expand Up @@ -812,6 +851,7 @@
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)
Expand Down Expand Up @@ -860,6 +900,7 @@
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:
Expand Down
6 changes: 5 additions & 1 deletion damnit/gui/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@
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

Check warning on line 122 in damnit/gui/table.py

View check run for this annotation

Codecov / codecov/patch

damnit/gui/table.py#L120-L122

Added lines #L120 - L122 were not covered by tests
Comment on lines +118 to +122
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This catch probably isn't needed any more with the fix in damnit_ctx.py, but I figured it was easier to leave it in.


self.setColumnHidden(column_index, not visible)

Expand Down