Skip to content

Commit

Permalink
remote test passing
Browse files Browse the repository at this point in the history
  • Loading branch information
dipinknair committed Jan 17, 2025
1 parent f04e0ad commit c38f577
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 30 deletions.
84 changes: 68 additions & 16 deletions src/ansys/mechanical/core/embedding/rpc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import time

import rpyc

import pathlib

class Client:
"""Client for connecting to Mechanical services."""
Expand Down Expand Up @@ -62,15 +62,14 @@ def __getattr__(self, attr):
return exposed_fget()
return self.__dict__.items[attr]

"""
def __setattr__(self, attr, value):
if hasattr(self.root, attr):
inner_prop = getattr(self.root.__class__, attr)
if isinstance(inner_prop, property):
inner_prop.fset(self.root, value)
else:
super().__setattr__(attr, value)
"""
# TODO - implement setattr
# def __setattr__(self, attr, value):
# if hasattr(self.root, attr):
# inner_prop = getattr(self.root.__class__, attr)
# if isinstance(inner_prop, property):
# inner_prop.fset(self.root, value)
# else:
# super().__setattr__(attr, value)

def _connect(self):
self._wait_until_ready()
Expand Down Expand Up @@ -135,7 +134,7 @@ def download(
recursive=False,
):
"""Download a file from the server."""

out_files = []
os.makedirs(target_dir, exist_ok=True)

