Merge "Release D doc update"
[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,
22                                        self.DATATYPE_NETWORK_PREFIX,
23                                        custom_def)
24         self.custom_def = custom_def
25
26     @property
27     def parent_type(self):
28         '''Return a datatype this datatype is derived from.'''
29         ptype = self.derived_from(self.defs)
30         if ptype:
31             return DataType(ptype, self.custom_def)
32         return None
33
34     @property
35     def value_type(self):
36         '''Return 'type' section in the datatype schema.'''
37         return self.entity_value(self.defs, 'type')
38
39     def get_all_properties_objects(self):
40         '''Return all properties objects defined in type and parent type.'''
41         props_def = self.get_properties_def_objects()
42         ptype = self.parent_type
43         while ptype:
44             props_def.extend(ptype.get_properties_def_objects())
45             ptype = ptype.parent_type
46         return props_def
47
48     def get_all_properties(self):
49         '''Return a dictionary of all property definition name-object pairs.'''
50         return {prop.name: prop
51                 for prop in self.get_all_properties_objects()}
52
53     def get_all_property_value(self, name):
54         '''Return the value of a given property name.'''
55         props_def = self.get_all_properties()
56         if props_def and name in props_def.key():
57             return props_def[name].value