Пример #1
0
def load_triggers_from_xml(file):
	xmlfile = parse(file)
	group = xmlfile.getroot()

	new_group = {}

	# Ensure these triggers have a group
	if group.tag != 'group':
		print "load_triggers_from_xml: File %s not contained in a <group>" % file
		return False

	# Gracefully handle group name being omitted
	try:
		new_group['name'] = group.attrib['name']
	except KeyError:
		print "load_triggers_from_xml: Group name not defined in %s" % file
		return False

	# Optionally apply group enabled/disabled
	try:
		new_group['enabled'] = group.attrib['enabled']
		new_group['enabled'] = str_to_bool(new_group['enabled'])
	except KeyError:
		new_group['enabled'] = False

	core.add_trigger_group(new_group['name'], new_group['enabled'])

	# Load triggers for group
	for trigger in group:
		new_trigger = {
			'name': None,
			'type': None,
			'pattern': None,
			'enabled': True,
			'function': [],
			'pfunction': [],
			'pdisable_trigger': [],
			'affliction': [],
			'cure': [],
			'bcure': [],
			'symptom': [],
			'gag': [],
			'enable_group': [],
			'disable_group': [],
			'enable_trigger': [],
			'disable_trigger': [],
			'ondef': [],
			'undef': [],
			'send': [],
			'deflist': None
		}

		for node in trigger:
			if node.tag == 'name':
				new_trigger['name'] = node.text

			elif node.tag == 'type':
				new_trigger['type'] = node.text

			elif node.tag == 'pattern':
				new_trigger['pattern'] = node.text

			elif node.tag == 'enabled':
				new_trigger['enabled'] = str_to_bool(node.text)

			elif node.tag == 'function':
				new_trigger['function'].append(node.text)

			elif node.tag == 'pfunction':
				new_trigger['pfunction'].append(node.text)

			elif node.tag == 'pdisable_trigger':
				new_trigger['pdisable_trigger'].append(node.text)

			elif node.tag == 'affliction':
				new_trigger['affliction'].append(node.text)

			elif node.tag == 'symptom':
				new_trigger['symptom'].append(node.text)

			elif node.tag == 'cure':
				new_trigger['cure'].append(node.text)

			elif node.tag == 'bcure':
				new_trigger['bcure'].append(node.text)

			elif node.tag == 'gag':
				new_trigger['gag'].append(node.text)

			elif node.tag == 'send':
				new_trigger['send'].append(node.text)

			elif node.tag == 'enable_group':
				new_trigger['enable_group'].append(node.text)

			elif node.tag == 'disable_group':
				new_trigger['disable_group'].append(node.text)

			elif node.tag == 'enable_trigger':
				new_trigger['enable_trigger'].append(node.text)

			elif node.tag == 'disable_trigger':
				new_trigger['disable_trigger'].append(node.text)

			elif node.tag == 'undef':
				new_trigger['undef'].append(node.text)

			elif node.tag == 'ondef':
				new_trigger['ondef'].append(node.text)

			elif node.tag == 'deflist':
				new_trigger['deflist'] = node.text

		# Validate <trigger>
		keys = new_trigger.keys()

		# There has to be a better way.....
		if 'name' not in keys or 'type' not in keys or 'pattern' not in keys:
			print "load_triggers_from_xml: Required trigger element missing in %s" % file
			return False

		# Validate trigger types
		if new_trigger['type'] not in ('exact', 'substring', 'regex', 'startswith', 'endswith'):
			print "load_triggers_from_xml: Invalid trigger type in %s" % file
			return False


		# Figure out enabled
		try:
			if new_trigger['enabled'] is False:
				pass
		except KeyError:
			new_trigger['enabled'] = False

		functions = []

		if len(new_trigger['affliction']) > 0:
			for affliction in new_trigger['affliction']:
				functions.append('affliction ' + affliction)

		if len(new_trigger['symptom']) > 0:
			for symptom in new_trigger['symptom']:
				functions.append('symptom ' + symptom)

		if len(new_trigger['cure']) > 0:
			for cure in new_trigger['cure']:
				functions.append('cure ' + cure)

		if len(new_trigger['bcure']) > 0:
			for bcure in new_trigger['bcure']:
				functions.append('bcure ' + bcure)

		if len(new_trigger['gag']) > 0:
			for gag in new_trigger['gag']:
				functions.append('gag ' + gag)

		if len(new_trigger['send']) > 0:
			for send in new_trigger['send']:
				functions.append('send ' + send)

		if len(new_trigger['enable_group']) > 0:
			for group in new_trigger['enable_group']:
				functions.append('enable_group ' + group)

		if len(new_trigger['disable_group']) > 0:
			for group in new_trigger['disable_group']:
				functions.append('disable_group ' + group)

		if len(new_trigger['enable_trigger']) > 0:
			for trigger in new_trigger['enable_trigger']:
				functions.append('enable_trigger ' + trigger)

		if len(new_trigger['disable_trigger']) > 0:
			for trigger in new_trigger['disable_trigger']:
				functions.append('disable_trigger ' + trigger)

		if len(new_trigger['ondef']) > 0:
			for defence in new_trigger['ondef']:
				functions.append('ondef ' + defence)

		if len(new_trigger['undef']) > 0:
			for defence in new_trigger['undef']:
				functions.append('undef ' + defence)

		if new_trigger['deflist'] is not None:
			functions.append('deflist ' + new_trigger['deflist'])

		if len(new_trigger['function']) > 0:
			for function in new_trigger['function']:
				functions.append(function)

		if len(new_trigger['pfunction']) > 0:
			for function in new_trigger['pfunction']:
				functions.append('pfunction ' + function)

		if len(new_trigger['pdisable_trigger']) > 0:
			for trigger in new_trigger['pdisable_trigger']:
				functions.append('pdisable_trigger ' + trigger)

		if len(functions) == 0:
			print "load_triggers_from_xml: No function defined for '%s' in %s" % new_trigger['name'], file
			#print "load_triggers_from_xml: No function defined for generic trigger in %s" % file

		core.add_trigger(
			new_trigger['name'],
			new_group['name'],
			new_trigger['type'],
			new_trigger['pattern'],
			functions,
			new_trigger['enabled'],
		)

	# Done!
	return new_group['name']