ariatosca-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tliron <...@git.apache.org>
Subject [GitHub] incubator-ariatosca pull request #188: ARIA-324 Refactor ctx proxy access
Date Sun, 30 Jul 2017 17:27:48 GMT
Github user tliron commented on a diff in the pull request:

    https://github.com/apache/incubator-ariatosca/pull/188#discussion_r130247602
  
    --- Diff: aria/orchestrator/execution_plugin/ctx_proxy/server.py ---
    @@ -150,43 +150,101 @@ def __exit__(self, *args, **kwargs):
             self.close()
     
     
    -def _process_ctx_request(ctx, args):
    +def _process_ctx_request(ctx, args): # pylint: disable=too-many-branches,too-many-statements
         current = ctx
    -    num_args = len(args)
         index = 0
    +
    +    try:
    +        # TODO: should there be a way to escape "=" in case it is needed as real argument?
    +        equals_index = args.index('=')
    +        if equals_index == 0:
    +            raise RuntimeError('The "=" argument cannot be first')
    +        if equals_index != len(args) - 2:
    +            raise RuntimeError('The "=" argument must be penultimate')
    +        modifying = True
    +        modifying_key = args[-3]
    +        modifying_value = args[-1]
    +        args = args[:-3]
    +    except ValueError:
    +        modifying = False
    +        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:
    +        if attr is not None:
                 current = getattr(current, attr)
    -        elif isinstance(current, collections.MutableMapping):
    -            key = arg
    -            path_dict = _PathDictAccess(current)
    -            if index + 1 == num_args:
    -                # read dict prop by path
    -                value = path_dict.get(key)
    -                current = value
    -            elif index + 2 == num_args:
    -                # set dict prop by path
    -                value = args[index + 1]
    -                path_dict.set(key, value)
    -                current = None
    -            else:
    -                raise RuntimeError('Illegal argument while accessing dict')
    -            break
    +
    +        # 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):
    -            kwargs = {}
    -            remaining_args = args[index:]
    -            if isinstance(remaining_args[-1], collections.MutableMapping):
    -                kwargs = remaining_args[-1]
    -                remaining_args = remaining_args[:-1]
    -            current = current(*remaining_args, **kwargs)
    -            break
    +            if isinstance(current, functools.partial):
    --- End diff --
    
    +1
    
    The main reason for all of this is that we sometimes need to continue access properties
on returned values on callables. The previous implementation simpled "gobbled" all remaining
args, but we might need those args. Example from Clearwater:
    
    `ctx service get_policy_by_type clearwater.Configuration properties zone`
    
    But generally I suggest you take a look at the various uses of `ctx` in the [Clearwater
example](https://github.com/apache/incubator-ariatosca/blob/ARIA-321-clearwater/examples/clearwater/scripts/host/configure.sh).
    
    
    



---
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.
---

Mime
View raw message