Preference trees

Preference trees are used to store various preferences.  Each component
can have its own set of preferences that use overlapping names.
This commit is contained in:
Bryan Schumaker 2010-11-14 17:26:52 -05:00
parent edd5d2b39b
commit 5594825502
1 changed files with 53 additions and 2 deletions

View File

@ -1,5 +1,6 @@
# Bryan Schumaker (11/06/2010)
from libsaria import data
from libsaria.path import sep
class Tree(dict):
@ -48,10 +49,19 @@ class Tree(dict):
child.insert(path[1:])
def lookup(self, path):
print self.keys(), path
child = self[path[0]]
child = self.get(path[0], None)
if child == None:
return child
if len(path) > 1:
return child.lookup(path[1:])
return child.keys()
def lookup_child(self, path):
child = self.get(path[0], None)
if child == None:
return child
if len(path) > 1:
return child.lookup_child(path[1:])
return child
@ -180,3 +190,44 @@ class DLValTree(DLTree, ValTree):
if len(path) > 1:
return child.insert(path[1:], vals)
return child
class PrefTree(Tree):
def __init__(self):
Tree.__init__(self)
def init_pref(self, pref, value):
path = pref.split('.')
if self.lookup(path) == None:
self.insert(path + [value])
def set_pref(self, pref, value):
path = pref.split('.')
child = self.lookup_child(path)
if child == None:
self.init_pref(pref, value)
return
del child[child.keys()[0]]
child.insert([value])
self.insert(path + [value])
def get_pref(self, pref):
path = pref.split('.')
val = self.lookup(path)
if val == None:
return None
return val[0]
class PersPrefTree(PrefTree):
def __init__(self, file = None):
PrefTree.__init__(self)
self.file = file
def init_pref(self, pref, value):
PrefTree.init_pref(self, pref, value)
data.save(self.file, ".tree")
def set_pref(self, pref, value):
PrefTree.set_pref(self, pref, value)
data.save(self.file, ".tree")