pp = pprint.PrettyPrinter( indent = 4, depth = 6, width = 120 )
for object_type in self.objects_read:
for object in self.objects_read[object_type]:
- object['templates'] = {}
- object['templates_applied'] = set([])
+ self._retrieve_templates( object_type, object )
+
+ #------------------------------------------------------
+ def _retrieve_templates( self, object_type, object ):
+ '''Recursive function to collect the templates of an object.
+ @param object_type: the type of the given object structure ('host', 'service', 'timeperiod' a.s.o.)
+ @type object_type: str
+ @param object: the nagios object
+ @type object: dict
+ @return: Ordered list of templates of this object
+ @rtype: list
+ '''
+
+ if not 'use' in object:
+ return []
+
+ file = object['use'][1]
+ row_num = object['use'][2]
+
+ if 'templates' in object:
+ return object['templates']
+
+ object['templates'] = []
+ object['templates_applied'] = set([])
+
+ for template_name in object['use'][0]:
+ if not template_name in self.templates[object_type]:
+ raise NagiosConfigError( "Template {0!r} for {1!r} in {2!r}({3}) not found.".format( template_name, object_type, file, row_num ) )
+ template_id = self.templates[object_type][template_name]
+ template_object = self.objects_read[object_type][template_id]
+ names = [ template_name ]
+ for name in self._retrieve_templates( object_type, template_name ):
+ names.append( name )
+ for name in names:
+ if name in object['templates_applied']:
+ self.logger.debug( "Template {0!r} for {1!r} allready applied.".format( name, object_type ) )
+ else:
+ object['templates'].append( name )
+ object['templates_applied'].add( name )
+
+ return object['templates']
#------------------------------------------------------
def register_templates( self ):