<s> from toscaparser . capabilities import Capability <EOL> from toscaparser . common . exception import ExceptionCollector <EOL> from toscaparser . common . exception import MissingRequiredFieldError <EOL> from toscaparser . common . exception import UnknownFieldError <EOL> from toscaparser . common . exception import ValidationError <EOL> from toscaparser . elements . grouptype import GroupType <EOL> from toscaparser . elements . interfaces import InterfacesDef <EOL> from toscaparser . elements . nodetype import NodeType <EOL> from toscaparser . elements . policytype import PolicyType <EOL> from toscaparser . elements . relationshiptype import RelationshipType <EOL> from toscaparser . properties import Property <EOL> from toscaparser . utils . gettextutils import _ <EOL> class EntityTemplate ( object ) : <EOL> '''<STR_LIT>''' <EOL> SECTIONS = ( DERIVED_FROM , PROPERTIES , REQUIREMENTS , <EOL> INTERFACES , CAPABILITIES , TYPE , DESCRIPTION , DIRECTIVES , <EOL> ATTRIBUTES , ARTIFACTS , NODE_FILTER , COPY ) = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:type>' , '<STR_LIT:description>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> REQUIREMENTS_SECTION = ( NODE , CAPABILITY , RELATIONSHIP , OCCURRENCES , NODE_FILTER ) = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> SPECIAL_SECTIONS = ( METADATA ) = ( '<STR_LIT>' ) <EOL> def __init__ ( self , name , template , entity_name , custom_def = None ) : <EOL> self . name = name <EOL> self . entity_tpl = template <EOL> self . custom_def = custom_def <EOL> self . _validate_field ( self . entity_tpl ) <EOL> if entity_name == '<STR_LIT>' : <EOL> type = self . entity_tpl . get ( '<STR_LIT:type>' ) <EOL> self . type_definition = NodeType ( type , custom_def ) if type is not None else None <EOL> if entity_name == '<STR_LIT>' : <EOL> relationship = template . get ( '<STR_LIT>' ) <EOL> type = None <EOL> if relationship and isinstance ( relationship , dict ) : <EOL> type = relationship . get ( '<STR_LIT:type>' ) <EOL> elif isinstance ( relationship , str ) : <EOL> type = self . entity_tpl [ '<STR_LIT>' ] <EOL> else : <EOL> type = self . entity_tpl [ '<STR_LIT:type>' ] <EOL> self . type_definition = RelationshipType ( type , <EOL> None , custom_def ) <EOL> if entity_name == '<STR_LIT>' : <EOL> type = self . entity_tpl . get ( '<STR_LIT:type>' ) <EOL> if not type : <EOL> msg = ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' '<STR_LIT>' ) % dict ( pname = name ) ) <EOL> ExceptionCollector . appendException ( <EOL> ValidationError ( msg ) ) <EOL> self . type_definition = PolicyType ( type , custom_def ) <EOL> if entity_name == '<STR_LIT>' : <EOL> type = self . entity_tpl . get ( '<STR_LIT:type>' ) <EOL> self . type_definition = GroupType ( type , custom_def ) if type is not None else None <EOL> self . _properties = None <EOL> self . _interfaces = None <EOL> self . _requirements = None <EOL> self . _capabilities = None <EOL> @ property <EOL> def type ( self ) : <EOL> if self . type_definition : <EOL> return self . type_definition . type <EOL> @ property <EOL> def requirements ( self ) : <EOL> if self . _requirements is None : <EOL> self . _requirements = self . type_definition . get_value ( <EOL> self . REQUIREMENTS , <EOL> self . entity_tpl ) or [ ] <EOL> return self . _requirements <EOL> def get_properties_objects ( self ) : <EOL> '''<STR_LIT>''' <EOL> if self . _properties is None : <EOL> self . _properties = self . _create_properties ( ) <EOL> return self . _properties <EOL> def get_properties ( self ) : <EOL> '''<STR_LIT>''' <EOL> return { prop . name : prop <EOL> for prop in self . get_properties_objects ( ) } <EOL> def get_property_value ( self , name ) : <EOL> '''<STR_LIT>''' <EOL> props = self . get_properties ( ) <EOL> if props and name in props . keys ( ) : <EOL> return props [ name ] . value <EOL> @ property <EOL> def interfaces ( self ) : <EOL> if self . _interfaces is None : <EOL> self . _interfaces = self . _create_interfaces ( ) <EOL> return self . _interfaces <EOL> def get_capabilities_objects ( self ) : <EOL> '''<STR_LIT>''' <EOL> if not self . _capabilities : <EOL> self . _capabilities = self . _create_capabilities ( ) <EOL> return self . _capabilities <EOL> def get_capabilities ( self ) : <EOL> '''<STR_LIT>''' <EOL> return { cap . name : cap <EOL> for cap in self . get_capabilities_objects ( ) } <EOL> def is_derived_from ( self , type_str ) : <EOL> '''<STR_LIT>''' <EOL> if not self . type : <EOL> return False <EOL> elif self . type == type_str : <EOL> return True <EOL> elif self . parent_type : <EOL> return self . parent_type . is_derived_from ( type_str ) <EOL> else : <EOL> return False <EOL> def _create_capabilities ( self ) : <EOL> capability = [ ] <EOL> caps = self . type_definition . get_value ( self . CAPABILITIES , <EOL> self . entity_tpl , True ) <EOL> if caps : <EOL> for name , props in caps . items ( ) : <EOL> capabilities = self . type_definition . get_capabilities ( ) <EOL> if name in capabilities . keys ( ) : <EOL> c = capabilities [ name ] <EOL> properties = { } <EOL> if c . properties : <EOL> for property_name in c . properties . keys ( ) : <EOL> prop_def = c . properties [ property_name ] <EOL> if '<STR_LIT:default>' in prop_def : <EOL> properties [ property_name ] = prop_def [ '<STR_LIT:default>' ] <EOL> if '<STR_LIT>' in props and props [ '<STR_LIT>' ] : <EOL> properties . update ( props [ '<STR_LIT>' ] ) <EOL> cap = Capability ( name , properties , c ) <EOL> capability . append ( cap ) <EOL> return capability <EOL> def _validate_properties ( self , template , entitytype ) : <EOL> properties = entitytype . get_value ( self . PROPERTIES , template ) <EOL> self . _common_validate_properties ( entitytype , properties ) <EOL> def _validate_capabilities ( self ) : <EOL> type_capabilities = self . type_definition . get_capabilities ( ) <EOL> allowed_caps = type_capabilities . keys ( ) if type_capabilities else [ ] <EOL> capabilities = self . type_definition . get_value ( self . CAPABILITIES , <EOL> self . entity_tpl ) <EOL> if capabilities : <EOL> self . _common_validate_field ( capabilities , allowed_caps , <EOL> '<STR_LIT>' ) <EOL> self . _validate_capabilities_properties ( capabilities ) <EOL> def _validate_capabilities_properties ( self , capabilities ) : <EOL> for cap , props in capabilities . items ( ) : <EOL> capabilitydef = self . get_capability ( cap ) . definition <EOL> self . _common_validate_properties ( capabilitydef , <EOL> props [ self . PROPERTIES ] ) <EOL> for prop in self . get_capability ( cap ) . get_properties_objects ( ) : <EOL> prop . validate ( ) <EOL> if cap == "<STR_LIT>" and prop . name == "<STR_LIT>" : <EOL> prop_dict = props [ self . PROPERTIES ] <EOL> min_instances = prop_dict . get ( "<STR_LIT>" ) <EOL> max_instances = prop_dict . get ( "<STR_LIT>" ) <EOL> default_instances = prop_dict . get ( "<STR_LIT>" ) <EOL> if not ( min_instances <= default_instances <EOL> <= max_instances ) : <EOL> err_msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> self . name ) <EOL> ExceptionCollector . appendException ( <EOL> ValidationError ( message = err_msg ) ) <EOL> def _common_validate_properties ( self , entitytype , properties ) : <EOL> allowed_props = [ ] <EOL> required_props = [ ] <EOL> for p in entitytype . get_properties_def_objects ( ) : <EOL> allowed_props . append ( p . name ) <EOL> if p . required and p . default is None : <EOL> required_props . append ( p . name ) <EOL> if properties : <EOL> req_props_no_value_or_default = [ ] <EOL> self . _common_validate_field ( properties , allowed_props , <EOL> '<STR_LIT>' ) <EOL> for r in required_props : <EOL> if r not in properties . keys ( ) : <EOL> req_props_no_value_or_default . append ( r ) <EOL> if req_props_no_value_or_default : <EOL> ExceptionCollector . appendException ( <EOL> MissingRequiredFieldError ( <EOL> what = '<STR_LIT>' % self . name , <EOL> required = req_props_no_value_or_default ) ) <EOL> else : <EOL> if required_props : <EOL> ExceptionCollector . appendException ( <EOL> MissingRequiredFieldError ( <EOL> what = '<STR_LIT>' % self . name , <EOL> required = required_props ) ) <EOL> def _validate_field ( self , template ) : <EOL> if not isinstance ( template , dict ) : <EOL> ExceptionCollector . appendException ( <EOL> MissingRequiredFieldError ( <EOL> what = '<STR_LIT>' % self . name , required = self . TYPE ) ) <EOL> try : <EOL> relationship = template . get ( '<STR_LIT>' ) <EOL> if relationship and not isinstance ( relationship , str ) : <EOL> relationship [ self . TYPE ] <EOL> elif isinstance ( relationship , str ) : <EOL> template [ '<STR_LIT>' ] <EOL> else : <EOL> template [ self . TYPE ] <EOL> except KeyError : <EOL> ExceptionCollector . appendException ( <EOL> MissingRequiredFieldError ( <EOL> what = '<STR_LIT>' % self . name , required = self . TYPE ) ) <EOL> def _common_validate_field ( self , schema , allowedlist , section ) : <EOL> for name in schema : <EOL> if name not in allowedlist : <EOL> ExceptionCollector . appendException ( <EOL> UnknownFieldError ( <EOL> what = ( '<STR_LIT>' <EOL> % { '<STR_LIT>' : section , '<STR_LIT>' : self . name } ) , <EOL> field = name ) ) <EOL> def _create_properties ( self ) : <EOL> props = [ ] <EOL> properties = self . type_definition . get_value ( self . PROPERTIES , <EOL> self . entity_tpl ) or { } <EOL> for name , value in properties . items ( ) : <EOL> props_def = self . type_definition . get_properties_def ( ) <EOL> if props_def and name in props_def : <EOL> prop = Property ( name , value , <EOL> props_def [ name ] . schema , self . custom_def ) <EOL> props . append ( prop ) <EOL> for p in self . type_definition . get_properties_def_objects ( ) : <EOL> if p . default is not None and p . name not in properties . keys ( ) : <EOL> prop = Property ( p . name , p . default , p . schema , self . custom_def ) <EOL> props . append ( prop ) <EOL> return props <EOL> def _create_interfaces ( self ) : <EOL> interfaces = [ ] <EOL> type_interfaces = None <EOL> if isinstance ( self . type_definition , RelationshipType ) : <EOL> if isinstance ( self . entity_tpl , dict ) : <EOL> if self . INTERFACES in self . entity_tpl : <EOL> type_interfaces = self . entity_tpl [ self . INTERFACES ] <EOL> else : <EOL> for rel_def , value in self . entity_tpl . items ( ) : <EOL> if rel_def != '<STR_LIT:type>' : <EOL> rel_def = self . entity_tpl . get ( rel_def ) <EOL> rel = None <EOL> if isinstance ( rel_def , dict ) : <EOL> rel = rel_def . get ( '<STR_LIT>' ) <EOL> if rel : <EOL> if self . INTERFACES in rel : <EOL> type_interfaces = rel [ self . INTERFACES ] <EOL> break <EOL> else : <EOL> type_interfaces = self . type_definition . get_value ( self . INTERFACES , <EOL> self . entity_tpl ) <EOL> if type_interfaces : <EOL> for interface_type , value in type_interfaces . items ( ) : <EOL> for op , op_def in value . items ( ) : <EOL> iface = InterfacesDef ( self . type_definition , <EOL> interfacetype = interface_type , <EOL> node_template = self , <EOL> name = op , <EOL> value = op_def ) <EOL> interfaces . append ( iface ) <EOL> return interfaces <EOL> def get_capability ( self , name ) : <EOL> """<STR_LIT>""" <EOL> caps = self . get_capabilities ( ) <EOL> if caps and name in caps . keys ( ) : <EOL> return caps [ name ] </s>
