[or-cvs] r23868: {arm} Tidying up the configuration code a bit. (in arm/trunk/src: interface util)

Damian Johnson atagar1 at gmail.com
Sun Nov 28 04:42:30 UTC 2010


Author: atagar
Date: 2010-11-28 04:42:30 +0000 (Sun, 28 Nov 2010)
New Revision: 23868

Modified:
   arm/trunk/src/interface/configPanel.py
   arm/trunk/src/interface/controller.py
   arm/trunk/src/interface/torrcPanel.py
   arm/trunk/src/util/torConfig.py
Log:
Tidying up the configuration code a bit.



Modified: arm/trunk/src/interface/configPanel.py
===================================================================
--- arm/trunk/src/interface/configPanel.py	2010-11-28 03:17:11 UTC (rev 23867)
+++ arm/trunk/src/interface/configPanel.py	2010-11-28 04:42:30 UTC (rev 23868)
@@ -14,6 +14,9 @@
                   "features.config.state.colWidth.option": 25,
                   "features.config.state.colWidth.value": 10}
 
+# TODO: The arm use cases are incomplete since they currently can't be
+# modified, have their descriptions fetched, or even get a complete listing
+# of what's available.
 TOR_STATE, ARM_STATE = range(1, 3) # state to be presented
 
 # mappings of option categories to the color for their entries
@@ -125,23 +128,14 @@
     self.scroller = uiTools.Scroller(True)
     self.valsLock = threading.RLock()
     
-    # TODO: this will need to be able to listen for SETCONF events (arg!)
-    
     if self.configType == TOR_STATE:
       conn = torTools.getConn()
+      customOptions = torConfig.getCustomOptions()
+      configOptionLines = conn.getInfo("config/names", "").strip().split("\n")
       
-      # gets options that differ from their default
-      setOptions = set()
-      configTextQuery = conn.getInfo("config-text", "").strip().split("\n")
-      for entry in configTextQuery: setOptions.add(entry[:entry.find(" ")])
-      
-      # for all recognized tor config options, provide their current value
-      configOptionQuery = conn.getInfo("config/names", "").strip().split("\n")
-      
-      for lineNum in range(len(configOptionQuery)):
+      for line in configOptionLines:
         # lines are of the form "<option> <type>", like:
         # UseEntryGuards Boolean
-        line = configOptionQuery[lineNum]
         confOption, confType = line.strip().split(" ", 1)
         
         # skips private and virtual entries if not set to show them
@@ -151,15 +145,14 @@
           continue
         
         manEntry = torConfig.getConfigDescription(confOption)
-        self.confContents.append(ConfigEntry(confOption, confType, not confOption in setOptions, manEntry))
+        self.confContents.append(ConfigEntry(confOption, confType, not confOption in customOptions, manEntry))
       
-      
       self.setSortOrder() # initial sorting of the contents
     elif self.configType == ARM_STATE:
       # loaded via the conf utility
       armConf = conf.getConfig("arm")
       for key in armConf.getKeys():
-        self.confContents.append(ConfigEntry("", key, ", ".join(armConf.getValue(key, [], True)), "", "", True))
+        pass # TODO: implement
   
   def getSelection(self):
     """
@@ -171,8 +164,11 @@
   def setSortOrder(self, ordering = None):
     """
     Sets the configuration attributes we're sorting by and resorts the
