Browse Source

improve asking for and printing lists

git-svn-id: svn://bind10.isc.org/svn/bind10/branches/trac384@3932 e5f2f494-b856-4b98-b285-d166d9295462
Jelte Jansen 14 years ago
parent
commit
7099d4df87
2 changed files with 66 additions and 55 deletions
  1. 5 5
      src/lib/python/isc/cc/data.py
  2. 61 50
      src/lib/python/isc/config/config_data.py

+ 5 - 5
src/lib/python/isc/cc/data.py

@@ -100,8 +100,8 @@ def split_identifier_list_indices(identifier):
 
     i = id_str.find('[')
     if i < 0:
-        if identifier.find(']') >= 0:
-            raise DataTypeError("Bad format in identifier: " + str(identifier))
+        if id_str.find(']') >= 0:
+            raise DataTypeError("Bad format in identifier (] but no [): " + str(identifier))
         return identifier, None
 
     # keep the non-index part of that to replace later
@@ -110,7 +110,7 @@ def split_identifier_list_indices(identifier):
     while i >= 0:
         e = id_str.find(']')
         if e < i + 1:
-            raise DataTypeError("Bad format in identifier: " + str(identifier))
+            raise DataTypeError("Bad format in identifier (] before [): " + str(identifier))
         try:
             indices.append(int(id_str[i+1:e]))
         except ValueError:
@@ -118,9 +118,9 @@ def split_identifier_list_indices(identifier):
         id_str = id_str[e + 1:]
         i = id_str.find('[')
         if i > 0:
-            raise DataTypeError("Bad format in identifier: " + str(identifier))
+            raise DataTypeError("Bad format in identifier ([ within []): " + str(identifier))
     if id.find(']') >= 0 or len(id_str) > 0:
-        raise DataTypeError("Bad format in identifier: " + str(identifier))
+        raise DataTypeError("Bad format in identifier (extra ]): " + str(identifier))
 
     # we replace the final part of the original identifier with
     # the stripped string

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

@@ -121,6 +121,7 @@ def find_spec_part(element, identifier):
         # strip list selector part
         # don't need it for the spec part, so just drop it
         id, list_indices = isc.cc.data.split_identifier_list_indices(id_part)
+        # is this part still needed? (see below)
         if type(cur_el) == dict and 'map_item_spec' in cur_el.keys():
             found = False
             for cur_el_item in cur_el['map_item_spec']:
@@ -129,11 +130,20 @@ def find_spec_part(element, identifier):
                     found = True
             if not found:
                 raise isc.cc.data.DataNotFoundError(id + " in " + str(cur_el))
+        elif type(cur_el) == dict and 'list_item_spec' in cur_el.keys():
+            cur_el = cur_el['list_item_spec']
         elif type(cur_el) == list:
             found = False
             for cur_el_item in cur_el:
                 if cur_el_item['item_name'] == id:
                     cur_el = cur_el_item
+                    # if we need to go further, we may need to 'skip' a step here
+                    # but not if we're done
+                    if id_parts[-1] != id_part and type(cur_el) == dict:
+                        if "map_item_spec" in cur_el:
+                            cur_el = cur_el["map_item_spec"]
+                        elif "list_item_spec" in cur_el:
+                            cur_el = cur_el["list_item_spec"]
                     found = True
             if not found:
                 raise isc.cc.data.DataNotFoundError(id + " in " + str(cur_el))
@@ -406,6 +416,56 @@ class MultiConfigData:
                 return value, self.DEFAULT
         return None, self.NONE
 
+    def _append_value_item(self, result, spec_part, identifier):
+        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)
+        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)
+            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)
+
     def get_value_maps(self, identifier = None):
         """Returns a list of dicts, containing the following values:
            name: name of the entry (string)
@@ -429,56 +489,7 @@ class MultiConfigData:
             spec = self.get_module_spec(module)
             if spec:
                 spec_part = find_spec_part(spec.get_config_spec(), id)
-                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(item['item_name'],
-                                                        item['item_type'],
-                                                        value, status)
-                        result.append(entry)
-                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:
-                            for list_value in value:
-                                result_part2 = _create_value_map_entry(
-                                                   li_spec['item_name'],
-                                                   li_spec['item_type'],
-                                                   list_value)
-                                result.append(result_part2)
-                        elif value is not None:
-                            entry = _create_value_map_entry(
-                                        li_spec['item_name'],
-                                        li_spec['item_type'],
-                                        value, status)
-                            result.append(entry)
-                    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(
-                                        map_name + map_item['item_name'],
-                                        map_item['item_type'],
-                                        value,
-                                        status)
-                            result.append(entry)
-                    else:
-                        value, status = self.get_value("/" + identifier)
-                        if value is not None:
-                            entry = _create_value_map_entry(
-                                        item['item_name'],
-                                        item['item_type'],
-                                        value, status)
-                            result.append(entry)
+                self._append_value_item(result, spec_part, identifier)
         return result
 
     def set_value(self, identifier, value):