confman.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import os
  2. import json
  3. from conferror import ConfError
  4. class ConfManager:
  5. def __init__(self,conf=None):
  6. if conf is not None:
  7. try:
  8. self.conf_file = open(os.path.expanduser(conf))
  9. except IOError:
  10. raise ConfError("could not open conf file '"+os.path.expanduser(conf)+"'")
  11. if conf is None:
  12. if os.environ.has_key('HOME'):
  13. try:
  14. self.conf_path = os.environ['HOME'] + '/.pybotrc'
  15. self.conf_file = open(self.conf_path)
  16. except IOError:
  17. raise ConfError("could not open conf file '"+self.conf_path+"'")
  18. else: # lines of with os.environ.has_key
  19. try:
  20. self.conf_file = open('.pybotrc')
  21. except IOError:
  22. self.conf_path = os.environ['HOME'] + '/.pybotrc'
  23. try:
  24. #self.conf_path = os.environ['HOME'] + conf
  25. self.conf_file = open(self.conf_path)
  26. except IOError:
  27. raise ConfError("could not open conf file '"+self.conf_path+"'")
  28. self.parsed = json.load(self.conf_file)
  29. def getOwner(self, net):
  30. return self.parsed[net]["owner"]
  31. def getIRCPass(self, net):
  32. return self.parsed[net]["ircpass"]
  33. def getDBPass(self, net):
  34. return self.parsed[net]["dbpass"]
  35. def getDBName(self, net):
  36. return self.parsed[net]["dbname"]
  37. def getDBUsername(self, net):
  38. return self.parsed[net]["dbusername"]
  39. def getNumChannels(self, net):
  40. return len(self.parsed[net]["channels"])
  41. def getNick(self, net):
  42. return self.parsed[net]["nick"]
  43. def getChannels(self, net):
  44. return self.parsed[net]["channels"]
  45. def getNetworks(self):
  46. l = list()
  47. for n in self.parsed.iterkeys():
  48. l.append(n)
  49. return l
  50. def getNumNets(self):
  51. i = 0
  52. for n in self.parsed.iterkeys():
  53. i += 1
  54. return i
  55. # deprecated
  56. def getNetwork(self):
  57. pass
  58. def getPort(self,net):
  59. return self.parsed[net]["port"]