Advertisement
agangofkittens

Untitled

Feb 8th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.66 KB | None | 0 0
  1. Traceback (most recent call last):
  2.   File "start.py", line 42, in <module>
  3.     bot = Bot(config, rcon, inter, log)
  4.   File "/home/main/UrTBot/bot/bot.py", line 29, in __init__
  5.     self.cmd_prefix = config.prefix
  6.   File "/home/main/UrTBot/bot/config.py", line 54, in __getattr__
  7.     return self.__dict__[attr]
  8. KeyError: 'prefix'
  9.  
  10. _________________________________
  11. "settings":{
  12.         "prefix": "!",
  13.         "trackplayers": True
  14. }
  15. _________________________________
  16.  
  17.         self.config = config
  18.         self.rcon = rcon
  19.         self.inter = inter
  20.         self.log = log
  21.         self.db = DatabaseManager(self.config, self.log)
  22.         self.api = API(self, self.log)
  23.  
  24.         self.players = {}
  25.  
  26.         self.alive = False
  27.         self.cmd_prefix = config.prefix
  28.  
  29.         self.g_gametype = None
  30.         self.mapname = None
  31.  
  32. _________________________________
  33.  
  34. import json, os
  35.  
  36. class ConfigFile(object):
  37.     def __init__(self, name='config', path=['.'], default={}):
  38.         self.configfile = name.replace('.cfg', '')
  39.         path.append(self.configfile+'.cfg')
  40.         self.configpath = os.path.join(*path)
  41.         self.default = default
  42.         self.config = self.load(self.default)
  43.  
  44.         self.check()
  45.         self.save()
  46.  
  47.     def load(self, default):
  48.         try:
  49.             with open(self.configpath, 'r') as f:
  50.                 return json.loads(''.join(f.readlines()))
  51.         except IOError:
  52.             print 'Creating config file!'
  53.             return default
  54.         except:
  55.             raise Exception('Invalid config file! Please check your JSON formatting!')
  56.  
  57.     def save(self):
  58.         s = json.dumps(self.config, sort_keys=True, indent=4)
  59.         with open(self.configpath, 'w') as f:
  60.             f.write(s)
  61.  
  62.     def check(self):
  63.         def checkDict(a, b, rmv=False):
  64.             mark = []
  65.             for key in a:
  66.                 if isinstance(key, dict): continue
  67.                 if key not in b.keys():
  68.                     if rmv:
  69.                         print 'Removing key %s' % key
  70.                         mark.append(key)
  71.                     else:
  72.                         print 'Adding key %s' % key
  73.                         b[key] = a[key]
  74.                 if key in b and isinstance(b[key], dict):
  75.                     checkDict(a[key], b[key], rmv)
  76.             for i in mark:
  77.                 del a[i]
  78.         checkDict(self.default, self.config)
  79.         checkDict(self.config, self.default, rmv=True)
  80.  
  81.     def __getitem__(self, attr):
  82.         return self.config[attr]
  83.  
  84.     def __getattr__(self, attr):
  85.         if attr in self.config.keys():
  86.             return self.config[attr]
  87.         return self.__dict__[attr]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement