Skip to content
Merged
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
37 changes: 23 additions & 14 deletions ansys/hps/client/jms/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,29 @@ def get_object(
)


def _check_object_types(objects: List[Object], obj_type: Type[Object]):

are_same = [isinstance(o, obj_type) for o in objects]
if not all(are_same):
actual_types = set([type(o) for o in objects])
if len(actual_types) == 1:
actual_types = actual_types.pop()
raise ClientError(f"Wrong object types: expected '{obj_type}', got {actual_types}.")


def create_objects(
session: Session, url: str, objects: List[Object], as_objects=True, **query_params
session: Session,
url: str,
objects: List[Object],
obj_type: Type[Object],
as_objects=True,
**query_params,
):
if not objects:
return []

are_same = [o.__class__ == objects[0].__class__ for o in objects[1:]]
if not all(are_same):
raise ClientError("Mixed object types")
_check_object_types(objects, obj_type)

obj_type = objects[0].__class__
rest_name = obj_type.Meta.rest_name

url = f"{url}/{rest_name}"
Expand All @@ -87,12 +99,10 @@ def update_objects(
**query_params,
):

if objects is None:
raise ClientError("objects can't be None")
if not objects:
return []

are_same = [o.__class__ == obj_type for o in objects]
if not all(are_same):
raise ClientError("Mixed object types")
_check_object_types(objects, obj_type)

rest_name = obj_type.Meta.rest_name

