Skip to content

Commit 509228a

Browse files
Mostieri/nexus ansys versioning (#86)
1 parent 3134fd3 commit 509228a

File tree

4 files changed

+114
-33
lines changed

4 files changed

+114
-33
lines changed

src/ansys/dynamicreporting/core/adr_report.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ def export_html(
310310
query=query,
311311
filename=filename,
312312
no_inline_files=no_inline_files,
313+
ansys_version=self.service._ansys_version,
313314
)
314315
success = True
315316
except Exception as e: # pragma: no cover

src/ansys/dynamicreporting/core/adr_service.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,9 @@ def connect(
310310
if self._url is not None: # pragma: no cover
311311
self.logger.warning("Already connected to a dynamic reporting service.\n")
312312
return
313-
self.serverobj = report_remote_server.Server(url=url, username=username, password=password)
313+
self.serverobj = report_remote_server.Server(
314+
url=url, username=username, password=password, ansys_version=self._ansys_version
315+
)
314316
try:
315317
self.serverobj.validate()
316318
except Exception:
@@ -471,7 +473,10 @@ def start(
471473
self.logger.error(f"Service started on port {self._port}")
472474
raise StartingServiceError
473475
self.serverobj = report_remote_server.Server(
474-
url=f"http://127.0.0.1:{self._port}", username=username, password=password
476+
url=f"http://127.0.0.1:{self._port}",
477+
username=username,
478+
password=password,
479+
ansys_version=self._ansys_version,
475480
)
476481

477482
else: # pragma: no cover

src/ansys/dynamicreporting/core/utils/report_download_html.py

Lines changed: 83 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
import base64
22
import os
33
import os.path
4+
import pathlib
5+
import platform
6+
import re
47
from typing import Optional
58
import urllib.parse
69

710
import requests
811

912
# TODO:
1013
# Improve MathJax download
14+
ANSYS_VERSION_FALLBACK = "242"
1115

1216

1317
class ReportDownloadHTML:
1418
def __init__(
15-
self, url=None, directory=None, debug=False, filename="index.html", no_inline_files=False
19+
self,
20+
url=None,
21+
directory=None,
22+
debug=False,
23+
filename="index.html",
24+
no_inline_files=False,
25+
ansys_version=None,
1626
):
1727
# Make sure that the print query has been specified. Set it to html if not set
1828
if url:
@@ -25,7 +35,11 @@ def __init__(
2535
query = "print=html"
2636
parsed._replace(query=query)
2737
url = urllib.parse.urlunparse(parsed)
28-
38+
self._ansys_version = str(ANSYS_VERSION_FALLBACK)
39+
if ansys_version:
40+
self._ansys_version = str(ansys_version)
41+
if int(self._ansys_version) < 242:
42+
self._ansys_version = ""
2943
self._url = url
3044
self._directory = directory
3145
self._filename = filename
@@ -73,7 +87,7 @@ def _replace_files(self, text: str, inline: bool = False, size_check: bool = Fal
7387
current = 0
7488
while True:
7589
try:
76-
idx1 = text.index("/static/ansys/", current)
90+
idx1 = text.index(f"/static/ansys{self._ansys_version}/", current)
7791
except ValueError:
7892
try:
7993
idx1 = text.index("/static/", current)
@@ -82,7 +96,7 @@ def _replace_files(self, text: str, inline: bool = False, size_check: bool = Fal
8296
idx1 = text.index("/media/", current)
8397
except ValueError:
8498
try:
85-
idx1 = text.index("/ansys/", current)
99+
idx1 = text.index(f"/ansys{self._ansys_version}/", current)
86100
except ValueError:
87101
return text
88102
quote = text[idx1 - 1]
@@ -187,15 +201,24 @@ def _download_special_files(self):
187201
"play.png",
188202
]
189203
self._download_static_files(
190-
images, "/ansys/nexus/images/", "ansys/nexus/images/", "viewer images II"
204+
images,
205+
f"/ansys{self._ansys_version}/nexus/images/",
206+
f"ansys{self._ansys_version}/nexus/images/",
207+
"viewer images II",
191208
)
192209
images = ["js-inflate.js", "js-unzip.js", "jquery.min.js"]
193210
self._download_static_files(
194-
images, "/ansys/nexus/utils/", "ansys/nexus/utils/", "viewer javascript support"
211+
images,
212+
f"/ansys{self._ansys_version}/nexus/utils/",
213+
f"ansys{self._ansys_version}/nexus/utils/",
214+
"viewer javascript support",
195215
)
196216
images = ["ANSYSViewer_min.js", "viewer-loader.js"]
197217
self._download_static_files(
198-
images, "/ansys/nexus/", "ansys/nexus/", "ansys-nexus-viewer js"
218+
images,
219+
f"/ansys{self._ansys_version}/nexus/",
220+
f"ansys{self._ansys_version}/nexus/",
221+
"ansys-nexus-viewer js",
199222
)
200223
images = [
201224
"jquery.contextMenu.min.css",
@@ -204,8 +227,8 @@ def _download_special_files(self):
204227
]
205228
self._download_static_files(
206229
images,
207-
"/ansys/nexus/novnc/vendor/jQuery-contextMenu/",
208-
"ansys/nexus/novnc/vendor/jQuery-contextMenu",
230+
f"/ansys{self._ansys_version}/nexus/novnc/vendor/jQuery-contextMenu/",
231+
f"ansys{self._ansys_version}/nexus/novnc/vendor/jQuery-contextMenu",
209232
"ansys-nexus-viewer vnc js",
210233
)
211234

@@ -217,7 +240,10 @@ def _download_special_files(self):
217240
"three.js",
218241
]
219242
self._download_static_files(
220-
image, "/ansys/nexus/threejs/", "ansys/nexus/threejs", "threejs core"
243+
image,
244+
f"/ansys{self._ansys_version}/nexus/threejs/",
245+
f"ansys{self._ansys_version}/nexus/threejs",
246+
"threejs core",
221247
)
222248

223249
image = [
@@ -228,14 +254,14 @@ def _download_special_files(self):
228254
]
229255
self._download_static_files(
230256
image,
231-
"/ansys/nexus/threejs/libs/draco/",
232-
"ansys/nexus/threejs/libs/draco",
257+
f"/ansys{self._ansys_version}/nexus/threejs/libs/draco/",
258+
f"ansys{self._ansys_version}/nexus/threejs/libs/draco",
233259
"threejs draco",
234260
)
235261
self._download_static_files(
236262
image,
237-
"/ansys/nexus/threejs/libs/draco/gltf/",
238-
"ansys/nexus/threejs/libs/draco/gltf",
263+
f"/ansys{self._ansys_version}/nexus/threejs/libs/draco/gltf/",
264+
f"ansys{self._ansys_version}/nexus/threejs/libs/draco/gltf",
239265
"threejs draco gltf",
240266
)
241267

@@ -250,15 +276,17 @@ def _download_special_files(self):
250276
self._download_static_files(fonts, "/static/website/webfonts/", "webfonts", "fonts")
251277

252278
@staticmethod
253-
def _fix_viewer_component_paths(filename, data):
279+
def _fix_viewer_component_paths(filename, data, ansys_version):
254280
# Special case for AVZ viewer: ANSYSViewer_min.js to set the base path for images
255281
if filename.endswith("ANSYSViewer_min.js"):
256282
data = data.decode("utf-8")
257283
data = data.replace(
258284
'"/static/website/images/"',
259285
r'document.URL.replace(/\\/g, "/").replace("index.html", "media/")',
260286
)
261-
data = data.replace('"/ansys/nexus/images/', '"./ansys/nexus/images/')
287+
data = data.replace(
288+
f'"/ansys{ansys_version}/nexus/images/', f'"./ansys{ansys_version}//nexus/images/'
289+
)
262290
# this one is interesting. by default, AVZ will throw an error if you attempt to read
263291
# a "file://" protocol src. In offline mode, if we are not using data URIs, then we
264292
# need to lie to the AVZ core and tell it to go ahead and try.
@@ -267,7 +295,9 @@ def _fix_viewer_component_paths(filename, data):
267295
# Special case for the AVZ viewer web component (loading proxy images and play arrow)
268296
elif filename.endswith("viewer-loader.js"):
269297
data = data.decode("utf-8")
270-
data = data.replace('"/ansys/nexus/images/', '"./ansys/nexus/images/')
298+
data = data.replace(
299+
f'"/ansys{ansys_version}/nexus/images/', f'"./ansys{ansys_version}//nexus/images/'
300+
)
271301
data = data.encode("utf-8")
272302
return data
273303

@@ -277,9 +307,12 @@ def _download_static_files(self, files, source_path, target_path, comment):
277307
url = tmp.scheme + "://" + tmp.netloc + source_path + f
278308
resp = requests.get(url, allow_redirects=True)
279309
if resp.status_code == requests.codes.ok:
280-
filename = os.path.join(self._directory, target_path, f)
310+
filename = self._directory + os.sep + target_path + os.sep + f
311+
filename = os.path.normpath(filename)
281312
try:
282-
data = self._fix_viewer_component_paths(filename, resp.content)
313+
data = self._fix_viewer_component_paths(
314+
str(filename), resp.content, self._ansys_version
315+
)
283316
open(filename, "wb").write(data)
284317
except Exception:
285318
print(f"Unable to download {comment}: {f}")
@@ -329,9 +362,9 @@ def _get_file(self, path_plus_queries: str, pathname: str, inline: bool = False)
329362
# we need to prefix the .bin file and scene.js file with the GUID
330363
basename = f"{os.path.basename(os.path.dirname(pathname))}_{basename}"
331364
else:
332-
tmp = self._fix_viewer_component_paths(basename, tmp)
365+
tmp = self._fix_viewer_component_paths(basename, tmp, self._ansys_version)
333366
# get the output filename
334-
if pathname.startswith("/static/ansys/"):
367+
if pathname.startswith(f"/static/ansys{self._ansys_version}/"):
335368
# if the content is part of the /ansys/ namespace, we keep the namespace,
336369
# but remove the /static prefix
337370
local_pathname = os.path.dirname(pathname).replace("/static/", "./")
@@ -361,7 +394,11 @@ def _find_block(text: str, start: int, prefix: int, suffix: str) -> (int, int, s
361394
return -1, -1, ""
362395
idx2 += len(suffix)
363396
block = text[idx1:idx2]
364-
if ("/media/" in block) or ("/static/" in block) or ("/ansys/" in block):
397+
if (
398+
("/media/" in block)
399+
or ("/static/" in block)
400+
or (re.match(r"/ansys([0-9]+)", block))
401+
):
365402
return idx1, idx2, text[idx1:idx2]
366403
start = idx2
367404

@@ -452,10 +489,29 @@ def _download(self):
452489
self._make_dir([self._directory, "media", "jax", "input", "AsciiMath"])
453490
self._make_dir([self._directory, "media", "images"])
454491
self._make_dir([self._directory, "webfonts"])
455-
self._make_dir([self._directory, "ansys", "nexus", "images"])
456-
self._make_dir([self._directory, "ansys", "nexus", "utils"])
457-
self._make_dir([self._directory, "ansys", "nexus", "threejs", "libs", "draco", "gltf"])
458-
self._make_dir([self._directory, "ansys", "nexus", "novnc", "vendor", "jQuery-contextMenu"])
492+
self._make_dir([self._directory, f"ansys{self._ansys_version}", "nexus", "images"])
493+
self._make_dir([self._directory, f"ansys{self._ansys_version}", "nexus", "utils"])
494+
self._make_dir(
495+
[
496+
self._directory,
497+
f"ansys{self._ansys_version}",
498+
"nexus",
499+
"threejs",
500+
"libs",
501+
"draco",
502+
"gltf",
503+
]
504+
)
505+
self._make_dir(
506+
[
507+
self._directory,
508+
f"ansys{self._ansys_version}",
509+
"nexus",
510+
"novnc",
511+
"vendor",
512+
"jQuery-contextMenu",
513+
]
514+
)
459515

460516
# get the webpage html source
461517
resp = requests.get(self._url)
@@ -480,7 +536,7 @@ def _download(self):
480536
# <img src="/media/7d6838fe-f28d-11e8-a5aa-1c1b0da59167_image.png" class="img-responsive"
481537
# ... style="margin: 0 auto; display:flex; justify-content:center;" alt="Image file not found">
482538
# in viewer-loader.js - this is handled in a special way
483-
# <img class="ansys-nexus-play" id="proxy-play" src="/ansys/nexus/images/play.png">
539+
# <img class="ansys-nexus-play" id="proxy-play" src="/ansys###/nexus/images/play.png">
484540
# video
485541
# <source src="/media/4a87c6c0-f34b-11e8-871b-1c1b0da59167_movie.mp4" type="video/mp4" />
486542
# slider template

src/ansys/dynamicreporting/core/utils/report_remote_server.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class Server:
103103
Implements REST protocols.
104104
"""
105105

106-
def __init__(self, url=None, username=None, password=None):
106+
def __init__(self, url=None, username=None, password=None, ansys_version=None):
107107
# Check on the validity of url formatting
108108
if url is not None:
109109
o = urlparse(url)
@@ -112,6 +112,7 @@ def __init__(self, url=None, username=None, password=None):
112112
if print_allowed():
113113
print("Error: invalid URL. Setting it to None")
114114
url = None
115+
self._ansys_version = ansys_version
115116
self.cur_url = url
116117
self.cur_username = username
117118
self.cur_password = password
@@ -858,7 +859,13 @@ def build_url_with_query(self, report_guid, query, rest_api=False):
858859
return url
859860

860861
def export_report_as_html(
861-
self, report_guid, directory_name, query=None, filename="index.html", no_inline_files=False
862+
self,
863+
report_guid,
864+
directory_name,
865+
query=None,
866+
filename="index.html",
867+
no_inline_files=False,
868+
ansys_version=None,
862869
):
863870
if query is None:
864871
query = {}
@@ -867,8 +874,15 @@ def export_report_as_html(
867874
from ansys.dynamicreporting.core.utils.report_download_html import ReportDownloadHTML
868875

869876
url = self.build_url_with_query(report_guid, query)
877+
_ansys_version = self._ansys_version
878+
if ansys_version:
879+
_ansys_version = ansys_version
870880
worker = ReportDownloadHTML(
871-
url=url, directory=directory_path, filename=filename, no_inline_files=no_inline_files
881+
url=url,
882+
directory=directory_path,
883+
filename=filename,
884+
no_inline_files=no_inline_files,
885+
ansys_version=_ansys_version,
872886
)
873887
worker.download()
874888

@@ -1491,7 +1505,12 @@ def launch_local_database_server(
14911505
# Check to see if there is already a server running on this URI
14921506
# build a server and try it
14931507

1494-
tmp_server = Server(url=f"http://127.0.0.1:{port}", username=username, password=password)
1508+
tmp_server = Server(
1509+
url=f"http://127.0.0.1:{port}",
1510+
username=username,
1511+
password=password,
1512+
ansys_version=ansys_version,
1513+
)
14951514
try:
14961515
# validate will throw exceptions or return a float.
14971516
_ = tmp_server.validate()

0 commit comments

Comments
 (0)