summaryrefslogtreecommitdiff
path: root/portato/session.py
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.net>2009-01-28 16:04:32 +0100
committerRené 'Necoro' Neumann <necoro@necoro.net>2009-01-28 16:04:32 +0100
commit1f25c87bad1ffcbe7708a3126c9465fba6644fe5 (patch)
tree387fc376121faedf858abff7d57050a0173a7374 /portato/session.py
parent2a5e9163052e607e509da61c674502cad30de65c (diff)
downloadportato-1f25c87bad1ffcbe7708a3126c9465fba6644fe5.tar.gz
portato-1f25c87bad1ffcbe7708a3126c9465fba6644fe5.tar.bz2
portato-1f25c87bad1ffcbe7708a3126c9465fba6644fe5.zip
Add a SectionDict class, which maps a section of a session to a dict.
Diffstat (limited to 'portato/session.py')
-rw-r--r--portato/session.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/portato/session.py b/portato/session.py
index 0bafee2..76e5540 100644
--- a/portato/session.py
+++ b/portato/session.py
@@ -13,6 +13,7 @@
from __future__ import absolute_import, with_statement
import os, os.path
+from UserDict import DictMixin
from .config_parser import ConfigParser, SectionNotFoundException
from .constants import SESSION_DIR
@@ -208,3 +209,29 @@ class Session (object):
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._session = session
+
+ def __getitem__ (self, name):
+ item = self._session.get(name, section = self._section)
+
+ if item is None:
+ raise KeyError, "%s not in section %s" % (name, self._section)
+ return item
+
+ def __setitem__ (self, name, value):
+ self._session.set(name, value, section = self._section)
+
+ def __delitem__ (self, name):
+ self._session.remove(name, self._section)
+
+ def keys (self):
+ return self._session._cfg.vars[self._section].keys()
+
+ def __contains__ (self, name):
+ return self._session.get(name, self._section) is not None