Browse Source

fix merge_alfred_batman to include batman-only nodes, too

This closes issue #1.
Helge Jung 9 years ago
parent
commit
da46dcf81c
1 changed files with 20 additions and 2 deletions
  1. 20 2
      ffstatus/__init__.py

+ 20 - 2
ffstatus/__init__.py

@@ -58,14 +58,21 @@ def dict_merge(a, b, overwrite_lists=True):
 def merge_alfred_batman(alfreddata, batmandata):
     merged = {}
 
+    # lookup dict to map MACs to node ids
     batlookup = {}
+    # list of (yet un-)handled BATMAN nodes
+    unhandled_batnodes = set()
+
+    # fill above variables from BATMAN data
     for nodeid in batmandata:
         batlookup[nodeid] = nodeid
+        unhandled_batnodes.add(str(nodeid))
         for bda in batmandata[nodeid]['aliases']:
             batlookup[bda] = nodeid
 
+    # iterate over ALFRED data
     for nodeid in alfreddata:
-        nodeinfo = alfreddata[nodeid]
+        nodeinfo = dict_merge({}, alfreddata[nodeid])
         candidates = set()
         candidates.add(nodeid)
         if 'mac' in nodeinfo:
@@ -86,9 +93,20 @@ def merge_alfred_batman(alfreddata, batmandata):
             nodeinfo['neighbours'] = {}
 
         for candidate_raw in candidates:
-            candidate = batlookup[candidate_raw] if candidate_raw in batlookup else candidate_raw
+            candidate = batlookup.get(candidate_raw, candidate_raw)
             if candidate in batmandata:
                 nodeinfo = dict_merge(nodeinfo, batmandata[candidate])
+                if candidate in unhandled_batnodes:
+                    unhandled_batnodes.remove(str(candidate))
+
+        merged[nodeid] = nodeinfo
+
+    # handle BATMAN nodes which aren't in ALFRED
+    for nodeid in unhandled_batnodes:
+        logger.debug("unhandled BATMAN node '%s'", nodeid)
+        nodeinfo = dict_merge({}, batmandata[nodeid])
+        if not 'node_id' in nodeinfo:
+            nodeinfo['node_id'] = nodeid
 
         merged[nodeid] = nodeinfo