Skip to content

Commit

Permalink
cleanup and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
searscr committed Apr 26, 2024
1 parent 515e524 commit 501e523
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 156 deletions.
3 changes: 3 additions & 0 deletions conda.recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ requirements:

run:
- python
- pyoncat
- oauthlib
- mantidqt

about:
home: {{ url }}
Expand Down
14 changes: 10 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ file = "src/pyoncatqt/_version.py"

[tool.setuptools.packages.find]
where = ["src"]
exclude = ["tests*", "docs*"]
exclude = ["tests*", "docs*", "scripts*"]

[project.gui-scripts]
pyoncatqt = "pyoncatqt.version:get_version"

[tool.setuptools.package-data]
"*" = ["*.yml","*.yaml","*.ini"]

[project.gui-scripts]
pyoncatqt = "pyoncatqt.pyoncatqt:gui"

[tool.pytest.ini_options]
pythonpath = [".", "src"]
testpaths = ["tests"]
Expand All @@ -48,7 +48,13 @@ norecursedirs = [".git", "tmp*", "_tmp*", "__pycache__", "*dataset*", "*data_set
exclude_lines = [
"except ImportError:",
"except ModuleNotFoundError:",
"except pyoncat.LoginRequiredError:",
"except pyoncat.InvalidRefreshTokenError:",
"except Exception:",
"except json.JSONDecodeError:",
"except KeyError:",
]
omit = ["src/pyoncatqt/_version.py"]

[tool.ruff]
line-length = 120
Expand Down
14 changes: 9 additions & 5 deletions src/pyoncatqt/mainwindow.py → scripts/sample_usage.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
"""
Main Qt window
"""

from qtpy.QtWidgets import QLabel, QListWidget, QVBoxLayout, QWidget
import sys

from pyoncatqt.login import ONCatLogin
from qtpy.QtWidgets import QApplication, QLabel, QListWidget, QVBoxLayout, QWidget


class MainWindow(QWidget):
Expand Down Expand Up @@ -47,3 +44,10 @@ def update_instrument_lists(self: QWidget, is_connected: bool) -> None:

for instrument in hfir_instruments:
self.hfir_list.addItem(instrument.get("name"))


if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
15 changes: 0 additions & 15 deletions src/pyoncatqt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +0,0 @@
"""
Contains the entry point for the application
"""

try:
from ._version import __version__ # noqa F401
except ImportError:
__version__ = "unknown"


def PyONCatQt(): # noqa ANN201
"""This is needed for backward compatibility because mantid workbench does "from shiver import Shiver" """
from .pyoncatqt import PyONCatQt as pyoncatqt

return pyoncatqt()
3 changes: 2 additions & 1 deletion src/pyoncatqt/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from configparser import ConfigParser

# configuration settings file path
CONFIG_PATH_FILE = "src/pyoncatqt/configuration.ini"
config_dir = os.path.dirname(os.path.abspath(__file__))
CONFIG_PATH_FILE = os.path.join(config_dir, "configuration.ini")


def get_data(section: str, name: str = None) -> dict | str | bool | None:
Expand Down
1 change: 0 additions & 1 deletion src/pyoncatqt/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ def __init__(self: QDialog, agent: pyoncat.ONCat = None, parent: QWidget = None,
def show_message(self: QDialog, msg: str) -> None:
"""Will show a error dialog with the given message"""
self.error.showMessage(msg)
# self.error.exec_()

def update_button_status(self: QDialog) -> None:
"""Update the button status"""
Expand Down
44 changes: 0 additions & 44 deletions src/pyoncatqt/pyoncatqt.py

This file was deleted.

12 changes: 8 additions & 4 deletions src/pyoncatqt/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
Will fall back to a default packagename is not installed"""

try:
from ._version import __version__
except ModuleNotFoundError:
__version__ = "0.0.1"

def get_version() -> None:
"""Get the version of the package"""
try:
from ._version import __version__
except ModuleNotFoundError:
__version__ = "0.0.1"
print(f"pyoncatqt version: {__version__}")
4 changes: 3 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

@pytest.fixture(autouse=True)
def _config_path(monkeypatch: pytest.fixture) -> None:
monkeypatch.setattr(pyoncatqt.configuration, "CONFIG_PATH_FILE", "tests/data/configuration.ini")
test_dir = os.path.dirname(os.path.abspath(__file__))
configuration_path = os.path.join(test_dir, "data", "configuration.ini")
monkeypatch.setattr(pyoncatqt.configuration, "CONFIG_PATH_FILE", configuration_path)


@pytest.fixture(autouse=True)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_login_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ def dialog_completed() -> None:
qtbot.waitUntil(dialog_completed, timeout=5000)


def test_get_agent() -> None:
dialog = ONCatLogin(key="test")
dialog.agent = MagicMock()
assert dialog.get_agent_instance() == dialog.agent


def test_is_connected() -> None:
dialog = ONCatLogin(key="test")
mock_agent = MagicMock()
dialog.agent = mock_agent
mock_agent.Instrument.list.return_value = [{"name": "Instrument1"}, {"name": "Instrument2"}]
dialog.login_dialog = MagicMock()

assert dialog.is_connected
dialog.connect_to_oncat()
assert not dialog.login_dialog.exec_.called


def test_login_dialog_nominal(qtbot: pytest.fixture) -> None:
agent = MagicMock()
dialog = ONCatLoginDialog(agent=agent)
Expand All @@ -78,6 +96,7 @@ def test_login_dialog_no_password(qtbot: pytest.fixture) -> None:
dialog.show()
qtbot.wait(2000)
assert dialog.user_pwd.text() == ""
assert dialog.button_login.isEnabled() is False
qtbot.mouseClick(dialog.button_login, QtCore.Qt.LeftButton)
assert mock_agent.login.called_once_with(os.getlogin(), "")
assert dialog.show_message.called_once_with("A username and/or password was not provided when logging in.")
Expand Down
35 changes: 0 additions & 35 deletions tests/test_main_window.py

This file was deleted.

46 changes: 0 additions & 46 deletions tests/test_pyoncatqt_gui.py

This file was deleted.

12 changes: 12 additions & 0 deletions tests/test_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from unittest.mock import patch

import pytest
from pyoncatqt.version import get_version


@patch("pyoncatqt._version.__version__", "1.0.0")
def test_get_version_existing_version(capsys: patch) -> None:
"""Test get_version when _version module exists"""
get_version()
captured = capsys.readouterr()
assert captured.out.strip() == "pyoncatqt version: 1.0.0"

0 comments on commit 501e523

Please sign in to comment.