[tor-commits] [nyx/master] Move field attributes to configuration

atagar at torproject.org atagar at torproject.org
Mon Jan 4 17:43:05 UTC 2016


commit 8ad0e0b87f95555775a0ddb5c0df5f12dde67ba1
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Sep 25 14:13:00 2015 -0700

    Move field attributes to configuration
    
    As with elsewhere moving our color mappings to our configuration to get it out
    of the code.
    
    Also changing the Fields enum to use labels for their values (as we ususally do
    with enumerations). This was probably separate so we could use the shorter name
    in the nyxrc, but those can use the label names.
---
 nyx/config/attributes.cfg |   17 +++++++++++
 nyx/config_panel.py       |   71 +++++++++++----------------------------------
 nyxrc.sample              |   20 ++++++-------
 3 files changed, 44 insertions(+), 64 deletions(-)

diff --git a/nyx/config/attributes.cfg b/nyx/config/attributes.cfg
index e1d1b9e..39faa1e 100644
--- a/nyx/config/attributes.cfg
+++ b/nyx/config/attributes.cfg
@@ -75,3 +75,20 @@ attr.connection.sort_color Fingerprint => cyan
 attr.connection.sort_color Nickname => cyan
 attr.connection.sort_color Country => blue
 
+attr.config.category_color General => green
+attr.config.category_color Client => blue
+attr.config.category_color Relay => yellow
+attr.config.category_color Directory => magenta
+attr.config.category_color Authority => red
+attr.config.category_color Hidden Service => cyan
+attr.config.category_color Testing => white
+attr.config.category_color Unknown => white
+
+attr.config.field_color Category => red
+attr.config.field_color Option Name => blue
+attr.config.field_color Value => cyan
+attr.config.field_color Arg Type => green
+attr.config.field_color Arg Usage => yellow
+attr.config.field_color Summary => green
+attr.config.field_color Description => white
+attr.config.field_color Man Page Entry => blue
diff --git a/nyx/config_panel.py b/nyx/config_panel.py
index 64552f1..3ef4a4e 100644
--- a/nyx/config_panel.py
+++ b/nyx/config_panel.py
@@ -15,45 +15,20 @@ import stem.control
 
 from stem.util import conf, enum, str_tools
 
-# mappings of option categories to the color for their entries
-
-CATEGORY_COLOR = {
-  tor_config.Category.GENERAL: 'green',
-  tor_config.Category.CLIENT: 'blue',
-  tor_config.Category.RELAY: 'yellow',
-  tor_config.Category.DIRECTORY: 'magenta',
-  tor_config.Category.AUTHORITY: 'red',
-  tor_config.Category.HIDDEN_SERVICE: 'cyan',
-  tor_config.Category.TESTING: 'white',
-  tor_config.Category.UNKNOWN: 'white',
-}
-
 # attributes of a ConfigEntry
 
 Field = enum.Enum(
-  'CATEGORY',
-  'OPTION',
-  'VALUE',
-  'TYPE',
-  'ARG_USAGE',
-  'SUMMARY',
-  'DESCRIPTION',
-  'MAN_ENTRY',
-  'IS_DEFAULT',
+  ('CATEGORY', 'Category'),
+  ('OPTION', 'Option Name'),
+  ('VALUE', 'Value'),
+  ('TYPE', 'Arg Type'),
+  ('ARG_USAGE', 'Arg Usage'),
+  ('SUMMARY', 'Summary'),
+  ('DESCRIPTION', 'Description'),
+  ('MAN_ENTRY', 'Man Page Entry'),
+  ('IS_DEFAULT', 'Is Default'),
 )
 
-FIELD_ATTR = {
-  Field.CATEGORY: ('Category', 'red'),
-  Field.OPTION: ('Option Name', 'blue'),
-  Field.VALUE: ('Value', 'cyan'),
-  Field.TYPE: ('Arg Type', 'green'),
-  Field.ARG_USAGE: ('Arg Usage', 'yellow'),
-  Field.SUMMARY: ('Summary', 'green'),
-  Field.DESCRIPTION: ('Description', 'white'),
-  Field.MAN_ENTRY: ('Man Page Entry', 'blue'),
-  Field.IS_DEFAULT: ('Is Default', 'magenta'),
-}
-
 
 def conf_handler(key, value):
   if key == 'features.config.selectionDetails.height':
