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

List default execution scripts #399

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
29 changes: 28 additions & 1 deletion src/ansys/hps/client/jms/api/jms_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def __init__(self, client: Client):
"""Initialize JMS API."""
self.client = client
self._fs_url = None
self._api_info = None

@property
def url(self) -> str:
Expand All @@ -81,14 +82,26 @@ def fs_url(self) -> str:
self._fs_url = _find_available_fs_url(self.get_storage())
return self._fs_url

def get_api_info(self):
def get_api_info(self) -> dict:
"""Get information of the JMS API that the client is connected to.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Get information of the JMS API that the client is connected to.
"""Get information on the JMS API that the client is connected to.

"of the JMS API" didn't read well. Hope "on the JMS API" is OK.


Information includes the version and build date.
"""
r = self.client.session.get(self.url)
return r.json()

@property
def api_info(self) -> dict:
"""Information of the JMS API that the client is connected to."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Information of the JMS API that the client is connected to."""
"""Information on the JMS API that the client is connected to."""

if self._api_info is None:
self._api_info = self.get_api_info()
return self._api_info

@property
def execution_script_default_bucket(self) -> str:
"""Default bucket for execution scripts."""
return self.api_info["settings"]["execution_script_default_bucket"]

################################################################
# Projects
def get_projects(self, as_objects=True, **query_params) -> List[Project]:
Expand Down Expand Up @@ -245,6 +258,20 @@ def update_task_definition_template_permissions(
as_objects,
)

# Execution Scripts
def list_default_execution_scripts(self) -> list[str]:
"""List default execution scripts.

Returns a list of available default execution scripts that can be passed
as input to the :meth:`ProjectApi.copy_default_execution_script` method.
"""
r = self.client.session.get(f"{self.fs_url}/list/{self.execution_script_default_bucket}")
file_list = [
f.replace(f"ansfs://{self.execution_script_default_bucket}/", "")
for f in r.json()["file_list"]
]
return file_list

################################################################
# Operations
def get_operations(self, as_objects=True, **query_params) -> List[Operation]:
Expand Down
20 changes: 14 additions & 6 deletions src/ansys/hps/client/jms/api/project_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,20 @@ def __init__(self, client: Client, project_id: str):
self.project_id = project_id
self._fs_url = None
self._fs_project_id = None
self._jms_api = None

@property
def jms_api_url(self) -> str:
"""Get the JMS API URL."""
return f"{self.client.url}/jms/api/v1"

@property
def jms_api(self) -> JmsApi:
"""Get the JMS API object."""
if self._jms_api is None:
self._jms_api = JmsApi(self.client)
return self._jms_api

@property
def url(self) -> str:
"""URL of the API."""
Expand All @@ -119,7 +127,7 @@ def url(self) -> str:
def fs_url(self) -> str:
"""URL of the file storage gateway."""
if self._fs_url is None:
self._fs_url = JmsApi(self.client).fs_url
self._fs_url = self.jms_api.fs_url
return self._fs_url

@property
Expand Down Expand Up @@ -593,16 +601,18 @@ def delete_license_contexts(self):
"""Delete license contexts."""
rest_name = LicenseContext.Meta.rest_name
url = f"{self.jms_api_url}/projects/{self.id}/{rest_name}"
r = self.client.session.delete(url)
_ = self.client.session.delete(url)

################################################################
def copy_default_execution_script(self, filename: str) -> File:
"""Copy a default execution script to the current project.

Example:

>>> file = project_api.copy_default_execution_script("exec_mapdl.py")
>>> file = project_api.copy_default_execution_script("mapdl-v241-exec_mapdl.py")

You can use the method :meth:`JmsApi.list_default_execution_scripts` to query
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
You can use the method :meth:`JmsApi.list_default_execution_scripts` to query
You can use the :meth:`JmsApi.list_default_execution_scripts` method to query

Noun indicating the type of object should follow the tagged code entity.

the list of available default execution scripts.
"""

# create file resource
Expand All @@ -611,9 +621,7 @@ def copy_default_execution_script(self, filename: str) -> File:
file = self.create_files([file])[0]

# query location of default execution scripts from server
jms_api = JmsApi(self.client)
info = jms_api.get_api_info()
execution_script_default_bucket = info["settings"]["execution_script_default_bucket"]
execution_script_default_bucket = self.jms_api.execution_script_default_bucket

# server side copy of the file to project bucket
checksum = _fs_copy_file(
Expand Down
23 changes: 23 additions & 0 deletions tests/jms/test_jms_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import pytest

from ansys.hps.client import Client, ClientError, HPSError
from ansys.hps.client import __ansys_apps_version__ as ansys_version
from ansys.hps.client.jms import JmsApi, ProjectApi
from ansys.hps.client.jms.api.jms_api import _find_available_fs_url
from ansys.hps.client.jms.resource import (
Expand All @@ -53,6 +54,10 @@ def test_jms_api_info(client):
assert "settings" in info
assert "time" in info

assert jms_api._api_info is None
_ = jms_api.execution_script_default_bucket
assert jms_api._api_info is not None


def test_unavailable_fs_url(client):

Expand Down Expand Up @@ -233,3 +238,21 @@ def test_objects_type_check(client):
)

JmsApi(client).delete_project(proj)


def test_list_default_execution_scripts(client):

jms_api = JmsApi(client)
file_names = jms_api.list_default_execution_scripts()
assert len(file_names) > 0
for fn in file_names:
assert isinstance(fn, str)
assert len(fn) > 0

ansys_short_version = f"v{ansys_version[2:4]}{ansys_version[6]}"
for product_name in ["mapdl", "lsdyna", "fluent"]:
script_name = f"{product_name}-{ansys_short_version}-exec_{product_name}.py"
assert script_name in file_names

assert "mechanical-exec_mechanical.py" in file_names
assert "web-sample_execution_script.py" in file_names
4 changes: 4 additions & 0 deletions tests/jms/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ def test_copy_exec_script(client):
proj = jms_api.create_project(proj)

project_api = ProjectApi(client, proj.id)
assert project_api._jms_api is None

ansys_short_version = f"v{ansys_version[2:4]}{ansys_version[6]}"
script_name = f"mapdl-{ansys_short_version}-exec_mapdl"
file = project_api.copy_default_execution_script(f"{script_name}.py")
Expand All @@ -290,4 +292,6 @@ def test_copy_exec_script(client):
assert file.hash is not None
assert file.storage_id is not None

assert project_api._jms_api is not None

jms_api.delete_project(proj)
Loading