summaryrefslogtreecommitdiff
path: root/portato/session.py
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.net>2009-01-27 22:01:27 +0100
committerRené 'Necoro' Neumann <necoro@necoro.net>2009-01-27 22:01:27 +0100
commit29297f7e706d8635d582c257aba63d7c3230efdb (patch)
tree97f0510981a39b0947ad4321c45b94011098adbd /portato/session.py
parent6224da7a5810ef8a9309bc0fd919759e832edcf6 (diff)
parent581f7e6b508c43250c4191ad740dd1b31a4ca9af (diff)
downloadportato-29297f7e706d8635d582c257aba63d7c3230efdb.tar.gz
portato-29297f7e706d8635d582c257aba63d7c3230efdb.tar.bz2
portato-29297f7e706d8635d582c257aba63d7c3230efdb.zip
Merged enhanced session handling
Diffstat (limited to 'portato/session.py')
-rw-r--r--portato/session.py54
1 files changed, 51 insertions, 3 deletions
diff --git a/portato/session.py b/portato/session.py
index 440d4bf..cd4737e 100644
--- a/portato/session.py
+++ b/portato/session.py
@@ -42,6 +42,7 @@ class Session (object):
self._cfg = None
self._handlers = []
+ self._name = name if name else "MAIN"
if not (os.path.exists(SESSION_DIR) and os.path.isdir(SESSION_DIR)):
os.mkdir(SESSION_DIR)
@@ -81,6 +82,8 @@ class Session (object):
- 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
"""
+
+ options = map(lambda x: (x, self._name) if not hasattr(x, "__iter__") else x, options)
self._handlers.append((options, load_fn, save_fn, default))
def load (self, defaults_only = False):
@@ -124,24 +127,69 @@ class Session (object):
self._cfg.write()
- def set (self, key, value, section):
+ def set (self, key, value, section = ""):
+ if not section: section = self._name
+
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):
+ def get (self, key, section = ""):
+ if not section: section = self._name
+
try:
return self._cfg.get(key, section)
except KeyError:
return None
- def get_boolean (self, key, section):
+ 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
+
+ section = section.upper()
+ key = key.lower()
+
+ val = self._cfg._access(key, section)
+ del self._cfg.cache[val.line]
+
+ self._cfg.write()
+
+ def remove_section (self, section):
+ section = section.upper()
+
+ 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 = ""):
+ if not section: section = self._name
+
+ val = self.get(old, section)
+ self.remove(old, section)
+ self._cfg.add(new, val, section, with_blankline = False)
+
+ 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()
+
def check_version (self, vers):
pass # do nothing atm