summaryrefslogtreecommitdiff
path: root/portato/session.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--portato/session.py134
1 files changed, 61 insertions, 73 deletions
diff --git a/portato/session.py b/portato/session.py
index 2017544..aadf6c4 100644
--- a/portato/session.py
+++ b/portato/session.py
@@ -3,7 +3,7 @@
# File: portato/session.py
# This file is part of the Portato-Project, a graphical portage-frontend.
#
-# Copyright (C) 2006-2009 René 'Necoro' Neumann
+# Copyright (C) 2006-2010 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.
@@ -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):
@@ -26,9 +28,6 @@ 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
@@ -44,38 +43,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 +83,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): (unicode(y).upper(), unicode(x).lower())
+ options = map(lambda x: (self._name, unicode(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 +103,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:
@@ -124,97 +118,91 @@ class Session (object):
for options, lfn, sfn, default in self._handlers:
vals = sfn()
- # map into list if necessairy
+ # map into list if necessary
if not hasattr(vals, "__iter__"):
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 = unicode(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 = unicode(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
+
+ def get_bool (self, key, section = None):
+ if section is None: section = self._name
+ section = unicode(section).upper()
try:
- return self._cfg.get_boolean(key, section)
- except KeyError:
+ return self._cfg.getboolean(section, key)
+ except NoSuchThing, ValueError:
return None
+
+ def remove (self, key, section = None):
+ if section is None: section = self._name
+ section = unicode(section).upper()
- def remove (self, key, section = ""):
- if not section: 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()
+ section = unicode(section).upper()
+ self._cfg.remove_section(section)
- sline = self._cfg.sections[section]
+ def rename (self, old, new, section = None):
+ if section is None: section = self._name
+ section = unicode(section).upper()
- 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 = ""):
- 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 = unicode(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."""
def __init__ (self, session, section):
- self._section = section.upper()
+ self._section = unicode(section).upper()
self._session = session
def __getitem__ (self, name):
@@ -231,7 +219,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)