Skip to content

Commit

Permalink
Mostieri/nexus ansys versioning (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariostieriansys authored Jan 10, 2024
1 parent 3134fd3 commit 509228a
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 33 deletions.
1 change: 1 addition & 0 deletions src/ansys/dynamicreporting/core/adr_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ def export_html(
query=query,
filename=filename,
no_inline_files=no_inline_files,
ansys_version=self.service._ansys_version,
)
success = True
except Exception as e: # pragma: no cover
Expand Down
9 changes: 7 additions & 2 deletions src/ansys/dynamicreporting/core/adr_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,9 @@ def connect(
if self._url is not None: # pragma: no cover
self.logger.warning("Already connected to a dynamic reporting service.\n")
return
self.serverobj = report_remote_server.Server(url=url, username=username, password=password)
self.serverobj = report_remote_server.Server(
url=url, username=username, password=password, ansys_version=self._ansys_version
)
try:
self.serverobj.validate()
except Exception:
Expand Down Expand Up @@ -471,7 +473,10 @@ def start(
self.logger.error(f"Service started on port {self._port}")
raise StartingServiceError
self.serverobj = report_remote_server.Server(
url=f"http://127.0.0.1:{self._port}", username=username, password=password
url=f"http://127.0.0.1:{self._port}",
username=username,
password=password,
ansys_version=self._ansys_version,
)

else: # pragma: no cover
Expand Down
110 changes: 83 additions & 27 deletions src/ansys/dynamicreporting/core/utils/report_download_html.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import base64
import os
import os.path
import pathlib
import platform
import re
from typing import Optional
import urllib.parse

import requests

# TODO:
# Improve MathJax download
ANSYS_VERSION_FALLBACK = "242"


class ReportDownloadHTML:
def __init__(
self, url=None, directory=None, debug=False, filename="index.html", no_inline_files=False
self,
url=None,
directory=None,
debug=False,
filename="index.html",
no_inline_files=False,
ansys_version=None,
):
# Make sure that the print query has been specified. Set it to html if not set
if url:
Expand All @@ -25,7 +35,11 @@ def __init__(
query = "print=html"
parsed._replace(query=query)
url = urllib.parse.urlunparse(parsed)

self._ansys_version = str(ANSYS_VERSION_FALLBACK)
if ansys_version:
self._ansys_version = str(ansys_version)
if int(self._ansys_version) < 242:
self._ansys_version = ""
self._url = url
self._directory = directory
self._filename = filename
Expand Down Expand Up @@ -73,7 +87,7 @@ def _replace_files(self, text: str, inline: bool = False, size_check: bool = Fal
current = 0
while True:
try:
idx1 = text.index("/static/ansys/", current)
idx1 = text.index(f"/static/ansys{self._ansys_version}/", current)
except ValueError:
try:
idx1 = text.index("/static/", current)
Expand All @@ -82,7 +96,7 @@ def _replace_files(self, text: str, inline: bool = False, size_check: bool = Fal
idx1 = text.index("/media/", current)
except ValueError:
try:
idx1 = text.index("/ansys/", current)
idx1 = text.index(f"/ansys{self._ansys_version}/", current)
except ValueError:
return text
quote = text[idx1 - 1]
Expand Down Expand Up @@ -187,15 +201,24 @@ def _download_special_files(self):
"play.png",
]
self._download_static_files(
images, "/ansys/nexus/images/", "ansys/nexus/images/", "viewer images II"
images,
f"/ansys{self._ansys_version}/nexus/images/",
f"ansys{self._ansys_version}/nexus/images/",
"viewer images II",
)
images = ["js-inflate.js", "js-unzip.js", "jquery.min.js"]
self._download_static_files(
images, "/ansys/nexus/utils/", "ansys/nexus/utils/", "viewer javascript support"
images,
f"/ansys{self._ansys_version}/nexus/utils/",
f"ansys{self._ansys_version}/nexus/utils/",
"viewer javascript support",
)
images = ["ANSYSViewer_min.js", "viewer-loader.js"]
self._download_static_files(
images, "/ansys/nexus/", "ansys/nexus/", "ansys-nexus-viewer js"
images,
f"/ansys{self._ansys_version}/nexus/",
f"ansys{self._ansys_version}/nexus/",
"ansys-nexus-viewer js",
)
images = [
"jquery.contextMenu.min.css",
Expand All @@ -204,8 +227,8 @@ def _download_special_files(self):
]
self._download_static_files(
images,
"/ansys/nexus/novnc/vendor/jQuery-contextMenu/",
"ansys/nexus/novnc/vendor/jQuery-contextMenu",
f"/ansys{self._ansys_version}/nexus/novnc/vendor/jQuery-contextMenu/",
f"ansys{self._ansys_version}/nexus/novnc/vendor/jQuery-contextMenu",
"ansys-nexus-viewer vnc js",
)

Expand All @@ -217,7 +240,10 @@ def _download_special_files(self):
"three.js",
]
self._download_static_files(
image, "/ansys/nexus/threejs/", "ansys/nexus/threejs", "threejs core"
image,
f"/ansys{self._ansys_version}/nexus/threejs/",
f"ansys{self._ansys_version}/nexus/threejs",
"threejs core",
)

image = [
Expand All @@ -228,14 +254,14 @@ def _download_special_files(self):
]
self._download_static_files(
image,
"/ansys/nexus/threejs/libs/draco/",
"ansys/nexus/threejs/libs/draco",
f"/ansys{self._ansys_version}/nexus/threejs/libs/draco/",
f"ansys{self._ansys_version}/nexus/threejs/libs/draco",
"threejs draco",
)
self._download_static_files(
image,
"/ansys/nexus/threejs/libs/draco/gltf/",
"ansys/nexus/threejs/libs/draco/gltf",
f"/ansys{self._ansys_version}/nexus/threejs/libs/draco/gltf/",
f"ansys{self._ansys_version}/nexus/threejs/libs/draco/gltf",
"threejs draco gltf",
)

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

@staticmethod
def _fix_viewer_component_paths(filename, data):
def _fix_viewer_component_paths(filename, data, ansys_version):
# Special case for AVZ viewer: ANSYSViewer_min.js to set the base path for images
if filename.endswith("ANSYSViewer_min.js"):
data = data.decode("utf-8")
data = data.replace(
'"/static/website/images/"',
r'document.URL.replace(/\\/g, "/").replace("index.html", "media/")',
)
data = data.replace('"/ansys/nexus/images/', '"./ansys/nexus/images/')
data = data.replace(
f'"/ansys{ansys_version}/nexus/images/', f'"./ansys{ansys_version}//nexus/images/'
)
# this one is interesting. by default, AVZ will throw an error if you attempt to read
# a "file://" protocol src. In offline mode, if we are not using data URIs, then we
# need to lie to the AVZ core and tell it to go ahead and try.
Expand All @@ -267,7 +295,9 @@ def _fix_viewer_component_paths(filename, data):
# Special case for the AVZ viewer web component (loading proxy images and play arrow)
elif filename.endswith("viewer-loader.js"):
data = data.decode("utf-8")
data = data.replace('"/ansys/nexus/images/', '"./ansys/nexus/images/')
data = data.replace(
f'"/ansys{ansys_version}/nexus/images/', f'"./ansys{ansys_version}//nexus/images/'
)
data = data.encode("utf-8")
return data

Expand All @@ -277,9 +307,12 @@ def _download_static_files(self, files, source_path, target_path, comment):
url = tmp.scheme + "://" + tmp.netloc + source_path + f
resp = requests.get(url, allow_redirects=True)
if resp.status_code == requests.codes.ok:
filename = os.path.join(self._directory, target_path, f)
filename = self._directory + os.sep + target_path + os.sep + f
filename = os.path.normpath(filename)
try:
data = self._fix_viewer_component_paths(filename, resp.content)
data = self._fix_viewer_component_paths(
str(filename), resp.content, self._ansys_version
)
open(filename, "wb").write(data)
except Exception:
print(f"Unable to download {comment}: {f}")
Expand Down Expand Up @@ -329,9 +362,9 @@ def _get_file(self, path_plus_queries: str, pathname: str, inline: bool = False)
# we need to prefix the .bin file and scene.js file with the GUID
basename = f"{os.path.basename(os.path.dirname(pathname))}_{basename}"
else:
tmp = self._fix_viewer_component_paths(basename, tmp)
tmp = self._fix_viewer_component_paths(basename, tmp, self._ansys_version)
# get the output filename
if pathname.startswith("/static/ansys/"):
if pathname.startswith(f"/static/ansys{self._ansys_version}/"):
# if the content is part of the /ansys/ namespace, we keep the namespace,
# but remove the /static prefix
local_pathname = os.path.dirname(pathname).replace("/static/", "./")
Expand Down Expand Up @@ -361,7 +394,11 @@ def _find_block(text: str, start: int, prefix: int, suffix: str) -> (int, int, s
return -1, -1, ""
idx2 += len(suffix)
block = text[idx1:idx2]
if ("/media/" in block) or ("/static/" in block) or ("/ansys/" in block):
if (
("/media/" in block)
or ("/static/" in block)
or (re.match(r"/ansys([0-9]+)", block))
):
return idx1, idx2, text[idx1:idx2]
start = idx2

Expand Down Expand Up @@ -452,10 +489,29 @@ def _download(self):
self._make_dir([self._directory, "media", "jax", "input", "AsciiMath"])
self._make_dir([self._directory, "media", "images"])
self._make_dir([self._directory, "webfonts"])
self._make_dir([self._directory, "ansys", "nexus", "images"])
self._make_dir([self._directory, "ansys", "nexus", "utils"])
self._make_dir([self._directory, "ansys", "nexus", "threejs", "libs", "draco", "gltf"])
self._make_dir([self._directory, "ansys", "nexus", "novnc", "vendor", "jQuery-contextMenu"])
self._make_dir([self._directory, f"ansys{self._ansys_version}", "nexus", "images"])
self._make_dir([self._directory, f"ansys{self._ansys_version}", "nexus", "utils"])
self._make_dir(
[
self._directory,
f"ansys{self._ansys_version}",
"nexus",
"threejs",
"libs",
"draco",
"gltf",
]
)
self._make_dir(
[
self._directory,
f"ansys{self._ansys_version}",
"nexus",
"novnc",
"vendor",
"jQuery-contextMenu",
]
)

# get the webpage html source
resp = requests.get(self._url)
Expand All @@ -480,7 +536,7 @@ def _download(self):
# <img src="/media/7d6838fe-f28d-11e8-a5aa-1c1b0da59167_image.png" class="img-responsive"
# ... style="margin: 0 auto; display:flex; justify-content:center;" alt="Image file not found">
# in viewer-loader.js - this is handled in a special way
# <img class="ansys-nexus-play" id="proxy-play" src="/ansys/nexus/images/play.png">
# <img class="ansys-nexus-play" id="proxy-play" src="/ansys###/nexus/images/play.png">
# video
# <source src="/media/4a87c6c0-f34b-11e8-871b-1c1b0da59167_movie.mp4" type="video/mp4" />
# slider template
Expand Down
27 changes: 23 additions & 4 deletions src/ansys/dynamicreporting/core/utils/report_remote_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class Server:
Implements REST protocols.
"""

def __init__(self, url=None, username=None, password=None):
def __init__(self, url=None, username=None, password=None, ansys_version=None):
# Check on the validity of url formatting
if url is not None:
o = urlparse(url)
Expand All @@ -112,6 +112,7 @@ def __init__(self, url=None, username=None, password=None):
if print_allowed():
print("Error: invalid URL. Setting it to None")
url = None
self._ansys_version = ansys_version
self.cur_url = url
self.cur_username = username
self.cur_password = password
Expand Down Expand Up @@ -858,7 +859,13 @@ def build_url_with_query(self, report_guid, query, rest_api=False):
return url

def export_report_as_html(
self, report_guid, directory_name, query=None, filename="index.html", no_inline_files=False
self,
report_guid,
directory_name,
query=None,
filename="index.html",
no_inline_files=False,
ansys_version=None,
):
if query is None:
query = {}
Expand All @@ -867,8 +874,15 @@ def export_report_as_html(
from ansys.dynamicreporting.core.utils.report_download_html import ReportDownloadHTML

url = self.build_url_with_query(report_guid, query)
_ansys_version = self._ansys_version
if ansys_version:
_ansys_version = ansys_version
worker = ReportDownloadHTML(
url=url, directory=directory_path, filename=filename, no_inline_files=no_inline_files
url=url,
directory=directory_path,
filename=filename,
no_inline_files=no_inline_files,
ansys_version=_ansys_version,
)
worker.download()

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

tmp_server = Server(url=f"http://127.0.0.1:{port}", username=username, password=password)
tmp_server = Server(
url=f"http://127.0.0.1:{port}",
username=username,
password=password,
ansys_version=ansys_version,
)
try:
# validate will throw exceptions or return a float.
_ = tmp_server.validate()
Expand Down

0 comments on commit 509228a

Please sign in to comment.