PageRenderTime 25ms CodeModel.GetById 11ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/src/xmlconfig.py

http://pyaimt.googlecode.com/
Python | 103 lines | 85 code | 7 blank | 11 comment | 24 complexity | 43f9eb15c4fe7461702ab283a95edf25 MD5 | raw file
  1# Copyright 2005-2006 Daniel Henninger <jadestorm@nc.rr.com>
  2# Licensed for distribution under the GPL version 2, check COPYING for details
  3
  4import utils
  5from twisted.words.xish.domish import Element
  6from debug import LogEvent, INFO, WARN, ERROR
  7import config
  8import sys
  9if type(True) != bool: from bool import bool
 10
 11def invalidError(text):
 12	print text
 13	print "Exiting..."
 14	sys.exit(1)
 15
 16def importFile(conffile):
 17	#if conffile[0] != "/":
 18	#	conffile = "../"+conffile
 19
 20	try:
 21		document = utils.parseFile(conffile)
 22	except Exception, e:
 23		invalidError("Error parsing configuration file: " + str(e))
 24
 25	for child in document.elements():
 26		tag = child.name
 27		cdata = child.__str__()
 28		children = [x for x in child.elements()]
 29		if not hasattr(config, tag):
 30			print "Ignoring unrecognized configuration option %r" % tag
 31		elif type(getattr(config, tag)) == dict:
 32			# For options like <settings><username>blar</username><password>foo</password></settings>
 33			try:
 34				if not cdata.isspace():
 35					invalidError("Tag %r in your configuration file should be a dictionary (ie, must have subtags)." % (tag))
 36				myDict = getattr(config, tag)
 37				for child in children:
 38					n = child.name
 39					s = child.__str__()
 40					myDict[n] = s
 41					LogEvent(INFO, msg="Adding %r=%r to config dictionary %r" % (n, s, tag), skipargs=True)
 42			except AttributeError:
 43				print "Ignoring configuration option %r" % tag
 44		elif type(getattr(config, tag)) == list:
 45			# For options like <admins><jid>user1@host.com</jid><jid>user2@host.com</jid></admins>
 46			try:
 47				if not cdata.isspace():
 48					invalidError("Tag %r in your configuration file should be a list (ie, must have subtags)." % (tag))
 49				myList = getattr(config, tag)
 50				for child in children:
 51					s = child.__str__()
 52					LogEvent(INFO, msg="Adding %r to config list %r" % (s, tag), skipargs=True)
 53					myList.append(s)
 54			except AttributeError:
 55				print "Ignoring configuration option %r" % tag
 56		elif type(getattr(config, tag)) == str:
 57			# For config options like <ip>127.0.0.1</ip>
 58			try:
 59				if not cdata:
 60					invalidError("Tag %r in your configuration file should be a string (ie, must have cdata)." % (tag))
 61				LogEvent(INFO, msg="Setting config option %r = %r" % (tag, cdata), skipargs=True)
 62				setattr(config, tag, cdata)
 63			except AttributeError:
 64				print "Ignoring configuration option %r" % tag
 65		elif type(getattr(config, tag)) == int:
 66			# For config options like <port>5347</port>
 67			try:
 68				if not cdata:
 69					invalidError("Tag %r in your configuration file should be an integer (ie, must have numeric cdata)." % (tag))
 70				LogEvent(INFO, msg="Setting config option %r = %r" % (tag, cdata), skipargs=True)
 71				try:
 72					setattr(config, tag, int(cdata))
 73				except:
 74					# Isn't an integer apparantly.
 75					invalidError("Tag %r in your configuration file should be an integer (ie, must have numeric cdata)." % (tag))
 76			except AttributeError:
 77				print "Ignoring configuration option %r" % tag
 78		elif type(getattr(config, tag)) == bool:
 79			# For config options like <crossChat/>
 80			try:
 81				if cdata:
 82					invalidError("Tag %r in your configuration file should be a boolean (ie, no cdata)." % (tag))
 83				LogEvent(INFO, msg="Enabling config option %r" % (tag), skipargs=True)
 84				setattr(config, tag, True)
 85			except AttributeError:
 86				print "Ignoring configuration option %r" % tag
 87		elif isinstance(getattr(config, tag), config.DeprecatedVariable):
 88			# For deprecated options, we will display a warning
 89			getattr(config, tag)()
 90		else:
 91			print "Ignoring unrecognized configuration option %r [unrecognized type %s]" % (tag, type(getattr(config, tag)))
 92
 93def importOptions(options):
 94	for o in options:
 95		LogEvent(INFO, msg="Setting config option %r = %r" % (o, options[o]), skipargs=True)
 96		setattr(config, o, options[o])
 97
 98def Import(file = None, options = None):
 99	LogEvent(INFO, msg="Created configuration entity", skipargs=True)
100	if file != None:
101		importFile(file)
102	if options != None:
103		importOptions(options)