SF.net SVN: fclient: [94] trunk/sandbox/fcp/fcp2_0_config.py
Status: Pre-Alpha
Brought to you by:
jurner
|
From: <ju...@us...> - 2008-01-30 17:16:35
|
Revision: 94
http://fclient.svn.sourceforge.net/fclient/?rev=94&view=rev
Author: jurner
Date: 2008-01-30 09:16:34 -0800 (Wed, 30 Jan 2008)
Log Message:
-----------
refactored and a few fixes
Modified Paths:
--------------
trunk/sandbox/fcp/fcp2_0_config.py
Modified: trunk/sandbox/fcp/fcp2_0_config.py
===================================================================
--- trunk/sandbox/fcp/fcp2_0_config.py 2008-01-30 14:36:38 UTC (rev 93)
+++ trunk/sandbox/fcp/fcp2_0_config.py 2008-01-30 17:16:34 UTC (rev 94)
@@ -6,14 +6,28 @@
#
#****************************************************************************************
class ConfigItem(object):
+ """Config item"""
+
def __init__(self, parent, name):
+ """
+ @param parent: parent item
+ @param name: name of the item
+
+ @ivar parent: parent item
+ @ivar name: name of the item
+ @ivar children: (dict) child items
+ @ivar values: dict of values
+ """
self.parent = parent
self.name = name
self.children = {}
self.values = {}
def key(self):
+ """Returns the key of the config item
+ @return: (str) key
+ """
out = []
parent = self
while parent is not None:
@@ -21,7 +35,7 @@
out.append(parent.name)
parent = parent.parent
out.reverse()
- return '.'.join(out)
+ return ConfigMessageParams.ComponentsSep.join(out)
#****************************************************************************************
@@ -31,20 +45,39 @@
"""Class representing fcp config tree
"""
+ ValueClassCurrent = ConfigMessageParams.ParamClassCurrent
+ ValueClassDefault = ConfigMessageParams.ParamClassDefault
+ ValueClassExpertFlag = ConfigMessageParams.ParamClassExpertFlag
+ ValueClassForceWriteFlag = ConfigMessageParams.ParamClassForceWriteFlag
+ ValueClassShortDescription = ConfigMessageParams.ParamClassShortDescription
+ ValueClassLongDescription = ConfigMessageParams.ParamClassLongDescription
+
+
def __init__(self, configDataMsg=None):
+ """
+ @param parent: parent item
+ @param name: name of the item
+
+ @ivar parent: parent item
+ @ivar name: name of the item
+ @ivar children: (dict) child items
+ """
+
self.parent = None
self.name = None
self.children = {}
- self.values = {} # for consistency
- self.configMessageParams = ConfigMessageParams()
+ self._configMessageParams = ConfigMessageParams()
if configDataMsg is not None:
for key, value in configDataMsg.params.items():
- self[key] = value
+ self.addNewValue(key, value)
-
def __getitem__(self, key):
+ """Returns the item associated to a key
+ @param key: (str) key to retrieve the item for (not including ValueClass prefix)
+ @return: (L{ConfigItem})
+ """
components = key.split('.')
parent = self
while components:
@@ -55,49 +88,69 @@
parent = item
return parent
-
- def __setitem__(self, key, value):
- paramClass, components = self.configMessageParams.splitParamClass(key)
- paramType = self.configMessageParams.get(key, None)
- components = self.configMessageParams.splitAll(components)
- valueName = components.pop()
-
- #TODO: how to handle non existent keys?
+
+ def addNewValue(self, key, value):
+ """Adds a value to an item
+ @param key: (str) key to add the value to (including ParamClass prefix)
+ @param value to associate
+ @note: if the item does not exist, it is created
+ """
+ paramClass, components = self._configMessageParams.splitParamClass(key)
+ paramType = self._configMessageParams.get(key, None)
if paramType is None:
- # log somehow
- return False
-
- # create new config item if necessary
+ raise ValueError('Unknown key: %r' % components)
+
+ # find or create new config item if necessary
+ components = self._configMessageParams.splitAll(components)
parent = self
for component in components:
item = parent.children.get(component, None)
if item is None:
item = ConfigItem(parent, component)
- self.children[component] = item
+ parent.children[component] = item
parent = item
+ parent.values[paramClass] = (value, paramType)
+
+
+
+ def removeParamClass(self, key):
+ """Removes the paramClass component if necessary
+ @param key: (str) key to remove the param class from
+ @return: (str) key
+ @note: if the key does not contain the paramClass it is returned unchanged
+ """
+ head, tail = self._configMessageParams.splitParamClass(key)
+ if head in self._configMessageParams.ParamClasses:
+ return tail
+ return tail
- # assign value
- values = parent.values.get(valueName, None)
- if values is None:
- values = {}
- parent.values[valueName] = values
- values[paramClass] = (value, paramType)
- return True
-
+ def toMessageParams(self):
+ """Dumps the contents of the config to a dict
+ @note: you can use this dict as params for FcpClient.modifyConfig()
+ """
+ out = {}
+ for node in self.walk():
+ if node.name is None:
+ continue
+
+ key = node.key()
+ result = node.values.get(self.ValueClassCurrent, None)
+ if result is not None:
+ out[key] = result[0]
+ return out
+
+
def walk(self):
+ """Walks over the config tree, returning the next L{ConfigItemm} in turn"""
def walker(node):
yield node
for child in node.children.values():
for x in walker(child):
yield x
- return walker(self)
-
+ return walker(self)
- def key(self):
- return None
-
#****************************************************************************************************
#
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|