Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 3AF1C200CDE for ; Tue, 8 Aug 2017 15:12:02 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 396D416742B; Tue, 8 Aug 2017 13:12:02 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 5F8E116742C for ; Tue, 8 Aug 2017 15:12:01 +0200 (CEST) Received: (qmail 99753 invoked by uid 500); 8 Aug 2017 13:12:00 -0000 Mailing-List: contact commits-help@ariatosca.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ariatosca.incubator.apache.org Delivered-To: mailing list commits@ariatosca.incubator.apache.org Received: (qmail 99678 invoked by uid 99); 8 Aug 2017 13:12:00 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 08 Aug 2017 13:12:00 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 0A707E0395; Tue, 8 Aug 2017 13:11:57 +0000 (UTC) From: mxmrlv To: commits@ariatosca.apache.org Reply-To: commits@ariatosca.apache.org References: In-Reply-To: Subject: [GitHub] incubator-ariatosca pull request #188: ARIA-324 Refactor ctx proxy access Content-Type: text/plain Message-Id: <20170808131159.0A707E0395@git1-us-west.apache.org> Date: Tue, 8 Aug 2017 13:11:57 +0000 (UTC) archived-at: Tue, 08 Aug 2017 13:12:02 -0000 Github user mxmrlv commented on a diff in the pull request: https://github.com/apache/incubator-ariatosca/pull/188#discussion_r131905254 --- Diff: aria/orchestrator/execution_plugin/ctx_proxy/server.py --- @@ -170,101 +178,59 @@ def _process_ctx_request(ctx, args): # pylint: disable=too-many-branches,too-man modifying_key = None modifying_value = None - num_args = len(args) - - while index < num_args: - arg = args[index] - - # Object attribute - attr = _desugar_attr(current, arg) - if attr is not None: - current = getattr(current, attr) - - # List entry - elif isinstance(current, list) and _is_int(arg): - current = current[int(arg)] - - # Dict (or dict-like object) value - elif hasattr(current, '__getitem__'): - if modifying and (not arg in current): - current[arg] = {} - current = current[arg] - - # Call - elif callable(current): - if isinstance(current, functools.partial): - argspec = inspect.getargspec(current.func) - # Don't count initial args fulfilled by the partial - spec_args = argspec.args[len(current.args):] - # Don't count keyword args fulfilled by the partial - spec_args = tuple(a for a in spec_args if a not in current.keywords) - else: - argspec = inspect.getargspec(current) - spec_args = argspec.args - - callable_kwargs = {} - if isinstance(arg, dict): - # If the first arg is a dict, treat it as our kwargs - # TODO: what if the first argument really needs to be a dict? - callable_kwargs = arg - index += 1 - - if argspec.varargs is not None: - # Gobble the rest of the args - callable_args = args[index:] - else: - # Take only what we need - spec_args = tuple(a for a in spec_args if a not in callable_kwargs) - spec_args_count = len(spec_args) - if inspect.ismethod(current): - # Don't count "self" argument - spec_args_count -= 1 - callable_args = args[index:index + spec_args_count] - # Note: we might actually have fewer args than the args_count, but that could be OK - # if the user expects subsequent args to have default values - - args_count = len(callable_args) - if args_count > 1: - index += args_count - 1 - - current = current(*callable_args, **callable_kwargs) - - else: - raise RuntimeError('`{0}` cannot be processed in {1}'.format(arg, args)) - - index += 1 - - if callable(current): - current = current() + # Parse all arguments + while len(args) > 0: + obj, args = _parse_argument(obj, args, modifying) if modifying: - if hasattr(current, '__setitem__'): - # Modify dict value - current[modifying_key] = modifying_value - else: + if hasattr(obj, '__setitem__'): + # Modify item value (dict, list, and similar) + if isinstance(obj, (list, tuple)): + modifying_key = int(modifying_key) + obj[modifying_key] = modifying_value + elif hasattr(obj, modifying_key): # Modify object attribute - setattr(current, modifying_key, modifying_value) - - return current + setattr(obj, modifying_key, modifying_value) + else: + raise ProcessingError('Cannot modify `{0}` of `{1!r}`' + .format(modifying_key, obj)) + return obj -def _desugar_attr(obj, attr): - if not isinstance(attr, basestring): - return None - if hasattr(obj, attr): - return attr - attr = attr.replace('-', '_') - if hasattr(obj, attr): - return attr - return None +def _parse_argument(obj, args, modifying): + args = list(args) + arg = args.pop(0) -def _is_int(arg): - try: - int(arg) - except ValueError: - return False - return True + # Call? + if arg == '[': + # TODO: should there be a way to escape "[" and "]" in case they are needed as real + # arguments? + try: + closing_index = args.index(']') + except ValueError: + raise ParsingError('Opening "[" without a closing "]') + callable_args = args[:closing_index] + args = args[closing_index + 1:] + if not callable(obj): + raise ProcessingError('Used "[" and "] on an object that is not callable') + return obj(*callable_args), args + + # Attribute? + if isinstance(arg, basestring): + if hasattr(obj, arg): + return getattr(obj, arg), args + arg_sugared = arg.replace('-', '_') + if hasattr(obj, arg_sugared): + return getattr(obj, arg_sugared), args + + # Item? (dict, lists, and similar) + if hasattr(obj, '__getitem__'): + if modifying and hasattr(obj, 'has_key') and (not obj.has_key(arg)): --- End diff -- but who promises my the support of `has_key`? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---