-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1054 from mantidproject/1052_operations_docs
User friendly docs for operations
- Loading branch information
Showing
30 changed files
with
250 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# Copyright (C) 2021 ISIS Rutherford Appleton Laboratory UKRI | ||
# SPDX - License - Identifier: GPL-3.0-or-later | ||
|
||
from typing import List | ||
import inspect | ||
|
||
from docutils import nodes | ||
from docutils.nodes import Node | ||
from docutils.statemachine import ViewList | ||
from docutils.parsers.rst import Directive | ||
from sphinx.util.nodes import nested_parse_with_titles | ||
"""Custom extension to add nicely formatted documentation for the operations. | ||
Use: | ||
.. operations_user_doc:: | ||
in a documentation rst file to generate. | ||
""" | ||
|
||
|
||
def make_heading(s: str, char: str) -> List[str]: | ||
return [s, len(s) * char, ""] | ||
|
||
|
||
def split_lines(s: str) -> List[str]: | ||
s = s.replace("\n\n", "DOUBLE_NEW_LINE") | ||
s = s.replace("\n", " ") | ||
s = s.replace("DOUBLE_NEW_LINE", "\n\n") | ||
return s.split("\n") | ||
|
||
|
||
PARAM_SKIP_LIST = ["images", "cores", "chunksize", "progress"] | ||
|
||
|
||
def get_params(s: str) -> List[str]: | ||
ret = [] | ||
for line in s.split("\n"): | ||
if line.strip().startswith(":param"): | ||
param_name = line.strip().split()[1].strip(':') | ||
if param_name in PARAM_SKIP_LIST: | ||
continue | ||
ret.append(line.strip()) | ||
elif line.strip().startswith(":return"): | ||
pass | ||
elif line.strip() and ret: | ||
ret[-1] = ret[-1] + " " + line.strip() | ||
|
||
return ret | ||
|
||
|
||
class OperationsUserDoc(Directive): | ||
def run(self) -> List[Node]: | ||
|
||
try: | ||
from mantidimaging.core.operations.loader import load_filter_packages | ||
except ImportError: | ||
raise ValueError("operations_user_doc could not import load_filter_packages") | ||
|
||
rst_lines = [] | ||
|
||
operations = load_filter_packages() | ||
for op in operations: | ||
# Title | ||
rst_lines += make_heading(op.filter_name, "-") | ||
|
||
# Description from class doc string | ||
rst_lines += split_lines(inspect.cleandoc(op.__doc__)) | ||
rst_lines.append("") | ||
|
||
# parameters from filter_func | ||
if op.filter_func.__doc__ is not None: | ||
rst_lines += get_params(op.filter_func.__doc__) | ||
rst_lines.append("") | ||
|
||
rst_lines.append(f":class:`{op.filter_name} API docs<{op.__module__}>`") | ||
rst_lines.append("") | ||
|
||
rst = ViewList() | ||
for n, rst_line in enumerate(rst_lines): | ||
rst.append(rst_line, "generated.rst", n) | ||
|
||
node = nodes.section() | ||
node.document = self.state.document | ||
|
||
nested_parse_with_titles(self.state, rst, node) | ||
|
||
return node.children | ||
|
||
|
||
def setup(app): | ||
app.add_directive("operations_user_doc", OperationsUserDoc) | ||
|
||
return { | ||
'version': '0.1', | ||
'parallel_read_safe': True, | ||
'parallel_write_safe': True, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,26 @@ | ||
# Copyright (C) 2021 ISIS Rutherford Appleton Laboratory UKRI | ||
# SPDX - License - Identifier: GPL-3.0-or-later | ||
|
||
from typing import Optional | ||
|
||
from PyQt5.QtCore import QUrl | ||
from PyQt5.QtGui import QDesktopServices | ||
|
||
DOCS_BASE = "https://mantidproject.github.io/mantidimaging" | ||
SECTION_API = f"{DOCS_BASE}/api/" | ||
SECTION_USER_GUIDE = f"{DOCS_BASE}/user_guide/" | ||
|
||
|
||
def open_api_webpage(page_url: str): | ||
open_help_webpage(SECTION_API, page_url) | ||
def open_user_operation_docs(operation_name: str): | ||
page_url = "operations/index" | ||
section = operation_name.lower().replace(" ", "-") | ||
open_help_webpage(SECTION_USER_GUIDE, page_url, section) | ||
|
||
|
||
def open_help_webpage(section_url: str, page_url: str, section: Optional[str] = None): | ||
if section is not None: | ||
url = f"{section_url}{page_url}.html#{section}" | ||
else: | ||
url = f"{section_url}{page_url}.html" | ||
|
||
def open_help_webpage(section_url: str, page_url: str): | ||
url = QUrl(f"{section_url}{page_url}.html") | ||
if not QDesktopServices.openUrl(url): | ||
raise RuntimeError(f"Url could not be opened: {url.toString()}") | ||
if not QDesktopServices.openUrl(QUrl(url)): | ||
raise RuntimeError(f"Url could not be opened: {url}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Copyright (C) 2021 ISIS Rutherford Appleton Laboratory UKRI | ||
# SPDX - License - Identifier: GPL-3.0-or-later |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Copyright (C) 2021 ISIS Rutherford Appleton Laboratory UKRI | ||
# SPDX - License - Identifier: GPL-3.0-or-later | ||
|
||
import unittest | ||
from unittest import mock | ||
from PyQt5.QtCore import QUrl | ||
|
||
from mantidimaging.core.net.help_pages import open_user_operation_docs, open_help_webpage, SECTION_USER_GUIDE | ||
|
||
|
||
class HelpPagesTest(unittest.TestCase): | ||
@mock.patch("mantidimaging.core.net.help_pages.open_help_webpage") | ||
def test_open_user_operation_docs(self, open_func: mock.Mock): | ||
open_user_operation_docs("Crop Coordinates") | ||
open_func.assert_called_with(SECTION_USER_GUIDE, "operations/index", "crop-coordinates") | ||
|
||
@mock.patch("mantidimaging.core.net.help_pages.QDesktopServices.openUrl") | ||
def test_open_help_webpage(self, open_url: mock.Mock): | ||
open_help_webpage(SECTION_USER_GUIDE, "reconstructions/center_of_rotation") | ||
expected = QUrl( | ||
"https://mantidproject.github.io/mantidimaging/user_guide/reconstructions/center_of_rotation.html") | ||
open_url.assert_called_with(expected) | ||
|
||
@mock.patch("mantidimaging.core.net.help_pages.QDesktopServices.openUrl") | ||
def test_open_help_webpage_with_section(self, open_url: mock.Mock): | ||
open_help_webpage(SECTION_USER_GUIDE, "operations/index", "crop-coordinates") | ||
expected = QUrl( | ||
"https://mantidproject.github.io/mantidimaging/user_guide/operations/index.html#crop-coordinates") | ||
open_url.assert_called_with(expected) | ||
|
||
@mock.patch("mantidimaging.core.net.help_pages.QDesktopServices.openUrl") | ||
def test_open_help_webpage_error(self, open_url: mock.Mock): | ||
open_url.return_value = False | ||
self.assertRaises(RuntimeError, open_help_webpage, SECTION_USER_GUIDE, "reconstructions/center_of_rotation") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.