Coverage for src/meshpy/utils/environment.py: 95%
20 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-28 04:21 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-28 04:21 +0000
1# The MIT License (MIT)
2#
3# Copyright (c) 2018-2025 MeshPy Authors
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in
13# all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21# THE SOFTWARE.
22"""Helper functions to interact with the MeshPy environment."""
24import os as _os
25from importlib.util import find_spec as _find_spec
28def cubitpy_is_available() -> bool:
29 """Check if CubitPy is installed.
31 Returns:
32 True if CubitPy is installed, False otherwise
33 """
35 if _find_spec("cubitpy") is None:
36 return False
37 return True
40def fourcipp_is_available() -> bool:
41 """Check if FourCIPP is installed.
43 Returns:
44 True if FourCIPP is installed, False otherwise
45 """
47 if _find_spec("fourcipp") is None:
48 return False
49 return True
52def is_mybinder():
53 """Check if the current environment is running on mybinder."""
54 return "BINDER_LAUNCH_HOST" in _os.environ.keys()
57def is_testing():
58 """Check if the current environment is a pytest testing run."""
59 return "PYTEST_CURRENT_TEST" in _os.environ
62def get_env_variable(name, *, default="default_not_set"):
63 """Return the value of an environment variable.
65 Args
66 ----
67 name: str
68 Name of the environment variable
69 default:
70 Value to be returned if the given named environment variable does
71 not exist. If this is not set and the name is not in the env
72 variables, then an error will be thrown.
73 """
74 if name in _os.environ.keys():
75 return _os.environ[name]
76 elif default == "default_not_set":
77 raise ValueError(f"Environment variable {name} is not set")
78 return default