Infrastructure updates for dependent changes.
[alexxy/gromacs.git] / python_packaging / src / gmxapi / version.py
1 # This file is part of the GROMACS molecular simulation package.
2 #
3 # Copyright (c) 2019, by the GROMACS development team, led by
4 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
5 # and including many others, as listed in the AUTHORS file in the
6 # top-level source directory and at http://www.gromacs.org.
7 #
8 # GROMACS is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public License
10 # as published by the Free Software Foundation; either version 2.1
11 # of the License, or (at your option) any later version.
12 #
13 # GROMACS is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with GROMACS; if not, see
20 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
21 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
22 #
23 # If you want to redistribute modifications to GROMACS, please
24 # consider that scientific software is very special. Version
25 # control is crucial - bugs must be traceable. We will be happy to
26 # consider code for inclusion in the official distribution, but
27 # derived work must not be called official GROMACS. Details are found
28 # in the README & COPYING files - if they are missing, get the
29 # official version at http://www.gromacs.org.
30 #
31 # To help us fund GROMACS development, we humbly ask that you cite
32 # the research papers on the package. Check out http://www.gromacs.org.
33 """
34 Provide version and release information.
35
36 Attributes:
37     major (int): gmxapi major version number.
38     minor (int): gmxapi minor version number.
39     patch (int): gmxapi patch level number.
40     release (bool): True if imported gmx module is an officially tagged release, else False.
41
42 """
43
44 # TODO: (pending infrastructure and further discussion) Configure with CMake.
45 # __version__ = "@PROJECT_VERSION@"
46 # major = @PROJECT_VERSION_MAJOR@
47 # minor = @PROJECT_VERSION_MINOR@
48 # patch = @PROJECT_VERSION_PATCH@
49
50 from gmxapi.exceptions import Error as GmxapiError
51
52 __version__ = "0.1.0-dev0"
53 major = 0
54 minor = 1
55 patch = 0
56
57 # Note: this is not automatically updated. See RELEASE.txt and https://github.com/kassonlab/gmxapi/issues/152
58 release = False
59
60 _named_features = []
61
62
63 class FeatureError(GmxapiError):
64     """Module exception to indicate missing features."""
65
66
67 def api_is_at_least(major_version, minor_version=0, patch_version=0):
68     """Allow client to check whether installed module supports the requested API level.
69
70     Arguments:
71         major_version (int): gmxapi major version number.
72         minor_version (int): optional gmxapi minor version number (default: 0).
73         patch_version (int): optional gmxapi patch level number (default: 0).
74
75     Returns:
76         True if installed gmx package is greater than or equal to the input level
77
78     Note that if gmxapi.version.release is False, the package is not guaranteed to correctly or
79     fully support the reported API level.
80     """
81     if not isinstance(major_version, int) or not isinstance(minor_version, int) or not isinstance(patch_version, int):
82         raise TypeError('Version levels must be provided as integers.')
83     if major >= major_version:
84         return True
85     elif major == major_version and minor >= minor_version:
86         return True
87     elif major == major_version and minor == minor_version and patch >= patch_version:
88         return True
89     else:
90         return False
91
92
93 def has_feature(name='', enable_exception=False):
94     """Query whether a named feature is available in the installed package.
95
96     Between updates to the API specification, new features or experimental aspects
97     may be introduced into the package and need to be detectable. This function
98     is intended to facilitate code testing and resolving differences between
99     development branches. Users should refer to the documentation for the package
100     modules and API level.
101
102     The function can be used to get a boolean result or can be used to raise an
103     exception in code block by setting `enable_exception=True`
104
105     Returns:
106         True if named feature is recognized by the installed package, else False.
107
108     Raises:
109         gmxapi.version.FeatureError if `enable_exception == True` and feature is not found.
110
111     """
112     if name in _named_features:
113         return True
114     else:
115         if enable_exception:
116             raise FeatureError('Feature {} not available.'.format(str(name)))
117         return False