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