22
33from datetime import datetime
44import os
5+ from pathlib import Path
56import subprocess
67
78from ansys_sphinx_theme import ansys_favicon , get_version_match
9+ import github
10+ import jinja2
11+ from PIL import Image
12+ import requests
813import sphinx
914from sphinx .builders .latex import LaTeXBuilder
15+ import toml
1016
1117from pyansys import __version__ as pyansys_version
1218
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
1456
1557project = "pyansys"
1658copyright = f"(c) { datetime .now ().year } ANSYS, Inc. All rights reserved"
90132# make rst_epilog a variable, so you can add other epilog parts to it
91133rst_epilog = ""
92134# 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 ()
95137
96138# Ignore certain URLs
97139linkcheck_ignore = [
123165
124166def generate_rst_files (versions : list [str ], tables : dict [str , list [str ]]):
125167 """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-
159168 # Create Jinja2 environment
160169 jinja_env = jinja2 .Environment (loader = jinja2 .BaseLoader ())
161170
@@ -174,23 +183,19 @@ def generate_rst_files(versions: list[str], tables: dict[str, list[str]]):
174183 output_filename = GENERATED_DIR / f"version_{ version } .rst"
175184
176185 # 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" )
179187
180188 # Generate the index.rst file
181189 index_template = jinja_env .from_string (INDEX_TEMPLATE )
182190 rendered_index = index_template .render (versions = versions )
183191
184192 # Write the rendered content to the file
185193 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" )
188195
189196
190197def get_documentation_link_from_pypi (library : str , version : str ) -> str :
191198 """Get the documentation link from PyPI for a specific library and version."""
192- import requests
193-
194199 # Get the PyPI metadata for the library
195200 resp = requests .get (f"https://pypi.org/pypi/{ library } /{ version } /json" )
196201 metadata = resp .json ()
@@ -224,8 +229,6 @@ def pyansys_multiversion_docs_link(docs_link: str, version: str) -> str:
224229 failure, it returns the default link. This is done on a best effort basis.
225230
226231 """
227- import requests
228-
229232 # First, let's check it is an official PyAnsys documentation link
230233 if "docs.pyansys.com" in docs_link :
231234 # Clean the link
@@ -247,15 +250,10 @@ def pyansys_multiversion_docs_link(docs_link: str, version: str) -> str:
247250
248251def build_versions_table (branch : str ) -> list [str ]:
249252 """Build the versions table for the PyAnsys libraries."""
250- import requests
251- import toml
252-
253- TMP_FILE = "tmp_pyproject.toml"
254-
255253 # Download the pyproject.toml file
256254 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 )
259257
260258 # Load the pyproject.toml file using TOML parser
261259 pyproject_toml = toml .load (TMP_FILE )
@@ -290,7 +288,7 @@ def build_versions_table(branch: str) -> list[str]:
290288 ]
291289
292290 # Delete the temporary file
293- os . remove ( TMP_FILE )
291+ TMP_FILE . unlink ( )
294292
295293 # Build the table
296294 table = []
@@ -329,8 +327,6 @@ def build_versions_table(branch: str) -> list[str]:
329327
330328def get_release_branches_in_metapackage ():
331329 """Retrieve the release branches in the PyAnsys metapackage."""
332- import github
333-
334330 # Get the PyAnsys metapackage repository
335331 g = github .Github (os .getenv ("GITHUB_TOKEN" , None ))
336332 github_repo = g .get_repo ("ansys/pyansys" )
@@ -377,8 +373,6 @@ def resize_with_background(input_image_path, output_image_path, target_size):
377373 target_size : tuple[int, int]
378374 The target size of the output image as a tuple (width, height) in pixels.
379375 """
380- from PIL import Image
381-
382376 # Open the input image
383377 img = Image .open (input_image_path ).convert ("RGBA" ) # Ensure the image has an alpha channel
384378
@@ -415,7 +409,6 @@ def resize_with_background(input_image_path, output_image_path, target_size):
415409def resize_thumbnails (app : sphinx .application .Sphinx ):
416410 """Resize all images in the current directory to 640x480 pixels."""
417411 # Process all images
418- from pathlib import Path
419412
420413 thumbnail_dir = Path (__file__ ).parent .absolute () / "_static" / "thumbnails"
421414
@@ -426,8 +419,6 @@ def resize_thumbnails(app: sphinx.application.Sphinx):
426419
427420def revert_thumbnails (app : sphinx .application .Sphinx , exception ):
428421 """Resize all images in the current directory to 640x480 pixels."""
429- from pathlib import Path
430-
431422 thumbnail_dir = Path (__file__ ).parent .absolute () / "_static" / "thumbnails"
432423
433424 subprocess .run (["git" , "checkout" , "--" , thumbnail_dir ])
0 commit comments