Added all common modules in conductor directory
[optf/has.git] / conductor / conductor / common / classes.py
1 #
2 # -------------------------------------------------------------------------
3 #   Copyright (c) 2015-2017 AT&T Intellectual Property
4 #
5 #   Licensed under the Apache License, Version 2.0 (the "License");
6 #   you may not use this file except in compliance with the License.
7 #   You may obtain a copy of the License at
8 #
9 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #   Unless required by applicable law or agreed to in writing, software
12 #   distributed under the License is distributed on an "AS IS" BASIS,
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #   See the License for the specific language governing permissions and
15 #   limitations under the License.
16 #
17 # -------------------------------------------------------------------------
18 #
19
20 """Class Helpers"""
21
22 from conductor.i18n import _LE  # pylint: disable=W0212
23
24
25 def get_class(kls):
26     """Returns a class given a fully qualified class name"""
27     parts = kls.split('.')
28     module = ".".join(parts[:-1])
29     mod = __import__(module)
30     for comp in parts[1:]:
31         mod = getattr(mod, comp)
32     return mod
33
34
35 class abstractclassmethod(classmethod):  # pylint: disable=C0103,R0903
36     """Abstract Class Method Decorator from Python 3.3's abc module"""
37
38     __isabstractmethod__ = True
39
40     def __init__(self, callable):  # pylint: disable=W0622
41         callable.__isabstractmethod__ = True
42         super(abstractclassmethod, self).__init__(callable)
43
44
45 class ClassPropertyDescriptor(object):  # pylint: disable=R0903
46     """Supports the notion of a class property"""
47
48     def __init__(self, fget, fset=None):
49         """Initializer"""
50         self.fget = fget
51         self.fset = fset
52
53     def __get__(self, obj, klass=None):
54         """Get attribute"""
55         if klass is None:
56             klass = type(obj)
57         return self.fget.__get__(obj, klass)()
58
59     def __set__(self, obj, value):
60         """Set attribute"""
61         if not self.fset:
62             raise AttributeError(_LE("Can't set attribute"))
63         type_ = type(obj)
64         return self.fset.__get__(obj, type_)(value)
65
66     def setter(self, func):
67         """Setter"""
68         if not isinstance(func, (classmethod, staticmethod)):
69             func = classmethod(func)
70         self.fset = func
71         return self
72
73
74 def classproperty(func):
75     """Class Property decorator"""
76     if not isinstance(func, (classmethod, staticmethod)):
77         func = classmethod(func)
78
79     return ClassPropertyDescriptor(func)