ansys-mechanical-core 0.11.12__py3-none-any.whl → 0.11.13__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ansys/mechanical/core/_version.py +48 -48
- ansys/mechanical/core/embedding/app.py +610 -610
- ansys/mechanical/core/embedding/background.py +11 -2
- ansys/mechanical/core/embedding/logger/__init__.py +219 -219
- ansys/mechanical/core/embedding/resolver.py +48 -41
- ansys/mechanical/core/embedding/rpc/__init__.py +36 -0
- ansys/mechanical/core/embedding/rpc/client.py +237 -0
- ansys/mechanical/core/embedding/rpc/server.py +382 -0
- ansys/mechanical/core/embedding/rpc/utils.py +120 -0
- ansys/mechanical/core/embedding/runtime.py +22 -0
- ansys/mechanical/core/feature_flags.py +1 -0
- ansys/mechanical/core/ide_config.py +212 -212
- ansys/mechanical/core/mechanical.py +2343 -2324
- ansys/mechanical/core/misc.py +176 -176
- ansys/mechanical/core/pool.py +712 -712
- ansys/mechanical/core/run.py +321 -321
- {ansys_mechanical_core-0.11.12.dist-info → ansys_mechanical_core-0.11.13.dist-info}/METADATA +35 -23
- {ansys_mechanical_core-0.11.12.dist-info → ansys_mechanical_core-0.11.13.dist-info}/RECORD +21 -17
- {ansys_mechanical_core-0.11.12.dist-info → ansys_mechanical_core-0.11.13.dist-info}/LICENSE +0 -0
- {ansys_mechanical_core-0.11.12.dist-info → ansys_mechanical_core-0.11.13.dist-info}/WHEEL +0 -0
- {ansys_mechanical_core-0.11.12.dist-info → ansys_mechanical_core-0.11.13.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,120 @@
|
|
1
|
+
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
|
2
|
+
# SPDX-License-Identifier: MIT
|
3
|
+
#
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
13
|
+
# copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
# SOFTWARE.
|
22
|
+
"""Utilities necessary for remote calls."""
|
23
|
+
import typing
|
24
|
+
|
25
|
+
|
26
|
+
class remote_method:
|
27
|
+
"""Decorator for passing remote methods."""
|
28
|
+
|
29
|
+
def __init__(self, func):
|
30
|
+
"""Initialize with the given function."""
|
31
|
+
self._func = func
|
32
|
+
|
33
|
+
def __call__(self, *args, **kwargs):
|
34
|
+
"""Call the stored function with provided arguments."""
|
35
|
+
return self._func(*args, **kwargs)
|
36
|
+
|
37
|
+
def __call_method__(self, instance, *args, **kwargs):
|
38
|
+
"""Call the stored function with the instance and provided arguments."""
|
39
|
+
return self._func(instance, *args, **kwargs)
|
40
|
+
|
41
|
+
def __get__(self, obj, objtype):
|
42
|
+
"""Return a partially applied method."""
|
43
|
+
from functools import partial
|
44
|
+
|
45
|
+
func = partial(self.__call_method__, obj)
|
46
|
+
func._is_remote = True
|
47
|
+
func.__name__ = self._func.__name__
|
48
|
+
func._owner = obj
|
49
|
+
return func
|
50
|
+
|
51
|
+
|
52
|
+
class MethodType:
|
53
|
+
"""Enum for method or property types."""
|
54
|
+
|
55
|
+
METHOD = 0
|
56
|
+
PROP = 1
|
57
|
+
|
58
|
+
|
59
|
+
def try_get_remote_method(methodname: str, obj: typing.Any) -> typing.Tuple[str, typing.Callable]:
|
60
|
+
"""Try to get a remote method."""
|
61
|
+
method = getattr(obj, methodname)
|
62
|
+
if not callable(method):
|
63
|
+
return None
|
64
|
+
if hasattr(method, "_is_remote") and method._is_remote is True:
|
65
|
+
return (methodname, method)
|
66
|
+
|
67
|
+
|
68
|
+
def try_get_remote_property(attrname: str, obj: typing.Any) -> typing.Tuple[str, property]:
|
69
|
+
"""Try to get a remote property."""
|
70
|
+
objclass: typing.Type = obj.__class__
|
71
|
+
class_attribute = getattr(objclass, attrname)
|
72
|
+
getmethod = None
|
73
|
+
setmethod = None
|
74
|
+
|
75
|
+
if class_attribute.fget:
|
76
|
+
if isinstance(class_attribute.fget, remote_method):
|
77
|
+
getmethod = class_attribute.fget
|
78
|
+
getmethod._owner = obj
|
79
|
+
if class_attribute.fset:
|
80
|
+
if isinstance(class_attribute.fset, remote_method):
|
81
|
+
setmethod = class_attribute.fset
|
82
|
+
setmethod._owner = obj
|
83
|
+
|
84
|
+
return (attrname, property(getmethod, setmethod))
|
85
|
+
|
86
|
+
|
87
|
+
def get_remote_methods(
|
88
|
+
obj,
|
89
|
+
) -> typing.Generator[typing.Tuple[str, typing.Callable, MethodType], None, None]:
|
90
|
+
"""Yield names and methods of an object's remote methods.
|
91
|
+
|
92
|
+
A remote method is identified by the presence of an attribute `_is_remote` set to `True`.
|
93
|
+
|
94
|
+
Parameters
|
95
|
+
----------
|
96
|
+
obj: Any
|
97
|
+
The object to inspect for remote methods.
|
98
|
+
|
99
|
+
Yields
|
100
|
+
------
|
101
|
+
Generator[Tuple[str, Callable], None, None]
|
102
|
+
A tuple containing the method name and the method itself
|
103
|
+
for each remote method found in the object
|
104
|
+
"""
|
105
|
+
print(f"Getting remote methods on {obj}")
|
106
|
+
objclass = obj.__class__
|
107
|
+
for attrname in dir(obj):
|
108
|
+
if attrname.startswith("__"):
|
109
|
+
continue
|
110
|
+
print(attrname)
|
111
|
+
if hasattr(objclass, attrname):
|
112
|
+
class_attribute = getattr(objclass, attrname)
|
113
|
+
if isinstance(class_attribute, property):
|
114
|
+
attrname, prop = try_get_remote_property(attrname, obj)
|
115
|
+
yield attrname, prop, MethodType.PROP
|
116
|
+
continue
|
117
|
+
result = try_get_remote_method(attrname, obj)
|
118
|
+
if result != None:
|
119
|
+
attrname, method = result
|
120
|
+
yield attrname, method, MethodType.METHOD
|
@@ -22,6 +22,7 @@
|
|
22
22
|
|
23
23
|
"""Runtime initialize for pythonnet in embedding."""
|
24
24
|
|
25
|
+
from importlib.metadata import distribution
|
25
26
|
import os
|
26
27
|
|
27
28
|
from ansys.mechanical.core.embedding.logger import Logger
|
@@ -44,6 +45,25 @@ def __register_function_codec():
|
|
44
45
|
Ansys.Mechanical.CPython.Codecs.FunctionCodec.Register()
|
45
46
|
|
46
47
|
|
48
|
+
def _bind_assembly_for_explicit_interface(assembly_name: str):
|
49
|
+
"""Bind the assembly for explicit interface implementation."""
|
50
|
+
# if pythonnet is not installed, we can't bind the assembly
|
51
|
+
try:
|
52
|
+
distribution("pythonnet")
|
53
|
+
return
|
54
|
+
except ModuleNotFoundError:
|
55
|
+
pass
|
56
|
+
|
57
|
+
import clr
|
58
|
+
|
59
|
+
assembly = clr.AddReference(assembly_name)
|
60
|
+
from Python.Runtime import BindingManager, BindingOptions
|
61
|
+
|
62
|
+
binding_options = BindingOptions()
|
63
|
+
binding_options.AllowExplicitInterfaceImplementation = True
|
64
|
+
BindingManager.SetBindingOptions(assembly, binding_options)
|
65
|
+
|
66
|
+
|
47
67
|
def initialize(version: int) -> None:
|
48
68
|
"""Initialize the runtime.
|
49
69
|
|
@@ -59,3 +79,5 @@ def initialize(version: int) -> None:
|
|
59
79
|
# function codec is distributed with pymechanical on linux only
|
60
80
|
# at version 242 or later
|
61
81
|
__register_function_codec()
|
82
|
+
|
83
|
+
_bind_assembly_for_explicit_interface("Ansys.ACT.WB1")
|
@@ -1,212 +1,212 @@
|
|
1
|
-
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
|
2
|
-
# SPDX-License-Identifier: MIT
|
3
|
-
#
|
4
|
-
#
|
5
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
# of this software and associated documentation files (the "Software"), to deal
|
7
|
-
# in the Software without restriction, including without limitation the rights
|
8
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
# copies of the Software, and to permit persons to whom the Software is
|
10
|
-
# furnished to do so, subject to the following conditions:
|
11
|
-
#
|
12
|
-
# The above copyright notice and this permission notice shall be included in all
|
13
|
-
# copies or substantial portions of the Software.
|
14
|
-
#
|
15
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
-
# SOFTWARE.
|
22
|
-
|
23
|
-
"""Convenience CLI to run mechanical."""
|
24
|
-
|
25
|
-
import json
|
26
|
-
import os
|
27
|
-
from pathlib import Path
|
28
|
-
import re
|
29
|
-
import site
|
30
|
-
import sys
|
31
|
-
|
32
|
-
import ansys.tools.path as atp
|
33
|
-
import click
|
34
|
-
|
35
|
-
|
36
|
-
def get_stubs_location():
|
37
|
-
"""Find the ansys-mechanical-stubs installation location in site-packages.
|
38
|
-
|
39
|
-
Returns
|
40
|
-
-------
|
41
|
-
pathlib.Path
|
42
|
-
The path to the ansys-mechanical-stubs installation in site-packages.
|
43
|
-
"""
|
44
|
-
site_packages = site.getsitepackages()
|
45
|
-
prefix_path = sys.prefix.replace("\\", "\\\\")
|
46
|
-
site_packages_regex = re.compile(f"{prefix_path}.*site-packages$")
|
47
|
-
site_packages_paths = list(filter(site_packages_regex.match, site_packages))
|
48
|
-
|
49
|
-
if len(site_packages_paths) == 1:
|
50
|
-
# Get the stubs location
|
51
|
-
stubs_location = Path(site_packages_paths[0]) / "ansys" / "mechanical" / "stubs"
|
52
|
-
return stubs_location
|
53
|
-
|
54
|
-
raise Exception("Could not retrieve the location of the ansys-mechanical-stubs package.")
|
55
|
-
|
56
|
-
|
57
|
-
def get_stubs_versions(stubs_location: Path):
|
58
|
-
"""Retrieve the revision numbers in ansys-mechanical-stubs.
|
59
|
-
|
60
|
-
Parameters
|
61
|
-
----------
|
62
|
-
pathlib.Path
|
63
|
-
The path to the ansys-mechanical-stubs installation in site-packages.
|
64
|
-
|
65
|
-
Returns
|
66
|
-
-------
|
67
|
-
list
|
68
|
-
The list containing minimum and maximum versions in the ansys-mechanical-stubs package.
|
69
|
-
"""
|
70
|
-
# Get revision numbers in stubs folder
|
71
|
-
revns = [
|
72
|
-
int(revision[1:])
|
73
|
-
for revision in os.listdir(stubs_location)
|
74
|
-
if os.path.isdir(os.path.join(stubs_location, revision)) and revision.startswith("v")
|
75
|
-
]
|
76
|
-
return revns
|
77
|
-
|
78
|
-
|
79
|
-
def _vscode_impl(
|
80
|
-
target: str = "user",
|
81
|
-
revision: int = None,
|
82
|
-
):
|
83
|
-
"""Get the IDE configuration for autocomplete in VS Code.
|
84
|
-
|
85
|
-
Parameters
|
86
|
-
----------
|
87
|
-
target: str
|
88
|
-
The type of settings to update. Either "user" or "workspace" in VS Code.
|
89
|
-
By default, it's ``user``.
|
90
|
-
revision: int
|
91
|
-
The Mechanical revision number. For example, "251".
|
92
|
-
If unspecified, it finds the default Mechanical version from ansys-tools-path.
|
93
|
-
"""
|
94
|
-
# Update the user or workspace settings
|
95
|
-
if target == "user":
|
96
|
-
# Get the path to the user's settings.json file depending on the platform
|
97
|
-
if "win" in sys.platform:
|
98
|
-
settings_json = (
|
99
|
-
Path(os.environ.get("APPDATA")) / "Code" / "User" / "settings.json"
|
100
|
-
) # pragma: no cover
|
101
|
-
elif "lin" in sys.platform:
|
102
|
-
settings_json = (
|
103
|
-
Path(os.environ.get("HOME")) / ".config" / "Code" / "User" / "settings.json"
|
104
|
-
)
|
105
|
-
elif target == "workspace":
|
106
|
-
# Get the current working directory
|
107
|
-
current_dir = Path.cwd()
|
108
|
-
# Get the path to the settings.json file based on the git root & .vscode folder
|
109
|
-
settings_json = current_dir / ".vscode" / "settings.json"
|
110
|
-
|
111
|
-
# Location where the stubs are installed -> .venv/Lib/site-packages, for example
|
112
|
-
stubs_location = get_stubs_location() / f"v{revision}"
|
113
|
-
|
114
|
-
# The settings to add to settings.json for autocomplete to work
|
115
|
-
settings_json_data = {
|
116
|
-
"python.autoComplete.extraPaths": [str(stubs_location)],
|
117
|
-
"python.analysis.extraPaths": [str(stubs_location)],
|
118
|
-
}
|
119
|
-
# Pretty print dictionary
|
120
|
-
pretty_dict = json.dumps(settings_json_data, indent=4)
|
121
|
-
|
122
|
-
print(f"Update {settings_json} with the following information:\n")
|
123
|
-
|
124
|
-
if target == "workspace":
|
125
|
-
print(
|
126
|
-
"Note: Please ensure the .vscode folder is in the root of your project or repository.\n"
|
127
|
-
)
|
128
|
-
|
129
|
-
print(pretty_dict)
|
130
|
-
|
131
|
-
|
132
|
-
def _cli_impl(
|
133
|
-
ide: str = "vscode",
|
134
|
-
target: str = "user",
|
135
|
-
revision: int = None,
|
136
|
-
):
|
137
|
-
"""Provide the user with the path to the settings.json file and IDE settings.
|
138
|
-
|
139
|
-
Parameters
|
140
|
-
----------
|
141
|
-
ide: str
|
142
|
-
The IDE to set up autocomplete settings. By default, it's ``vscode``.
|
143
|
-
target: str
|
144
|
-
The type of settings to update. Either "user" or "workspace" in VS Code.
|
145
|
-
By default, it's ``user``.
|
146
|
-
revision: int
|
147
|
-
The Mechanical revision number. For example, "251".
|
148
|
-
If unspecified, it finds the default Mechanical version from ansys-tools-path.
|
149
|
-
"""
|
150
|
-
# Get the ansys-mechanical-stubs install location
|
151
|
-
stubs_location = get_stubs_location()
|
152
|
-
# Get all revision numbers available in ansys-mechanical-stubs
|
153
|
-
revns = get_stubs_versions(stubs_location)
|
154
|
-
# Check the IDE and raise an exception if it's not VS Code
|
155
|
-
if revision < min(revns) or revision > max(revns):
|
156
|
-
raise Exception(f"PyMechanical Stubs are not available for {revision}")
|
157
|
-
elif ide != "vscode":
|
158
|
-
raise Exception(f"{ide} is not supported at the moment.")
|
159
|
-
else:
|
160
|
-
return _vscode_impl(target, revision)
|
161
|
-
|
162
|
-
|
163
|
-
@click.command()
|
164
|
-
@click.help_option("--help", "-h")
|
165
|
-
@click.option(
|
166
|
-
"--ide",
|
167
|
-
default="vscode",
|
168
|
-
type=str,
|
169
|
-
help="The IDE being used. By default, it's ``vscode``.",
|
170
|
-
)
|
171
|
-
@click.option(
|
172
|
-
"--target",
|
173
|
-
default="user",
|
174
|
-
type=str,
|
175
|
-
help="The type of settings to update - either ``user`` or ``workspace`` settings in VS Code.",
|
176
|
-
)
|
177
|
-
@click.option(
|
178
|
-
"--revision",
|
179
|
-
default=None,
|
180
|
-
type=int,
|
181
|
-
help='The Mechanical revision number, e.g. "251" or "242". If unspecified,\
|
182
|
-
it finds and uses the default version from ansys-tools-path.',
|
183
|
-
)
|
184
|
-
def cli(ide: str, target: str, revision: int) -> None:
|
185
|
-
"""CLI tool to update settings.json files for autocomplete with ansys-mechanical-stubs.
|
186
|
-
|
187
|
-
Parameters
|
188
|
-
----------
|
189
|
-
ide: str
|
190
|
-
The IDE to set up autocomplete settings. By default, it's ``vscode``.
|
191
|
-
target: str
|
192
|
-
The type of settings to update. Either "user" or "workspace" in VS Code.
|
193
|
-
By default, it's ``user``.
|
194
|
-
revision: int
|
195
|
-
The Mechanical revision number. For example, "251".
|
196
|
-
If unspecified, it finds the default Mechanical version from ansys-tools-path.
|
197
|
-
|
198
|
-
Usage
|
199
|
-
-----
|
200
|
-
The following example demonstrates the main use of this tool:
|
201
|
-
|
202
|
-
$ ansys-mechanical-ideconfig --ide vscode --target user --revision 251
|
203
|
-
|
204
|
-
"""
|
205
|
-
exe = atp.get_mechanical_path(allow_input=False, version=revision)
|
206
|
-
version = atp.version_from_path("mechanical", exe)
|
207
|
-
|
208
|
-
return _cli_impl(
|
209
|
-
ide,
|
210
|
-
target,
|
211
|
-
version,
|
212
|
-
)
|
1
|
+
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
|
2
|
+
# SPDX-License-Identifier: MIT
|
3
|
+
#
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
13
|
+
# copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
# SOFTWARE.
|
22
|
+
|
23
|
+
"""Convenience CLI to run mechanical."""
|
24
|
+
|
25
|
+
import json
|
26
|
+
import os
|
27
|
+
from pathlib import Path
|
28
|
+
import re
|
29
|
+
import site
|
30
|
+
import sys
|
31
|
+
|
32
|
+
import ansys.tools.path as atp
|
33
|
+
import click
|
34
|
+
|
35
|
+
|
36
|
+
def get_stubs_location():
|
37
|
+
"""Find the ansys-mechanical-stubs installation location in site-packages.
|
38
|
+
|
39
|
+
Returns
|
40
|
+
-------
|
41
|
+
pathlib.Path
|
42
|
+
The path to the ansys-mechanical-stubs installation in site-packages.
|
43
|
+
"""
|
44
|
+
site_packages = site.getsitepackages()
|
45
|
+
prefix_path = sys.prefix.replace("\\", "\\\\")
|
46
|
+
site_packages_regex = re.compile(f"{prefix_path}.*site-packages$")
|
47
|
+
site_packages_paths = list(filter(site_packages_regex.match, site_packages))
|
48
|
+
|
49
|
+
if len(site_packages_paths) == 1:
|
50
|
+
# Get the stubs location
|
51
|
+
stubs_location = Path(site_packages_paths[0]) / "ansys" / "mechanical" / "stubs"
|
52
|
+
return stubs_location
|
53
|
+
|
54
|
+
raise Exception("Could not retrieve the location of the ansys-mechanical-stubs package.")
|
55
|
+
|
56
|
+
|
57
|
+
def get_stubs_versions(stubs_location: Path):
|
58
|
+
"""Retrieve the revision numbers in ansys-mechanical-stubs.
|
59
|
+
|
60
|
+
Parameters
|
61
|
+
----------
|
62
|
+
pathlib.Path
|
63
|
+
The path to the ansys-mechanical-stubs installation in site-packages.
|
64
|
+
|
65
|
+
Returns
|
66
|
+
-------
|
67
|
+
list
|
68
|
+
The list containing minimum and maximum versions in the ansys-mechanical-stubs package.
|
69
|
+
"""
|
70
|
+
# Get revision numbers in stubs folder
|
71
|
+
revns = [
|
72
|
+
int(revision[1:])
|
73
|
+
for revision in os.listdir(stubs_location)
|
74
|
+
if os.path.isdir(os.path.join(stubs_location, revision)) and revision.startswith("v")
|
75
|
+
]
|
76
|
+
return revns
|
77
|
+
|
78
|
+
|
79
|
+
def _vscode_impl(
|
80
|
+
target: str = "user",
|
81
|
+
revision: int = None,
|
82
|
+
):
|
83
|
+
"""Get the IDE configuration for autocomplete in VS Code.
|
84
|
+
|
85
|
+
Parameters
|
86
|
+
----------
|
87
|
+
target: str
|
88
|
+
The type of settings to update. Either "user" or "workspace" in VS Code.
|
89
|
+
By default, it's ``user``.
|
90
|
+
revision: int
|
91
|
+
The Mechanical revision number. For example, "251".
|
92
|
+
If unspecified, it finds the default Mechanical version from ansys-tools-path.
|
93
|
+
"""
|
94
|
+
# Update the user or workspace settings
|
95
|
+
if target == "user":
|
96
|
+
# Get the path to the user's settings.json file depending on the platform
|
97
|
+
if "win" in sys.platform:
|
98
|
+
settings_json = (
|
99
|
+
Path(os.environ.get("APPDATA")) / "Code" / "User" / "settings.json"
|
100
|
+
) # pragma: no cover
|
101
|
+
elif "lin" in sys.platform:
|
102
|
+
settings_json = (
|
103
|
+
Path(os.environ.get("HOME")) / ".config" / "Code" / "User" / "settings.json"
|
104
|
+
)
|
105
|
+
elif target == "workspace":
|
106
|
+
# Get the current working directory
|
107
|
+
current_dir = Path.cwd()
|
108
|
+
# Get the path to the settings.json file based on the git root & .vscode folder
|
109
|
+
settings_json = current_dir / ".vscode" / "settings.json"
|
110
|
+
|
111
|
+
# Location where the stubs are installed -> .venv/Lib/site-packages, for example
|
112
|
+
stubs_location = get_stubs_location() / f"v{revision}"
|
113
|
+
|
114
|
+
# The settings to add to settings.json for autocomplete to work
|
115
|
+
settings_json_data = {
|
116
|
+
"python.autoComplete.extraPaths": [str(stubs_location)],
|
117
|
+
"python.analysis.extraPaths": [str(stubs_location)],
|
118
|
+
}
|
119
|
+
# Pretty print dictionary
|
120
|
+
pretty_dict = json.dumps(settings_json_data, indent=4)
|
121
|
+
|
122
|
+
print(f"Update {settings_json} with the following information:\n")
|
123
|
+
|
124
|
+
if target == "workspace":
|
125
|
+
print(
|
126
|
+
"Note: Please ensure the .vscode folder is in the root of your project or repository.\n"
|
127
|
+
)
|
128
|
+
|
129
|
+
print(pretty_dict)
|
130
|
+
|
131
|
+
|
132
|
+
def _cli_impl(
|
133
|
+
ide: str = "vscode",
|
134
|
+
target: str = "user",
|
135
|
+
revision: int = None,
|
136
|
+
):
|
137
|
+
"""Provide the user with the path to the settings.json file and IDE settings.
|
138
|
+
|
139
|
+
Parameters
|
140
|
+
----------
|
141
|
+
ide: str
|
142
|
+
The IDE to set up autocomplete settings. By default, it's ``vscode``.
|
143
|
+
target: str
|
144
|
+
The type of settings to update. Either "user" or "workspace" in VS Code.
|
145
|
+
By default, it's ``user``.
|
146
|
+
revision: int
|
147
|
+
The Mechanical revision number. For example, "251".
|
148
|
+
If unspecified, it finds the default Mechanical version from ansys-tools-path.
|
149
|
+
"""
|
150
|
+
# Get the ansys-mechanical-stubs install location
|
151
|
+
stubs_location = get_stubs_location()
|
152
|
+
# Get all revision numbers available in ansys-mechanical-stubs
|
153
|
+
revns = get_stubs_versions(stubs_location)
|
154
|
+
# Check the IDE and raise an exception if it's not VS Code
|
155
|
+
if revision < min(revns) or revision > max(revns):
|
156
|
+
raise Exception(f"PyMechanical Stubs are not available for {revision}")
|
157
|
+
elif ide != "vscode":
|
158
|
+
raise Exception(f"{ide} is not supported at the moment.")
|
159
|
+
else:
|
160
|
+
return _vscode_impl(target, revision)
|
161
|
+
|
162
|
+
|
163
|
+
@click.command()
|
164
|
+
@click.help_option("--help", "-h")
|
165
|
+
@click.option(
|
166
|
+
"--ide",
|
167
|
+
default="vscode",
|
168
|
+
type=str,
|
169
|
+
help="The IDE being used. By default, it's ``vscode``.",
|
170
|
+
)
|
171
|
+
@click.option(
|
172
|
+
"--target",
|
173
|
+
default="user",
|
174
|
+
type=str,
|
175
|
+
help="The type of settings to update - either ``user`` or ``workspace`` settings in VS Code.",
|
176
|
+
)
|
177
|
+
@click.option(
|
178
|
+
"--revision",
|
179
|
+
default=None,
|
180
|
+
type=int,
|
181
|
+
help='The Mechanical revision number, e.g. "251" or "242". If unspecified,\
|
182
|
+
it finds and uses the default version from ansys-tools-path.',
|
183
|
+
)
|
184
|
+
def cli(ide: str, target: str, revision: int) -> None:
|
185
|
+
"""CLI tool to update settings.json files for autocomplete with ansys-mechanical-stubs.
|
186
|
+
|
187
|
+
Parameters
|
188
|
+
----------
|
189
|
+
ide: str
|
190
|
+
The IDE to set up autocomplete settings. By default, it's ``vscode``.
|
191
|
+
target: str
|
192
|
+
The type of settings to update. Either "user" or "workspace" in VS Code.
|
193
|
+
By default, it's ``user``.
|
194
|
+
revision: int
|
195
|
+
The Mechanical revision number. For example, "251".
|
196
|
+
If unspecified, it finds the default Mechanical version from ansys-tools-path.
|
197
|
+
|
198
|
+
Usage
|
199
|
+
-----
|
200
|
+
The following example demonstrates the main use of this tool:
|
201
|
+
|
202
|
+
$ ansys-mechanical-ideconfig --ide vscode --target user --revision 251
|
203
|
+
|
204
|
+
"""
|
205
|
+
exe = atp.get_mechanical_path(allow_input=False, version=revision)
|
206
|
+
version = atp.version_from_path("mechanical", exe)
|
207
|
+
|
208
|
+
return _cli_impl(
|
209
|
+
ide,
|
210
|
+
target,
|
211
|
+
version,
|
212
|
+
)
|