From 2a5e9163052e607e509da61c674502cad30de65c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Wed, 28 Jan 2009 15:40:50 +0100 Subject: Add a global session list --- portato/session.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'portato/session.py') diff --git a/portato/session.py b/portato/session.py index cd4737e..0bafee2 100644 --- a/portato/session.py +++ b/portato/session.py @@ -18,6 +18,8 @@ from .config_parser import ConfigParser, SectionNotFoundException from .constants import SESSION_DIR from .helper import debug, info +sessionlist = [] + class Session (object): """ A small class allowing to save certain states of a program. @@ -31,13 +33,14 @@ class Session (object): # the current session format version VERSION = 1 - def __init__ (self, file, name="", oldfiles = []): + def __init__ (self, file, name="", oldfiles = [], register = True): """ Initialize a session with a certain file inside L{SESSION_DIR}. @param file: the file in L{SESSION_DIR}, where the options will be saved. @param oldfiles: old file names for the same file @param name: short name describing the type of session + @param register: register in the global sessionlist, which is closed at the end """ self._cfg = None @@ -60,9 +63,9 @@ class Session (object): self._cfg = ConfigParser(file) if name: - i = _("Loading '%s' session from '%s'.") % (name, self._cfg.file) + i = _("Loading '%s' session from %s.") % (name, self._cfg.file) else: - i = _("Loading session from '%s'.") % self._cfg.file + i = _("Loading session from %s.") % self._cfg.file info(i) @@ -72,6 +75,9 @@ class Session (object): if e.errno == 2: pass else: raise + # register + if register: sessionlist.append(self) + # add version check self.add_handler(([("version", "session")], self.check_version, lambda: self.VERSION)) @@ -127,6 +133,15 @@ class Session (object): self._cfg.write() + @classmethod + def close (cls): + for s in sessionlist: + if s._name != "MAIN": + info(_("Saving '%s' session to %s.") % (s._name, s._cfg.file)) + else: + info(_("Saving session to %s.") % s._cfg.file) + s.save() + def set (self, key, value, section = ""): if not section: section = self._name -- cgit v1.2.3