Skip to content

Commit 8215db3

Browse files
authored
Oncat connectivity status +configuration mechanism (#146)
* configuration mechanism for version update, oncat connectivity status * oncat sync update and test * update connection status on the startup * configuration tests updated for version field, conf file with version added * group attributed added to false, needed for latest mantid versions
1 parent f490bdd commit 8215db3

File tree

7 files changed

+110
-31
lines changed

7 files changed

+110
-31
lines changed

src/shiver/configuration.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from configparser import ConfigParser
99
from pathlib import Path
1010
from mantid.kernel import Logger
11+
from shiver.version import __version__ as current_version
1112

1213
logger = Logger("SHIVER")
1314

@@ -31,6 +32,7 @@ def __init__(self):
3132
self.config_file_path = CONFIG_PATH_FILE
3233
logger.information(f"{self.config_file_path} with be used")
3334

35+
version_update = None
3436
# if template conf file path exists
3537
if os.path.exists(self.template_file_path):
3638
# file does not exist create it from template
@@ -41,20 +43,32 @@ def __init__(self):
4143
shutil.copy2(self.template_file_path, self.config_file_path)
4244

4345
self.config = ConfigParser(allow_no_value=True, comment_prefixes="/")
46+
47+
# the file already exists, check the version
48+
self.config.read(self.config_file_path)
49+
config_version = get_data("software.info", "version")
50+
51+
# in case of missing version or version mismatch
52+
if not config_version or config_version != current_version:
53+
# update the whole configuration file and the version
54+
shutil.copy2(self.template_file_path, self.config_file_path)
55+
version_update = current_version
56+
4457
# parse the file
4558
try:
4659
self.config.read(self.config_file_path)
4760
# validate the file has the all the latest variables
48-
self.validate()
61+
self.validate(version_update)
4962
except ValueError as err:
5063
logger.error(str(err))
5164
logger.error(f"Problem with the file: {self.config_file_path}")
5265
else:
5366
logger.error(f"Template configuration file: {self.template_file_path} is missing!")
5467

55-
def validate(self):
68+
def validate(self, version=None):
5669
"""validates that the fields exist at the config_file_path and writes any missing fields/data
57-
using the template configuration file: configuration_template.ini as a guide"""
70+
using the template configuration file: configuration_template.ini as a guide
71+
if version is not None, the version value is set/updated in the configuration file"""
5872
template_config = ConfigParser(allow_no_value=True, comment_prefixes="/")
5973
template_config.read(self.template_file_path)
6074
for section in template_config.sections():
@@ -65,6 +79,9 @@ def validate(self):
6579

6680
for item in template_config.items(section):
6781
field, _ = item
82+
# if a new version is passed set that in the file
83+
if version and field == "version":
84+
self.config[section][field] = version
6885
if field not in self.config[section]:
6986
# copy the field
7087
self.config[section][field] = template_config[section][field]

src/shiver/configuration_template.ini

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#url to oncat portal
33
oncat_url = https://oncat.ornl.gov
44
#client id for on cat; it is unique for Shiver
5-
client_id = 99025bb3-ce06-4f4b-bcf2-36ebf925cd1d
5+
client_id = 46c478f0-a472-4551-9264-a937626d5fc2
66
#the flag (bool: True/False) indicates the location of the names of the datasets (notes/comments vs. sequence name)
77
use_notes = False
88

@@ -21,3 +21,8 @@ save_history = True
2121

2222
[global.other]
2323
help_url = https://neutrons.github.io/Shiver/GUI/
24+
25+
[software.info]
26+
#software default information
27+
#version is populated during the creation of the configuration file based on the current software's version
28+
version = 0.0.0

src/shiver/presenters/refine_ub.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ def __init__(self, ws, model, parent=None): # pylint: disable=super-init-not-ca
3838
self.ads_observer = WorkspaceDisplayADSObserver(self)
3939
self.presenter.refresh()
4040
self.container = self
41+
# set PeaksTableWorkspaceDisplay group equals to False for newer Mantid versions
42+
self.group = False
4143

4244
def emit_close(self):
4345
"""Handle closing"""

src/shiver/views/oncat.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ def __init__(self, parent=None):
5858
self.oncat_options_layout.addWidget(self.angle_target_label, 3, 0)
5959
self.oncat_options_layout.addWidget(self.angle_target, 3, 1)
6060

61-
self.oncat_login = ONCatLogin(key="shiver", parent=self)
61+
client_id = get_data("generate_tab.oncat", "client_id")
62+
self.oncat_login = ONCatLogin(key="shiver", client_id=client_id, parent=self)
6263
self.oncat_login.connection_updated.connect(self.connect_to_oncat)
6364
self.oncat_options_layout.addWidget(self.oncat_login, 4, 0, 1, 2)
6465

@@ -75,15 +76,6 @@ def __init__(self, parent=None):
7576
# Sync with remote
7677
self.sync_with_remote(refresh=True)
7778

78-
# Sync with remote every 60 seconds
79-
# NOTE: make the refresh interval configurable
80-
# in application settings
81-
self.update_connection_status_timer = QTimer()
82-
self.update_connection_status_timer.timeout.connect(
83-
self.sync_with_remote,
84-
)
85-
self.update_connection_status_timer.start(60_000)
86-
8779
# change in instrument should trigger update of
8880
# - IPTS
8981
# - dataset
@@ -94,6 +86,15 @@ def __init__(self, parent=None):
9486
# - dataset
9587
self.ipts.currentTextChanged.connect(self.update_datasets)
9688

89+
self.show_connection_status_briefly()
90+
91+
def show_connection_status_briefly(self):
92+
"""Show connection status for 5 seconds"""
93+
# connection status appears for 5 seconds only
94+
self.oncat_login.status_label.show()
95+
timer = QTimer()
96+
timer.singleShot(5000, self.oncat_login.status_label.hide)
97+
9798
@property
9899
def connected_to_oncat(self) -> bool:
99100
"""Check if connected to OnCat"""
@@ -135,6 +136,7 @@ def connect_to_oncat(self):
135136
"""Connect to OnCat"""
136137
# update connection status
137138
self.sync_with_remote(refresh=True)
139+
self.show_connection_status_briefly()
138140

139141
def sync_with_remote(self, refresh=False):
140142
"""Update all items within OnCat widget."""

tests/conftest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from mantidqt.gui_helper import set_matplotlib_backend
88
from mantid.simpleapi import mtd
99
from shiver import Shiver
10+
from shiver.version import __version__ as current_version
1011

1112
# make sure matplotlib is correctly set before we run tests
1213
set_matplotlib_backend()
@@ -39,6 +40,21 @@ def user_conf_file(tmp_path_factory, request):
3940
return user_path
4041

4142

43+
@pytest.fixture(scope="session")
44+
def user_conf_file_with_version(tmp_path_factory, request):
45+
"""Fixture to create a custom configuration file in tmp_path"""
46+
# custom configuration file
47+
user_config = ConfigParser(allow_no_value=True)
48+
# include current version
49+
version_block = f"[software.info]\nversion = {current_version}\n"
50+
config_data = request.param + version_block
51+
user_config.read_string(config_data)
52+
user_path = os.path.join(tmp_path_factory.mktemp("data"), "test_config.ini")
53+
with open(user_path, "w", encoding="utf8") as config_file:
54+
user_config.write(config_file)
55+
return user_path
56+
57+
4258
@pytest.fixture(autouse=True)
4359
def _get_login(monkeypatch: pytest.fixture) -> None:
4460
monkeypatch.setattr(os, "getlogin", lambda: "test")

tests/models/test_configuration.py

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from qtpy.QtWidgets import QApplication
99
from shiver.shiver import Shiver
1010
from shiver.configuration import Configuration, get_data
11+
from shiver.version import __version__ as current_version
1112

1213

1314
def test_config_path_default():
@@ -47,29 +48,29 @@ def test_config_path_does_not_exist(monkeypatch, tmp_path):
4748

4849

4950
@pytest.mark.parametrize(
50-
"user_conf_file",
51+
"user_conf_file_with_version",
5152
[
5253
"""
5354
[generate_tab.oncat]
5455
#url to oncat portal
5556
oncat_url = https://oncat.ornl.gov
5657
#client id for on cat; it is unique for Shiver
57-
client_id = 99025bb3-ce06-4f4b-bcf2-36ebf925cd1d
58+
client_id = 46c478f0-a472-4551-9264-a937626d5fc2
5859
5960
"""
6061
],
6162
indirect=True,
6263
)
63-
def test_field_validate_fields_exist(monkeypatch, user_conf_file):
64+
def test_field_validate_fields_exist(monkeypatch, user_conf_file_with_version):
6465
"""Test configuration validate all fields exist with the same values as templates
6566
Note: update the parameters if the fields increase"""
6667
# read the custom configuration file
67-
monkeypatch.setattr("shiver.configuration.CONFIG_PATH_FILE", user_conf_file)
68+
monkeypatch.setattr("shiver.configuration.CONFIG_PATH_FILE", user_conf_file_with_version)
6869
user_config = Configuration()
6970

70-
assert user_config.config_file_path.endswith(user_conf_file) is True
71+
assert user_config.config_file_path.endswith(user_conf_file_with_version) is True
7172
# check if the file exists
72-
assert os.path.exists(user_conf_file)
73+
assert os.path.exists(user_conf_file_with_version)
7374

7475
# check all fields are the same as the configuration template file
7576
project_directory = Path(__file__).resolve().parent.parent.parent
@@ -79,11 +80,14 @@ def test_field_validate_fields_exist(monkeypatch, user_conf_file):
7980
# comments should be copied too
8081
for section in user_config.config.sections():
8182
for field in user_config.config[section]:
82-
assert user_config.config[section][field] == template_config[section][field]
83+
if field != "version":
84+
assert user_config.config[section][field] == template_config[section][field]
85+
else:
86+
assert user_config.config[section][field] == current_version
8387

8488

8589
@pytest.mark.parametrize(
86-
"user_conf_file",
90+
"user_conf_file_with_version",
8791
[
8892
"""
8993
[generate_tab.oncat]
@@ -94,36 +98,40 @@ def test_field_validate_fields_exist(monkeypatch, user_conf_file):
9498
],
9599
indirect=True,
96100
)
97-
def test_field_validate_fields_same(monkeypatch, user_conf_file):
98-
"""Test configuration validate all fields exist with their values; different from the template"""
101+
def test_field_validate_fields_same(monkeypatch, user_conf_file_with_version):
102+
"""Test configuration validate all fields exist with their values; different from the template,
103+
provided the version is the same"""
99104

