From 0aa372792f4bbb38819f40a02f7dcba764ade35a Mon Sep 17 00:00:00 2001 From: Salvo 'LtWorf' Tomaselli Date: Mon, 3 Aug 2015 15:56:37 +0200 Subject: [PATCH] Session management Adds some functions to store and load an entire session as a binary blob. In this way the user does not need to re-load all the relations from the previous session. --- relational/maintenance.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/relational/maintenance.py b/relational/maintenance.py index 2adfa9d..d4c5137 100644 --- a/relational/maintenance.py +++ b/relational/maintenance.py @@ -21,6 +21,7 @@ import http.client import urllib.parse import os.path +import pickle from relational.relation import relation from relational import parser @@ -67,7 +68,7 @@ class UserInterface (object): ''' def __init__(self): - self.relations = {} + self.session_reset() def load(self, filename, name): '''Loads a relation from file, and gives it a name to @@ -83,6 +84,40 @@ class UserInterface (object): '''Stores a relation to file.''' pass + def session_dump(self, filename=None): + ''' + Dumps the session. + + If a filename is specified, the session is dumped + inside the file, and None is returned. + + If no filename is specified, the session is returned + as bytes. + ''' + if filename: + with open(filename,'w') as f: + pickle.dump(self.relations, f) + return None + return pickle.dumps(self.relations) + + def session_restore(self, session=None, filename=None): + ''' + Restores a session. + + Either from bytes or from a file + ''' + if session: + self.relations = pickle.loads(session) + elif filename: + with open(filename) as f: + self.relations = pickle.load(f) + + def session_reset(self): + ''' + Resets the session to a clean one + ''' + self.relations = {} + def get_relation(self, name): '''Returns the relation corresponding to name.''' return self.relations[name]