Repository: ambari Updated Branches: refs/heads/trunk 1767fcfa4 -> e4b2d34b6
AMBARI-8317 Refactor the OS-dependent Ambari Server Windows components Pass 1.1 Preliminary changes. Introducing OS-dependent abstraction for functions. Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/e4b2d34b Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/e4b2d34b Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/e4b2d34b Branch: refs/heads/trunk Commit: e4b2d34b6bf1f1c31944576277cb71f8d771c2e8 Parents: 1767fcf Author: Florian Barca <fba...@hortonworks.com> Authored: Mon Jan 5 15:51:48 2015 -0800 Committer: Florian Barca <fba...@hortonworks.com> Committed: Mon Jan 5 15:51:48 2015 -0800 ---------------------------------------------------------------------- .../python/ambari_commons/os_family_impl.py | 42 +++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/e4b2d34b/ambari-common/src/main/python/ambari_commons/os_family_impl.py ---------------------------------------------------------------------- diff --git a/ambari-common/src/main/python/ambari_commons/os_family_impl.py b/ambari-common/src/main/python/ambari_commons/os_family_impl.py index f8a3379..65a502a 100644 --- a/ambari-common/src/main/python/ambari_commons/os_family_impl.py +++ b/ambari-common/src/main/python/ambari_commons/os_family_impl.py @@ -17,17 +17,20 @@ limitations under the License. ''' import types -from os_check import OSCheck +from ambari_commons import OSCheck class OsFamilyImpl(object): """ - Base class for os depended factory. Usage:: + Base class for os dependent factory. Usage:: class BaseFoo(object): pass - @Factory("windows") - class OsFoo(object):pass - print BaseFoo()# OsFoo + @OsFamilyImpl(os_family="windows") + class OsFooW(BaseFoo):pass + print BaseFoo()# OsFooW + @OsFamilyImpl(os_family=OsFamilyImpl.DEFAULT) + class OsFooD(BaseFoo):pass + print BaseFoo()# OsFooD """ @@ -62,3 +65,32 @@ class OsFamilyImpl(object): base_cls.__new__ = types.MethodType(new, base_cls) return cls + +class OsFamilyFuncImpl(object): + """ + Base class for os dependent function. Usage:: + + @OSFamilyFuncImpl(os_family="windows") + def os_foo(...):pass + + """ + _func_impls = {} + + def _createFunctionInstance(self, func): + self._func_impls[func.__module__ + "." + func.__name__ + "." + self.os_const] = func + + def thunk(*args, **kwargs): + fn_id_base = func.__module__ + "." + func.__name__ + fn_id = fn_id_base + "." + OSCheck.get_os_family() + if fn_id not in self._func_impls: + fn_id = fn_id_base + "." + OsFamilyImpl.DEFAULT + + fn = self._func_impls[fn_id] + return fn(*args, **kwargs) + return thunk + + def __init__(self, os_family): + self.os_const = os_family + + def __call__(self, func): + return self._createFunctionInstance(func)