-    contents. If the ordering isn't defined then this resorts based on the
-    last set ordering.
+    contents.
+    
+    Arguments:
+      ordering - new ordering, if undefined then this resorts with the last
+                 set ordering
     """
     
     self.valsLock.acquire()
@@ -180,13 +176,6 @@
     self.confContents.sort(key=lambda i: (i.getAttr(self.sortOrdering)))
     self.valsLock.release()
   
-  def getSortOrder(self):
-    """
-    Provides the current configuration attributes we're sorting by.
-    """
-    
-    return self.sortOrdering
-  
   def handleKey(self, key):
     self.valsLock.acquire()
     if uiTools.isScrollKey(key):
@@ -212,13 +201,13 @@
       # no detail panel
       detailPanelHeight = 0
       scrollLoc = self.scroller.getScrollLoc(self.confContents, height - 1)
-      cursorSelection = self.scroller.getCursorSelection(self.confContents)
+      cursorSelection = self.getSelection()
     else:
       # Shrink detail panel if there isn't sufficient room for the whole
       # thing. The extra line is for the bottom border.
       detailPanelHeight = min(height - 1, detailPanelHeight + 1)
       scrollLoc = self.scroller.getScrollLoc(self.confContents, height - 1 - detailPanelHeight)
-      cursorSelection = self.scroller.getCursorSelection(self.confContents)
+      cursorSelection = self.getSelection()
       
       self._drawSelectionPanel(cursorSelection, width, detailPanelHeight, titleLabel)
     
@@ -228,38 +217,25 @@
       scrollOffset = 3
       self.addScrollBar(scrollLoc, scrollLoc + height - detailPanelHeight - 1, len(self.confContents), 1 + detailPanelHeight)
     
-    # determines the width for the columns
-    optionColWidth, valueColWidth = 0, 0
+    optionWidth = self._config["features.config.state.colWidth.option"]
+    valueWidth = self._config["features.config.state.colWidth.value"]
+    descriptionWidth = max(0, width - scrollOffset - optionWidth - valueWidth - 2)
     
-    # constructs a mapping of entries to their current values
-    # TODO: just skip dynamic widths entirely?
-    entryToValues = {}
-    for entry in self.confContents:
-      entryToValues[entry] = entry.get(FIELD_VALUE)
-      #optionColWidth = max(optionColWidth, len(entry.get(FIELD_OPTION)))
-      #valueColWidth = max(valueColWidth, len(entryToValues[entry]))
-    
-    #optionColWidth = min(self._config["features.config.state.colWidth.option"], optionColWidth)
-    #valueColWidth = min(self._config["features.config.state.colWidth.value"], valueColWidth)
-    optionColWidth = self._config["features.config.state.colWidth.option"]
-    valueColWidth = self._config["features.config.state.colWidth.value"]
-    descriptionColWidth = max(0, width - scrollOffset - optionColWidth - valueColWidth - 2)
-    
     for lineNum in range(scrollLoc, len(self.confContents)):
       entry = self.confContents[lineNum]
       drawLine = lineNum + detailPanelHeight + 1 - scrollLoc
       
-      # TODO: need to cut off description at the first newline
-      optionLabel = uiTools.cropStr(entry.get(FIELD_OPTION), optionColWidth)
-      valueLabel = uiTools.cropStr(entryToValues[entry], valueColWidth)
-      descriptionLabel = uiTools.cropStr(entry.get(FIELD_DESCRIPTION), descriptionColWidth, None)
+      optionLabel = uiTools.cropStr(entry.get(FIELD_OPTION), optionWidth)
+      valueLabel = uiTools.cropStr(entry.get(FIELD_VALUE), valueWidth)
       
+      # ends description at the first newline
+      descriptionLabel = uiTools.cropStr(entry.get(FIELD_DESCRIPTION).split("\n")[0], descriptionWidth, None)
+      
       lineFormat = curses.A_NORMAL if entry.get(FIELD_IS_DEFAULT) else curses.A_BOLD
       if entry.get(FIELD_CATEGORY): lineFormat |= uiTools.getColor(CATEGORY_COLOR[entry.get(FIELD_CATEGORY)])
-      #lineFormat = uiTools.getColor("green") if entry.isDefault else curses.A_BOLD | uiTools.getColor("yellow")
       if entry == cursorSelection: lineFormat |= curses.A_STANDOUT
       
-      lineTextLayout = "%%-%is %%-%is %%-%is" % (optionColWidth, valueColWidth, descriptionColWidth)
+      lineTextLayout = "%%-%is %%-%is %%-%is" % (optionWidth, valueWidth, descriptionWidth)
       lineText = lineTextLayout % (optionLabel, valueLabel, descriptionLabel)
       self.addstr(drawLine, scrollOffset, lineText, lineFormat)
       

Modified: arm/trunk/src/interface/controller.py
===================================================================
--- arm/trunk/src/interface/controller.py	2010-11-28 03:17:11 UTC (rev 23867)
+++ arm/trunk/src/interface/controller.py	2010-11-28 04:42:30 UTC (rev 23868)
@@ -1671,11 +1671,9 @@
             conn.setOption(configOption, newConfigValue)
             
             # resets the isDefault flag
-            setOptions = set()
-            configTextQuery = conn.getInfo("config-text", "").strip().split("\n")
-            for entry in configTextQuery: setOptions.add(entry[:entry.find(" ")])
+            customOptions = torConfig.getCustomOptions()
+            selection.fields[configPanel.FIELD_IS_DEFAULT] = not configOption in customOptions
             
-            selection.fields[configPanel.FIELD_IS_DEFAULT] = not configOption in setOptions
             panels["config"].redraw(True)
           except Exception, exc:
             errorMsg = "%s (press any key)" % exc

Modified: arm/trunk/src/interface/torrcPanel.py
===================================================================
--- arm/trunk/src/interface/torrcPanel.py	2010-11-28 03:17:11 UTC (rev 23867)
+++ arm/trunk/src/interface/torrcPanel.py	2010-11-28 04:42:30 UTC (rev 23868)
@@ -11,7 +11,9 @@
 DEFAULT_CONFIG = {"features.config.file.showScrollbars": True,
                   "features.config.file.maxLinesPerEntry": 8}
 
-TORRC, ARMRC = range(1, 3) # configuration file types that can  be displayed
+# TODO: The armrc use case is incomplete. There should be equivilant reloading
+# and validation capabilities to the torrc.
+TORRC, ARMRC = range(1, 3) # configuration file types that can be displayed
 
 class TorrcPanel(panel.Panel):
   """
