sputnik13 commented on a change in pull request #288: Refactor config URL: https://github.com/apache/fluo-muchos/pull/288#discussion_r332239284
########## File path: lib/muchos/config/decorators.py ########## @@ -0,0 +1,98 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from collections.abc import Iterable +from functools import wraps + + +_host_vars = [] +_play_vars = [] +_extra_vars = [] +_ansible_vars = dict( + host=[], + play=[], + extra=[] +) + +def get_ansible_vars(var_type): + return _ansible_vars.get(var_type) + +# ansible hosts inventory variables +def ansible_host_var(name=None): + return ansible_var_decorator('host', name) + +# ansible group/all variables +def ansible_play_var(name=None): + return ansible_var_decorator('play', name) + +# ansible extra variables +def ansible_extra_var(name=None): + return ansible_var_decorator('extra', name) + +def ansible_var_decorator(var_type, name): + def _decorator(func): + _ansible_vars[var_type].append((name if isinstance(name, str) else func.__name__, func.__qualname__.split('.')[0], func.__name__)) Review comment: We tried storing the func pointer as the first few iterations, which worked because we didn't allow abstract methods to be defined as ansible vars. However, that made it so that we have to decorate all cluster type configs individually for standard variables (like node_type_map). I personally favor being verbose/explicit but thought it would be useful shorthand to be able to just define the output locations (play/host/extra var) in the base class for common variables. If we allow this, however, then the base implementation is what's stored, and so at render time there is no useful output provided. By storing the function name, at render time, we retrieve the method from the config object itself with getattr to ensure we always use the concrete implementation rather than the abstract base implementation. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
