Skip to content

Commit 6557b68

Browse files
committed
Generalize log-once signal logging utility
log_plot_option_usage_once only supported the plot sidebar message format, so it could not be reused elsewhere. Replace it with log_once_on_signal in ert.gui.utils, which takes the message and log level from the caller, and let the plot controls pass their own message.
1 parent 36250ff commit 6557b68

7 files changed

Lines changed: 39 additions & 28 deletions

File tree

src/ert/gui/plotting/utils/logging_utils.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/ert/gui/plotting/utils/qt_creator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
QWidget,
1616
)
1717

18-
from ert.gui.plotting.utils.logging_utils import log_plot_option_usage_once
18+
from ert.gui.utils import log_once
1919

2020

2121
def create_side_panel(title: str, widget: QWidget) -> QWidget:
@@ -61,7 +61,7 @@ def create_checkbox_with_tooltip(
6161
checkbox.setToolTip(tooltip)
6262
checkbox.setChecked(initial_checked)
6363
checkbox.stateChanged.connect(connection_point)
64-
log_plot_option_usage_once(checkbox.clicked, logger, name)
64+
log_once(checkbox.clicked, logger, f"Plot sidebar option used: '{name}'")
6565
return checkbox
6666

6767

@@ -81,7 +81,7 @@ def create_spinbox_with_tooltip(
8181
spinbox.setRange(minimum, maximum)
8282
spinbox.setValue(initial_value)
8383
spinbox.valueChanged.connect(connection_point)
84-
log_plot_option_usage_once(spinbox.valueChanged, logger, name)
84+
log_once(spinbox.valueChanged, logger, f"Plot sidebar option used: '{name}'")
8585
return spinbox
8686

8787

src/ert/gui/plotting/widgets/plot_controls/custom_palette_dialog.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
QWidget,
1111
)
1212

13-
from ert.gui.plotting.utils.logging_utils import log_plot_option_usage_once
1413
from ert.gui.plotting.utils.plot_color_palettes import MINIMUM_COLOR_CYCLE_LENGTH
1514
from ert.gui.plotting.widgets.plot_controls.color_chooser import ColorBox
15+
from ert.gui.utils import log_once
1616

1717
logger = logging.getLogger(__name__)
1818

@@ -49,7 +49,9 @@ def __init__(
4949
buttons.accepted.connect(self.accept)
5050
buttons.rejected.connect(self.reject)
5151
layout.addWidget(buttons)
52-
log_plot_option_usage_once(self.accepted, logger, "Custom palette colors")
52+
log_once(
53+
self.accepted, logger, "Plot sidebar option used: 'Custom palette colors'"
54+
)
5355

5456
def get_color_cycle(self) -> list[tuple[str, float]]:
5557
return [(box.color.name(), box.color.alphaF()) for box in self._color_boxes]

src/ert/gui/plotting/widgets/plot_controls/everest_controls_plot_options.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
)
88

99
from ert.gui.plotting.utils import PlotContext
10-
from ert.gui.plotting.utils.logging_utils import log_plot_option_usage_once
1110
from ert.gui.plotting.utils.qt_creator import (
1211
create_group_layout,
1312
)
1413
from ert.gui.plotting.widgets.collapsible_section import CollapsibleSection
14+
from ert.gui.utils import log_once
1515

1616
logger = logging.getLogger(__name__)
1717

@@ -28,10 +28,10 @@ def __init__(self, connection_point: Callable[..., object]) -> None:
2828
self._display_over_button_group.addButton(self._display_over_batches_radio)
2929
self._display_over_button_group.addButton(self._display_over_controls_radio)
3030
self._display_over_button_group.buttonClicked.connect(connection_point)
31-
log_plot_option_usage_once(
31+
log_once(
3232
self._display_over_button_group.buttonClicked,
3333
logger,
34-
"X-axis display option",
34+
"Plot sidebar option used: 'X-axis display option'",
3535
)
3636
self._display_over_group = CollapsibleSection(
3737
"X-axis:",

src/ert/gui/plotting/widgets/plot_controls/observation_color.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
from PyQt6.QtWidgets import QCheckBox, QHBoxLayout, QLabel, QWidget
55

6-
from ert.gui.plotting.utils.logging_utils import log_plot_option_usage_once
76
from ert.gui.plotting.utils.plot_config import PlotConfig
87
from ert.gui.plotting.widgets.plot_controls.color_chooser import ColorBox
8+
from ert.gui.utils import log_once
99

1010
logger = logging.getLogger(__name__)
1111

@@ -23,8 +23,10 @@ def __init__(
2323
self._observations_color_box.colorChanged.connect(
2424
lambda _color: connection_point()
2525
)
26-
log_plot_option_usage_once(
27-
self._observations_color_box.colorChanged, logger, "Observation color"
26+
log_once(
27+
self._observations_color_box.colorChanged,
28+
logger,
29+
"Plot sidebar option used: 'Observation color'",
2830
)
2931

3032
layout = QHBoxLayout(self)

src/ert/gui/plotting/widgets/plot_controls/plot_color_palette_selector.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from PyQt6.QtWidgets import QComboBox, QPushButton
66

77
from ert.gui.icon_utils import load_icon
8-
from ert.gui.plotting.utils.logging_utils import log_plot_option_usage_once
98
from ert.gui.plotting.utils.plot_color_palettes import PALETTES_WITH_DESCRIPTIONS
109
from ert.gui.plotting.widgets.plot_controls import CustomPaletteDialog
10+
from ert.gui.utils import log_once
1111

1212
logger = logging.getLogger(__name__)
1313

@@ -35,7 +35,7 @@ def __init__(self, connection_point: Callable[..., object]) -> None:
3535
Qt.ItemDataRole.ToolTipRole,
3636
)
3737
self.activated.connect(connection_point)
38-
log_plot_option_usage_once(self.activated, logger, "Color palette")
38+
log_once(self.activated, logger, "Plot sidebar option used: 'Color palette'")
3939
self.custom_palette_button = QPushButton(
4040
load_icon("add_circle_outlined.svg"), "Create custom palette"
4141
)
@@ -44,8 +44,10 @@ def __init__(self, connection_point: Callable[..., object]) -> None:
4444
"The selected palette will be applied to all plots."
4545
)
4646
self.custom_palette_button.clicked.connect(self._open_custom_palette_dialog)
47-
log_plot_option_usage_once(
48-
self.custom_palette_button.clicked, logger, "Create custom palette"
47+
log_once(
48+
self.custom_palette_button.clicked,
49+
logger,
50+
"Plot sidebar option used: 'Create custom palette'",
4951
)
5052

5153
def _open_custom_palette_dialog(self) -> None:

src/ert/gui/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import logging
2+
3+
from PyQt6.QtCore import pyqtBoundSignal
14
from PyQt6.QtWidgets import QApplication
25

36
LEGEND_THRESHOLD = 5
@@ -29,3 +32,18 @@ def truncate_dropdown_item(text: str) -> str:
2932

3033
def truncate_experiment_name(name: str) -> str:
3134
return truncate_string(name, LONGEST_DEFAULT_EXPERIMENT_NAME)
35+
36+
37+
def log_once(
38+
signal: pyqtBoundSignal,
39+
logger: logging.Logger,
40+
message: str,
41+
level: int = logging.INFO,
42+
) -> None:
43+
"""Log a message the first time a signal is emitted, then disconnect."""
44+
45+
def log_and_disconnect(*_signal_args: object) -> None:
46+
logger.log(level, message)
47+
signal.disconnect(log_and_disconnect)
48+
49+
signal.connect(log_and_disconnect)

0 commit comments

Comments
 (0)