[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[or-cvs] r23757: {arm} Moving logic for feting CSV values into config util, with ad (in arm/trunk/src: interface util)
Author: atagar
Date: 2010-11-05 15:43:39 +0000 (Fri, 05 Nov 2010)
New Revision: 23757
Modified:
arm/trunk/src/interface/configStatePanel.py
arm/trunk/src/util/conf.py
Log:
Moving logic for feting CSV values into config util, with additional error handling and testing.
Modified: arm/trunk/src/interface/configStatePanel.py
===================================================================
--- arm/trunk/src/interface/configStatePanel.py 2010-11-05 04:18:27 UTC (rev 23756)
+++ arm/trunk/src/interface/configStatePanel.py 2010-11-05 15:43:39 UTC (rev 23757)
@@ -6,13 +6,12 @@
import curses
import threading
-from util import conf, log, panel, torTools, torConfig, uiTools
+from util import conf, panel, torTools, torConfig, uiTools
DEFAULT_CONFIG = {"features.config.showPrivateOptions": False,
"features.config.showVirtualOptions": False,
"features.config.state.colWidth.option": 25,
- "features.config.state.colWidth.value": 15,
- "log.configEntryTypeError": log.NOTICE}
+ "features.config.state.colWidth.value": 15}
TOR_STATE, ARM_STATE = range(1, 3) # state to be presented
@@ -108,28 +107,7 @@
"features.config.state.colWidth.option": 5,
"features.config.state.colWidth.value": 5})
- # overrides the initial sort orderting if set
- confSortOrder = config.get("features.config.order")
- if confSortOrder:
- # validates the input, setting the errorMsg if there's a problem
- confSortOrderComp, errorMsg = confSortOrder.split(","), None
- defaultOrderStr = ", ".join([str(i) for i in self.sortOrdering])
- baseErrorMsg = "config entry 'features.config.order' is expected to %%s, defaulting to '%s'" % defaultOrderStr
-
- if len(confSortOrderComp) != 3:
- # checks that we got three comma separated values
- errorMsg = baseErrorMsg % "be three comma separated values"
- else:
- # checks that values are numeric and in the right range
- for val in confSortOrderComp:
- val = val.strip()
- if not val.isdigit() or int(val) < 0 or int(val) > 6:
- errorMsg = baseErrorMsg % "only have numeric entries ranging 0-6"
- break
-
- if errorMsg: log.log(self._config["log.configEntryTypeError"], errorMsg)
- else:
- self.sortOrdering = [int(val.strip()) for val in confSortOrderComp]
+ self.sortOrdering = config.getIntCSV("features.config.order", self.sortOrdering, 3, 0, 6)
self.configType = configType
self.confContents = []
Modified: arm/trunk/src/util/conf.py
===================================================================
--- arm/trunk/src/util/conf.py 2010-11-05 04:18:27 UTC (rev 23756)
+++ arm/trunk/src/util/conf.py 2010-11-05 15:43:39 UTC (rev 23757)
@@ -14,7 +14,6 @@
If a key's defined multiple times then the last instance of it is used.
"""
-import os
import threading
from util import log
@@ -115,7 +114,8 @@
if val.lower() in ("none", "debug", "info", "notice", "warn", "err"):
val = log.strToRunlevel(val)
else:
- msg = "config entry '%s' is expected to be a runlevel, defaulting to '%s'" % (key, callDefault)
+ msg = "config entry '%s' is expected to be a runlevel" % key
+ if default != None: msg += ", defaulting to '%s'" % callDefault
log.log(CONFIG["log.configEntryTypeError"], msg)
val = default
elif isinstance(default, bool):
@@ -152,6 +152,77 @@
return val
+ def getStrCSV(self, key, default = None, count = None):
+ """
+ Fetches the given key as a comma separated value. This provides back a list
+ with the stripped values.
+
+ Arguments:
+ key - config setting to be fetched
+ default - value provided if no such key exists or doesn't match the count
+ count - if set, then a TypeError is logged (and default returned) if
+ the number of elements doesn't match the count
+ """
+
+ confValue = self.getValue(key)
+ if confValue == None: return default
+ else:
+ confComp = [entry.strip() for entry in confValue.split(",")]
+
+ # check if the count doesn't match
+ if count != None and len(confComp) != count:
+ msg = "config entry '%s' is expected to be %i comma separated values" % (key, count)
+ if default != None and (isinstance(default, list) or isinstance(default, tuple)):
+ defaultStr = ", ".join([str(i) for i in default])
+ msg += ", defaulting to '%s'" % defaultStr
+
+ log.log(CONFIG["log.configEntryTypeError"], msg)
+ return default
+
+ return confComp
+
+ def getIntCSV(self, key, default = None, count = None, minValue = None, maxValue = None):
+ """
+ Fetches the given comma separated value, logging a TypeError (and returning
+ the default) if the values arne't ints or aren't constrained to the given
+ bounds.
+
+ Arguments:
+ key - config setting to be fetched
+ default - value provided if no such key exists, doesn't match the count,
+ values aren't all integers, or doesn't match the bounds
+ count - checks that the number of values matches this if set
+ minValue - checks that all values are over this if set
+ maxValue - checks that all values are less than this if set
+ """
+
+ confComp = self.getStrCSV(key, default, count)
+ if confComp == default: return default
+
+ # validates the input, setting the errorMsg if there's a problem
+ errorMsg = None
+ baseErrorMsg = "config entry '%s' is expected to %%s" % key
+ if default != None and (isinstance(default, list) or isinstance(default, tuple)):
+ defaultStr = ", ".join([str(i) for i in default])
+ baseErrorMsg += ", defaulting to '%s'" % defaultStr
+
+ for val in confComp:
+ if not val.isdigit():
+ errorMsg = baseErrorMsg % "only have integer values"
+ break
+ else:
+ if minValue != None and int(val) < minValue:
+ errorMsg = baseErrorMsg % "only have values over %i" % minValue
+ break
+ elif maxValue != None and int(val) > maxValue:
+ errorMsg = baseErrorMsg % "only have values less than %i" % maxValue
+ break
+
+ if errorMsg:
+ log.log(CONFIG["log.configEntryTypeError"], errorMsg)
+ return default
+ else: return [int(val) for val in confComp]
+
def update(self, confMappings, limits = {}):
"""
Revises a set of key/value mappings to reflect the current configuration.