/src/xmlconfig.py

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