summaryrefslogtreecommitdiff
path: root/portato
diff options
context:
space:
mode:
authorNecoro <>2007-11-21 14:05:01 +0000
committerNecoro <>2007-11-21 14:05:01 +0000
commitedcd2ceedb958e311fc3ee701cd1c326ab23d8b3 (patch)
treeff14a206df4c95667a9cd47fab36329305cb42df /portato
parent3418b2b5cc63ee2d8648ad6127c9c25d51739385 (diff)
downloadportato-edcd2ceedb958e311fc3ee701cd1c326ab23d8b3.tar.gz
portato-edcd2ceedb958e311fc3ee701cd1c326ab23d8b3.tar.bz2
portato-edcd2ceedb958e311fc3ee701cd1c326ab23d8b3.zip
r568@Devoty: necoro | 2007-11-21 15:04:15 +0100
Yes - we should add session files...
Diffstat (limited to '')
-rw-r--r--portato/session.py89
1 files changed, 89 insertions, 0 deletions
diff --git a/portato/session.py b/portato/session.py
new file mode 100644
index 0000000..2a049d1
--- /dev/null
+++ b/portato/session.py
@@ -0,0 +1,89 @@
+# -*- coding: utf-8 -*-
+#
+# File: portato/session.py
+# This file is part of the Portato-Project, a graphical portage-frontend.
+#
+# Copyright (C) 2007 René 'Necoro' Neumann
+# This is free software. You may redistribute copies of it under the terms of
+# the GNU General Public License version 2.
+# There is NO WARRANTY, to the extent permitted by law.
+#
+# Written by René 'Necoro' Neumann <necoro@necoro.net>
+
+from __future__ import absolute_import, with_statement
+
+import os, os.path
+
+from .config_parser import ConfigParser
+from .constants import SESSION_DIR
+from .helper import debug
+
+class Session (object):
+ """
+ A small class allowing to save certain states of a program.
+ This class works in a quite abstract manner, as it works with handlers, which
+ define what options to use out of the config file and how to apply them to the program.
+
+ Note: This class currently does not work with boolean config options. If you
+ want to define boolean values, use 0 and 1. This is future proof.
+ """
+
+ def __init__ (self, file):
+ """
+ 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.
+ """
+
+ self._cfg = None
+ self._handlers = []
+
+ if not (os.path.exists(SESSION_DIR) and os.path.isdir(SESSION_DIR)):
+ os.mkdir(SESSION_DIR)
+ self._cfg = ConfigParser(os.path.join(SESSION_DIR, file))
+ try:
+ self._cfg.parse()
+ except IOError, e:
+ if e.errno == 2: pass
+ else: raise
+
+ def add_handler (self, (options, load_fn, save_fn)):
+ """
+ Adds a handler to this session. A handler is a three-tuple consisting of:
+ - a list of (key,section) values
+ - a function getting number of option arguments and applying them to the program
+ - a function returning the number of option return values - getting them out of the program
+ """
+ self._handlers.append((options, load_fn, save_fn))
+
+ def load (self):
+ """
+ Loads and applies all values of the session.
+ """
+ for options, lfn, sfn in self._handlers:
+ try:
+ loaded = [self._cfg.get(*x) for x in options]
+ except KeyError: # does not exist -> ignore
+ debug("No values for %s.", options)
+ else:
+ debug("Loading %s with values %s.", options, loaded)
+ lfn(*loaded)
+
+ def save (self):
+ """
+ Saves all options into the file.
+ """
+
+ for options, lfn, sfn in self._handlers:
+ vals = sfn()
+
+ # map into tuple if necessairy
+ if not hasattr(vals, "__iter__"):
+ vals = (vals,)
+ debug("Saving %s with values %s", options, vals)
+
+ for value, (option, section) in zip(vals, options):
+ self._cfg.add_section(section)
+ self._cfg.add(option, str(value), section = section, with_blankline = False)
+
+ self._cfg.write()