Debian Codegraph @

cassandra/datastax/insights/util.py

raw
1 # Copyright DataStax, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import logging
16 import traceback
17 from warnings import warn
18
19 from cassandra.util import Version
20
21
22 DSE_60 = Version('6.0.0')
23 DSE_51_MIN_SUPPORTED = Version('5.1.13')
24 DSE_60_MIN_SUPPORTED = Version('6.0.5')
25
26
27 log = logging.getLogger(__name__)
28
29
30 def namespace(cls):
31 """
32 Best-effort method for getting the namespace in which a class is defined.
33 """
34 try:
35 # __module__ can be None
36 module = cls.__module__ or ''
37 except Exception:
38 warn("Unable to obtain namespace for {cls} for Insights, returning ''. "
39 "Exception: \n{e}".format(e=traceback.format_exc(), cls=cls))
40 module = ''
41
42 module_internal_namespace = _module_internal_namespace_or_emtpy_string(cls)
43 if module_internal_namespace:
44 return '.'.join((module, module_internal_namespace))
45 return module
46
47
48 def _module_internal_namespace_or_emtpy_string(cls):
49 """
50 Best-effort method for getting the module-internal namespace in which a
51 class is defined -- i.e. the namespace _inside_ the module.
52 """
53 try:
54 qualname = cls.__qualname__
55 except AttributeError:
56 return ''
57
58 return '.'.join(
59 # the last segment is the name of the class -- use everything else
60 qualname.split('.')[:-1]
61 )
62
63
64 def version_supports_insights(dse_version):
65 if dse_version:
66 try:
67 dse_version = Version(dse_version)
68 return (DSE_51_MIN_SUPPORTED <= dse_version < DSE_60
69 or
70 DSE_60_MIN_SUPPORTED <= dse_version)
71 except Exception:
72 warn("Unable to check version {v} for Insights compatibility, returning False. "
73 "Exception: \n{e}".format(e=traceback.format_exc(), v=dse_version))
74
75 return False