Skip to content

Commit 7c8ba0b

Browse files
authored
REFACTOR: pathlib refactor multi-files (#5943)
2 parents 10d9716 + 94fafb9 commit 7c8ba0b

8 files changed

Lines changed: 58 additions & 55 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pathlib refactor multi-files

src/ansys/aedt/core/maxwellcircuit.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"""This module contains the ``MaxwellCircuit`` class."""
2626

2727
import math
28-
import os
28+
from pathlib import Path
2929

3030
from ansys.aedt.core.application.analysis_maxwell_circuit import AnalysisMaxwellCircuit
3131
from ansys.aedt.core.generic.file_utils import open_file
@@ -223,7 +223,7 @@ def export_netlist_from_schematic(self, output_file):
223223
224224
Parameters
225225
----------
226-
output_file : str
226+
output_file : str or :class:`pathlib.Path`
227227
File path to export the netlist to.
228228
229229
Returns
@@ -232,7 +232,7 @@ def export_netlist_from_schematic(self, output_file):
232232
Netlist file path when successful, ``False`` when failed.
233233
234234
"""
235-
if os.path.splitext(output_file)[1] != ".sph":
235+
if Path(output_file).suffix != ".sph":
236236
self.logger.error("Invalid file extension. It must be ``.sph``.")
237237
return False
238238
try:

src/ansys/aedt/core/modeler/cad/object_3d.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"""
3535

3636
import math
37-
import os
37+
from pathlib import Path
3838
import re
3939

4040
from ansys.aedt.core.generic.constants import AEDT_UNITS
@@ -184,11 +184,11 @@ def _bounding_box_sat(self):
184184
185185
"""
186186
tmp_path = self._primitives._app.working_directory
187-
filename = os.path.join(tmp_path, self.name + ".sat")
187+
filename = Path(tmp_path) / (self.name + ".sat")
188188

189189
self._primitives._app.export_3d_model(self.name, tmp_path, ".sat", [self.name])
190190

191-
if not os.path.isfile(filename):
191+
if not Path(filename).is_file():
192192
raise Exception(f"Cannot export the ACIS SAT file for object {self.name}")
193193

194194
with open_file(filename, "r") as fh:
@@ -212,7 +212,7 @@ def _bounding_box_sat(self):
212212
return False
213213

214214
try:
215-
os.remove(filename)
215+
Path(filename).unlink()
216216
except Exception:
217217
self.logger.warning("ERROR: Cannot remove temp file.")
218218
return bb
@@ -309,7 +309,7 @@ def export_image(self, output_file=None):
309309
310310
Parameters
311311
----------
312-
output_file : str, optional
312+
output_file : str or :class:`pathlib.Path`, optional
313313
File name with full path. If `None` the exported image will be a ``png`` file that
314314
will be saved in ``working_directory``.
315315
To access the ``working_directory`` the use ``app.working_directory`` property.
@@ -320,7 +320,7 @@ def export_image(self, output_file=None):
320320
File path.
321321
"""
322322
if not output_file:
323-
output_file = os.path.join(self._primitives._app.working_directory, self.name + ".png")
323+
output_file = Path(self._primitives._app.working_directory) / (self.name + ".png")
324324
model_obj = self._primitives._app.post.plot_model_obj(
325325
objects=[self.name],
326326
show=False,

src/ansys/aedt/core/modeler/modeler_pcb.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
# SOFTWARE.
2424

25-
import os
25+
from pathlib import Path
2626
import re
2727
from warnings import warn
2828

@@ -134,11 +134,11 @@ def o_model_manager(self): # pragma: no cover
134134

135135
@property
136136
def _edb_folder(self):
137-
return os.path.join(self._app.project_path, self._app.project_name + ".aedb")
137+
return Path(self._app.project_path) / (self._app.project_name + ".aedb")
138138

139139
@property
140140
def _edb_file(self):
141-
return os.path.join(self._edb_folder, "edb.def")
141+
return Path(self._edb_folder) / "edb.def"
142142

143143
@property
144144
def edb(self):
@@ -156,7 +156,7 @@ def edb(self):
156156
from pyedb import Edb
157157

158158
self._edb = None
159-
if os.path.exists(self._edb_file) or inside_desktop:
159+
if Path(self._edb_file).exists() or inside_desktop:
160160
self._edb = Edb(
161161
self._edb_folder,
162162
self._app.design_name,
@@ -519,7 +519,7 @@ def import_cadence_brd(self, input_file, output_dir=None, name=None):
519519
output_dir : str, optional
520520
Path where the EDB is to be created. The default is ``None``, in which
521521
case the project directory is used.
522-
name : str, optional
522+
name : str or :class:`pathlib.Path`, optional
523523
Name of the EDB. The default is ``None``, in which
524524
case the board name is used.
525525
@@ -535,11 +535,11 @@ def import_cadence_brd(self, input_file, output_dir=None, name=None):
535535
if not output_dir:
536536
output_dir = self.projdir
537537
if not name:
538-
name = os.path.basename(input_file)
539-
name = os.path.splitext(name)[0]
538+
name = Path(input_file).name
539+
name = Path(name).stem
540540

541541
self._oimportexport.ImportExtracta(
542-
input_file, os.path.join(output_dir, name + ".aedb"), os.path.join(output_dir, name + ".xml")
542+
input_file, str(Path(output_dir) / (name + ".aedb")), str(Path(output_dir) / (name + ".xml"))
543543
)
544544
self._app.__init__(self._app.desktop_class.active_project().GetName())
545545
return True
@@ -572,7 +572,7 @@ def import_ipc2581(self, input_file, output_dir=None, name=None):
572572
output_dir : str, optional
573573
Path where the EDB is to be created. The default is ``None``, in which
574574
case the project directory is used.
575-
name : str, optional
575+
name : str or :class:`pathlib.Path`, optional
576576
Name of the EDB. The default is ``None``, in which
577577
case the board name is used.
578578
@@ -588,11 +588,11 @@ def import_ipc2581(self, input_file, output_dir=None, name=None):
588588
if not output_dir:
589589
output_dir = self.projdir
590590
if not name:
591-
name = os.path.basename(input_file)
592-
name = os.path.splitext(name)[0]
591+
name = Path(input_file).name
592+
name = Path(name).stem
593593

594594
self._oimportexport.ImportIPC(
595-
input_file, os.path.join(output_dir, name + ".aedb"), os.path.join(output_dir, name + ".xml")
595+
input_file, str(Pathn(output_dir) / (name + ".aedb")), str(Path(output_dir) / (name + ".xml"))
596596
)
597597
self._app.__init__(self._app.desktop_class.active_project().GetName())
598598
return True
@@ -955,9 +955,9 @@ def set_touchstone_model(self, assignment, input_file, model_name=None):
955955
----------
956956
assignment : str
957957
Name of the component.
958-
input_file : str, optional
958+
input_file : str or :class:`pathlib.Path`, optional
959959
Full path to the model file. The default is ``None``.
960-
model_name : str, optional
960+
model_name : str or :class:`pathlib.Path`, optional
961961
Name of the model. The default is ``None``, in which case the model name is the file name without an
962962
extension.
963963
@@ -977,12 +977,12 @@ def set_touchstone_model(self, assignment, input_file, model_name=None):
977977
"""
978978

979979
if not model_name:
980-
model_name = os.path.splitext(os.path.basename(input_file))[0]
980+
model_name = Path(Path(input_file).name).stem
981981
if "." in model_name:
982982
model_name = model_name.replace(".", "_")
983983
if model_name in list(self.omodel_manager.GetNames()):
984984
model_name = generate_unique_name(model_name, n=2)
985-
num_terminal = int(os.path.splitext(input_file)[1].lower().strip(".sp"))
985+
num_terminal = int(Path(input_file).suffix.lower().strip(".sp"))
986986

987987
port_names = []
988988
with open_file(input_file, "r") as f:
@@ -1019,7 +1019,7 @@ def set_touchstone_model(self, assignment, input_file, model_name=None):
10191019
["NAME:PortInfoBlk"],
10201020
["NAME:PortOrderBlk"],
10211021
"filename:=",
1022-
input_file,
1022+
str(input_file),
10231023
"numberofports:=",
10241024
num_terminal,
10251025
"sssfilename:=",

src/ansys/aedt/core/q3d.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
"""This module contains these classes: ``Q2d``, ``Q3d``, and ``QExtractor``."""
2626

27-
import os
27+
from pathlib import Path
2828
import re
2929
import warnings
3030

@@ -59,7 +59,7 @@ class QExtractor(FieldAnalysis3D, object):
5959
@property
6060
def design_file(self):
6161
"""Design file."""
62-
design_file = os.path.join(self.working_directory, "design_data.json")
62+
design_file = Path(self.working_directory) / "design_data.json"
6363
return design_file
6464

6565
def __init__(
@@ -239,7 +239,7 @@ def export_mesh_stats(self, setup, variations="", output_file=None, setup_type="
239239
Setup name.
240240
variations : str, optional
241241
Variation list. The default is ``""``.
242-
output_file : str, optional
242+
output_file : str or :class:`pathlib.Path`, optional
243243
Full path to the mesh statistics file. The default is ``None``, in which
244244
case the working directory is used.
245245
setup_type : str, optional
@@ -256,9 +256,9 @@ def export_mesh_stats(self, setup, variations="", output_file=None, setup_type="
256256
>>> oDesign.ExportMeshStats
257257
"""
258258
if not output_file:
259-
output_file = os.path.join(self.working_directory, "meshstats.ms")
260-
self.odesign.ExportMeshStats(setup, variations, setup_type, output_file)
261-
return output_file
259+
output_file = Path(self.working_directory) / "meshstats.ms"
260+
self.odesign.ExportMeshStats(setup, variations, setup_type, str(output_file))
261+
return str(output_file)
262262

263263
@pyaedt_function_handler()
264264
def edit_sources(
@@ -503,7 +503,7 @@ def export_matrix_data(
503503
bool
504504
``True`` when successful, ``False`` when failed.
505505
"""
506-
if os.path.splitext(file_name)[1] not in [".m", ".lvl", ".csv", ".txt"]:
506+
if Path(file_name).suffix not in [".m", ".lvl", ".csv", ".txt"]:
507507
self.logger.error("Extension is invalid. Possible extensions are *.m, *.lvl, *.csv, and *.txt.")
508508
return False
509509

@@ -921,7 +921,7 @@ def export_equivalent_circuit(
921921
>>> aedtapp.export_equivalent_circuit(output_file="test_export_circuit.cir",
922922
... setup=mysetup.name,sweep="LastAdaptive", variations=["d: 20mm"])
923923
"""
924-
if os.path.splitext(output_file)[1] not in [
924+
if Path(output_file).suffix not in [
925925
".cir",
926926
".sml",
927927
".sp",
@@ -2507,7 +2507,7 @@ def export_w_elements(self, analyze=False, export_folder=None):
25072507
analyze : bool, optional
25082508
Whether to analyze before export. Solutions must be present for the design.
25092509
The default is ``False``.
2510-
export_folder : str, optional
2510+
export_folder : str or :class:`pathlib.Path`, optional
25112511
Full path to the folder to export files to. The default is ``None``, in
25122512
which case the working directory is used.
25132513
@@ -2519,8 +2519,8 @@ def export_w_elements(self, analyze=False, export_folder=None):
25192519
exported_files = []
25202520
if not export_folder:
25212521
export_folder = self.working_directory
2522-
if not os.path.exists(export_folder):
2523-
os.makedirs(export_folder)
2522+
if not Path(export_folder).exists():
2523+
Path(export_folder).mkdir()
25242524
if analyze:
25252525
self.analyze()
25262526
setups = self.oanalysis.GetSetups()
@@ -2535,12 +2535,12 @@ def export_w_elements(self, analyze=False, export_folder=None):
25352535
if len(variation_array) == 1:
25362536
try:
25372537
export_file = f"{self.project_name}_{s}_{sweep}.sp"
2538-
export_path = os.path.join(export_folder, export_file)
2538+
export_path = Path(export_folder) / export_file
25392539
subckt_name = f"w_{self.project_name}"
25402540
self.oanalysis.ExportCircuit(
25412541
solution_name,
25422542
variation_array[0],
2543-
export_path,
2543+
str(export_path),
25442544
[
25452545
"NAME:CircuitData",
25462546
"MatrixName:=",
@@ -2579,12 +2579,12 @@ def export_w_elements(self, analyze=False, export_folder=None):
25792579
varCount += 1
25802580
try:
25812581
export_file = f"{self.project_name}_{s}_{sweep}_{varCount}.sp"
2582-
export_path = os.path.join(export_folder, export_file)
2582+
export_path = Path(export_folder) / export_file
25832583
subckt_name = f"w_{self.project_name}_{varCount}"
25842584
self.oanalysis.ExportCircuit(
25852585
solution_name,
25862586
variation,
2587-
export_path,
2587+
str(export_path),
25882588
[
25892589
"NAME:CircuitData",
25902590
"MatrixName:=",

tests/system/general/test_01_3dlayout_edb.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
# SOFTWARE.
2424

2525
import os
26+
from pathlib import Path
2627

2728
from ansys.aedt.core import Hfss3dLayout
2829
from ansys.aedt.core.generic.settings import is_linux
@@ -230,7 +231,7 @@ def test_05_change_property(self, aedtapp):
230231
assert aedtapp.modeler.change_property(f"Excitations:{ports[0].name}", "Impedance", "49ohm", "EM Design")
231232

232233
def test_06_assign_touchstone_model(self, aedtapp):
233-
model_path = os.path.join(TESTS_GENERAL_PATH, "example_models", "TEDB", "GRM32_DC0V_25degC_series.s2p")
234+
model_path = Path(TESTS_GENERAL_PATH) / "example_models" / "TEDB" / "GRM32_DC0V_25degC_series.s2p"
234235
assert aedtapp.modeler.set_touchstone_model(assignment="C217", input_file=model_path, model_name="Test1")
235236

236237
def test_07_assign_spice_model(self, aedtapp):

tests/system/general/test_36_Q2D_PostProcessing.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
# SOFTWARE.
2424

25-
import os
25+
from pathlib import Path
2626

2727
from ansys.aedt.core import Q2d
2828
import pytest
@@ -59,29 +59,29 @@ def init(self, q2d_solved_sweep_app, q2d_solved_nominal_app, local_scratch):
5959
self.local_scratch = local_scratch
6060

6161
def test_01_export_w_elements_from_sweep(self, q2d_solved_sweep_app, local_scratch):
62-
export_folder = os.path.join(local_scratch.path, "export_folder")
62+
export_folder = Path(local_scratch.path) / "export_folder"
6363
files = q2d_solved_sweep_app.export_w_elements(False, export_folder)
6464
assert len(files) == 3
6565
for file in files:
66-
_, ext = os.path.splitext(file)
66+
ext = Path(file).suffix
6767
assert ext == ".sp"
68-
assert os.path.isfile(file)
68+
assert Path(file).is_file()
6969

7070
def test_02_export_w_elements_from_nominal(self, q2d_solved_nominal_app, local_scratch):
71-
export_folder = os.path.join(local_scratch.path, "export_folder")
71+
export_folder = Path(local_scratch.path) / "export_folder"
7272
files = q2d_solved_nominal_app.export_w_elements(False, export_folder)
7373
assert len(files) == 1
7474
for file in files:
75-
_, ext = os.path.splitext(file)
75+
ext = Path(file).suffix
7676
assert ext == ".sp"
77-
assert os.path.isfile(file)
77+
assert Path(file).is_file()
7878

7979
def test_03_export_w_elements_to_working_directory(self, q2d_solved_nominal_app):
8080
files = q2d_solved_nominal_app.export_w_elements(False)
8181
assert len(files) == 1
8282
for file in files:
83-
_, ext = os.path.splitext(file)
83+
ext = Path(file).suffix
8484
assert ext == ".sp"
85-
assert os.path.isfile(file)
86-
file_dir = os.path.abspath(os.path.dirname(file))
87-
assert file_dir == os.path.abspath(self.q2d_solved_nominal_app.working_directory)
85+
assert Path(file).is_file()
86+
file_dir = Path(file).parent.absolute()
87+
assert file_dir == Path(self.q2d_solved_nominal_app.working_directory).absolute()

tests/system/visualization/test_12_PostProcessing.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
# SOFTWARE.
2424

2525
import os
26+
from pathlib import Path
2627
import sys
2728
import tempfile
2829

@@ -567,8 +568,8 @@ def test_60_test_parse_vector(self):
567568
assert isinstance(out, list)
568569

569570
def test_61_export_mesh(self, q3dtest):
570-
assert os.path.exists(q3dtest.export_mesh_stats("Setup1"))
571-
assert os.path.exists(q3dtest.export_mesh_stats("Setup1"))
571+
assert Path(q3dtest.export_mesh_stats("Setup1")).exists()
572+
assert Path(q3dtest.export_mesh_stats("Setup1")).exists()
572573

573574
def test_62_eye_diagram(self, eye_test):
574575
rep = eye_test.post.reports_by_category.eye_diagram("AEYEPROBE(OutputEye)", "QuickEyeAnalysis")

0 commit comments

Comments
 (0)