Skip to content

Commit

Permalink
Fix hidden operators exposure (#1821)
Browse files Browse the repository at this point in the history
* Fix mustache code style

Signed-off-by: paul.profizi <paul.profizi@ansys.com>

* Filter-out hidden operators from available operators in ansys.dpf.core.operators.build.build_operators()

Signed-off-by: paul.profizi <paul.profizi@ansys.com>

* Use binary string to not change line endings when writing to file

Signed-off-by: paul.profizi <paul.profizi@ansys.com>

* Generate __init__py files directly in ansys.dpf.core.operators.build.build_operators()

Signed-off-by: paul.profizi <paul.profizi@ansys.com>

* Remove usage of python_generator operator in .ci/code_generation.py

Signed-off-by: paul.profizi <paul.profizi@ansys.com>

* Fix operator import

Signed-off-by: paul.profizi <paul.profizi@ansys.com>

* Add an exception list for some hidden operators to still expose until deprecation

Signed-off-by: paul.profizi <paul.profizi@ansys.com>

---------

Signed-off-by: paul.profizi <paul.profizi@ansys.com>
  • Loading branch information
PProfizi authored Oct 24, 2024
1 parent e82965b commit aa61495
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 79 deletions.
77 changes: 2 additions & 75 deletions .ci/code_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,9 @@
import os
import glob
from pathlib import Path
import time
import shutil


core.set_default_server_context(core.AvailableServerContexts.premium)

if os.name == "posix":
LIB_TO_GENERATE = [
"libAns.Dpf.Native.so",
"libAns.Dpf.FEMutils.so",
"libmapdlOperatorsCore.so",
"libmeshOperatorsCore.so",
"libAns.Dpf.Math.so",
"libAns.Dpf.Hdf5.so",
"libAns.Dpf.LSDYNAHGP.so",
"libAns.Dpf.LivePost.so",
"libans.dpf.pointcloudsearch.so",
"libAns.Dpf.Vtk.so",
"libAns.Dpf.MechanicalResults.so",
]
LIB_OPTIONAL_TO_GENERATE = [
"libAns.Dpf.SystemCouplingMapping.so",
]
else:
LIB_TO_GENERATE = [
"Ans.Dpf.Native.dll",
"Ans.Dpf.FEMutils.dll",
"meshOperatorsCore.dll",
"mapdlOperatorsCore.dll",
"Ans.Dpf.Math.dll",
"Ans.Dpf.PythonPluginWrapper.dll",
"Ans.Dpf.Hdf5.dll",
"Ans.Dpf.FlowDiagram.dll",
"Ans.Dpf.LSDYNAHGP.dll",
"Ans.Dpf.LivePost.dll",
"Ans.Dpf.PointCloudSearch.dll",
"Ans.Dpf.Vtk.dll",
"Ans.Dpf.MechanicalResults.dll",
]
LIB_OPTIONAL_TO_GENERATE = [
"Ans.Dpf.SystemCouplingMapping.dll",
]

local_dir = os.path.dirname(os.path.abspath(__file__))
TARGET_PATH = os.path.join(local_dir, os.pardir, "src", "ansys", "dpf", "core", "operators")
files = glob.glob(os.path.join(TARGET_PATH, "*"))
Expand All @@ -65,41 +25,8 @@
os.remove(f)
except:
pass
core.start_local_server(config=core.AvailableServerConfigs.LegacyGrpcServer)
code_gen = core.Operator("python_generator")
code_gen.connect(1, TARGET_PATH)
for lib in LIB_TO_GENERATE:
try:
code_gen.connect(0, lib)
if lib != LIB_TO_GENERATE[0]:
code_gen.connect(2, False)
else:
code_gen.connect(2, True)
print(f"Generating {lib} operators for server {core.SERVER.version}...")
code_gen.run()
time.sleep(0.1)
except Exception as e:
print(f"Could not generate operators for library {lib}:\n{str(e)}")
raise e

for lib in LIB_OPTIONAL_TO_GENERATE:
try:
code_gen.connect(0, lib)
if lib != LIB_OPTIONAL_TO_GENERATE[0]:
code_gen.connect(2, False)
else:
code_gen.connect(2, True)
print(f"Generating optional {lib} operators for server {core.SERVER.version}...")
code_gen.run()
time.sleep(0.1)
except Exception as e:
print(f"Could not generate operators for optional library {lib}:\n{str(e)}")

# Reorder imports alphabetically in __init__.py files to reduce changes raised
for init_file_path in glob.glob(os.path.join(TARGET_PATH, "**/__init__.py"), recursive=True):
with open(init_file_path, "r") as init_file:
lines = init_file.readlines()
with open(init_file_path, "w") as init_file:
init_file.writelines(sorted(lines))
core.set_default_server_context(core.AvailableServerContexts.premium)
core.start_local_server(config=core.AvailableServerConfigs.LegacyGrpcServer)

build.build_operators()
41 changes: 37 additions & 4 deletions src/ansys/dpf/core/operators/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,30 @@ def build_operators():

succeeded = 0
done = 0
hidden = 0
# List of hidden operators to still expose for retro-compatibility
# until they are fully deprecated
hidden_to_expose = [ # Use internal names
"rescope_fc", # Switch to "change_fc" once server is updated
"dot", "dot_tensor",
"scale_by_field", "scale_by_field_fc",
"invert", "invert_fc",
]
categories = set()
for operator_name in available_operators:
if succeeded == done + 100:
done += 100
print(f"{done} operators done...")
specification = dpf.Operator.operator_specification(operator_name)

if (specification.properties["exposure"] in ["hidden", "private"]
and
operator_name not in hidden_to_expose):
hidden += 1
continue

category = specification.properties.get("category", "")
categories.add(category)
if not category:
raise ValueError(f"Category not defined for operator {operator_name}.")
scripting_name = specification.properties.get("scripting_name", "")
Expand All @@ -211,7 +228,7 @@ def build_operators():

# Write to operator file
operator_file = os.path.join(category_path, scripting_name + ".py")
with open(operator_file, "w") as f:
with open(operator_file, "wb") as f:
try:
operator_str = build_operator(
specification,
Expand All @@ -221,7 +238,7 @@ def build_operators():
category,
)
exec(operator_str, globals())
f.write(operator_str)
f.write(operator_str.encode())
succeeded += 1
except SyntaxError as e:
error_message = (
Expand All @@ -233,8 +250,24 @@ def build_operators():
error_file.write(f"Class: {operator_str}")
print(error_message)

print(f"Generated {succeeded} out of {len(available_operators)}")
if succeeded == len(available_operators):
print(f"Generated {succeeded} out of {len(available_operators)} ({hidden} hidden)")

# Create __init__.py files
print(f"Generating __init__.py files...")
with open(os.path.join(this_path, "__init__.py"), "wb") as main_init:
for category in sorted(categories):
# Add category to main init file imports
main_init.write(f"from . import {category}\n".encode())
# Create category init file
category_operators = os.listdir(os.path.join(this_path, category.split(".")[0]))
with open(os.path.join(this_path, category, "__init__.py"), "wb") as category_init:
for category_operator in category_operators:
operator_name = category_operator.split(".")[0]
category_init.write(
f"from .{operator_name} import {operator_name}\n".encode()
)

if succeeded == len(available_operators) - hidden:
print("Success")
exit(0)
else:
Expand Down
1 change: 1 addition & 0 deletions src/ansys/dpf/core/operators/operator.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{{class_name_underlining}}
Autogenerated DPF operator classes.
"""

from warnings import warn
from ansys.dpf.core.dpf_operator import Operator
from ansys.dpf.core.inputs import Input, _Inputs
Expand Down

0 comments on commit aa61495

Please sign in to comment.