Skip to content

Commit 28d89ac

Browse files
FEAT: Add option for pep8 aliases in binding (#1234)
Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent 09c9425 commit 28d89ac

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

doc/changelog.d/1234.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add option for pep8 aliases in binding

src/ansys/mechanical/core/embedding/app.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ class App:
144144
Whether to enable logging. Default is True.
145145
log_level : str, optional
146146
The logging level for the application. Default is "WARNING".
147+
pep8 : bool, optional
148+
Whether to enable PEP 8 style binding for the assembly. Default is False.
147149
148150
Examples
149151
--------
@@ -231,7 +233,8 @@ def __init__(self, db_file=None, private_appdata=False, **kwargs):
231233
profile = UniqueUserProfile(new_profile_name, copy_profile=copy_profile)
232234
profile.update_environment(os.environ)
233235

234-
runtime.initialize(self._version)
236+
pep8_alias = kwargs.get("pep8", False)
237+
runtime.initialize(self._version, pep8_aliases=pep8_alias)
235238
self._app = _start_application(configuration, self._version, db_file)
236239
connect_warnings(self)
237240
self._poster = None

src/ansys/mechanical/core/embedding/runtime.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,44 @@ def __register_function_codec():
4545
Ansys.Mechanical.CPython.Codecs.FunctionCodec.Register()
4646

4747

48-
def _bind_assembly_for_explicit_interface(assembly_name: str):
49-
"""Bind the assembly for explicit interface implementation."""
48+
def _bind_assembly(
49+
assembly_name: str, explicit_interface: bool = False, pep8_aliases: bool = False
50+
) -> None:
51+
"""Bind the assembly for explicit interface and/or pep8 aliases.
52+
53+
Parameters
54+
----------
55+
assembly_name : str
56+
The name of the assembly to bind.
57+
explicit_interface : bool, optional
58+
If True, allows explicit interface implementation. Default is False.
59+
pep8_aliases : bool, optional
60+
If True, enables PEP 8 aliases. Default is False.
61+
"""
5062
# if pythonnet is not installed, we can't bind the assembly
5163
try:
5264
distribution("pythonnet")
5365
Logger.warning("Cannot bind for explicit interface because pythonnet is installed")
5466
return
5567
except ModuleNotFoundError:
5668
pass
57-
58-
Logger.debug(f"Binding assembly for explicit interface {assembly_name}")
5969
import clr
6070

61-
Logger.debug(f"Binding assembly for explicit interface, Loading {assembly_name}")
71+
Logger.debug(f"Binding assembly {assembly_name}")
6272
assembly = clr.AddReference(assembly_name)
63-
Logger.debug(f"Binding assembly for explicit interface, Loaded {assembly_name}")
6473
from Python.Runtime import BindingManager, BindingOptions
6574

6675
binding_options = BindingOptions()
67-
binding_options.AllowExplicitInterfaceImplementation = True
76+
if explicit_interface:
77+
Logger.debug(f"Binding explicit interface for {assembly_name}")
78+
binding_options.AllowExplicitInterfaceImplementation = True
79+
if pep8_aliases:
80+
Logger.debug(f"Setting PEP 8 aliases for {assembly_name}")
81+
binding_options.Pep8Aliases = True
6882
BindingManager.SetBindingOptions(assembly, binding_options)
6983

7084

71-
def initialize(version: int) -> None:
85+
def initialize(version: int, pep8_aliases: bool = False) -> None:
7286
"""Initialize the runtime.
7387
7488
Pythonnet is already initialized but we need to
@@ -86,4 +100,14 @@ def initialize(version: int) -> None:
86100
__register_function_codec()
87101
Logger.debug("Registered function codec")
88102

89-
_bind_assembly_for_explicit_interface("Ansys.ACT.WB1")
103+
explicit_interface = True
104+
105+
if os.environ.get("PYMECHANICAL_EXPLICIT_INTERFACE") == "0":
106+
explicit_interface = False
107+
108+
# TODO: When the pep8_aliases option is enabled (True by default),
109+
# keep three environment variables to turn explicit, pep8, and both off.
110+
111+
_bind_assembly(
112+
"Ansys.ACT.WB1", explicit_interface=explicit_interface, pep8_aliases=pep8_aliases
113+
)

0 commit comments

Comments
 (0)