Support costum datatype in capability
[parser.git] / tosca2heat / tosca-parser / toscaparser / capabilities.py
1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
2 #    not use this file except in compliance with the License. You may obtain
3 #    a copy of the License at
4 #
5 #         http://www.apache.org/licenses/LICENSE-2.0
6 #
7 #    Unless required by applicable law or agreed to in writing, software
8 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 #    License for the specific language governing permissions and limitations
11 #    under the License.
12
13 from toscaparser.properties import Property
14
15
16 class Capability(object):
17     '''TOSCA built-in capabilities type.'''
18
19     def __init__(self, name, properties, definition, custom_def):
20         self.name = name
21         self._properties = properties
22         self.definition = definition
23         self.custom_def = custom_def
24
25     def get_properties_objects(self):
26         '''Return a list of property objects.'''
27         properties = []
28         props = self._properties
29         if props:
30             for name, value in props.items():
31                 props_def = self.definition.get_properties_def()
32                 if props_def and name in props_def:
33                     properties.append(Property(name, value,
34                                                props_def[name].schema,
35                                                self.custom_def))
36         return properties
37
38     def get_properties(self):
39         '''Return a dictionary of property name-object pairs.'''
40         return {prop.name: prop
41                 for prop in self.get_properties_objects()}
42
43     def get_property_value(self, name):
44         '''Return the value of a given property name.'''
45         props = self.get_properties()
46         if props and name in props:
47             return props[name].value