confman.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import os
  2. import json
  3. from conferror import ConfError
  4. class ConfManager:
  5. """
  6. Singleton class. Opens and parses a JSON-formatted conf file from (generally) the running user's home folder. Looks for .pybotrc.
  7. This allows each thread to know only its own network name, and always get back the information specified for that network from the confman.
  8. """
  9. def __init__(self,conf=None, string=False):
  10. if string:
  11. self.parsed = json.loads(conf)
  12. else:
  13. if conf is not None:
  14. try:
  15. self.conf_file = open(os.path.expanduser(conf))
  16. except IOError:
  17. raise ConfError("could not open conf file '"+os.path.expanduser(conf)+"'")
  18. if conf is None:
  19. if os.environ.has_key('HOME'):
  20. try:
  21. self.conf_path = os.environ['HOME'] + '/.pybotrc'
  22. self.conf_file = open(self.conf_path)
  23. except IOError:
  24. raise ConfError("could not open conf file '"+self.conf_path+"'")
  25. else: # lines of with os.environ.has_key
  26. try:
  27. self.conf_file = open('.pybotrc')
  28. except IOError:
  29. self.conf_path = os.environ['HOME'] + '/.pybotrc'
  30. try:
  31. #self.conf_path = os.environ['HOME'] + conf
  32. self.conf_file = open(self.conf_path)
  33. except IOError:
  34. raise ConfError("could not open conf file '"+self.conf_path+"'")
  35. self.parsed = json.load(self.conf_file)
  36. def getDBType(self):
  37. try:
  38. db_type = self.parsed["__pybot_conf"]["db_type"]
  39. except:
  40. return "mysql"
  41. return db_type
  42. def getOwner(self, net):
  43. return self.parsed[net]["owner"]
  44. def getTimeout(self, net):
  45. try:
  46. return int(self.parsed[net]["timeout"])
  47. except:
  48. return 120
  49. def getIRCPass(self, net):
  50. return self.parsed[net]["ircpass"]
  51. def getDBPass(self, net):
  52. return self.parsed[net]["dbpass"]
  53. def getDBName(self, net):
  54. return self.parsed[net]["dbname"]
  55. def getDBUsername(self, net):
  56. return self.parsed[net]["dbusername"]
  57. def getNumChannels(self, net):
  58. return len(self.parsed[net]["channels"])
  59. def getNick(self, net):
  60. return self.parsed[net]["nick"]
  61. def getChannels(self, net):
  62. return self.parsed[net]["channels"]
  63. def getNetworks(self):
  64. l = list()
  65. for n in self.parsed.iterkeys():
  66. if n != "__pybot_conf":
  67. l.append(n)
  68. return l
  69. def getNumNets(self):
  70. i = 0
  71. for n in self.parsed.iterkeys():
  72. if n != "__pybot_conf":
  73. i += 1
  74. return i
  75. # deprecated
  76. def getNetwork(self):
  77. pass
  78. def getPort(self,net):
  79. return self.parsed[net]["port"]