def create(pSettingid, pDescription, pType='list', pFilename=None, pFiletype='dict'): #check for pickle problems if pFiletype == 'dict': try: pickle_data = list(map(str, list(range(1, 10)))) pickle_file = open(selfdatatest, 'wb') pickle.dump(pickle_data, pickle_file) pickle_file.flush() pickle_file.close() pickle_file = open(selfdatatest, 'rb') pickle_file.seek(0) pickle_test = pickle.load(pickle_file) pickle_file.close() os.remove(selfdatatest) if pickle_test != pickle_data: pFiletype = 'keyvalues' except: pFiletype = 'keyvalues' else: pFiletype = 'keyvalues' if not pFilename: if pFiletype == 'keyvalues': pFilename = '%s/%s.txt' % (selfdatapath, pSettingid) elif pFiletype == 'dict': pFilename = '%s/%s.pkl' % (selfdatapath, pSettingid) #create new setting if pType == 'list': gSettings[pSettingid] = list.Setting_list(pSettingid, pDescription, pFilename, pFiletype) elif pType == 'toggle': gSettings[pSettingid] = toggle.Setting_toggle(pSettingid, pDescription, pFilename, pFiletype) else: return None return gSettings[pSettingid]
def information(self, listlevel): if listlevel >= 1: es.dbgmsg(0, ' ') es.dbgmsg(0, self.name) if listlevel >= 1: es.dbgmsg(0, ' Description: '+str(self.keyvalues['config']['descr'])) es.dbgmsg(0, ' Re-Send: '+str(self.keyvalues['config']['resend'])) es.dbgmsg(0, ' Soundfile: '+str(self.keyvalues['config']['sound'])) if listlevel >= 2: es.dbgmsg(0, ' Players: '+str(len(list(self.keyvalues['users'].keys())))) es.dbgmsg(0, ' Options: '+str(len(list(self.keyvalues['options'].keys())))) for option in list(self.keyvalues['options'].keys()): es.dbgmsg(0,' ['+str(self.keyvalues['options'][option]['keyname'])+']: '+self.keyvalues['options'][option]['display'])
def isValidOption(self, option, passthrough=False): #checks for valid options if option in self.keyvalues['options']: return option for thisoption in list(self.keyvalues['options'].keys()): if self.keyvalues['options'][thisoption]['keyname'] == option: return thisoption return option if passthrough else False
def cleanTypes(self): resend = self.keyvalues['config']['resend'] if isinstance(resend, str): try: self.keyvalues['config']['resend'] = int(resend) except: self.keyvalues['config']['resend'] = 1 for option in list(self.keyvalues['options'].keys()): if 'state' in self.keyvalues['options'][option]: state = self.keyvalues['options'][option]['state'] if isinstance(state, str): try: self.keyvalues['options'][option]['state'] = int(state) except: self.keyvalues['options'][option]['state'] = 0 if 'globstate' in self.keyvalues['options'][option]: state = self.keyvalues['options'][option]['globstate'] if isinstance(state, str): try: self.keyvalues['options'][option]['globstate'] = int( state) except: self.keyvalues['options'][option]['globstate'] = 0 for steamid in list(self.keyvalues['users'].keys()): if 'time' in self.keyvalues['users'][steamid]: state = self.keyvalues['users'][steamid]['time'] if isinstance(state, str): try: self.keyvalues['users'][steamid]['time'] = int(state) except: self.keyvalues['users'][steamid]['time'] = int( time.time()) if 'data' in self.keyvalues['users'][steamid]: for option in list( self.keyvalues['users'][steamid]['data'].keys()): if 'state' in self.keyvalues['users'][steamid]['data'][ option]: state = self.keyvalues['users'][steamid]['data'][ option]['state'] if isinstance(state, str): try: self.keyvalues['users'][steamid]['data'][ option]['state'] = int(state) except: self.keyvalues['users'][steamid]['data'][ option]['state'] = 0
def deloption(self, key): #delete an option option = self.isValidOption(key, True) if option in self.keyvalues['options']: del self.keyvalues['options'][option] for steamid in list(self.keyvalues['users'].keys()): del self.keyvalues['users'][steamid]['data'][option] self.save() else: raise IndexError('Settinglib: Cannot delete option %s, it does not exists'%key)
def initUser(self, userid): if es.exists('userid',userid): steamid = playerlib.uniqueid(userid, True) self.keyvalues['users'][steamid] = self.createKey(steamid) self.keyvalues['users'][steamid]['time'] = int(time.time()) self.keyvalues['users'][steamid]['data'] = self.createKey('data') for option in list(self.keyvalues['options'].keys()): self.keyvalues['users'][steamid]['data'][option] = self.createKey(option) self.keyvalues['users'][steamid]['data'][option]['state'] = self.keyvalues['options'][option]['state']
def clear(self, timestamp=None): clearlist = [] if timestamp: timediff = int(time.time()) - int(timestamp) for steamid in list(self.keyvalues['users'].keys()): if self.keyvalues['users'][steamid]['time'] < timediff: clearlist.append(steamid) for steamid in clearlist: del self.keyvalues['users'][steamid] else: self.keyvalues['users'] = self.createKey('users') clearlist = [] for steamid in list(self.keyvalues['variables'].keys()): for variable in list(self.keyvalues['variables'][steamid].keys()): clearlist.append((steamid, variable)) for steamid, variable in clearlist: del self.keyvalues['variables'][steamid][variable] for option in list(self.keyvalues['options'].keys()): self.keyvalues['options'][option]['globstate'] = self.keyvalues['options'][option]['state']
def deloption(self, key): #delete an option option = self.isValidOption(key, True) if option in self.keyvalues['options']: del self.keyvalues['options'][option] for steamid in list(self.keyvalues['users'].keys()): del self.keyvalues['users'][steamid]['data'][option] self.save() else: raise IndexError( f'Settinglib: Cannot delete option {key}, it does not exists')
def clear(self, timestamp=None): clearlist = [] if timestamp: timediff = int(time.time()) - int(timestamp) for steamid in list(self.keyvalues['users'].keys()): if self.keyvalues['users'][steamid]['time'] < timediff: clearlist.append(steamid) for steamid in clearlist: del self.keyvalues['users'][steamid] else: self.keyvalues['users'] = self.createKey('users') clearlist = [] for steamid in list(self.keyvalues['variables'].keys()): for variable in list(self.keyvalues['variables'][steamid].keys()): clearlist.append((steamid, variable)) for steamid, variable in clearlist: del self.keyvalues['variables'][steamid][variable] for option in list(self.keyvalues['options'].keys()): self.keyvalues['options'][option]['globstate'] = self.keyvalues[ 'options'][option]['state']
def initUser(self, userid): if es.exists('userid', userid): steamid = playerlib.uniqueid(userid, True) self.keyvalues['users'][steamid] = self.createKey(steamid) self.keyvalues['users'][steamid]['time'] = int(time.time()) self.keyvalues['users'][steamid]['data'] = self.createKey('data') for option in list(self.keyvalues['options'].keys()): self.keyvalues['users'][steamid]['data'][ option] = self.createKey(option) self.keyvalues['users'][steamid]['data'][option][ 'state'] = self.keyvalues['options'][option]['state']
def cleanTypes(self): resend = self.keyvalues['config']['resend'] if isinstance(resend, str): try: self.keyvalues['config']['resend'] = int(resend) except: self.keyvalues['config']['resend'] = 1 for option in list(self.keyvalues['options'].keys()): if 'state' in self.keyvalues['options'][option]: state = self.keyvalues['options'][option]['state'] if isinstance(state, str): try: self.keyvalues['options'][option]['state'] = int(state) except: self.keyvalues['options'][option]['state'] = 0 if 'globstate' in self.keyvalues['options'][option]: state = self.keyvalues['options'][option]['globstate'] if isinstance(state, str): try: self.keyvalues['options'][option]['globstate'] = int(state) except: self.keyvalues['options'][option]['globstate'] = 0 for steamid in list(self.keyvalues['users'].keys()): if 'time' in self.keyvalues['users'][steamid]: state = self.keyvalues['users'][steamid]['time'] if isinstance(state, str): try: self.keyvalues['users'][steamid]['time'] = int(state) except: self.keyvalues['users'][steamid]['time'] = int(time.time()) if 'data' in self.keyvalues['users'][steamid]: for option in list(self.keyvalues['users'][steamid]['data'].keys()): if 'state' in self.keyvalues['users'][steamid]['data'][option]: state = self.keyvalues['users'][steamid]['data'][option]['state'] if isinstance(state, str): try: self.keyvalues['users'][steamid]['data'][option]['state'] = int(state) except: self.keyvalues['users'][steamid]['data'][option]['state'] = 0
def information(self, listlevel): if listlevel >= 1: es.dbgmsg(0, ' ') es.dbgmsg(0, self.name) if listlevel >= 1: es.dbgmsg( 0, ' Description: ' + str(self.keyvalues['config']['descr'])) es.dbgmsg( 0, ' Re-Send: ' + str(self.keyvalues['config']['resend'])) es.dbgmsg( 0, ' Soundfile: ' + str(self.keyvalues['config']['sound'])) if listlevel >= 2: es.dbgmsg( 0, ' Players: ' + str(len(list(self.keyvalues['users'].keys())))) es.dbgmsg( 0, ' Options: ' + str(len(list(self.keyvalues['options'].keys())))) for option in list(self.keyvalues['options'].keys()): es.dbgmsg( 0, ' [' + str(self.keyvalues['options'][option]['keyname']) + ']: ' + self.keyvalues['options'][option]['display'])
def clearoption(self): optionlist = [] for option in list(self.keyvalues['options'].keys()): optionlist.append(option) for option in optionlist: self.deloption(option)