]> code.delx.au - pymsnt/blob - src/xmlconfig.py
Google Talk fixes
[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 from tlib import xmlw
5
6 import sys, os
7
8 import config
9
10
11
12 def invalidError(text):
13 print text
14 print "Exiting..."
15 sys.exit(1)
16
17
18 def importFile(configFile):
19 # Check the file exists
20 if not os.path.isfile(configFile):
21 print "Configuration file not found. You need to create a config.xml file in the PyMSNt directory."
22 sys.exit(1)
23
24 # Get ourself a DOM
25 try:
26 root = xmlw.parseFile(configFile)
27 except Exception, e:
28 invalidError("Error parsing configuration file: " + str(e))
29
30 # Store all the options in config
31 for el in root.elements():
32 try:
33 tag = el.name
34 cdata = str(el)
35 children = [x for x in el.elements()]
36 if children:
37 # For options like <admins><jid>user1@host.com</jid><jid>user2@host.com</jid></admins>
38 if type(getattr(config, tag)) != list:
39 invalidError("Tag %s in your configuration file should be a list (ie, must have subtags)." % (tag))
40 myList = getattr(config, tag)
41 for child in children:
42 s = child.__str__()
43 myList.append(s)
44 elif cdata:
45 # For config options like <ip>127.0.0.1</ip>
46 if type(getattr(config, tag)) != str:
47 invalidError("Tag %s in your configuration file should not be a string (ie, no cdata)." % (tag))
48 setattr(config, tag, cdata)
49 else:
50 # For config options like <sessionGreeting/>
51 t = type(getattr(config, tag))
52 if not (t == bool or t == int):
53 invalidError("Tag %s in your configuration file should not be a boolean (ie, must have cdata or subtags)." % (tag))
54 setattr(config, tag, True)
55 except AttributeError:
56 print "Tag %s in your configuration file is not a defined tag. Ignoring!" % (tag)
57
58
59 def importOptions(options):
60 for o in options:
61 if hasattr(config, o):
62 setattr(config, o, options[0])
63 else:
64 print "Option %s is not a defined option. Ignoring!" % (o)
65
66 def reloadConfig(file=None, options=None):
67 if file:
68 importFile(file)
69 if options:
70 importOptions(options)
71