11import base64
22import os
33import os .path
4+ import pathlib
5+ import platform
6+ import re
47from typing import Optional
58import urllib .parse
69
710import requests
811
912# TODO:
1013# Improve MathJax download
14+ ANSYS_VERSION_FALLBACK = "242"
1115
1216
1317class 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
0 commit comments