2
2
3
3
from datetime import datetime
4
4
import os
5
+ from pathlib import Path
5
6
import subprocess
6
7
7
8
from ansys_sphinx_theme import ansys_favicon , get_version_match
9
+ import github
10
+ import jinja2
11
+ from PIL import Image
12
+ import requests
8
13
import sphinx
9
14
from sphinx .builders .latex import LaTeXBuilder
15
+ import toml
10
16
11
17
from pyansys import __version__ as pyansys_version
12
18
13
- LaTeXBuilder .supported_image_types = ["image/png" , "image/pdf" , "image/svg+xml" ] # noqa: E501
19
+ # Declare constants
20
+ GENERATED_DIR = Path (__file__ ).parent / "package_versions"
21
+
22
+ VERSIONS_TEMPLATE = """
23
+ Package versions in PyAnsys {{ version }}
24
+ ============================{{ "=" * version|length }}
25
+
26
+ The PyAnsys packages delivered in version {{ version }} are:
27
+ {{ ' ' }}
28
+ {%- for entry in table %}
29
+ {{ entry }}
30
+ {%- endfor %}
31
+ """
32
+
33
+ INDEX_TEMPLATE = """
34
+ Package versions
35
+ ================
36
+
37
+ Users can find below the list of PyAnsys packages available in the various
38
+ PyAnsys metapackages. The tables shows the package versions available in each
39
+ metapackage release.
40
+
41
+ .. toctree::
42
+ :maxdepth: 3
43
+ {{ ' ' }}
44
+ {%- for version in versions %}
45
+ version_{{ version }}
46
+ {%- endfor %}
47
+ """
48
+
49
+ TMP_FILE = Path ("tmp_pyproject.toml" )
50
+
51
+ LaTeXBuilder .supported_image_types = [
52
+ "image/png" ,
53
+ "image/pdf" ,
54
+ "image/svg+xml" ,
55
+ ] # noqa: E501
14
56
15
57
project = "pyansys"
16
58
copyright = f"(c) { datetime .now ().year } ANSYS, Inc. All rights reserved"
90
132
# make rst_epilog a variable, so you can add other epilog parts to it
91
133
rst_epilog = ""
92
134
# Read link all targets from file
93
- with open ("links.rst" ) as f :
94
- rst_epilog += f .read ()
135
+ with Path . open ("links.rst" ) as file :
136
+ rst_epilog += file .read ()
95
137
96
138
# Ignore certain URLs
97
139
linkcheck_ignore = [
123
165
124
166
def generate_rst_files (versions : list [str ], tables : dict [str , list [str ]]):
125
167
"""Generate the .rst files for the package versions."""
126
- from pathlib import Path
127
-
128
- import jinja2
129
-
130
- GENERATED_DIR = Path (__file__ ).parent / "package_versions"
131
-
132
- VERSIONS_TEMPLATE = """
133
- Package versions in PyAnsys {{ version }}
134
- ============================{{ "=" * version|length }}
135
-
136
- The PyAnsys packages delivered in version {{ version }} are:
137
- {{ ' ' }}
138
- {%- for entry in table %}
139
- {{ entry }}
140
- {%- endfor %}
141
- """
142
-
143
- INDEX_TEMPLATE = """
144
- Package versions
145
- ================
146
-
147
- Users can find below the list of PyAnsys packages available in the various
148
- PyAnsys metapackages. The tables shows the package versions available in each
149
- metapackage release.
150
-
151
- .. toctree::
152
- :maxdepth: 3
153
- {{ ' ' }}
154
- {%- for version in versions %}
155
- version_{{ version }}
156
- {%- endfor %}
157
- """
158
-
159
168
# Create Jinja2 environment
160
169
jinja_env = jinja2 .Environment (loader = jinja2 .BaseLoader ())
161
170
@@ -174,23 +183,19 @@ def generate_rst_files(versions: list[str], tables: dict[str, list[str]]):
174
183
output_filename = GENERATED_DIR / f"version_{ version } .rst"
175
184
176
185
# Write the rendered content to the file
177
- with open (output_filename , "w" ) as f :
178
- f .write (rendered_content )
186
+ output_filename .write_text (rendered_content , encoding = "utf-8" )
179
187
180
188
# Generate the index.rst file
181
189
index_template = jinja_env .from_string (INDEX_TEMPLATE )
182
190
rendered_index = index_template .render (versions = versions )
183
191
184
192
# Write the rendered content to the file
185
193
output_filename = GENERATED_DIR / "index.rst"
186
- with open (output_filename , "w" ) as f :
187
- f .write (rendered_index )
194
+ output_filename .write_text (rendered_index , encoding = "utf-8" )
188
195
189
196
190
197
def get_documentation_link_from_pypi (library : str , version : str ) -> str :
191
198
"""Get the documentation link from PyPI for a specific library and version."""
192
- import requests
193
-
194
199
# Get the PyPI metadata for the library
195
200
resp = requests .get (f"https://pypi.org/pypi/{ library } /{ version } /json" )
196
201
metadata = resp .json ()
@@ -224,8 +229,6 @@ def pyansys_multiversion_docs_link(docs_link: str, version: str) -> str:
224
229
failure, it returns the default link. This is done on a best effort basis.
225
230
226
231
"""
227
- import requests
228
-
229
232
# First, let's check it is an official PyAnsys documentation link
230
233
if "docs.pyansys.com" in docs_link :
231
234
# Clean the link
@@ -247,15 +250,10 @@ def pyansys_multiversion_docs_link(docs_link: str, version: str) -> str:
247
250
248
251
def build_versions_table (branch : str ) -> list [str ]:
249
252
"""Build the versions table for the PyAnsys libraries."""
250
- import requests
251
- import toml
252
-
253
- TMP_FILE = "tmp_pyproject.toml"
254
-
255
253
# Download the pyproject.toml file
256
254
resp = requests .get (f"https://raw.githubusercontent.com/ansys/pyansys/{ branch } /pyproject.toml" )
257
- with open ("tmp_pyproject.toml" , " wb" ) as f :
258
- f .write (resp .content )
255
+ with TMP_FILE . open ("wb" ) as file :
256
+ file .write (resp .content )
259
257
260
258
# Load the pyproject.toml file using TOML parser
261
259
pyproject_toml = toml .load (TMP_FILE )
@@ -290,7 +288,7 @@ def build_versions_table(branch: str) -> list[str]:
290
288
]
291
289
292
290
# Delete the temporary file
293
- os . remove ( TMP_FILE )
291
+ TMP_FILE . unlink ( )
294
292
295
293
# Build the table
296
294
table = []
@@ -329,8 +327,6 @@ def build_versions_table(branch: str) -> list[str]:
329
327
330
328
def get_release_branches_in_metapackage ():
331
329
"""Retrieve the release branches in the PyAnsys metapackage."""
332
- import github
333
-
334
330
# Get the PyAnsys metapackage repository
335
331
g = github .Github (os .getenv ("GITHUB_TOKEN" , None ))
336
332
github_repo = g .get_repo ("ansys/pyansys" )
@@ -377,8 +373,6 @@ def resize_with_background(input_image_path, output_image_path, target_size):
377
373
target_size : tuple[int, int]
378
374
The target size of the output image as a tuple (width, height) in pixels.
379
375
"""
380
- from PIL import Image
381
-
382
376
# Open the input image
383
377
img = Image .open (input_image_path ).convert ("RGBA" ) # Ensure the image has an alpha channel
384
378
@@ -415,7 +409,6 @@ def resize_with_background(input_image_path, output_image_path, target_size):
415
409
def resize_thumbnails (app : sphinx .application .Sphinx ):
416
410
"""Resize all images in the current directory to 640x480 pixels."""
417
411
# Process all images
418
- from pathlib import Path
419
412
420
413
thumbnail_dir = Path (__file__ ).parent .absolute () / "_static" / "thumbnails"
421
414
@@ -426,8 +419,6 @@ def resize_thumbnails(app: sphinx.application.Sphinx):
426
419
427
420
def revert_thumbnails (app : sphinx .application .Sphinx , exception ):
428
421
"""Resize all images in the current directory to 640x480 pixels."""
429
- from pathlib import Path
430
-
431
422
thumbnail_dir = Path (__file__ ).parent .absolute () / "_static" / "thumbnails"
432
423
433
424
subprocess .run (["git" , "checkout" , "--" , thumbnail_dir ])
0 commit comments