Source code for tracesuite
"""Python interface to TRACE and POST.
This is the root module of the Python interface of ``TRACE`` and ``POST``.
It contains common attributes of both interfaces like shared enumeration
classes and basic information like the :py:attr:`.version` of the Python
interface:
.. code-block:: python
print(f"TraceSuite Version: {tracesuite.version}")
print(f"PyTraceSuite API: {tracesuite.api_version}")
The specific interfaces for TRACE and POST are implemented in the
sub-packages :py:mod:`.trace` and :py:mod:`.post` respectively.
"""
# ruff: noqa: E402
from __future__ import annotations
import atexit
import sys
import mpi4py.rc
mpi4py.rc.initialize = False
from packaging.version import Version
from . import _trace_suite
from ._trace_suite import (
AsciiOutputType,
BlockGroupType,
BoundaryType,
CoordinateSystem,
FamilyType,
GridLocation,
OutputType,
RowType,
SpatialAveraging,
TemporalAveraging,
has_post,
has_trace,
)
__all__ = (
"AsciiOutputType",
"BlockGroupType",
"BoundaryType",
"CoordinateSystem",
"FamilyType",
"GridLocation",
"OutputType",
"RowType",
"SpatialAveraging",
"TemporalAveraging",
"has_post",
"has_trace",
)
MINIMUM_PYTHON_VERSION = (3, 9, 0)
if sys.version_info < MINIMUM_PYTHON_VERSION:
msg = (f"Your python version is {sys.version}. "
f"The py_trace_suite is only tested with python {'.'.join(str(part) for part in MINIMUM_PYTHON_VERSION)} or higher.")
raise ImportError(msg)
# For full version syntax please consult: https://www.python.org/dev/peps/pep-0440/
__MAJOR_VERSION = 1 # change when function prototypes or classes were changed or removed
__MINOR_VERSION = 0 # change when you implemented new functionality
__PRE_RELEASE = "" # empty string for released versions
# Make sure that the communication environment is set up
# The noqa is required because the import must be after the type definitions
# in order to avoid circular imports due to the definitions above
from tracesuite.trace._trace import comm_finalize, comm_initialize # noqa: I001
if comm_initialize():
atexit.register(comm_finalize)
class TraceSuiteVersion(Version):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@property
def major(self):
try:
return self.release[0]
except IndexError:
return None
@property
def minor(self):
try:
return self.release[1]
except IndexError:
return None
@property
def patch(self):
try:
return self.release[2]
except IndexError:
return None
api_version = TraceSuiteVersion(f"{__MAJOR_VERSION}.{__MINOR_VERSION}{__PRE_RELEASE}")
"""Current pytracesuite API version.
It is recommended in any script importing pytracesuite to check for the correct API version.
This allows for early error messages and warnings if the interface did change.
`api_version` does have a major (the first number) and a minor part (the second number).
These are accessible as attribute as well.
The minor part is changed, when there is new functionality in the API,
that means there are new classes, functions or new optional arguments to one of the former.
A change in the major version number indicates a broken backwards compatibility.
Some functions were removed from the API or some arguments changed their meaning or were removed.
.. code-block:: python
#!/usr/bin/env python3
import tracesuite
assert(1 <= tracesuite.api_version.major <= 2) # only allow API version 1 or 2
print(f"Using pytracesuite API {tracesuite.api_version}")
post = tracesuite.post.POST()
if tracesuite.api_version.major == 1:
post.version_1_function()
else:
assert(tracesuite.api_version.major == 2)
post.version_1_function(changed_args)
post.version_2_function()
if tracesuite.api_version.minor >= 3:
post.function_new_in_2_3()
"""
version = TraceSuiteVersion(_trace_suite.version())
"""tracesuite version.
This is the version of the TraceSuite. It is based on the
`semantic versioning standard <https://semver.org/>`_.
It consists of three parts (MAJOR.MINOR.PATCH).
MAJOR and MINOR are increased every official release, while the PATCH part is increased every single commit.
These are accessible as attributes.
.. code-block:: python
#!/usr/bin/env python3
import tracesuite
print(f"Using tracesuite version {tracesuite.version}")
"""
# this attribute will be replaced by CMake
__version__ = "9.8.1"
if __version__ != str(version):
message = f"Version of the source files ({__version__}) does not match library ({version})!"
raise RuntimeError(message)
git_revision = _trace_suite.git_revision()
"""GIT Revision.
This is the git revision of the TraceSuite. It is generated
by git describe and can be used to uniquely identify one
specific version of the source code. Normally this should
not be required and the :py:attr:`.version` and
:py:attr:`api_version` should be more relevant.
"""
del Version