Browse Source

update for showing of list contents

git-svn-id: svn://bind10.isc.org/svn/bind10/branches/trac384@3962 e5f2f494-b856-4b98-b285-d166d9295462
Jelte Jansen 14 years ago
parent
commit
ff385d569e

+ 20 - 3
src/bin/bindctl/bindcmd.py

@@ -552,13 +552,30 @@ class BindCmdInterpreter(Cmd):
                     return
 
             if cmd.command == "show":
-                values = self.config_data.get_value_maps(identifier)
+                # check if we have the 'all' argument
+                show_all = False
+                if 'argument' in cmd.params:
+                    if cmd.params['argument'] == 'all':
+                        show_all = True
+                    elif 'identifier' not in cmd.params:
+                        # no 'all', no identifier, assume this is the
+                        #identifier
+                        identifier += cmd.params['argument']
+                    else:
+                        print("Error: unknown argument " + cmd.params['argument'] + ", or multiple identifiers given")
+                        return
+                values = self.config_data.get_value_maps(identifier, show_all)
                 for value_map in values:
                     line = value_map['name']
-                    if value_map['type'] in [ 'module', 'map', 'list' ]:
+                    if value_map['type'] in [ 'module', 'map' ]:
+                        line += "/"
+                    elif len(value_map) > 1 and value_map['type'] == 'list' \
+                         and (value_map['value'] != []):
+                        # do not print content of non-empty lists if
+                        # we have more data to show
                         line += "/"
                     else:
-                        line += ":\t" + json.dumps(value_map['value'])
+                        line += "\t" + json.dumps(value_map['value'])
                     line += "\t" + value_map['type']
                     line += "\t"
                     if value_map['default']:

+ 2 - 0
src/bin/bindctl/bindctl-source.py.in

@@ -37,6 +37,8 @@ def prepare_config_commands(tool):
     '''Prepare fixed commands for local configuration editing'''
     module = ModuleInfo(name = CONFIG_MODULE_NAME, desc = "Configuration commands")
     cmd = CommandInfo(name = "show", desc = "Show configuration")
+    param = ParamInfo(name = "argument", type = "string", optional=True)
+    cmd.add_param(param)
     param = ParamInfo(name = "identifier", type = "string", optional=True)
     cmd.add_param(param)
     module.add_command(cmd)

+ 49 - 50
src/lib/python/isc/config/config_data.py

@@ -416,57 +416,50 @@ class MultiConfigData:
                 return value, self.DEFAULT
         return None, self.NONE
 
-    def _append_value_item(self, result, spec_part, identifier):
+    def _append_value_item(self, result, spec_part, identifier, all, first = False):
+        # Look at the spec; it is a list of items, or a map containing 'item_name' etc
         if type(spec_part) == list:
-            # list of items to show
-            for item in spec_part:
-                value, status = self.get_value("/" + identifier\
-                                      + "/" + item['item_name'])
-                entry = _create_value_map_entry(identifier + "/" + item['item_name'],
-                                                item['item_type'],
-                                                value, status)
-                result.append(entry)
+            for spec_part_element in spec_part:
+                spec_part_element_name = spec_part_element['item_name']
+                self._append_value_item(result, spec_part_element, identifier + "/" + spec_part_element_name, all)
         elif type(spec_part) == dict:
-            # Two 'special cases' for easier viewing;
-            # If the item is a map, show the first-level contents
-            #
-            # If the item is a list, show all elements (with index in the name).
-            #
-            item = spec_part
-            if item['item_type'] == 'list':
-                li_spec = item['list_item_spec']
-                value, status =  self.get_value("/" + identifier)
-                if type(value) == list:
-                    i = 0;
-                    for list_value in value:
-                        result_part2 = _create_value_map_entry(
-                                           "%s[%d]" % (identifier, i),
-                                           li_spec['item_type'],
-                                           list_value)
-                        result.append(result_part2)
-                        i = i + 1
-                elif value is not None:
-                    self._append_value_item(result, li_spec, identifier)
-            elif item['item_type'] == 'map':
-                map_name = item['item_name'] + '/'
-                for map_item in item['map_item_spec']:
-                    value, status =  self.get_value('/' + identifier + '/' + map_item['item_name'])
-                    entry = _create_value_map_entry(
-                                identifier + "/" + map_item['item_name'],
-                                map_item['item_type'],
-                                value,
-                                status)
-                    result.append(entry)
+            # depending on item type, and the value of argument 'all'
+            # we need to either add an item, or recursively go on
+            # In the case of a list that is empty, we do need to show that
+            item_name = spec_part['item_name']
+            item_type = spec_part['item_type']
+            if item_type == "list" and (all or first):
+                spec_part_list = spec_part['list_item_spec']
+                list_value, status = self.get_value(identifier)
+                if list_value is None:
+                    print("Error: %s not found" % identifier)
+                    return
+                if type(list_value) != list:
+                    # the identifier specified a single element
+                    self._append_value_item(result, spec_part_list, identifier, all)
+                else:
+                    list_len = len(list_value)
+                    if len(list_value) == 0 and (all or first):
+                        entry = _create_value_map_entry(identifier,
+                                                        item_type,
+                                                        [], status)
+                        result.append(entry)
+                    else:
+                        for i in range(len(list_value)):
+                            self._append_value_item(result, spec_part_list, "%s[%d]" % (identifier, i), all)
+            elif item_type == "map":
+                spec_part_map = spec_part['map_item_spec']
+                self._append_value_item(result, spec_part_map, identifier, all)
             else:
-                value, status = self.get_value("/" + identifier)
-                if value is not None:
-                    entry = _create_value_map_entry(
-                                identifier,
-                                item['item_type'],
-                                value, status)
-                    result.append(entry)
+                value, status = self.get_value(identifier)
+                entry = _create_value_map_entry(identifier,
+                                                item_type,
+                                                value, status)
+                result.append(entry)
+        return
+
 
-    def get_value_maps(self, identifier = None):
+    def get_value_maps(self, identifier = None, all = False):
         """Returns a list of dicts, containing the following values:
            name: name of the entry (string)
            type: string containing the type of the value (or 'module')
@@ -480,8 +473,14 @@ class MultiConfigData:
         if not identifier:
             # No identifier, so we need the list of current modules
             for module in self._specifications.keys():
-                entry = _create_value_map_entry(module, 'module', None)
-                result.append(entry)
+                if all:
+                    spec = self.get_module_spec(module)
+                    if spec:
+                        spec_part = spec.get_config_spec()
+                        self._append_value_item(result, spec_part, module, all, True)
+                else:
+                    entry = _create_value_map_entry(module, 'module', None)
+                    result.append(entry)
         else:
             if identifier[0] == '/':
                 identifier = identifier[1:]
@@ -489,7 +488,7 @@ class MultiConfigData:
             spec = self.get_module_spec(module)
             if spec:
                 spec_part = find_spec_part(spec.get_config_spec(), id)
-                self._append_value_item(result, spec_part, identifier)
+                self._append_value_item(result, spec_part, identifier, all, True)
         return result
 
     def set_value(self, identifier, value):

+ 21 - 21
src/lib/python/isc/config/tests/config_data_test.py

@@ -423,32 +423,32 @@ class TestMultiConfigData(unittest.TestCase):
         self.mcd._set_current_config({ "Spec2": { "item1": 2 } })
         self.mcd.set_value("Spec2/item3", False)
         maps = self.mcd.get_value_maps("/Spec2")
-        self.assertEqual([{'default': False, 'type': 'integer', 'name': 'item1', 'value': 2, 'modified': False},
-                          {'default': True, 'type': 'real', 'name': 'item2', 'value': 1.1, 'modified': False},
-                          {'default': False, 'type': 'boolean', 'name': 'item3', 'value': False, 'modified': True},
-                          {'default': True, 'type': 'string', 'name': 'item4', 'value': 'test', 'modified': False},
-                          {'default': True, 'type': 'list', 'name': 'item5', 'value': ['a', 'b'], 'modified': False},
-                          {'default': True, 'type': 'map', 'name': 'item6', 'value': {}, 'modified': False}], maps)
+        self.assertEqual([{'default': False, 'type': 'integer', 'name': 'Spec2/item1', 'value': 2, 'modified': False},
+                          {'default': True, 'type': 'real', 'name': 'Spec2/item2', 'value': 1.1, 'modified': False},
+                          {'default': False, 'type': 'boolean', 'name': 'Spec2/item3', 'value': False, 'modified': True},
+                          {'default': True, 'type': 'string', 'name': 'Spec2/item4', 'value': 'test', 'modified': False},
+                          {'default': True, 'type': 'list', 'name': 'Spec2/item5', 'value': ['a', 'b'], 'modified': False},
+                          {'default': True, 'type': 'map', 'name': 'Spec2/item6', 'value': {}, 'modified': False}], maps)
         maps = self.mcd.get_value_maps("Spec2")
-        self.assertEqual([{'default': False, 'type': 'integer', 'name': 'item1', 'value': 2, 'modified': False},
-                          {'default': True, 'type': 'real', 'name': 'item2', 'value': 1.1, 'modified': False},
-                          {'default': False, 'type': 'boolean', 'name': 'item3', 'value': False, 'modified': True},
-                          {'default': True, 'type': 'string', 'name': 'item4', 'value': 'test', 'modified': False},
-                          {'default': True, 'type': 'list', 'name': 'item5', 'value': ['a', 'b'], 'modified': False},
-                          {'default': True, 'type': 'map', 'name': 'item6', 'value': {}, 'modified': False}], maps)
+        self.assertEqual([{'default': False, 'type': 'integer', 'name': 'Spec2/item1', 'value': 2, 'modified': False},
+                          {'default': True, 'type': 'real', 'name': 'Spec2/item2', 'value': 1.1, 'modified': False},
+                          {'default': False, 'type': 'boolean', 'name': 'Spec2/item3', 'value': False, 'modified': True},
+                          {'default': True, 'type': 'string', 'name': 'Spec2/item4', 'value': 'test', 'modified': False},
+                          {'default': True, 'type': 'list', 'name': 'Spec2/item5', 'value': ['a', 'b'], 'modified': False},
+                          {'default': True, 'type': 'map', 'name': 'Spec2/item6', 'value': {}, 'modified': False}], maps)
         maps = self.mcd.get_value_maps("/Spec2/item5")
-        self.assertEqual([{'default': False, 'type': 'string', 'name': 'list_element', 'value': 'a', 'modified': False},
-                          {'default': False, 'type': 'string', 'name': 'list_element', 'value': 'b', 'modified': False}], maps)
+        self.assertEqual([{'default': False, 'type': 'string', 'name': 'Spec2/item5[0]', 'value': 'a', 'modified': False},
+                          {'default': False, 'type': 'string', 'name': 'Spec2/item5[1]', 'value': 'b', 'modified': False}], maps)
         maps = self.mcd.get_value_maps("/Spec2/item5[0]")
-        self.assertEqual([{'default': True, 'modified': False, 'name': 'list_element', 'type': 'string', 'value': 'a'}], maps)
+        self.assertEqual([{'default': True, 'modified': False, 'name': 'Spec2/item5[0]', 'type': 'string', 'value': 'a'}], maps)
         maps = self.mcd.get_value_maps("/Spec2/item1")
-        self.assertEqual([{'default': False, 'type': 'integer', 'name': 'item1', 'value': 2, 'modified': False}], maps)
+        self.assertEqual([{'default': False, 'type': 'integer', 'name': 'Spec2/item1', 'value': 2, 'modified': False}], maps)
         maps = self.mcd.get_value_maps("/Spec2/item2")
-        self.assertEqual([{'default': True, 'type': 'real', 'name': 'item2', 'value': 1.1, 'modified': False}], maps)
+        self.assertEqual([{'default': True, 'type': 'real', 'name': 'Spec2/item2', 'value': 1.1, 'modified': False}], maps)
         maps = self.mcd.get_value_maps("/Spec2/item3")
-        self.assertEqual([{'default': False, 'type': 'boolean', 'name': 'item3', 'value': False, 'modified': True}], maps)
+        self.assertEqual([{'default': False, 'type': 'boolean', 'name': 'Spec2/item3', 'value': False, 'modified': True}], maps)
         maps = self.mcd.get_value_maps("/Spec2/item4")
-        self.assertEqual([{'default': True, 'type': 'string', 'name': 'item4', 'value': 'test', 'modified': False}], maps)
+        self.assertEqual([{'default': True, 'type': 'string', 'name': 'Spec2/item4', 'value': 'test', 'modified': False}], maps)
 
         module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec24.spec")
         self.mcd.set_specification(module_spec)
@@ -462,12 +462,12 @@ class TestMultiConfigData(unittest.TestCase):
         self.mcd.set_specification(module_spec)
         expected = [{'default': True,
                      'modified': False,
-                     'name': 'value9/v91',
+                     'name': 'Spec22/value9/v91',
                      'type': 'string',
                      'value': 'def'},
                     {'default': True,
                      'modified': False,
-                     'name': 'value9/v92',
+                     'name': 'Spec22/value9/v92',
                      'type': 'map',
                      'value': {}
                     }