response = self.root.exposed_download(files)
Expand All @@ -147,12 +146,15 @@ def download(
local_file_dir = os.path.dirname(local_file_path)
os.makedirs(local_file_dir, exist_ok=True)

self._download_file(full_remote_path, local_file_path, chunk_size, overwrite=True)
out_file_path = self._download_file(full_remote_path, local_file_path, chunk_size, overwrite=True)
else:
self._download_file(
out_file_path = self._download_file(
files, os.path.join(target_dir, os.path.basename(files)), chunk_size, overwrite=True
)
out_files.append(out_file_path)

return out_files

def _download_file(self, remote_file_path, local_file_path, chunk_size=1024, overwrite=False):
"""Download a single file from the server."""

Expand All @@ -170,6 +172,56 @@ def _download_file(self, remote_file_path, local_file_path, chunk_size=1024, ove

print(f"File {remote_file_path} downloaded to {local_file_path}")

# @property
# def project_directory(self):
# return self.root.exposed_run_python_script("ExtAPI.DataModel.Project.ProjectDirectory")
return local_file_path


def download_project(self, extensions=None, target_dir=None, progress_bar=False):
"""Download all project files in the working directory of the Mechanical instance.
"""
destination_directory = target_dir.rstrip("\\/")
if destination_directory:
path = pathlib.Path(destination_directory)
path.mkdir(parents=True, exist_ok=True)
else:
destination_directory = os.getcwd()
# relative directory?
if os.path.isdir(destination_directory):
if not os.path.isabs(destination_directory):
# construct full path
destination_directory = os.path.join(os.getcwd(), destination_directory)
_project_directory = self.root.exposed_propget_project_directory()
_project_directory = _project_directory.rstrip("\\/")

# this is where .mechddb resides
parent_directory = os.path.dirname(_project_directory)

list_of_files = []

if not extensions:
files = self.root.exposed_list_files()
else:
files = []
for each_extension in extensions:
# mechdb resides one level above project directory
if "mechdb" == each_extension.lower():
file_temp = os.path.join(parent_directory, f"*.{each_extension}")
else:
file_temp = os.path.join(_project_directory, "**", f"*.{each_extension}")


list_files = self.exposed__get_files(file_temp, recursive=False)

files.extend(list_files)

for file in files:
# create similar hierarchy locally
new_path = file.replace(parent_directory, destination_directory)
new_path_dir = os.path.dirname(new_path)
temp_files = self.download(
files=file, target_dir=new_path_dir, progress_bar=progress_bar
)
list_of_files.extend(temp_files)
return list_of_files

def test(self):
print(self.root.exposed_propget_project_directory())
73 changes: 64 additions & 9 deletions src/ansys/mechanical/core/embedding/rpc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import rpyc
from rpyc.utils.server import ThreadedServer
import toolz
import fnmatch

import ansys.mechanical.core as mech
import ansys.mechanical.core.embedding.utils as utils
Expand Down Expand Up @@ -86,7 +87,7 @@ def curried():
else:
setattr(prop._owner, propname, *arg)

return self._poster.post(curried)
return self._poster.try_post(curried)

return posted

Expand All @@ -99,7 +100,7 @@ def curried():
result = original_method(*args, **kwargs)
return result

return self._poster.post(curried)
return self._poster.try_post(curried)

return posted

Expand All @@ -112,7 +113,7 @@ def posted(*args, **kwargs):
def curried():
return curried_method(self._app, *args, **kwargs)

return self._poster.post(curried)
return self._poster.try_post(curried)

return posted

Expand Down Expand Up @@ -319,11 +320,13 @@ def run_python_script(
self, script: str, enable_logging=False, log_level="WARNING", progress_interval=2000
):
"""Run scripts using Internal python engine."""
try:
result = self._app.execute_script(script)
return result
except Exception as e:
raise RuntimeError(f"Server-side error during script execution: {str(e)}")
result = self._app.execute_script(script)
return result
# try:
# result = self._app.execute_script(script)
# return result
# except Exception as e:
# raise RuntimeError(f"Server-side error during script execution: {str(e)}")

@remote_method
def run_python_script_from_file(
Expand All @@ -343,7 +346,59 @@ def clear(self):
@property
@remote_method
def project_directory(self):
return self._app.execute_script("ExtAPI.DataModel.Project.ProjectDirectory")
return self._app.execute_script("""ExtAPI.DataModel.Project.ProjectDirectory""")

@remote_method
def list_files(self):
list = []
mechdbPath = self._app.execute_script("""ExtAPI.DataModel.Project.FilePath""")
# currently the mechdb is not stored under project directory
# replace this with python API once it is available
if mechdbPath != "":
list.append(mechdbPath)
rootDir = self._app.execute_script("""ExtAPI.DataModel.Project.ProjectDirectory""")

for dirPath, dirNames, fileNames in os.walk(rootDir):
for fileName in fileNames:
list.append(os.path.join(dirPath, fileName))
files_out = "\n".join(list).splitlines()
if not files_out: # pragma: no cover
print("No files listed")
return files_out

@remote_method
def _get_files(self, files, recursive=False):
self_files = self.list_files() # to avoid calling it too much

if isinstance(files, str):
if files in self_files:
list_files = [files]
elif "*" in files:
list_files = fnmatch.filter(self_files, files)
if not list_files:
raise ValueError(
f"The `'files'` parameter ({files}) didn't match any file using "
f"glob expressions in the remote server."
)
else:
raise ValueError(
f"The `'files'` parameter ('{files}') does not match any file or pattern."
)

elif isinstance(files, (list, tuple)):
if not all([isinstance(each, str) for each in files]):
raise ValueError(
"The parameter `'files'` can be a list or tuple, but it "
"should only contain strings."
)
list_files = files
else:
raise ValueError(
f"The `file` parameter type ({type(files)}) is not supported."
"Only strings, tuple of strings, or list of strings are allowed."
)

return list_files


class MechanicalDefaultServer(MechanicalEmbeddedServer):
Expand Down
7 changes: 2 additions & 5 deletions src/ansys/mechanical/core/embedding/rpc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class remote_method:

def __init__(self, func):
"""Initialize with the given function."""
print("init remote_method")
self._func = func

def __call__(self, *args, **kwargs):
Expand All @@ -48,12 +47,10 @@ def __get__(self, obj, objtype):
"""Return a partially applied method."""
from functools import partial

print("getting func")
func = partial(self.__call_method__, obj)
func._is_remote = True
func.__name__ = self._func.__name__
func._owner = obj
print("done getting func")
return func


Expand All @@ -63,11 +60,11 @@ class MethodType:


def try_get_remote_method(methodname: str, obj: typing.Any) -> typing.Tuple[str, typing.Callable]:
print(f"checking if {methodname} is callable and remote")
#print(f"checking if {methodname} is callable and remote")
method = getattr(obj, methodname)
if not callable(method):
return None
print(f"yielding {methodname}")
#print(f"yielding {methodname}")
if hasattr(method, "_is_remote") and method._is_remote is True:
return (methodname, method)

Expand Down
75 changes: 75 additions & 0 deletions tests/assets/hsec.x_t
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
**ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz**************************
**PARASOLID !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~0123456789**************************
**PART1;
MC=x86;
MC_MODEL=x86 Family 6 Model 3 Stepping 4, GenuineIntel;
MC_ID=unknown;
OS=Windows_NT;
OS_RELEASE=unknown;
FRU=Parasolid Version 10.0, build 211, 10-16-1998;
APPL=SolidWorks 98Plus-1998293;
SITE=;
USER=unknown;
FORMAT=text;
GUISE=transmit;
KEY=HSEC;
FILE=HSEC.x_t;
DATE=Wed Dec 23 09:41:54 1998;
**PART2;
SCH=SCH_1000211_10004;
USFLD_SIZE=0;
**PART3;
**END_OF_HEADER*****************************************************************
T51 : TRANSMIT FILE created by modeller version 100021117 SCH_1000211_100040 10
1 6 0 0 0 0 0 0 0 1e3 1e-8 0 0 0 1 0 1 2 11 2 5 0 1 3 4 1 5 0 5 0 12 3 117 6 7
0 0 0 0 1e3 1e-8 2 8 0 1 0 1 1 9 10 11 12 13 14 15 100 4 6 2 0 0 1 0 0 0 1 0 0
0 1 -971445146547012e-31 0 .33 1 1 ?11 5 3 0 1 3 16 1 17 2 17 2 100 16 4 5 0 0
1 2636785530864285e-31 -2101593308484715e-47 -3433562374547175e-48 174963799595
9505e-31 1 2636785530864285e-31 -1 1749637995959505e-31 -90205620750794e-30 .135
.33 1 3 ?11 17 1 0 1 3 18 1 0 5 0 5 100 18 2 17 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0
?81 1 6 117 19 3 0 0 0 0 20 70 7 0 3 0 0 4 2 20 4 21 21 1 T13 9 25 0 3 0 22 0 0
23 0 50 10 91 0 24 25 0 0 +0 0 .03 0 1 0 0 0 1 30 11 87 0 14 26 0 0 +0 0 .03 0
0 -1 29 12 23 0 15 27 0 0 0 .03 19 13 37 0 3 23 0 28 V16 14 81 0 ?29 0 30 11 0
0 3 18 15 22 0 31 0 32 12 ?3 17 31 33 34 35 15 29 14 0 36 -18 32 20 0 37 15 38
27 ?3 17 37 39 40 41 32 42 30 0 43 -18 38 18 0 44 32 45 46 ?3 29 27 21 0 32 46
12 .1 0 .03 29 46 19 0 38 47 27 .1 .3 .03 29 47 17 0 45 48 46 0 .3 .03 18 45 16
0 49 38 50 47 ?3 29 48 70 0 50 51 47 0 0 0 18 50 55 0 29 45 52 48 ?3 29 51 71 0
52 53 48 .1 0 0 18 52 58 0 42 50 54 51 ?3 29 53 72 0 54 55 51 .1 .3 0 18 54 57
0 56 52 57 53 ?3 29 55 73 0 57 0 53 0 .3 0 18 57 56 0 58 54 0 55 ?3 17 58 33 35
34 57 49 59 0 60 +15 33 82 0 31 22 0 17 35 33 31 58 50 60 61 0 62 -17 34 33 58
31 45 36 63 0 64 +17 49 65 66 67 45 58 59 0 34 -16 59 74 0 ?58 68 69 70 0 0 3 1
7 60 71 72 62 57 35 61 0 67 +15 71 62 0 60 73 0 17 72 71 74 60 54 67 75 0 76 -17
62 71 60 74 50 41 77 0 0 -16 61 47 0 ?60 75 0 78 0 0 3 17 67 65 49 56 57 72 75
0 0 +15 65 34 0 66 79 0 17 56 65 67 66 54 44 68 0 72 +16 75 43 0 ?67 80 61 81 0
0 3 16 80 51 0 ?74 77 75 82 0 0 3 30 81 68 0 75 78 82 0 +.1 .3 0 -1 0 0 30 78 6
9 0 61 0 81 0 +0 0 0 0 1 0 30 82 67 0 80 81 83 0 +.1 .3 0 -462592926927149e-31 -
1 0 30 83 66 0 77 82 84 0 +0 0 0 1 0 0 16 77 39 0 ?41 63 80 83 0 0 3 30 84 5 0 6
3 83 85 0 +0 0 .03 0 1 0 16 63 4 0 ?34 86 77 84 0 0 3 30 85 8 0 86 84 87 0 +.1 .
3 .03 -1 0 0 16 86 7 0 ?64 88 63 85 0 0 3 30 87 11 0 88 85 89 0 +.1 .3 .03 -4625
92926927149e-31 -1 0 16 88 10 0 ?43 69 86 87 0 0 3 30 89 14 0 69 87 70 0 +0 0 .0
3 1 0 0 16 69 13 0 ?90 59 88 89 0 0 3 30 70 84 0 59 89 91 0 +0 .3 .03 0 0 -1 30
91 85 0 68 70 26 0 +.1 .3 .03 0 0 -1 16 68 75 0 ?56 30 59 91 0 0 3 30 26 86 0 3
0 91 11 0 +.1 0 .03 0 0 -1 16 30 78 0 ?42 14 68 26 0 0 3 17 42 92 76 43 52 37 30
0 74 +15 92 76 0 44 93 0 17 76 92 44 42 54 74 80 0 0 -17 43 92 42 44 32 94 88 0
90 +17 74 71 62 72 52 76 80 0 41 +17 41 39 37 29 52 62 77 0 0 +15 39 79 0 37 24
0 17 29 39 41 40 50 31 14 0 35 +17 40 39 29 37 15 90 69 0 0 -17 90 95 94 36 32
40 69 0 0 +15 95 3 0 36 96 0 17 94 95 64 90 38 43 88 0 0 -17 36 95 90 64 15 34
63 0 40 -17 64 95 36 94 45 66 86 0 0 +17 66 65 56 49 38 64 86 0 94 -14 96 1 97
?73 79 95 9 98 +0 0 79 93 28 81 1 97 115 99 96 0 0 100 101 102 14 73 61 101 ?0
96 71 9 103 -0 0 0 79 28 14 79 35 100 ?96 93 65 9 104 -0 0 73 96 28 50 98 2 0 9
6 103 105 0 +0 .3 .03 0 0 1 1 0 0 14 93 77 106 ?79 24 92 9 25 +0 0 96 24 28 13 2
8 36 0 0 0 0 0 0 13 22 14 22 83 107 ?24 0 33 9 105 +0 0 24 0 28 81 1 107 111 99
22 0 0 0 108 109 14 24 80 108 ?93 22 39 9 10 -0 0 93 22 28 50 105 88 0 22 98 10
4 0 +0 0 .03 -1 0 0 0 0 1 50 104 89 0 79 105 25 0 +.1 .3 .03 0 -1 0 0 0 -1 50 25
90 0 93 104 10 0 +.1 .3 .03 1 -462592926927149e-31 0 462592926927149e-31 1 0 81
1 108 112 99 24 0 0 107 106 110 80 1 99 0 111 8001 0 0 0 0 3 5 0 0 FFFFTFTFFFFF
F2 81 1 106 113 99 93 0 0 108 100 112 83 3 110 .752941176470588 .752941176470588
.752941176470588 81 1 100 114 99 79 0 0 106 97 113 83 3 112 .752941176470588 .7
52941176470588 .752941176470588 83 3 113 .752941176470588 .752941176470588 .7529
41176470588 79 15 111 SDL/TYSA_COLOUR83 3 109 .752941176470588 .752941176470588
.752941176470588 50 103 65 0 73 0 98 0 +0 .3 0 0 0 1 1 0 0 81 1 101 116 99 73 0
0 97 0 114 83 3 114 .752941176470588 .752941176470588 .752941176470588 83 3 102
.752941176470588 .752941176470588 .752941176470588 17 44 92 43 76 38 56 68 0 66
-19 23 24 0 3 0 13 9 S74 20 21 2 0 101 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 80
1 19 115 116 8017 0 0 0 0 0 0 0 0 TTTTTTTTTTTTT3 84 7 20 rectbar79 13 116 SDL/T
YSA_NAME1 0

0 comments on commit c38f577

Please sign in to comment.