100105
# read the custom configuration file
101-
monkeypatch.setattr("shiver.configuration.CONFIG_PATH_FILE", user_conf_file)
106+
monkeypatch.setattr("shiver.configuration.CONFIG_PATH_FILE", user_conf_file_with_version)
102107
user_config = Configuration()
103108

104109
# check if the file exists
105-
assert os.path.exists(user_conf_file)
106-
assert user_config.config_file_path == user_conf_file
110+
assert os.path.exists(user_conf_file_with_version)
111+
assert user_config.config_file_path == user_conf_file_with_version
107112

108113
# check all field values have the same values as the user configuration file
109114
assert get_data("generate_tab.oncat", "oncat_url") == "test_url"
110115
assert get_data("generate_tab.oncat", "client_id") == "0000-0000"
111116
# cast to bool
112117
assert get_data("generate_tab.oncat", "use_notes") is True
118+
assert get_data("software.info", "version") == current_version
113119

114120

115121
@pytest.mark.parametrize(
116122
"user_conf_file",
117123
[
118124
"""
119125
[generate_tab.oncat]
126+
oncat_url = test_url
120127
client_id = 0000-0000
128+
use_notes = True
121129
"""
122130
],
123131
indirect=True,
124132
)
125-
def test_field_validate_fields_missing(monkeypatch, user_conf_file):
126-
"""Test configuration validate missing fields added from the template"""
133+
def test_field_validate_fields_update_different_version(monkeypatch, user_conf_file):
134+
"""Test configuration update all fields, provided the version is not the same"""
127135

