Skip to content

Expose operator version, and setter for custom Operators #2153

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
50 changes: 48 additions & 2 deletions src/ansys/dpf/core/operator_specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@
from __future__ import annotations

import abc
from typing import Union
from typing import Union, Tuple

from ansys.dpf.core import common, mapping_types, server as server_module
from ansys.dpf.core.check_version import server_meet_version, version_requires
from ansys.dpf.gate import (
integral_types,
operator_specification_capi,
operator_specification_grpcapi,
semantic_version_capi
)

import ctypes

class PinSpecification:
"""Documents an input or output pin of an Operator.
Expand Down Expand Up @@ -496,6 +497,23 @@
document=option_doc,
)
return self._config_specification

@property
def version(self) -> str:
semver_obj = lambda: None
semver_obj._internal_obj = self._api.operator_specification_get_version(self)

Check warning on line 504 in src/ansys/dpf/core/operator_specification.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/operator_specification.py#L503-L504

Added lines #L503 - L504 were not covered by tests

semver_api = self._server.get_api_for_type(

Check warning on line 506 in src/ansys/dpf/core/operator_specification.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/operator_specification.py#L506

Added line #L506 was not covered by tests
capi=semantic_version_capi.SemanticVersionCAPI, grpcapi=None
)

major = ctypes.c_uint16(0)
minor = ctypes.c_uint16(0)
patch = ctypes.c_uint16(0)
semver_api.semantic_vesion_get_components(semver_obj, ctypes.byref(major), ctypes.byref(minor), ctypes.byref(patch))

Check warning on line 513 in src/ansys/dpf/core/operator_specification.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/operator_specification.py#L510-L513

Added lines #L510 - L513 were not covered by tests

return f"{major}.{minor}.{patch}"

Check warning on line 515 in src/ansys/dpf/core/operator_specification.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/operator_specification.py#L515

Added line #L515 was not covered by tests



class CustomConfigOptionSpec(ConfigOptionSpec):
Expand Down Expand Up @@ -866,3 +884,31 @@
for key, value in val.items():
if value is not None:
self._api.operator_specification_set_property(self, key, value)

@property
def version(self) -> str:
return super().version

Check warning on line 890 in src/ansys/dpf/core/operator_specification.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/operator_specification.py#L890

Added line #L890 was not covered by tests

@version.setter
def version(self, ver_obj: Union[Tuple[int], Tuple[int, int], Tuple[int, int, int]]):
major = 0
minor = 0
patch = 0
if isinstance(ver_obj, tuple):
if len(ver_obj) > 0 :
major = ver_obj[0]
if len(ver_obj) > 1:
minor = ver_obj[1]
if len(ver_obj) > 2:
patch = ver_obj[2]

Check warning on line 903 in src/ansys/dpf/core/operator_specification.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/operator_specification.py#L894-L903

Added lines #L894 - L903 were not covered by tests

semver_api: semantic_version_capi.semantic_version_abstract_api.SemanticVersionAbstractAPI = self._server.get_api_for_type(

Check warning on line 905 in src/ansys/dpf/core/operator_specification.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/operator_specification.py#L905

Added line #L905 was not covered by tests
capi=semantic_version_capi.SemanticVersionCAPI,
grpcapi=None
)

# proxy obj
semver_obj = lambda: None
semver_obj._internal_obj = semver_api.semantic_version_new(major, minor, patch)

Check warning on line 912 in src/ansys/dpf/core/operator_specification.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/operator_specification.py#L911-L912

Added lines #L911 - L912 were not covered by tests

self._api.operator_specification_set_version(self, semver_obj)

Check warning on line 914 in src/ansys/dpf/core/operator_specification.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/operator_specification.py#L914

Added line #L914 was not covered by tests
24 changes: 24 additions & 0 deletions tests/test_python_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
CustomSpecification,
PinSpecification,
SpecificationProperties,
Specification
)
import conftest
from conftest import (
Expand Down Expand Up @@ -408,3 +409,26 @@ def test_custom_op_with_spec(server_type_remote_process, testfiles_dir):
outf = op.outputs.field()
expected = np.ones((3, 3), dtype=np.float64) + 4.0
assert np.allclose(outf.data, expected)


def test_custom_op_without_version(testfiles_dir):
dpf.load_library(
dpf.path_utilities.to_server_os(
Path(testfiles_dir) / "pythonPlugins"
),
"py_operator_with_spec",
"load_operators",
)
spec = Specification("custom_add_to_field")
assert spec.version == "0.0.0"

def test_custom_op_with_version(testfiles_dir):
dpf.load_library(
dpf.path_utilities.to_server_os(
Path(testfiles_dir) / "pythonPlugins",
),
"py_operator_with_spec",
"load_operators"
)
spec = Specification("__op_with_version")
assert spec.version == "2.3.1"
18 changes: 18 additions & 0 deletions tests/testfiles/pythonPlugins/operator_with_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ def specification(self):
def name(self):
return "custom_add_to_field"

class OpWithVersion(CustomOperatorBase):
def run(self):
self.set_output("The brown fox jumps over the lazy dog.")
self.set_succeeded()

@property
def specification(self):
spec = CustomSpecification()
spec.description = "Outputs a string"
spec.outputs = {
0: PinSpecification("message", [str], "Important message")
}
spec.version = (2, 3, 1)
return spec
@property
def name(self):
return "__op_with_version"

def load_operators(*args):
record_operator(AddFloatToFieldData, *args)
record_operator(OpWithVersion, *args)
Loading