confman.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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):
  10. if conf is not None:
  11. try:
  12. self.conf_file = open(os.path.expanduser(conf))
  13. except IOError:
  14. raise ConfError("could not open conf file '"+os.path.expanduser(conf)+"'")
  15. if conf is None:
  16. if 'HOME' in os.environ:
  17. try:
  18. self.conf_path = os.environ['HOME'] + '/.pybotrc'
  19. self.conf_file = open(self.conf_path)
  20. except IOError:
  21. raise ConfError("could not open conf file '"+self.conf_path+"'")
  22. else: # lines of with os.environ.has_key
  23. try:
  24. self.conf_file = open('.pybotrc')
  25. except IOError:
  26. self.conf_path = os.environ['HOME'] + '/.pybotrc'
  27. try:
  28. #self.conf_path = os.environ['HOME'] + conf
  29. self.conf_file = open(self.conf_path)
  30. except IOError:
  31. raise ConfError("could not open conf file '"+self.conf_path+"'")
  32. self.parsed = json.load(self.conf_file)
  33. def getDBType(self):
  34. try:
  35. db_type = self.parsed["__pybot_conf"]["db_type"]
  36. except:
  37. return "mysql"
  38. return db_type
  39. def getOwner(self, net):
  40. return self.parsed[net]["owner"]
  41. def getTimeout(self, net):
  42. try:
  43. return int(self.parsed[net]["timeout"])
  44. except:
  45. return 120
  46. def getIRCPass(self, net):
  47. return self.parsed[net]["ircpass"]
  48. def getDBPass(self, net):
  49. return self.parsed[net]["dbpass"]
  50. def getDBName(self, net):
  51. return self.parsed[net]["dbname"]
  52. def getDBUsername(self, net):
  53. return self.parsed[net]["dbusername"]
  54. def getNumChannels(self, net):
  55. return len(self.parsed[net]["channels"])
  56. def getNick(self, net):
  57. return self.parsed[net]["nick"]
  58. def getChannels(self, net):
  59. return self.parsed[net]["channels"]
  60. def getNetworks(self):
  61. l = list()
  62. for n in list(self.parsed.keys()):
  63. if n != "__pybot_conf":
  64. l.append(n)
  65. return l
  66. def getNumNets(self):
  67. i = 0
  68. for n in list(self.parsed.keys()):
  69. if n != "__pybot_conf":
  70. i += 1
  71. return i
  72. # deprecated
  73. def getNetwork(self):
  74. pass
  75. def getPort(self,net):
  76. return self.parsed[net]["port"]