changed function names and cleaned code a little bit to reflect better the changes to object percistance

This commit is contained in:
Manuel Cortez 2021-06-25 23:35:33 -05:00
parent b8f822830f
commit 71358ea74d
2 changed files with 25 additions and 36 deletions

View File

@ -651,8 +651,8 @@ class Controller(object):
if sessions.sessions[item].logged == False: continue if sessions.sessions[item].logged == False: continue
log.debug("Disconnecting streams for %s session" % (sessions.sessions[item].session_id,)) log.debug("Disconnecting streams for %s session" % (sessions.sessions[item].session_id,))
sessions.sessions[item].sound.cleaner.cancel() sessions.sessions[item].sound.cleaner.cancel()
log.debug("Shelving database for " + sessions.sessions[item].session_id) log.debug("Saving database for " + sessions.sessions[item].session_id)
sessions.sessions[item].shelve() sessions.sessions[item].save_persistent_data()
if system == "Windows": if system == "Windows":
self.systrayIcon.RemoveIcon() self.systrayIcon.RemoveIcon()
pidpath = os.path.join(os.getenv("temp"), "{}.pid".format(application.name)) pidpath = os.path.join(os.getenv("temp"), "{}.pid".format(application.name))
@ -1625,4 +1625,4 @@ class Controller(object):
def save_data_in_db(self): def save_data_in_db(self):
for i in sessions.sessions: for i in sessions.sessions:
sessions.sessions[i].shelve() sessions.sessions[i].save_persistent_data()

View File

@ -60,7 +60,7 @@ class baseSession(object):
log.debug("Creating config file %s" % (file_,)) log.debug("Creating config file %s" % (file_,))
self.settings = config_utils.load_config(os.path.join(paths.config_path(), file_), os.path.join(paths.app_path(), "Conf.defaults")) self.settings = config_utils.load_config(os.path.join(paths.config_path(), file_), os.path.join(paths.app_path(), "Conf.defaults"))
self.init_sound() self.init_sound()
self.deshelve() self.load_persistent_data()
def init_sound(self): def init_sound(self):
try: self.sound = sound.soundSystem(self.settings["sound"]) try: self.sound = sound.soundSystem(self.settings["sound"])
@ -74,52 +74,41 @@ class baseSession(object):
def authorise(self): def authorise(self):
pass pass
def shelve(self): def save_persistent_data(self):
"""Shelve the database to allow for persistance.""" """ Save the data to a persistant sqlite backed file. ."""
shelfname=os.path.join(paths.config_path(), str(self.session_id), "cache") dbname=os.path.join(paths.config_path(), str(self.session_id), "cache.db")
# persist_size set to 0 means not saving data actually.
if self.settings["general"]["persist_size"] == 0: if self.settings["general"]["persist_size"] == 0:
if os.path.exists(shelfname+".dat"): if os.path.exists(dbname):
os.remove(shelfname+".dat") os.remove(dbname)
return return
try: try:
self.db.commit() self.db.commit()
# if not os.path.exists(shelfname+".dat"):
# output.speak("Generating database, this might take a while.",True)
# shelf=shelve.open(os.path.join(paths.config_path(), shelfname),'c')
# for key, value in list(self.db.items()):
# if type(key) != str and type(key) != str:
# output.speak("Uh oh, while shelving the database, a key of type " + str(type(key)) + " has been found. It will be converted to type str, but this will cause all sorts of problems on deshelve. Please bring this to the attention of the " + application.name + " developers immediately. More information about the error will be written to the error log.",True)
# log.error("Uh oh, " + str(key) + " is of type " + str(type(key)) + "!")
# if type(value) == list and self.settings["general"]["persist_size"] != -1 and len(value) > self.settings["general"]["persist_size"]:
# shelf[key]=value[self.settings["general"]["persist_size"]:]
# else:
# shelf[key]=value
# shelf.close()
except: except:
output.speak("An exception occurred while shelving the " + application.name + " database. It will be deleted and rebuilt automatically. If this error persists, send the error log to the " + application.name + " developers.",True) output.speak(_("An exception occurred while saving the {app} database. It will be deleted and rebuilt automatically. If this error persists, send the error log to the {app} developers.").format(app=application.name),True)
log.exception("Exception while shelving" + shelfname) log.exception("Exception while saving {}".format(dbname))
os.remove(shelfname) os.remove(dbname)
def deshelve(self): def load_persistent_data(self):
"""Import a shelved database.""" """Import data from a database file from user config."""
shelfname=os.path.join(paths.config_path(), str(self.session_id)+"/cache") dbname=os.path.join(paths.config_path(), str(self.session_id), "cache.db")
# If persist_size is set to 0, we should remove the db file as we are no longer going to save anything.
if self.settings["general"]["persist_size"] == 0: if self.settings["general"]["persist_size"] == 0:
if os.path.exists(shelfname+".dat"): if os.path.exists(dbname):
os.remove(shelfname+".dat") os.remove(dbname)
# Let's return from here, as we are not loading anything.
return return
# try to load the db file.
try: try:
self.db=sqlitedict.SqliteDict(os.path.join(paths.config_path(), shelfname), 'c') self.db=sqlitedict.SqliteDict(os.path.join(paths.config_path(), dbname), 'c')
if self.db.get("cursors") == None: if self.db.get("cursors") == None:
cursors = dict(direct_messages=-1) cursors = dict(direct_messages=-1)
self.db["cursors"] = cursors self.db["cursors"] = cursors
# for key,value in list(shelf.items()):
# self.db[key]=value
# shelf.close()
except: except:
output.speak("An exception occurred while deshelving the " + application.name + " database. It will be deleted and rebuilt automatically. If this error persists, send the error log to the " + application.name + " developers.",True) output.speak(_("An exception occurred while loading the {app} database. It will be deleted and rebuilt automatically. If this error persists, send the error log to the {app} developers.").format(app=application.name), True)
log.exception("Exception while deshelving" + shelfname) log.exception("Exception while loading {}".format(dbname))
try: try:
os.remove(shelfname) os.remove(dbname)
except: except:
pass pass