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
47 changes: 34 additions & 13 deletions generate_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@
allow code completion.
"""

from dataclasses import dataclass
import importlib
import os
from typing import List, Tuple

import marshmallow

from ansys.hps.client.common.restricted_value import RestrictedValue
from ansys.hps.client.jms.schema.object_reference import IdReference, IdReferenceList

# we define here which resources to auto-generate
# some are excluded or done only partially (e.g. File)
# some are excluded or done only partially (File and FitnessDefinition)
# because they require more customization
JMS_RESOURCES = [
{
Expand All @@ -51,23 +53,26 @@
"schema_filename": "file",
"rest_name": "files",
"additional_fields": [],
"class": "FileBase",
"class": "File",
"resource_filename": "file_base",
},
{
"schema": "FitnessDefinitionSchema",
"schema_filename": "fitness_definition",
"rest_name": None,
"additional_fields": [],
"class": "FitnessDefinitionBase",
"class": "FitnessDefinition",
"resource_filename": "fitness_definition_base",
"additional_imports": [
"from .fitness_term_definition_base import FitnessTermDefinition",
],
},
{
"schema": "FitnessTermDefinitionSchema",
"schema_filename": "fitness_definition",
"rest_name": None,
"additional_fields": [],
"class": "FitnessTermDefinitionBase",
"class": "FitnessTermDefinition",
"resource_filename": "fitness_term_definition_base",
},
{
Expand All @@ -85,6 +90,9 @@
"additional_fields": [],
"class": "JobDefinition",
"resource_filename": "job_definition",
"additional_imports": [
"from .fitness_definition import FitnessDefinition",
],
},
{
"schema": "LicenseContextSchema",
Expand Down Expand Up @@ -221,6 +229,7 @@
"additional_fields": [],
"class": "TaskDefinitionTemplate",
"resource_filename": "task_definition_template",
"additional_imports": ["from .task_definition import HpcResources, Software"],
},
{
"schema": "TaskSchema",
Expand All @@ -229,6 +238,7 @@
"additional_fields": [],
"class": "Task",
"resource_filename": "task",
"additional_imports": ["from .task_definition import TaskDefinition"],
},
{
"schema": "ParameterDefinitionSchema",
Expand Down Expand Up @@ -301,28 +311,35 @@
marshmallow.fields.Nested: "object",
IdReferenceList: "list[str]",
IdReference: "str",
RestrictedValue: "int | float | str | bool",
RestrictedValue: "Union[int, float, str, bool]",
}


def extract_field_info(name: str, field_object: marshmallow.fields, resources):
@dataclass
class Field:
name: str
type: str = "Any"

field = name

def extract_field_info(name: str, field_object: marshmallow.fields, resources) -> Tuple[Field, str]:

field = Field(name=name)
v = field_object

# Ensure that we use the attribute name if defined
if getattr(v, "attribute", None) is not None:
field = v.attribute
field.name = v.attribute

# build attribute doc
field_doc = f"{field}"
field_doc = f"{field.name}"

field_type = _extract_field_type(v, resources)
if field_type:
field_doc += f" : {field_type}"
if v.allow_none:
field_doc += ", optional"
field_doc += "\n"
field.type = field_type.replace("list", "List").replace("dict", "Dict")
elif v.allow_none:
field_doc += " : any, optional\n"
desc = v.metadata.get("description", None)
Expand Down Expand Up @@ -359,7 +376,7 @@ def _extract_field_type(v, resources) -> str:
return field_type


def declared_fields(schema, resources):
def declared_fields(schema, resources) -> Tuple[List[Field], List[str]]:
"""
Helper function to retrieve the fields that is defined as class members for an object
"""
Expand All @@ -377,11 +394,15 @@ def declared_fields(schema, resources):
def get_resource_imports(resource, base_class):

imports = [
"from datetime import datetime",
"from typing import List, Dict, Any, Union",
"from marshmallow.utils import missing",
"from ansys.hps.client.common import Object",
# f"from {base_class['path']}.{base_class['filename']} import {base_class['name']}",
f"from ..schema.{resource['schema_filename']} import {resource['schema']}",
]
if "additional_imports" in resource:
imports.extend(resource["additional_imports"])
return imports


Expand All @@ -405,12 +426,12 @@ def get_module_docstring(file_name: str) -> str:
return code


def get_resource_code(resource, base_class, fields, field_docs):
def get_resource_code(resource, base_class, fields: List[Field], field_docs: List[str]):

fields_str = ""
for k in fields:
fields_str += f" self.{k} = {k}\n"
init_fields_str = ",\n".join([f" {k}=missing" for k in fields])
fields_str += f" self.{k.name} = {k.name}\n"
init_fields_str = ",\n".join([f" {k.name}: {k.type} = missing" for k in fields])

additional_initialization = " self.obj_type = self.__class__.__name__"
if resource.get("init_with_kwargs", True):
Expand Down
15 changes: 9 additions & 6 deletions src/ansys/hps/client/auth/resource/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

# autogenerated code
"""Module providing the user resource."""
from datetime import datetime
from typing import Any, Dict, List, Union

from marshmallow.utils import missing

from ansys.hps.client.common import Object
Expand Down Expand Up @@ -54,12 +57,12 @@ class Meta:

def __init__(
self,
id=missing,
username=missing,
password=missing,
first_name=missing,
last_name=missing,
email=missing,
id: str = missing,
username: str = missing,
password: str = missing,
first_name: str = missing,
last_name: str = missing,
email: str = missing,
**kwargs
):
self.id = id
Expand Down
21 changes: 12 additions & 9 deletions src/ansys/hps/client/jms/resource/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

# autogenerated code
"""Module providing the algorithm resource."""
from datetime import datetime
from typing import Any, Dict, List, Union

from marshmallow.utils import missing

from ansys.hps.client.common import Object
Expand Down Expand Up @@ -60,15 +63,15 @@ class Meta:

def __init__(
self,
id=missing,
creation_time=missing,
modification_time=missing,
created_by=missing,
modified_by=missing,
name=missing,
description=missing,
data=missing,
jobs=missing,
id: str = missing,
creation_time: datetime = missing,
modification_time: datetime = missing,
created_by: str = missing,
modified_by: str = missing,
name: str = missing,
description: str = missing,
data: str = missing,
jobs: List[str] = missing,
**kwargs
):
self.id = id
Expand Down
40 changes: 22 additions & 18 deletions src/ansys/hps/client/jms/resource/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Module providing the file resource."""
from datetime import datetime
import io
from typing import Union

