Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Login #2

Merged
merged 10 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ coverage.xml
.hypothesis/
.pytest_cache/
cover/
tests/data/token.json

# Translations
*.mo
Expand Down
2 changes: 1 addition & 1 deletion .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ python:

sphinx:
builder: html
configuration: docs/source/conf.py
configuration: docs/conf.py
fail_on_warning: true

conda:
Expand Down
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
21 changes: 21 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Purpose: Convenience scripts to simplify some common Sphinx operations, such as rendering the content.

# Minimal makefile for Sphinx documentation

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
13 changes: 12 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@
"sphinx.ext.githubpages",
"sphinx.ext.intersphinx",
"sphinx.ext.napoleon",
"sphinxcontrib.mermaid",
]

autodoc_mock_imports = [
"qtpy",
"qtpy.uic",
"qtpy.QtWidgets",
"mantidqt",
]

templates_path = ["_templates"]

# The suffix(es) of source filenames.
Expand Down Expand Up @@ -69,7 +78,9 @@
napoleon_google_docstring = False
napoleon_numpy_docstring = True

html_static_path = ["_static"]
# html_static_path = ["source/_static"]

html_theme_options = {"page_width": "75%"}

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = True
32 changes: 32 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.. Purpose: The root document of the project, which serves as welcome page and contains the root
.. of the "table of contents tree" (or toctree).

PyONCatQt Documentation
=======================

PyONCatQt is a Python package designed to provide a Qt-based GUI for authentication
and connection to the ONCat service, a data cataloging and management system developed
by Oak Ridge National Laboratory. It serves as a plugin for other applications,
offering a convenient PyQt-based GUI component,
ONCatLoginDialog, for securely inputting login credentials. Upon successful authentication,
the package establishes a connection to the ONCat service, granting access to various data
management functionalities. PyONCatQt aims to streamline the login process and enhance user
experience when interacting with ONCat within Python applications.

Getting Started
---------------
Install instructions coming soon.


.. note::

This project is under active development.

Contents
--------

.. toctree::
:maxdepth: 1

source/reference
source/sample
37 changes: 37 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
REM Purpose: Convenience scripts to simplify some common Sphinx operations, such as rendering the content.

@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=source
set BUILDDIR=build

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)

if "%1" == "" goto help

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
23 changes: 23 additions & 0 deletions docs/source/reference.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Reference
=========

PyONCatQt
---------

.. module:: PyONCatQt
.. automodule:: pyoncatqt.pyoncatqt
:members:

ONCatLoginDialog
----------------

.. module:: ONCatLoginDialog
.. automodule:: pyoncatqt.login.ONCatLoginDialog
:members:

ONCatLogin
----------

.. module:: ONCatLogin
.. automodule:: pyoncatqt.login.ONCatLogin
:members:
106 changes: 106 additions & 0 deletions docs/source/sample.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
Sample Usage
============

ONCatLoginDialog
----------------
The following is a simple example of how to This example demonstrates how to
integrate the `ONCatLoginDialog` into a PyQt application.

- The application consists of a single button labeled "Login to ONCat".
- When the button is clicked, it triggers the opening of the `ONCatLoginDialog`,
allowing the user to input their ONCat login credentials securely.
- Upon successful login, the dialog closes, and the application can proceed with its functionality,
utilizing the authenticated ONCat connection for data management tasks.

.. code:: python

class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
layout = QVBoxLayout()

# Create a button to open the ONCat login dialog
self.login_button = QPushButton("Login to ONCat")
self.login_button.clicked.connect(self.open_oncat_login_dialog)
layout.addWidget(self.login_button)
self.setLayout(layout)
self.setWindowTitle("ONCat Login Example")

# Create an instance of the pyoncat agent pyoncat.ONCat
self.agent = pyoncat.ONCat(
self.oncat_url,
client_id=self.client_id,
# Pass in token getter/setter callbacks here:
token_getter=self.read_token,
token_setter=self.write_token,
flow=pyoncat.RESOURCE_OWNER_CREDENTIALS_FLOW,
)

