Update tosca lib to version 0.5
[parser.git] / tosca2heat / tosca-parser / toscaparser / elements / datatype.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
14 from toscaparser.elements.statefulentitytype import StatefulEntityType
15
16
17 class DataType(StatefulEntityType):
18     '''TOSCA built-in and user defined complex data type.'''
19
20     def __init__(self, datatypename, custom_def=None):
21         super(DataType, self).__init__(datatypename, self.DATATYPE_PREFIX,
22                                        custom_def)
23         self.custom_def = custom_def
24
25     @property
26     def parent_type(self):
27         '''Return a datatype this datatype is derived from.'''
28         ptype = self.derived_from(self.defs)
29         if ptype:
30             return DataType(ptype, self.custom_def)
31         return None
32
33     @property
34     def value_type(self):
35         '''Return 'type' section in the datatype schema.'''
36         return self.entity_value(self.defs, 'type')
37
38     def get_all_properties_objects(self):
39         '''Return all properties objects defined in type and parent type.'''
40         props_def = self.get_properties_def_objects()
41         ptype = self.parent_type
42         while ptype:
43             props_def.extend(ptype.get_properties_def_objects())
44             ptype = ptype.parent_type
45         return props_def
46
47     def get_all_properties(self):
48         '''Return a dictionary of all property definition name-object pairs.'''
49         return {prop.name: prop
50                 for prop in self.get_all_properties_objects()}
51
52     def get_all_property_value(self, name):
53         '''Return the value of a given property name.'''
54         props_def = self.get_all_properties()
55         if props_def and name in props_def.key():
56             return props_def[name].value