@@ -67,6 +42,8 @@ def conf_handler(key, value):
 
 
 CONFIG = conf.config_dict('nyx', {
+  'attr.config.category_color': {},
+  'attr.config.field_color': {},
   'features.config.order': [Field.MAN_ENTRY, Field.OPTION, Field.IS_DEFAULT],
   'features.config.selectionDetails.height': 6,
   'features.config.prepopulateEditValues': True,
@@ -77,17 +54,6 @@ CONFIG = conf.config_dict('nyx', {
 }, conf_handler)
 
 
-def get_field_from_label(field_label):
-  """
-  Converts field labels back to their enumeration, raising a ValueError if it
-  doesn't exist.
-  """
-
-  for entry_enum in FIELD_ATTR:
-    if field_label == FIELD_ATTR[entry_enum][0]:
-      return entry_enum
-
-
 class ConfigEntry():
   """
   Configuration option in the panel.
@@ -331,15 +297,12 @@ class ConfigPanel(panel.Panel):
     # set ordering for config options
 
     title_label = 'Config Option Ordering:'
-    options = [FIELD_ATTR[field][0] for field in Field]
-    old_selection = [FIELD_ATTR[field][0] for field in CONFIG['features.config.order']]
-    option_colors = dict([FIELD_ATTR[field] for field in Field])
-    results = nyx.popups.show_sort_dialog(title_label, options, old_selection, option_colors)
+    old_selection = CONFIG['features.config.order']
+    option_colors = dict([(field, CONFIG['attr.config.field_color'].get(field, 'white')) for field in Field])
+    results = nyx.popups.show_sort_dialog(title_label, Field, old_selection, option_colors)
 
     if results:
-      # converts labels back to enums
-      result_enums = [get_field_from_label(label) for label in results]
-      self.set_sort_order(result_enums)
+      self.set_sort_order(results)
 
   def handle_key(self, key):
     with self._vals_lock:
@@ -613,7 +576,7 @@ class ConfigPanel(panel.Panel):
         line_format = [curses.A_NORMAL if entry.get(Field.IS_DEFAULT) else curses.A_BOLD]
 
         if entry.get(Field.CATEGORY):
-          line_format += [CATEGORY_COLOR[entry.get(Field.CATEGORY)]]
+          line_format += [CONFIG['attr.config.category_color'].get(entry.get(Field.CATEGORY), 'white')]
 
         if entry == cursor_selection:
           line_format += [curses.A_STANDOUT]
@@ -640,7 +603,7 @@ class ConfigPanel(panel.Panel):
     if is_scrollbar_visible:
       self.addch(detail_panel_height, 1, curses.ACS_TTEE)
 
-    selection_format = (curses.A_BOLD, CATEGORY_COLOR[selection.get(Field.CATEGORY)])
+    selection_format = (curses.A_BOLD, CONFIG['attr.config.category_color'].get(selection.get(Field.CATEGORY), 'white'))
 
     # first entry:
     # <option> (<category> Option)
diff --git a/nyxrc.sample b/nyxrc.sample
index c623de0..666a21e 100644
--- a/nyxrc.sample
+++ b/nyxrc.sample
@@ -88,15 +88,15 @@ features.log.maxRefreshRate 300
 # order
 #   three comma separated configuration attributes, options including:
 #
-#     * CATEGORY
-#     * OPTION
-#     * VALUE
-#     * TYPE
-#     * ARG_USAGE
-#     * SUMMARY
-#     * DESCRIPTION
-#     * MAN_ENTRY
-#     * IS_DEFAULT
+#     * Category
+#     * Option Name
+#     * Value
+#     * Arg Type
+#     * Arg Usage
+#     * Summary
+#     * Description
+#     * Man Page Entry
+#     * Is Default
 #
 # selectionDetails.height
 #   rows of data for the panel showing details on the current selection, this
@@ -117,7 +117,7 @@ features.log.maxRefreshRate 300
 # file.maxLinesPerEntry
 #   max number of lines to display for a single entry in the torrc
 
-features.config.order MAN_ENTRY, OPTION, IS_DEFAULT
+features.config.order Man Page Entry, Option Name, Is Default
 features.config.selectionDetails.height 6
 features.config.prepopulateEditValues true
 features.config.state.colWidth.option 25





More information about the tor-commits mailing list