@@ -87,8 +89,6 @@
       
       loadedTorrc.getLock().release()
     else:
-      # TODO: The armrc use case is incomplete. There should be equivilant
-      # reloading and validation capabilities to the torrc.
       loadedArmrc = conf.getConfig("arm")
       confLocation = loadedArmrc.path
       renderedContents = list(loadedArmrc.rawContents)
@@ -113,7 +113,7 @@
       locationLabel = " (%s)" % confLocation if confLocation else ""
       self.addstr(0, 0, "%s Configuration File%s:" % (sourceLabel, locationLabel), curses.A_STANDOUT)
     
-    isMultiline = False # set if we're in the middle of a multiline torrc entry
+    isMultiline = False # true if we're in the middle of a multiline torrc entry
     for lineNumber in range(0, len(renderedContents)):
       lineText = renderedContents[lineNumber]
       lineText = lineText.rstrip() # remove ending whitespace
@@ -218,7 +218,4 @@
         self.redraw(True)
     
     self.valsLock.release()
-  
-  def redraw(self, forceRedraw=False, block=False):
-    panel.Panel.redraw(self, forceRedraw, block)
 

Modified: arm/trunk/src/util/torConfig.py
===================================================================
--- arm/trunk/src/util/torConfig.py	2010-11-28 03:17:11 UTC (rev 23867)
+++ arm/trunk/src/util/torConfig.py	2010-11-28 04:42:30 UTC (rev 23868)
@@ -3,7 +3,6 @@
 """
 
 import os
-import curses
 import threading
 
 from util import log, sysTools, torTools, uiTools
@@ -326,6 +325,16 @@
   
   return tuple(MULTILINE_PARAM)
 
+def getCustomOptions():
+  """
+  Provides the set of torrc parameters that differ from their defaults.
+  """
+  
+  customOptions, conn = set(), torTools.getConn()
+  configTextQuery = conn.getInfo("config-text", "").strip().split("\n")
+  for entry in configTextQuery: customOptions.add(entry[:entry.find(" ")])
+  return customOptions
+
 def validate(contents = None):
   """
   Performs validation on the given torrc contents, providing back a listing of
@@ -337,13 +346,9 @@
   """
   
   conn = torTools.getConn()
+  customOptions = getCustomOptions()
   issuesFound, seenOptions = [], []
   
-  # used later to check if either any options match their defaults or custom
-  # options aren't in the torrc
-  configTextQuery = conn.getInfo("config-text", "").strip().split("\n")
-  customOptions = [entry[:entry.find(" ")] for entry in configTextQuery]
-  
   # Strips comments and collapses multiline multi-line entries, for more
   # information see:
   # https://trac.torproject.org/projects/tor/ticket/1929
@@ -430,10 +435,7 @@
         
         issuesFound.append((lineNumber, VAL_MISMATCH, ", ".join(displayValues)))
   
-  # checks if any options that differ from their defaults aren't in the torrc
-  configTextQuery = conn.getInfo("config-text", "").strip().split("\n")
-  configOptions = [entry[:entry.find(" ")] for entry in configTextQuery]
-  
+  # checks if any custom options are missing from the torrc
   for option in customOptions:
     if not option in seenOptions:
       issuesFound.append((None, VAL_MISSING, option))



More information about the tor-commits mailing list