diff options
author | René 'Necoro' Neumann <necoro@necoro.net> | 2008-09-02 13:01:17 +0200 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.net> | 2008-09-02 13:01:17 +0200 |
commit | afa1de13f0576ace6dcbb0176490fd20922950cd (patch) | |
tree | 056a5fd646f53dfa83f2fe231ec0943747b15ffc /portato/session.py | |
parent | 02d96210d9102f0cdec95b4e0f595cbd8fdd1e10 (diff) | |
download | portato-afa1de13f0576ace6dcbb0176490fd20922950cd.tar.gz portato-afa1de13f0576ace6dcbb0176490fd20922950cd.tar.bz2 portato-afa1de13f0576ace6dcbb0176490fd20922950cd.zip |
Switch from tabs to 4 spaces
Diffstat (limited to '')
-rw-r--r-- | portato/session.py | 202 |
1 files changed, 101 insertions, 101 deletions
diff --git a/portato/session.py b/portato/session.py index 5d1a640..9f08ff7 100644 --- a/portato/session.py +++ b/portato/session.py @@ -19,104 +19,104 @@ from .constants import SESSION_DIR from .helper import debug, info 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. - """ - - # the current session format version - VERSION = 1 - - 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)) - info(_("Loading session from '%s'.") % self._cfg.file) - try: - self._cfg.parse() - except IOError, e: - if e.errno == 2: pass - else: raise - - # add version check - self.add_handler(([("version", "session")], self.check_version, lambda: self.VERSION)) - - def add_handler (self, (options, load_fn, save_fn), default = None): - """ - 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, default)) - - def load (self): - """ - Loads and applies all values of the session. - """ - for options, lfn, sfn, default 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) - continue - - if default: - debug("Loading %s with defaults %s.", options, default) - lfn(*default) - - def save (self): - """ - Saves all options into the file. - """ - - for options, lfn, sfn, default in self._handlers: - vals = sfn() - - # map into list 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.set(option, str(value), section) - - self._cfg.write() - - def set (self, key, value, section): - try: - self._cfg.add(key, value, section, with_blankline = False) - except SectionNotFoundException: - self._cfg.add_section(section) - self._cfg.add(key, value, section, with_blankline = False) - - def get (self, key, section): - try: - return self._cfg.get(key, section) - except KeyError: - return None - - def get_boolean (self, key, section): - try: - return self._cfg.get_boolean(key, section) - except KeyError: - return None - - def check_version (self, vers): - pass # do nothing atm + """ + 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. + """ + + # the current session format version + VERSION = 1 + + 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)) + info(_("Loading session from '%s'.") % self._cfg.file) + try: + self._cfg.parse() + except IOError, e: + if e.errno == 2: pass + else: raise + + # add version check + self.add_handler(([("version", "session")], self.check_version, lambda: self.VERSION)) + + def add_handler (self, (options, load_fn, save_fn), default = None): + """ + 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, default)) + + def load (self): + """ + Loads and applies all values of the session. + """ + for options, lfn, sfn, default 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) + continue + + if default: + debug("Loading %s with defaults %s.", options, default) + lfn(*default) + + def save (self): + """ + Saves all options into the file. + """ + + for options, lfn, sfn, default in self._handlers: + vals = sfn() + + # map into list 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.set(option, str(value), section) + + self._cfg.write() + + def set (self, key, value, section): + try: + self._cfg.add(key, value, section, with_blankline = False) + except SectionNotFoundException: + self._cfg.add_section(section) + self._cfg.add(key, value, section, with_blankline = False) + + def get (self, key, section): + try: + return self._cfg.get(key, section) + except KeyError: + return None + + def get_boolean (self, key, section): + try: + return self._cfg.get_boolean(key, section) + except KeyError: + return None + + def check_version (self, vers): + pass # do nothing atm |