def open_oncat_login_dialog(self):
dialog = ONCatLoginDialog(agent=self.agent, parent=self)
dialog.exec_()

if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

ONCatLogin Widget
-----------------
The following is a simple example of how to use the ONCatLogin widget in a PyQt application.
This example creates a main window with an ONCatLogin widget and two QListWidgets to display
the instrument lists for the SNS and HFIR facilities. The instrument lists are updated when
the connection status changes.

.. code:: python

class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
layout = QVBoxLayout()

# Create and add the Oncat widget
self.oncat_widget = ONCatLogin(key="", parent=self)
self.oncat_widget.connection_updated.connect(self.update_instrument_lists)
layout.addWidget(self.oncat_widget)

# Add list widgets for the instrument lists
self.sns_list = QListWidget()
self.hfir_list = QListWidget()

layout.addWidget(QLabel("SNS Instruments:"))
layout.addWidget(self.sns_list)
layout.addWidget(QLabel("HFIR Instruments:"))
layout.addWidget(self.hfir_list)

self.setLayout(layout)
self.setWindowTitle("ONCat Application")
self.oncat_widget.update_connection_status()

def update_instrument_lists(self, is_connected):
"""Update the contents of the instrument lists based on the connection status."""
self.sns_list.clear()
self.hfir_list.clear()

if is_connected:
sns_instruments = self.oncat_widget.agent.Instrument.list(facility="SNS")
hfir_instruments = self.oncat_widget.agent.Instrument.list(facility="HFIR")

for instrument in sns_instruments:
self.sns_list.addItem(instrument.get("name"))

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_())
3 changes: 3 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ dependencies:
- python-build
- pytest
- pytest-cov
- pytest-qt
- setuptools
- sphinx
- sphinxcontrib-mermaid
- versioningit
28 changes: 18 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,37 @@ 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"]

# TODO: Define once the package is ready for distribution
#[project.scripts]
#packagename-cli = "packagenamepy.packagename:main"

#[project.gui-scripts]
#packagenamepy = "packagenamepy.packagename:gui"

[tool.pytest.ini_options]
pythonpath = [".", "src", "scripts"]
pythonpath = [".", "src"]
testpaths = ["tests"]
python_files = ["test*.py"]
norecursedirs = [".git", "tmp*", "_tmp*", "__pycache__", "*dataset*", "*data_set*"]

[tool.coverage.report]
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

[tool.ruff.lint]
# https://beta.ruff.rs/docs/rules/
select = ["A", "ARG", "BLE", "E", "F", "I", "PT"]
select = ["A", "ARG", "BLE", "E", "F", "I", "PT", "ANN"]
ignore = ["F403", "F405", "F401", # wild imports and unknown names
]
53 changes: 53 additions & 0 deletions scripts/sample_usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import sys

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


class MainWindow(QWidget):
"""Main widget"""

def __init__(self: QWidget, key: str = "shiver", parent: QWidget = None) -> None:
super().__init__(parent=parent)

layout = QVBoxLayout()

# Create and add the Oncat widget
self.oncat_widget = ONCatLogin(key=key, parent=self)
self.oncat_widget.connection_updated.connect(self.update_instrument_lists)
layout.addWidget(self.oncat_widget)

# Add text input boxes for wavelength and run number
self.sns_list = QListWidget()
self.hfir_list = QListWidget()

layout.addWidget(QLabel("SNS Instruments:"))
layout.addWidget(self.sns_list)
layout.addWidget(QLabel("HFIR Instruments:"))
layout.addWidget(self.hfir_list)

self.setLayout(layout)
self.setWindowTitle("ONCat Application")
self.oncat_widget.update_connection_status()

def update_instrument_lists(self: QWidget, is_connected: bool) -> None:
"""Update the contents of the instrument lists based on the connection status."""
self.sns_list.clear()
self.hfir_list.clear()

if is_connected:
sns_instruments = self.oncat_widget.agent.Instrument.list(facility="SNS")
hfir_instruments = self.oncat_widget.agent.Instrument.list(facility="HFIR")

for instrument in sns_instruments:
self.sns_list.addItem(instrument.get("name"))

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_())
Loading
Loading