from marshmallow.utils import missing

from ansys.hps.client.common import Object
Expand Down Expand Up @@ -78,24 +82,24 @@ class Meta:

def __init__(
self,
src=None,
id=missing,
name=missing,
type=missing,
storage_id=missing,
size=missing,
hash=missing,
creation_time=missing,
modification_time=missing,
created_by=missing,
modified_by=missing,
expiry_time=missing,
format=missing,
evaluation_path=missing,
monitor=missing,
collect=missing,
collect_interval=missing,
reference_id=missing,
src: Union[str, io.IOBase] = None,
id: str = missing,
creation_time: datetime = missing,
modification_time: datetime = missing,
created_by: str = missing,
modified_by: str = missing,
name: str = missing,
type: str = missing,
storage_id: str = missing,
size: int = missing,
hash: str = missing,
expiry_time: datetime = missing,
format: str = missing,
evaluation_path: str = missing,
monitor: bool = missing,
collect: bool = missing,
collect_interval: int = missing,
reference_id: str = missing,
**kwargs,
):
self.src = src
Expand Down
41 changes: 28 additions & 13 deletions src/ansys/hps/client/jms/resource/fitness_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# SOFTWARE.
"""Module providing the fitness definition and fitness term definition resources."""
import logging
from typing import List

from marshmallow.utils import missing

Expand Down Expand Up @@ -84,14 +85,22 @@ class Meta:
schema = FitnessTermDefinitionSchema
rest_name = "None"

def __init__(self, **kwargs):
self.id = missing
self.name = missing
self.expression = missing
self.type = missing
self.weighting_factor = missing
def __init__(
self,
id: str = missing,
name: str = missing,
expression: str = missing,
type: str = missing,
weighting_factor: float = missing,
**kwargs
):
self.id = id
self.name = name
self.expression = expression
self.type = type
self.weighting_factor = weighting_factor

super().__init__(**kwargs)
self.obj_type = self.__class__.__name__


FitnessTermDefinitionSchema.Meta.object_class = FitnessTermDefinition
Expand Down Expand Up @@ -129,12 +138,18 @@ class Meta:
schema = FitnessDefinitionSchema
rest_name = "None"

def __init__(self, **kwargs):
self.id = missing
self.fitness_term_definitions = missing
self.error_fitness = missing

super().__init__(**kwargs)
def __init__(
self,
id: str = missing,
fitness_term_definitions: List[FitnessTermDefinition] = missing,
error_fitness: float = missing,
**kwargs
):
self.id = id
self.fitness_term_definitions = fitness_term_definitions
self.error_fitness = error_fitness

self.obj_type = self.__class__.__name__

def add_fitness_term(self, **kwargs):
"""
Expand Down
39 changes: 21 additions & 18 deletions src/ansys/hps/client/jms/resource/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

# autogenerated code
"""Module providing the job resource."""
from datetime import datetime
from typing import Any, Dict, List, Union

from marshmallow.utils import missing

from ansys.hps.client.common import Object
Expand Down Expand Up @@ -78,24 +81,24 @@ class Meta:

def __init__(
self,
id=missing,
creation_time=missing,
modification_time=missing,
created_by=missing,
modified_by=missing,
name=missing,
eval_status=missing,
job_definition_id=missing,
priority=missing,
values=missing,
fitness=missing,
fitness_term_values=missing,
note=missing,
creator=missing,
executed_level=missing,
elapsed_time=missing,
host_ids=missing,
file_ids=missing,
id: str = missing,
creation_time: datetime = missing,
modification_time: datetime = missing,
created_by: str = missing,
modified_by: str = missing,
name: str = missing,
eval_status: str = missing,
job_definition_id: str = missing,
priority: int = missing,
values: Dict[str, any] = missing,
fitness: float = missing,
fitness_term_values: Dict[str, float] = missing,
note: str = missing,
creator: str = missing,
executed_level: int = missing,
elapsed_time: float = missing,
host_ids: List = missing,
file_ids: List[str] = missing,
**kwargs
):
self.id = id
Expand Down
Loading