Skip to content

Commit

Permalink
Merge pull request #3 from AbbyGi/fixes
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
AbbyGi authored Oct 22, 2024
2 parents afa852e + 2118fef commit 68be72c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
58 changes: 32 additions & 26 deletions PyMca5/PyMcaGui/io/TiledCatalogSelector.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(
_logger.debug("TiledCatalogSelector.__init__()...")

self._url = url
self.client = client
self._client = client
self.validators = defaultdict(list)
if validators:
self.validators.update(validators)
Expand All @@ -84,10 +84,38 @@ def url(self, value: str):
Emits the 'url_changed' signal.
"""
old_value = self._url
self._url = value
self._url_buffer = value
# TODO: Perhaps this should only emit if the value is changed?
self.url_changed.emit()
if value != old_value:
self.url_changed.emit()

@property
def client(self):
return self._client

@client.setter
def client(self, _):
raise NotImplementedError("Call set_root_client() instead")

def set_root_client(self) -> None:
"""Update the model with content from the new root client.
Emits the 'table_changed' signal when a client is defined."""
try:
new_client = self.client_from_url(self.url)
except Exception as exception:
error_message = str(exception)
_logger.error(error_message)
self.client_connection_error.emit(error_message)
return

self._client = new_client
self.client_connected.emit(self._client.uri, str(self._client.context.api_uri))
self.node_path_parts = ()
self._current_page = 0
if self.client is not None:
self.table_changed.emit(self.node_path_parts)

def on_url_text_edited(self, new_text: str):
"""Handle a notification that the URL is being edited."""
Expand Down Expand Up @@ -116,33 +144,11 @@ def on_connect_clicked(self, checked: bool = False):
"""Handle a button click to connect to the Tiled client."""
_logger.debug("TiledCatalogSelector.on_connect_clicked()...")

try:
new_client = self.client_from_url(self.url)
except Exception as exception:
error_message = str(exception)
_logger.error(error_message)
self.client_connection_error.emit(error_message)
return

if self.client:
# TODO: Clean-up previously connected client?
...

# TODO: These steps might move to a property: @client.setter
self.client = new_client
self.client_connected.emit(self.client.uri, str(self.client.context.api_uri))
self.set_root_client(self.client)

def set_root_client(self, client: BaseClient) -> None:
"""Update the model with content from the new root client.
Emits the 'table_changed' signal when a client is defined."""
# TODO: Should probably use self.client rather than a func parameter
self.client = client
self.node_path_parts = ()
self._current_page = 0
if client is not None:
self.table_changed.emit(self.node_path_parts)
self.set_root_client()

def get_current_node(self) -> BaseClient:
"""Fetch a Tiled client corresponding to the current node path."""
Expand Down
12 changes: 9 additions & 3 deletions PyMca5/tests/ut/test_tiled_catalog_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ def test_on_url_text_edited():
assert model.url == expected_url


def test_on_url_editing_finished():
@pytest.mark.parametrize(
"expected_url, expected_emit",
(
("after", "assert_called_once_with"),
("before", "assert_not_called"),
)
)
def test_on_url_editing_finished(expected_url: str, expected_emit: str):
"""Event handler replaces the existing url with the contents of the buffer."""
expected_url = "after"
model = TiledCatalogSelector(url="before")
# Behave as if the user had edited the url in the dialog widget
# Behave as if the user had edited the url in the dialog widget
Expand All @@ -72,7 +78,7 @@ def test_on_url_editing_finished():
assert model.url == expected_url
assert model._url_buffer == expected_url

mock_signal.emit.assert_called_once_with()
getattr(mock_signal.emit, expected_emit)()


def test_on_connect_clicked():
Expand Down

0 comments on commit 68be72c

Please sign in to comment.