1
1
import base64
2
2
import os
3
3
import os .path
4
+ import pathlib
5
+ import platform
6
+ import re
4
7
from typing import Optional
5
8
import urllib .parse
6
9
7
10
import requests
8
11
9
12
# TODO:
10
13
# Improve MathJax download
14
+ ANSYS_VERSION_FALLBACK = "242"
11
15
12
16
13
17
class ReportDownloadHTML :
14
18
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 ,
16
26
):
17
27
# Make sure that the print query has been specified. Set it to html if not set
18
28
if url :
@@ -25,7 +35,11 @@ def __init__(
25
35
query = "print=html"
26
36
parsed ._replace (query = query )
27
37
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 = ""
29
43
self ._url = url
30
44
self ._directory = directory
31
45
self ._filename = filename
@@ -73,7 +87,7 @@ def _replace_files(self, text: str, inline: bool = False, size_check: bool = Fal
73
87
current = 0
74
88
while True :
75
89
try :
76
- idx1 = text .index ("/static/ansys/" , current )
90
+ idx1 = text .index (f "/static/ansys{ self . _ansys_version } /" , current )
77
91
except ValueError :
78
92
try :
79
93
idx1 = text .index ("/static/" , current )
@@ -82,7 +96,7 @@ def _replace_files(self, text: str, inline: bool = False, size_check: bool = Fal
82
96
idx1 = text .index ("/media/" , current )
83
97
except ValueError :
84
98
try :
85
- idx1 = text .index ("/ansys/" , current )
99
+ idx1 = text .index (f "/ansys{ self . _ansys_version } /" , current )
86
100
except ValueError :
87
101
return text
88
102
quote = text [idx1 - 1 ]
@@ -187,15 +201,24 @@ def _download_special_files(self):
187
201
"play.png" ,
188
202
]
189
203
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" ,
191
208
)
192
209
images = ["js-inflate.js" , "js-unzip.js" , "jquery.min.js" ]
193
210
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" ,
195
215
)
196
216
images = ["ANSYSViewer_min.js" , "viewer-loader.js" ]
197
217
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" ,
199
222
)
200
223
images = [
201
224
"jquery.contextMenu.min.css" ,
@@ -204,8 +227,8 @@ def _download_special_files(self):
204
227
]
205
228
self ._download_static_files (
206
229
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" ,
209
232
"ansys-nexus-viewer vnc js" ,
210
233
)
211
234
@@ -217,7 +240,10 @@ def _download_special_files(self):
217
240
"three.js" ,
218
241
]
219
242
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" ,
221
247
)
222
248
223
249
image = [
@@ -228,14 +254,14 @@ def _download_special_files(self):
228
254
]
229
255
self ._download_static_files (
230
256
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" ,
233
259
"threejs draco" ,
234
260
)
235
261
self ._download_static_files (
236
262
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" ,
239
265
"threejs draco gltf" ,
240
266
)
241
267
@@ -250,15 +276,17 @@ def _download_special_files(self):
250
276
self ._download_static_files (fonts , "/static/website/webfonts/" , "webfonts" , "fonts" )
251
277
252
278
@staticmethod
253
- def _fix_viewer_component_paths (filename , data ):
279
+ def _fix_viewer_component_paths (filename , data , ansys_version ):
254
280
# Special case for AVZ viewer: ANSYSViewer_min.js to set the base path for images
255
281
if filename .endswith ("ANSYSViewer_min.js" ):
256
282
data = data .decode ("utf-8" )
257
283
data = data .replace (
258
284
'"/static/website/images/"' ,
259
285
r'document.URL.replace(/\\/g, "/").replace("index.html", "media/")' ,
260
286
)
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
+ )
262
290
# this one is interesting. by default, AVZ will throw an error if you attempt to read
263
291
# a "file://" protocol src. In offline mode, if we are not using data URIs, then we
264
292
# 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):
267
295
# Special case for the AVZ viewer web component (loading proxy images and play arrow)
268
296
elif filename .endswith ("viewer-loader.js" ):
269
297
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
+ )
271
301
data = data .encode ("utf-8" )
272
302
return data
273
303
@@ -277,9 +307,12 @@ def _download_static_files(self, files, source_path, target_path, comment):
277
307
url = tmp .scheme + "://" + tmp .netloc + source_path + f
278
308
resp = requests .get (url , allow_redirects = True )
279
309
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 )
281
312
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
+ )
283
316
open (filename , "wb" ).write (data )
284
317
except Exception :
285
318
print (f"Unable to download { comment } : { f } " )
@@ -329,9 +362,9 @@ def _get_file(self, path_plus_queries: str, pathname: str, inline: bool = False)
329
362
# we need to prefix the .bin file and scene.js file with the GUID
330
363
basename = f"{ os .path .basename (os .path .dirname (pathname ))} _{ basename } "
331
364
else :
332
- tmp = self ._fix_viewer_component_paths (basename , tmp )
365
+ tmp = self ._fix_viewer_component_paths (basename , tmp , self . _ansys_version )
333
366
# get the output filename
334
- if pathname .startswith ("/static/ansys/" ):
367
+ if pathname .startswith (f "/static/ansys{ self . _ansys_version } /" ):
335
368
# if the content is part of the /ansys/ namespace, we keep the namespace,
336
369
# but remove the /static prefix
337
370
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
361
394
return - 1 , - 1 , ""
362
395
idx2 += len (suffix )
363
396
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
+ ):
365
402
return idx1 , idx2 , text [idx1 :idx2 ]
366
403
start = idx2
367
404
@@ -452,10 +489,29 @@ def _download(self):
452
489
self ._make_dir ([self ._directory , "media" , "jax" , "input" , "AsciiMath" ])
453
490
self ._make_dir ([self ._directory , "media" , "images" ])
454
491
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
+ )
459
515
460
516
# get the webpage html source
461
517
resp = requests .get (self ._url )
@@ -480,7 +536,7 @@ def _download(self):
480
536
# <img src="/media/7d6838fe-f28d-11e8-a5aa-1c1b0da59167_image.png" class="img-responsive"
481
537
# ... style="margin: 0 auto; display:flex; justify-content:center;" alt="Image file not found">
482
538
# 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">
484
540
# video
485
541
# <source src="/media/4a87c6c0-f34b-11e8-871b-1c1b0da59167_movie.mp4" type="video/mp4" />
486
542
# slider template
0 commit comments