]> code.delx.au - pymsnt/blob - src/xmlconfig.py
Reverted a change that broke challenges.
[pymsnt] / src / xmlconfig.py
1 # Copyright 2004-2005 James Bunton <james@delx.cjb.net>
2 # Licensed for distribution under the GPL version 2, check COPYING for details
3
4
5 import sys
6 import os
7
8 import utils
9 import config
10
11
12
13 def invalidError(text):
14 print text
15 print "Exiting..."
16 sys.exit(1)
17
18
19 def reloadConfig():
20 # Find out where the config file is
21 configFile = config.configFile
22 if len(sys.argv) == 2:
23 configFile = sys.argv[1]
24
25 # Check the file exists
26 if not os.path.isfile(configFile):
27 print "Configuration file not found. You need to create a config.xml file in the PyMSNt directory."
28 sys.exit(1)
29
30 # Get ourself a DOM
31 try:
32 root = utils.parseFile(configFile)
33 except Exception, e:
34 invalidError("Error parsing configuration file: " + str(e))
35
36 # Store all the options in config
37 for el in root.elements():
38 try:
39 tag = el.name
40 cdata = str(el)
41 children = [x for x in el.elements()]
42 if children:
43 # For options like <admins><jid>user1@host.com</jid><jid>user2@host.com</jid></admins>
44 if type(getattr(config, tag)) != list:
45 invalidError("Tag %s in your configuration file should be a list (ie, must have subtags)." % (tag))
46 myList = getattr(config, tag)
47 for child in children:
48 s = child.__str__()
49 myList.append(s)
50 elif cdata:
51 # For config options like <ip>127.0.0.1</ip>
52 if type(getattr(config, tag)) != str:
53 invalidError("Tag %s in your configuration file should not be a string (ie, no cdata)." % (tag))
54 setattr(config, tag, cdata)
55 else:
56 # For config options like <sessionGreeting/>
57 t = type(getattr(config, tag))
58 if not (t == bool or t == int):
59 invalidError("Tag %s in your configuration file should not be a boolean (ie, must have cdata or subtags)." % (tag))
60 setattr(config, tag, True)
61 except AttributeError:
62 print "Tag %s in your configuration file is not a defined tag. Ignoring!" % (tag)
63
64