Skip to content

Commit

Permalink
BUILD: Modify TOML dependency (#5548)
Browse files Browse the repository at this point in the history
Co-authored-by: Maxime Rey <[email protected]>
  • Loading branch information
SMoraisAnsys and MaxJPRey authored Dec 10, 2024
1 parent 9713da3 commit 1fff44f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ dependencies = [
"pyedb>=0.4.0; python_version == '3.7'",
"pyedb>=0.24.0; python_version > '3.7'",
"pyedb!=0.28.0; python_version > '3.7'",
"pytomlpp; python_version < '3.12'",
"tomli; python_version < '3.11'",
"tomli-w",
"rpyc>=6.0.0,<6.1",
"pyyaml",
"defusedxml>=0.7,<8.0"
Expand Down
17 changes: 6 additions & 11 deletions src/ansys/aedt/core/generic/general_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,11 +514,10 @@ def read_toml(file_path): # pragma: no cover
dict
Parsed TOML file as a dictionary.
"""
current_python_version = sys.version_info[:2]
if current_python_version < (3, 12):
import pytomlpp as tomllib
else:
try:
import tomllib
except (ImportError, ModuleNotFoundError):
import tomli as tomllib

with open_file(file_path, "rb") as fb:
return tomllib.load(fb)
Expand Down Expand Up @@ -1298,11 +1297,7 @@ def is_digit(c):

@pyaedt_function_handler()
def _create_toml_file(input_dict, full_toml_path):
current_python_version = sys.version_info[:2]
if current_python_version < (3, 12):
import pytomlpp as tomllib
else:
import tomllib
import tomli_w

if not os.path.exists(os.path.dirname(full_toml_path)):
os.makedirs(os.path.dirname(full_toml_path))
Expand All @@ -1322,8 +1317,8 @@ def _dict_toml(d):
return new_dict

new_dict = _dict_toml(input_dict)
with open_file(full_toml_path, "w") as fp:
tomllib.dump(new_dict, fp)
with open_file(full_toml_path, "wb") as fp:
tomli_w.dump(new_dict, fp)
settings.logger.info(f"{full_toml_path} correctly created.")
return True

Expand Down
2 changes: 1 addition & 1 deletion src/ansys/aedt/core/modules/material_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ def _aedmattolibrary(self, matname):

@pyaedt_function_handler(full_json_path="output_file")
def export_materials_to_file(self, output_file):
"""Export all materials to a JSON or TOML file.
"""Export all materials to a JSON or TOML file.
Parameters
----------
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
SETTINGS_RELEASE_ON_EXCEPTION = settings.release_on_exception
SETTINGS_ENABLE_ERROR_HANDLER = settings.enable_error_handler
ERROR_MESSAGE = "Dummy message."
TOML_DATA = {"key_0": "dummy", "key_1": 12, "key_2": [1, 2], "key_3": {"key_4": 42}}


@pytest.fixture(scope="module", autouse=True)
Expand Down Expand Up @@ -237,3 +238,31 @@ def test_settings_check_allowed_env_variables():
allowed_env_var_expected.remove("ANS_NODEPCHECK")

assert sorted(allowed_env_var_expected) == sorted(env_variables)


def test_read_toml(tmp_path):
"""Test loading a TOML file."""
from ansys.aedt.core.generic.general_methods import read_toml

file_path = tmp_path / "dummy.toml"
content = """
key_0 = 'dummy'
key_1 = 12
key_2 = [1,2]
[key_3]
key_4 = 42
"""
file_path.write_text(content, encoding="utf-8")

res = read_toml(file_path)
assert TOML_DATA == res


def test_write_toml(tmp_path):
"""Test writing a TOML file."""
from ansys.aedt.core.generic.general_methods import _create_toml_file

file_path = tmp_path / "dummy.toml"
_create_toml_file(TOML_DATA, file_path)

assert file_path.exists()

0 comments on commit 1fff44f

Please sign in to comment.