summaryrefslogtreecommitdiffstats
path: root/azure/aria/aria-extension-cloudify/src/aria/extensions/aria_extension_tosca/simple_v1_0/presentation/types.py
blob: 5f9750e18f253c7c2f9d7efd12b20c34e73216d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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.


def convert_name_to_full_type_name(context, name, types_dict): # pylint: disable=unused-argument
    """
    Converts a type name to its full type name, or else returns it unchanged.

    Works by checking for ``shorthand_name`` and ``type_qualified_name`` in the types'
    ``_extensions`` field. See also
    :class:`aria_extension_tosca.v1_0.presentation.extensible.ExtensiblePresentation`.

    Can be used as the conversion function argument in ``type_validator`` and
    ``derived_from_validator``.
    """

    if (name is not None) and types_dict and (name not in types_dict):
        for full_name, the_type in types_dict.iteritems():
            if hasattr(the_type, '_extensions') and the_type._extensions \
                and ((the_type._extensions.get('shorthand_name') == name) \
                     or (the_type._extensions.get('type_qualified_name') == name)):
                return full_name
    return name


def get_type_by_name(context, name, *types_dict_names):
    """
    Gets a type either by its full name or its shorthand name or type-qualified name.

    Works by checking for ``shorthand_name`` and ``type_qualified_name`` in the types'
    ``_extensions`` field. See also
    :class:`~aria_extension_tosca.v1_0.presentation.extensible.ExtensiblePresentation`.

    The arguments from the third onwards are used to locate a nested field under
    ``service_template`` under the root presenter.
    """

    if name is not None:
        types_dict = context.presentation.get('service_template', *types_dict_names)
        if types_dict:
            the_type = types_dict.get(name)
            if the_type is not None:
                # Full name
                return the_type
            for the_type in types_dict.itervalues():
                if hasattr(the_type, '_extensions') and the_type._extensions \
                    and ((the_type._extensions.get('shorthand_name') == name) \
                         or (the_type._extensions.get('type_qualified_name') == name)):
                    # Shorthand name
                    return the_type
    return None