128136
# read the custom configuration file
129137
monkeypatch.setattr("shiver.configuration.CONFIG_PATH_FILE", user_conf_file)
@@ -133,6 +141,34 @@ def test_field_validate_fields_missing(monkeypatch, user_conf_file):
133141
assert os.path.exists(user_conf_file)
134142
assert user_config.config_file_path == user_conf_file
135143

144+
# check all field values have the same values as the user configuration file
145+
assert get_data("generate_tab.oncat", "oncat_url") != "test_url"
146+
assert get_data("generate_tab.oncat", "client_id") != "0000-0000"
147+
# version is included with the current version
148+
assert get_data("software.info", "version") == current_version
149+
150+
151+
@pytest.mark.parametrize(
152+
"user_conf_file_with_version",
153+
[
154+
"""
155+
[generate_tab.oncat]
156+
client_id = 0000-0000
157+
"""
158+
],
159+
indirect=True,
160+
)
161+
def test_field_validate_fields_missing(monkeypatch, user_conf_file_with_version):
162+
"""Test configuration validate missing fields added from the template"""
163+
164+
# read the custom configuration file
165+
monkeypatch.setattr("shiver.configuration.CONFIG_PATH_FILE", user_conf_file_with_version)
166+
user_config = Configuration()
167+
168+
# check if the file exists
169+
assert os.path.exists(user_conf_file_with_version)
170+
assert user_config.config_file_path == user_conf_file_with_version
171+
136172
# check all field values have the same values as the user configuration file
137173
assert get_data("generate_tab.oncat", "oncat_url") == "https://oncat.ornl.gov"
138174
assert get_data("generate_tab.oncat", "client_id") == "0000-0000"
@@ -151,7 +187,7 @@ def test_get_data_valid(monkeypatch, user_conf_file):
151187
assert len(get_data("generate_tab.oncat", "")) == 3
152188
# fields
153189
assert get_data("generate_tab.oncat", "oncat_url") == "https://oncat.ornl.gov"
154-
assert get_data("generate_tab.oncat", "client_id") == "99025bb3-ce06-4f4b-bcf2-36ebf925cd1d"
190+
assert get_data("generate_tab.oncat", "client_id") == "46c478f0-a472-4551-9264-a937626d5fc2"
155191
assert get_data("generate_tab.oncat", "use_notes") is False
156192

157193
assert config.is_valid()

tests/views/test_oncat.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# pylint: disable=all
33
"""Test the views for the ONCat application."""
4-
from qtpy.QtWidgets import QGroupBox
4+
from qtpy.QtWidgets import QGroupBox, QLabel
55
from qtpy.QtCore import Signal
66
import pytest
77
from shiver.views.oncat import (
@@ -17,6 +17,7 @@ def test_oncat(monkeypatch, qtbot):
1717

1818
class MockLogin(QGroupBox):
1919
connection_updated = Signal(bool)
20+
status_label = QLabel("Test")
2021

2122
def __init__(self, *args, parent, **kwargs):
2223
super().__init__(parent=parent)

0 commit comments

Comments
 (0)