diff options
author | René 'Necoro' Neumann <necoro@necoro.net> | 2010-03-06 16:26:29 +0100 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.net> | 2010-03-14 18:56:10 +0100 |
commit | c0ef9b8f98eaf9fe46b87759d8083a8bd24620fa (patch) | |
tree | 4cc5f635470fc51e71146a797f28eb152b00cfb5 | |
parent | 42c9e86bd9fe4d48f58e4688c0cdcd8f0066afb1 (diff) | |
download | portato-c0ef9b8f98eaf9fe46b87759d8083a8bd24620fa.tar.gz portato-c0ef9b8f98eaf9fe46b87759d8083a8bd24620fa.tar.bz2 portato-c0ef9b8f98eaf9fe46b87759d8083a8bd24620fa.zip |
Rewrote Session as to use the normal ConfigParser and not our own one
(cherry picked from commit 397a31e6deb851de102bca3d7c8d754f40b0656d)
Diffstat (limited to '')
-rw-r--r-- | portato/gui/windows/main.py | 2 | ||||
-rw-r--r-- | portato/session.py | 116 |
2 files changed, 50 insertions, 68 deletions
diff --git a/portato/gui/windows/main.py b/portato/gui/windows/main.py index a06756d..bc40a1f 100644 --- a/portato/gui/windows/main.py +++ b/portato/gui/windows/main.py @@ -1070,7 +1070,7 @@ class MainWindow (Window): queue = plugin.get_plugin_queue() if queue: for p in queue.get_plugins(): - self.session.add_handler(([(p.name.replace(" ","_"), "plugins")], load_plugin(p), save_plugin(p))) + self.session.add_handler(([(p.name.replace(" ","_").replace(":","_"), "plugins")], load_plugin(p), save_plugin(p))) # the other things def load_cfg ((name, cat)): diff --git a/portato/session.py b/portato/session.py index 54761a2..be13275 100644 --- a/portato/session.py +++ b/portato/session.py @@ -15,10 +15,12 @@ from __future__ import absolute_import, with_statement import os from UserDict import DictMixin -from .config_parser import ConfigParser, SectionNotFoundException +from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError from .constants import SESSION_DIR from .helper import debug, info +from .odict import OrderedDict +NoSuchThing = (NoSectionError, NoOptionError) sessionlist = [] class Session (object): @@ -44,38 +46,32 @@ class Session (object): @param register: register in the global sessionlist, which is closed at the end """ - self._cfg = None + self._cfg = SafeConfigParser({}, OrderedDict) self._handlers = [] self._name = name if name else "MAIN" + self._file = os.path.join(SESSION_DIR, file) if not (os.path.exists(SESSION_DIR) and os.path.isdir(SESSION_DIR)): os.mkdir(SESSION_DIR) - file = os.path.join(SESSION_DIR, file) oldfiles = [os.path.join(SESSION_DIR, x) for x in oldfiles] - if not os.path.exists(file): + if not os.path.exists(self._file): for o in oldfiles: if os.path.exists(o): debug("Moving old session file '%s' to '%s'." % (o, file)) - os.rename(o,file) + os.rename(o,self._file) break - self._cfg = ConfigParser(file) + self._cfg.read([self._file]) if name: - i = _("Loading '%s' session from %s.") % (name, self._cfg.file) + i = _("Loading '%s' session from %s.") % (name, self._file) else: - i = _("Loading session from %s.") % self._cfg.file + i = _("Loading session from %s.") % self._file info(i) - try: - self._cfg.parse() - except IOError, e: - if e.errno == 2: pass - else: raise - # register if register: sessionlist.append(self) @@ -90,7 +86,8 @@ class Session (object): - a function returning the number of option return values - getting them out of the program """ - options = map(lambda x: (x, self._name) if not hasattr(x, "__iter__") else x, options) + convert = lambda (x,y): (y.upper(), x.lower()) + options = map(lambda x: (self._name, x.lower()) if not hasattr(x, "__iter__") else convert(x), options) self._handlers.append((options, load_fn, save_fn, default)) def load (self, defaults_only = False): @@ -109,7 +106,7 @@ class Session (object): else: try: loaded = [self._cfg.get(*x) for x in options] - except KeyError: # does not exist -> ignore + except NoSuchThing: # does not exist -> ignore debug("No values for %s.", options) ldefault(options, lfn, default) else: @@ -129,87 +126,72 @@ class Session (object): vals = [vals] debug("Saving %s with values %s", options, vals) - for value, (option, section) in zip(vals, options): + for value, (section, option) in zip(vals, options): self.set(option, str(value), section) - self._cfg.write() + with open(self._file, "w") as f: + self._cfg.write(f) @classmethod def close (cls): for s in sessionlist: if s._name != "MAIN": - info(_("Saving '%s' session to %s.") % (s._name, s._cfg.file)) + info(_("Saving '%s' session to %s.") % (s._name, s._file)) else: - info(_("Saving session to %s.") % s._cfg.file) + info(_("Saving session to %s.") % s._file) s.save() - def set (self, key, value, section = ""): - if not section: section = self._name + def set (self, key, value, section = None): + if section is None: section = self._name + section = section.upper() try: - self._cfg.add(key, value, section, with_blankline = False) - except SectionNotFoundException: + self._cfg.set(section, key, value) + except NoSectionError: self._cfg.add_section(section) - self._cfg.add(key, value, section, with_blankline = False) + self._cfg.set(section, key, value) - def get (self, key, section = ""): - if not section: section = self._name + def get (self, key, section = None): + if section is None: section = self._name + section = section.upper() try: - return self._cfg.get(key, section) - except KeyError: + return self._cfg.get(section, key) + except NoSuchThing: return None - def get_boolean (self, key, section = ""): - if not section: section = self._name - - try: - return self._cfg.get_boolean(key, section) - except KeyError: - return None - - def remove (self, key, section = ""): - if not section: section = self._name - + def remove (self, key, section = None): + if section is None: section = self._name section = section.upper() - key = key.lower() - - val = self._cfg._access(key, section) - del self._cfg.cache[val.line] - self._cfg.write() + self._cfg.remove_option(section, key) def remove_section (self, section): section = section.upper() + self._cfg.remove_section(section) - sline = self._cfg.sections[section] - - try: - mline = max(v.line for v in self._cfg.vars[section].itervalues()) - except ValueError: # nothing in the section - mline = sline - - sline = max(sline - 1, 0) # remove blank line too - but only if there is one ;) - - del self._cfg.cache[sline:mline+1] - self._cfg.write() + def rename (self, old, new, section = None): + if section is None: section = self._name + section = section.upper() - def rename (self, old, new, section = ""): - if not section: section = self._name - val = self.get(old, section) - self.remove(old, section) - self._cfg.add(new, val, section, with_blankline = False) + + if val is not None: + self.remove(old, section) + self.set(new, val, section) def rename_section (self, old, new): - old = old.upper() - line = self._cfg.sections[old] - self._cfg.cache[line] = "[%s]\n" % new.upper() - self._cfg.write() + new = new.upper() + + values = self._cfg.items(old) + self.remove_section(old) + for k,v in values: + self.set(k,v,new) def check_version (self, vers): pass # do nothing atm + class SectionDict (DictMixin): """A class, which maps a specific section of a session to a dictionary.""" @@ -231,7 +213,7 @@ class SectionDict (DictMixin): self._session.remove(name, self._section) def keys (self): - return self._session._cfg.vars[self._section].keys() + return self._session._cfg.options(self._section) def __contains__ (self, name): - return self._session.get(name, self._section) is not None + return self._session._cfg.has_option(self._section, name) |