Skip to content

Commit

Permalink
refactor: __init__ file (#3490)
Browse files Browse the repository at this point in the history
* refactor: cleaning up the init file

* feat: centralizing globals in `__init__` file

* chore: adding changelog file 3490.added.md [dependabot-skip]

* feat: adding missing import

---------

Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com>
  • Loading branch information
germa89 and pyansys-ci-bot authored Oct 17, 2024
1 parent 84085c3 commit 4bb5192
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 163 deletions.
1 change: 1 addition & 0 deletions doc/changelog.d/3490.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
refactor: `__init__` file
169 changes: 76 additions & 93 deletions src/ansys/mapdl/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,102 +20,89 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# Importing logging
import importlib.metadata as importlib_metadata

###############################################################################
# Imports
# =======
#
import logging
import os
import sys
from typing import Dict, List, Tuple
from warnings import warn

import platformdirs

# Setup data directory
USER_DATA_PATH = platformdirs.user_data_dir(
appname="ansys_mapdl_core", appauthor="Ansys"
)
if not os.path.exists(USER_DATA_PATH): # pragma: no cover
os.makedirs(USER_DATA_PATH)

DEPRECATING_MINIMUM_PYTHON_VERSION = True
MINIMUM_PYTHON_VERSION = (3, 10)

first_time_file = os.path.join(USER_DATA_PATH, ".firstime")
if not os.path.exists(first_time_file): # pragma: no cover
py_ver = f"{sys.version_info[0]}.{sys.version_info[1]}"
py_ver_min = f"{MINIMUM_PYTHON_VERSION[0]}.{MINIMUM_PYTHON_VERSION[1]}"

if (
sys.version_info[1] == MINIMUM_PYTHON_VERSION[1]
and DEPRECATING_MINIMUM_PYTHON_VERSION
):
warn(
f"Support for Python {py_ver} will be dropped in the next minor " "release."
)

if sys.version_info[1] <= MINIMUM_PYTHON_VERSION[1]:
warn(
f"Python {py_ver} is not being tested or officially supported. "
"It is recommended you use a newer version of Python. "
f"The mininimum supported and tested version is {py_ver_min}.\n\n"
"**This warning is shown only the first time you run PyMAPDL.**\n"
)

with open(first_time_file, "w") as fid:
fid.write("")

EXAMPLES_PATH = os.path.join(USER_DATA_PATH, "examples")
from platformdirs import user_data_dir

###############################################################################
# Logging
# =======
#
from ansys.mapdl.core.logging import Logger

LOG = Logger(level=logging.ERROR, to_file=False, to_stdout=True)
LOG.debug("Loaded logging module as LOG")

###############################################################################
# Globals
# =======
#
from ansys.mapdl.core.helpers import is_installed, run_every_import, run_first_time

BUILDING_GALLERY = False
RUNNING_TESTS = False

if RUNNING_TESTS: # pragma: no cover
LOG.debug("Running tests on Pytest")

_LOCAL_PORTS = []

__version__: str = importlib_metadata.version(__name__.replace(".", "-"))

try:
from ansys.tools.visualization_interface import Plotter
# A dictionary relating PyMAPDL server versions with the unified install ones
VERSION_MAP: Dict[Tuple[int, int, int], str] = {
(0, 0, 0): "2020R2",
(0, 3, 0): "2021R1",
(0, 4, 0): "2021R2",
(0, 4, 1): "2021R2",
(0, 5, 0): "2022R1",
(0, 5, 1): "2022R2",
}

_HAS_VISUALIZER = True
except ModuleNotFoundError: # pragma: no cover
LOG.debug("The module 'ansys-tools-visualization_interface' is not installed.")
_HAS_VISUALIZER = False
BUILDING_GALLERY: bool = False
RUNNING_TESTS: bool = False

try:
import pyvista as pv
DEPRECATING_MINIMUM_PYTHON_VERSION: bool = True
MINIMUM_PYTHON_VERSION: Tuple[int, int] = (3, 10)

_HAS_PYVISTA = True
except ModuleNotFoundError: # pragma: no cover
LOG.debug("The module 'pyvista' is not installed.")
_HAS_PYVISTA = False
# Import related globals
_HAS_ATP: bool = is_installed("ansys.tools.path")
_HAS_PIM: bool = is_installed("ansys.platform.instancemanagement")
_HAS_PYANSYS_REPORT: bool = is_installed("ansys.tools.report")
_HAS_PYVISTA: bool = is_installed("pyvista")
_HAS_TQDM: bool = is_installed("tqdm")
_HAS_VISUALIZER: bool = is_installed("ansys.tools.visualization_interface")

# Setup directories
USER_DATA_PATH: str = user_data_dir(appname="ansys_mapdl_core", appauthor="Ansys")
EXAMPLES_PATH = os.path.join(USER_DATA_PATH, "examples")

try:
import importlib.metadata as importlib_metadata
except ModuleNotFoundError: # pragma: no cover
import importlib_metadata
# Store local ports
_LOCAL_PORTS: List[int] = []

__version__ = importlib_metadata.version(__name__.replace(".", "-"))
###############################################################################
# First time
# ==========
#
# This function runs only the first time PyMAPDL is importad after it is installed.
# It creates the required directories and raise Python version related warnings.
#
run_first_time()

try:
from ansys.tools.path.path import (
change_default_ansys_path,
find_ansys,
get_ansys_path,
get_available_ansys_installations,
save_ansys_path,
)
except:
# We don't really use these imports in the library. They are here for
# convenience.
pass
###############################################################################
# Runs every time
# ===============
#
# This function runs every time that PyMAPDL is imported.
#
run_every_import()

###############################################################################
# Library imports
# ===============
#
from ansys.mapdl.core._version import SUPPORTED_ANSYS_VERSIONS
from ansys.mapdl.core.convert import convert_apdl_block, convert_script
from ansys.mapdl.core.launcher import close_all_local_instances
Expand All @@ -131,23 +118,19 @@
from ansys.mapdl.core.misc import Report, _check_has_ansys
from ansys.mapdl.core.pool import MapdlPool

_HAS_ANSYS = _check_has_ansys()
###############################################################################
# Convenient imports
# ==================
#
# For compatibility with other versions or for convenience
if _HAS_ATP:
from ansys.tools.path.path import (
change_default_ansys_path,
find_ansys,
get_ansys_path,
get_available_ansys_installations,
save_ansys_path,
)

if _HAS_VISUALIZER:
from ansys.mapdl.core.plotting.theme import _apply_default_theme

_apply_default_theme()

BUILDING_GALLERY = False
RUNNING_TESTS = False


VERSION_MAP = {
(0, 0, 0): "2020R2",
(0, 3, 0): "2021R1",
(0, 4, 0): "2021R2",
(0, 4, 1): "2021R2",
(0, 5, 0): "2022R1",
(0, 5, 1): "2022R2", # as of 21 Mar 2022 unreleased
}
"""A dictionary relating PyMAPDL server versions with the unified install ones."""
from ansys.tools.visualization_interface import Plotter
102 changes: 102 additions & 0 deletions src/ansys/mapdl/core/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Copyright (C) 2016 - 2024 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""Module for helper functions"""

import importlib
import os
import sys
from warnings import warn

from ansys.mapdl.core import LOG


def is_installed(package_name: str) -> bool:
"""Check if a package is installed"""

if os.name == "nt":
package_name = package_name.replace("-", ".")

try:
importlib.import_module(package_name)

return True
except ModuleNotFoundError: # pragma: no cover
LOG.debug(f"The module '{package_name}' is not installed.")
return False


def run_first_time() -> None:
"""Run this function the first time PyMAPDL is imported"""
from ansys.mapdl.core import (
DEPRECATING_MINIMUM_PYTHON_VERSION,
MINIMUM_PYTHON_VERSION,
USER_DATA_PATH,
)

first_time_file: str = os.path.join(USER_DATA_PATH, ".firstime")

# Run the first time only
if not os.path.exists(first_time_file): # pragma: no cover

# Create USER_DATA_PATH directory
if not os.path.exists(USER_DATA_PATH): # pragma: no cover
os.makedirs(USER_DATA_PATH)

# Show warning about Python compatibility
py_ver = f"{sys.version_info[0]}.{sys.version_info[1]}"
py_ver_min = f"{MINIMUM_PYTHON_VERSION[0]}.{MINIMUM_PYTHON_VERSION[1]}"

if (
sys.version_info[1] == MINIMUM_PYTHON_VERSION[1]
and DEPRECATING_MINIMUM_PYTHON_VERSION
):
warn(
f"Support for Python {py_ver} will be dropped in the next minor "
"release."
)

if sys.version_info[1] <= MINIMUM_PYTHON_VERSION[1]:
warn(
f"Python {py_ver} is not being tested or officially supported. "
"It is recommended you use a newer version of Python. "
f"The mininimum supported and tested version is {py_ver_min}.\n\n"
"**This warning is shown only the first time you run PyMAPDL.**\n"
)

with open(first_time_file, "w") as fid:
fid.write("")


def run_every_import() -> None:
# Run every time we import PyMAPDL
from ansys.mapdl.core import _HAS_VISUALIZER, RUNNING_TESTS

# Apply custom theme
if _HAS_VISUALIZER:
from ansys.mapdl.core.plotting.theme import _apply_default_theme

_apply_default_theme()

# In case we want to do something specific for testing.
if RUNNING_TESTS: # pragma: no cover
LOG.debug("Running tests on Pytest")
23 changes: 7 additions & 16 deletions src/ansys/mapdl/core/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,8 @@

import psutil

try:
import ansys.platform.instancemanagement as pypim

_HAS_PIM = True

except ModuleNotFoundError: # pragma: no cover
_HAS_PIM = False

try:
from ansys.tools.path import find_ansys, get_ansys_path, version_from_path

_HAS_ATP = True
except ModuleNotFoundError:
_HAS_ATP = False

from ansys.mapdl import core as pymapdl
from ansys.mapdl.core import LOG
from ansys.mapdl.core import _HAS_ATP, _HAS_PIM, LOG
from ansys.mapdl.core._version import SUPPORTED_ANSYS_VERSIONS
from ansys.mapdl.core.errors import (
LockFileException,
Expand All @@ -72,6 +57,12 @@
threaded,
)

if _HAS_PIM:
import ansys.platform.instancemanagement as pypim

if _HAS_ATP:
from ansys.tools.path import find_ansys, get_ansys_path, version_from_path

if TYPE_CHECKING: # pragma: no cover
from ansys.mapdl.core.mapdl_console import MapdlConsole

Expand Down
10 changes: 2 additions & 8 deletions src/ansys/mapdl/core/licensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,13 @@
import subprocess
import time

from ansys.mapdl.core import LOG
from ansys.mapdl.core import _HAS_ATP, LOG
from ansys.mapdl.core.errors import LicenseServerConnectionError
from ansys.mapdl.core.misc import threaded_daemon

try:
if _HAS_ATP:
from ansys.tools.path import get_ansys_path, version_from_path

_HAS_ATP = True

except ModuleNotFoundError:
_HAS_ATP = False


LOCALHOST = "127.0.0.1"
LIC_PATH_ENVAR = "ANSYSLIC_DIR"
LIC_FILE_ENVAR = "ANSYSLMD_LICENSE_FILE"
Expand Down
Loading

0 comments on commit 4bb5192

Please sign in to comment.