Expand All @@ -109,13 +119,12 @@ def update_objects(
return schema.load(data)


def delete_objects(session: Session, url: str, objects: List[Object]):
def delete_objects(session: Session, url: str, objects: List[Object], obj_type: Type[Object]):

if not objects:
return

are_same = [o.__class__ == objects[0].__class__ for o in objects[1:]]
if not all(are_same):
raise ClientError("Mixed object types")
_check_object_types(objects, obj_type)

obj_type = objects[0].__class__
rest_name = obj_type.Meta.rest_name
Expand Down
11 changes: 9 additions & 2 deletions ansys/hps/client/jms/api/jms_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,14 @@ def create_task_definition_templates(
templates (list of :class:`ansys.hps.client.jms.TaskDefinitionTemplate`):
A list of task definition templates
"""
return create_objects(self.client.session, self.url, templates, as_objects, **query_params)
return create_objects(
self.client.session,
self.url,
templates,
TaskDefinitionTemplate,
as_objects,
**query_params,
)

def update_task_definition_templates(
self, templates: List[TaskDefinitionTemplate], as_objects=True, **query_params
Expand All @@ -156,7 +163,7 @@ def delete_task_definition_templates(self, templates: List[TaskDefinitionTemplat
templates (list of :class:`ansys.hps.client.jms.TaskDefinitionTemplate`):
A list of task definition templates
"""
return delete_objects(self.client.session, self.url, templates)
return delete_objects(self.client.session, self.url, templates, TaskDefinitionTemplate)

def copy_task_definition_templates(
self, templates: List[TaskDefinitionTemplate], wait: bool = True
Expand Down
46 changes: 25 additions & 21 deletions ansys/hps/client/jms/api/project_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def update_files(self, files: List[File], as_objects=True):
return update_files(self, files, as_objects=as_objects)

def delete_files(self, files: List[File]):
return self._delete_objects(files)
return self._delete_objects(files, File)

def download_file(
self,
Expand All @@ -163,17 +163,17 @@ def get_parameter_definitions(
return self._get_objects(ParameterDefinition, as_objects, **query_params)

def create_parameter_definitions(
self, parameter_definitions, as_objects=True
self, parameter_definitions: List[ParameterDefinition], as_objects=True
) -> List[ParameterDefinition]:
return self._create_objects(parameter_definitions, as_objects)
return self._create_objects(parameter_definitions, ParameterDefinition, as_objects)

def update_parameter_definitions(
self, parameter_definitions: List[ParameterDefinition], as_objects=True
) -> List[ParameterDefinition]:
return self._update_objects(parameter_definitions, ParameterDefinition, as_objects)

def delete_parameter_definitions(self, parameter_definitions: List[ParameterDefinition]):
return self._delete_objects(parameter_definitions)
return self._delete_objects(parameter_definitions, ParameterDefinition)

################################################################
# Parameter mappings
Expand All @@ -183,15 +183,15 @@ def get_parameter_mappings(self, as_objects=True, **query_params) -> List[Parame
def create_parameter_mappings(
self, parameter_mappings: List[ParameterMapping], as_objects=True
) -> List[ParameterMapping]:
return self._create_objects(parameter_mappings, as_objects=as_objects)
return self._create_objects(parameter_mappings, ParameterMapping, as_objects=as_objects)

def update_parameter_mappings(
self, parameter_mappings: List[ParameterMapping], as_objects=True
) -> List[ParameterMapping]:
return self._update_objects(parameter_mappings, ParameterMapping, as_objects=as_objects)

def delete_parameter_mappings(self, parameter_mappings: List[ParameterMapping]):
return self._delete_objects(parameter_mappings)
return self._delete_objects(parameter_mappings, ParameterMapping)

################################################################
# Task definitions
Expand All @@ -201,15 +201,15 @@ def get_task_definitions(self, as_objects=True, **query_params) -> List[TaskDefi
def create_task_definitions(
self, task_definitions: List[TaskDefinition], as_objects=True
) -> List[TaskDefinition]:
return self._create_objects(task_definitions, as_objects=as_objects)
return self._create_objects(task_definitions, TaskDefinition, as_objects=as_objects)

def update_task_definitions(
self, task_definitions: List[TaskDefinition], as_objects=True
) -> List[TaskDefinition]:
return self._update_objects(task_definitions, TaskDefinition, as_objects=as_objects)

def delete_task_definitions(self, task_definitions: List[TaskDefinition]):
return self._delete_objects(task_definitions)
return self._delete_objects(task_definitions, TaskDefinition)

def copy_task_definitions(
self, task_definitions: List[TaskDefinition], wait: bool = True
Expand Down Expand Up @@ -242,15 +242,15 @@ def get_job_definitions(self, as_objects=True, **query_params) -> List[JobDefini
def create_job_definitions(
self, job_definitions: List[JobDefinition], as_objects=True
) -> List[JobDefinition]:
return self._create_objects(job_definitions, as_objects=as_objects)
return self._create_objects(job_definitions, JobDefinition, as_objects=as_objects)

def update_job_definitions(
self, job_definitions: List[JobDefinition], as_objects=True
) -> List[JobDefinition]:
return self._update_objects(job_definitions, JobDefinition, as_objects=as_objects)

def delete_job_definitions(self, job_definitions: List[JobDefinition]):
return self._delete_objects(job_definitions)
return self._delete_objects(job_definitions, JobDefinition)

def copy_job_definitions(
self, job_definitions: List[JobDefinition], wait: bool = True
Expand Down Expand Up @@ -290,7 +290,7 @@ def create_jobs(self, jobs: List[Job], as_objects=True) -> List[Job]:
Returns:
List of :class:`ansys.hps.client.jms.Job` or list of dict if `as_objects` is False
"""
return self._create_objects(jobs, as_objects=as_objects)
return self._create_objects(jobs, Job, as_objects=as_objects)

def copy_jobs(self, jobs: List[Job], wait: bool = True) -> Union[str, List[str]]:
"""Create new jobs by copying existing ones
Expand Down Expand Up @@ -342,7 +342,7 @@ def delete_jobs(self, jobs: List[Job]):
>>> project_api.delete_jobs(jobs_to_delete)

"""
return self._delete_objects(jobs)
return self._delete_objects(jobs, Job)

def sync_jobs(self, jobs: List[Job]):
return sync_jobs(self, jobs)
Expand Down Expand Up @@ -372,29 +372,29 @@ def get_job_selections(self, as_objects=True, **query_params) -> List[JobSelecti
def create_job_selections(
self, selections: List[JobSelection], as_objects=True
) -> List[JobSelection]:
return self._create_objects(selections, as_objects=as_objects)
return self._create_objects(selections, JobSelection, as_objects=as_objects)

def update_job_selections(
self, selections: List[JobSelection], as_objects=True
) -> List[JobSelection]:
return self._update_objects(selections, JobSelection, as_objects=as_objects)

def delete_job_selections(self, selections: List[JobSelection]):
return self._delete_objects(selections)
return self._delete_objects(selections, JobSelection)

################################################################
# Algorithms
def get_algorithms(self, as_objects=True, **query_params) -> List[Algorithm]:
return self._get_objects(Algorithm, as_objects=as_objects, **query_params)

def create_algorithms(self, algorithms: List[Algorithm], as_objects=True) -> List[Algorithm]:
return self._create_objects(algorithms, as_objects=as_objects)
return self._create_objects(algorithms, Algorithm, as_objects=as_objects)

def update_algorithms(self, algorithms: List[Algorithm], as_objects=True) -> List[Algorithm]:
return self._update_objects(algorithms, Algorithm, as_objects=as_objects)

def delete_algorithms(self, algorithms: List[Algorithm]):
return self._delete_objects(algorithms)
return self._delete_objects(algorithms, Algorithm)

################################################################
# Permissions
Expand Down Expand Up @@ -466,8 +466,12 @@ def copy_default_execution_script(self, filename: str) -> File:
def _get_objects(self, obj_type: Object, as_objects=True, **query_params):
return get_objects(self.client.session, self.url, obj_type, as_objects, **query_params)

def _create_objects(self, objects: List[Object], as_objects=True, **query_params):
return create_objects(self.client.session, self.url, objects, as_objects, **query_params)
def _create_objects(
self, objects: List[Object], obj_type: Type[Object], as_objects=True, **query_params
):
return create_objects(
self.client.session, self.url, objects, obj_type, as_objects, **query_params
)

def _update_objects(
self, objects: List[Object], obj_type: Type[Object], as_objects=True, **query_params
Expand All @@ -476,8 +480,8 @@ def _update_objects(
self.client.session, self.url, objects, obj_type, as_objects, **query_params
)

def _delete_objects(self, objects: List[Object]):
delete_objects(self.client.session, self.url, objects)
def _delete_objects(self, objects: List[Object], obj_type: Type[Object]):
delete_objects(self.client.session, self.url, objects, obj_type)


def _download_files(project_api: ProjectApi, files: List[File]):
Expand Down Expand Up @@ -534,7 +538,7 @@ def _upload_files(project_api: ProjectApi, files):
def create_files(project_api: ProjectApi, files, as_objects=True) -> List[File]:
# (1) Create file resources in JMS
created_files = create_objects(
project_api.client.session, project_api.url, files, as_objects=as_objects
project_api.client.session, project_api.url, files, File, as_objects=as_objects
)

# (2) Check if there are src properties, files to upload
Expand Down
4 changes: 2 additions & 2 deletions ansys/hps/client/rms/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ def update_objects(
**query_params,
):

if objects is None:
raise ClientError("objects can't be None")
if not objects:
return []

are_same = [o.__class__ == obj_type for o in objects]
if not all(are_same):
Expand Down
29 changes: 14 additions & 15 deletions examples/python_multi_process_step/project_setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Project setup script for multi process step and task file replacement testing.
Project setup script for multi steps (task definitions) and task file replacement testing.

Author(s): R.Walker

Expand All @@ -25,8 +25,6 @@
import os
import random

from task_files import update_task_files

from ansys.hps.client import Client, HPSError
from ansys.hps.client.jms import (
File,
Expand All @@ -44,6 +42,8 @@
TaskDefinition,
)

from .task_files import update_task_files

log = logging.getLogger(__name__)


Expand All @@ -57,8 +57,8 @@ def main(
change_job_tasks,
inactive,
sequential,
):
"""Python project implementing multiple process steps and optional image generation."""
) -> Project:
"""Python project implementing multiple steps and optional image generation."""
log.debug("=== Project")
name = f"Python - {num_task_definitions} Task Defs {' - Img' if images else ''}"
name += f"{' - Sequential' if sequential else ' - Parallel'}"
Expand Down Expand Up @@ -136,23 +136,20 @@ def main(
params = []
mappings = []
for i in range(num_task_definitions):
int_params = [
new_params = [
IntParameterDefinition(name=f"period{i}", lower_limit=1, upper_limit=period, units="s"),
IntParameterDefinition(
name=f"duration{i}", lower_limit=0, upper_limit=duration, units="s"
),
IntParameterDefinition(name=f"steps{i}", units=""),
]
str_params = [
StringParameterDefinition(
name=f"color{i}",
value_list=["red", "blue", "green", "yellow", "cyan"],
default='"orange"',
),
]
int_params = project_api.create_parameter_definitions(int_params)
str_params = project_api.create_parameter_definitions(str_params)
params.extend(int_params + str_params)
new_params = project_api.create_parameter_definitions(new_params)
params.extend(new_params)

input_file_id = file_ids[f"td{i}_input"]
result_file_id = file_ids[f"td{i}_results_json"]
Expand All @@ -161,23 +158,23 @@ def main(
ParameterMapping(
key_string='"period"',
tokenizer=":",
parameter_definition_id=int_params[0].id,
parameter_definition_id=new_params[0].id,
file_id=input_file_id,
)
)
mappings.append(
ParameterMapping(
key_string='"duration"',
tokenizer=":",
parameter_definition_id=int_params[1].id,
parameter_definition_id=new_params[1].id,
file_id=input_file_id,
)
)
mappings.append(
ParameterMapping(
key_string='"steps"',
tokenizer=":",
parameter_definition_id=int_params[2].id,
parameter_definition_id=new_params[2].id,
file_id=result_file_id,
)
)
Expand All @@ -186,7 +183,7 @@ def main(
key_string='"color"',
tokenizer=":",
string_quote='"',
parameter_definition_id=str_params[0].id,
parameter_definition_id=new_params[3].id,
file_id=input_file_id,
)
)
Expand Down Expand Up @@ -260,6 +257,8 @@ def main(

log.info(f"Created project '{proj.name}', ID='{proj.id}'")

return proj


if __name__ == "__main__":

Expand Down
Loading