Skip to content

Commit

Permalink
Refactor ExecEnv class and update delay property
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreRaybaut committed Mar 8, 2024
1 parent 72566aa commit a428b70
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
21 changes: 11 additions & 10 deletions guidata/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ class ExecEnv:
ACCEPTDIALOGS_ENV = "GUIDATA_ACCEPT_DIALOGS"
VERBOSE_ENV = "GUIDATA_VERBOSE"
SCREENSHOT_ENV = "GUIDATA_SCREENSHOT"
DELAY_ENV = "GUIDATA_DELAY_BEFORE_QUIT"
DELAY_ENV = "GUIDATA_DELAY"

def __init__(self):
if PARSE:
self.parse_args()
# Check that calling `to_dict` do not raise any exception
self.to_dict()
if self.unattended: # Do not execute this code in production
# Check that calling `to_dict` do not raise any exception
self.to_dict()

def to_dict(self):
"""Return a dictionary representation of the object"""
Expand Down Expand Up @@ -136,15 +137,15 @@ def verbose(self, value):
os.environ[self.VERBOSE_ENV] = value

@property
def delay_before_quit(self):
def delay(self):
"""Delay (seconds) before quitting application in unattended mode"""
try:
return int(os.environ.get(self.DELAY_ENV))
except (TypeError, ValueError):
return 0

@delay_before_quit.setter
def delay_before_quit(self, value: int):
@delay.setter
def delay(self, value: int):
"""Set delay (seconds) before quitting application in unattended mode"""
os.environ[self.DELAY_ENV] = str(value)

Expand Down Expand Up @@ -211,7 +212,7 @@ def log(self, source: Any, *objects: Any) -> None:
def print(self, *objects, sep=" ", end="\n", file=sys.stdout, flush=False):
"""Print in file, depending on verbosity level"""
# print(f"unattended={self.unattended} ; verbose={self.verbose} ; ")
# print(f"screenshot={self.screenshot}; delay_before_quit={self.delay_before_quit}")
# print(f"screenshot={self.screenshot}; delay={self.delay}")
if self.verbose != VerbosityLevels.QUIET.value or DEBUG:
print(*objects, sep=sep, end=end, file=file, flush=flush)

Expand Down Expand Up @@ -243,7 +244,7 @@ def context(
unattended=None,
accept_dialogs=None,
screenshot=None,
delay_before_quit=None,
delay=None,
verbose=None,
) -> Generator[None, None, None]:
"""Return a context manager that sets some execenv properties at enter,
Expand All @@ -255,7 +256,7 @@ def context(
unattended: whether to run in unattended mode
accept_dialogs: whether to accept dialogs in unattended mode
screenshot: whether to take screenshots
delay_before_quit: delay (seconds) before quitting application in unattended mode
delay: delay (seconds) before quitting application in unattended mode
verbose: verbosity level
.. note::
Expand All @@ -266,7 +267,7 @@ def context(
"unattended": unattended,
"accept_dialogs": accept_dialogs,
"screenshot": screenshot,
"delay_before_quit": delay_before_quit,
"delay": delay,
"verbose": verbose,
}
for key, value in new_values.items():
Expand Down
10 changes: 5 additions & 5 deletions guidata/qthelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,15 +505,15 @@ def qt_app_context(exec_loop: bool = False) -> Generator[QW.QApplication, None,
exception_occured = True
finally:
if execenv.unattended: # pragma: no cover
if execenv.delay_before_quit > 0:
if execenv.delay > 0:
mode = "Screenshot" if execenv.screenshot else "Unattended"
message = f"{mode} mode (delay: {execenv.delay_before_quit}s)"
msec = execenv.delay_before_quit * 1000 - 200
message = f"{mode} mode (delay: {execenv.delay}s)"
msec = execenv.delay * 1000 - 200
for widget in QW.QApplication.instance().topLevelWidgets():
if isinstance(widget, QW.QMainWindow):
widget.statusBar().showMessage(message, msec)
QC.QTimer.singleShot(
execenv.delay_before_quit * 1000,
execenv.delay * 1000,
lambda: close_widgets_and_quit(screenshot=execenv.screenshot),
)
if exec_loop and not exception_occured:
Expand All @@ -534,7 +534,7 @@ def exec_dialog(dlg: QW.QDialog) -> int:
"""
if execenv.unattended:
QC.QTimer.singleShot(
execenv.delay_before_quit * 1000,
execenv.delay * 1000,
lambda: close_dialog_and_quit(dlg, screenshot=execenv.screenshot),
)
delete_later = not dlg.testAttribute(QC.Qt.WA_DeleteOnClose)
Expand Down

0 comments on commit a428b70

Please sign in to comment.