Port obmc.mapper to python3.5

Change-Id: I472d92ddae72f004fb9d00103f160c9b108143fd
Signed-off-by: Balaji B Rao <balajibapu@gmail.com>
diff --git a/obmc/mapper/bindings.py b/obmc/mapper/bindings.py
index dadec32..bd19f19 100644
--- a/obmc/mapper/bindings.py
+++ b/obmc/mapper/bindings.py
@@ -39,7 +39,7 @@
         while count < retries:
             try:
                 return func()
-            except dbus.exceptions.DBusException, e:
+            except dbus.exceptions.DBusException as e:
                 if e.get_dbus_name() not in \
                     ['org.freedesktop.DBus.Error.ObjectPathInUse',
                      'org.freedesktop.DBus.Error.LimitsExceeded']:
@@ -82,7 +82,7 @@
     def __try_properties_interface(f, *a):
         try:
             return f(*a)
-        except dbus.exceptions.DBusException, e:
+        except dbus.exceptions.DBusException as e:
             if obmc.dbuslib.enums.DBUS_UNKNOWN_INTERFACE in \
                     e.get_dbus_name():
                 # interface doesn't have any properties
@@ -122,7 +122,7 @@
 
         obj = {}
 
-        for owner, interfaces in mapper_data[path].iteritems():
+        for owner, interfaces in mapper_data[path].items():
             obj.update(
                 self.__get_properties_on_bus(
                     path, owner, interfaces, match))
@@ -140,8 +140,8 @@
 
         # look for objectmanager implementations as they result
         # in fewer dbus calls
-        for path, bus_data in mapper_data.iteritems():
-            for owner, interfaces in bus_data.iteritems():
+        for path, bus_data in mapper_data.items():
+            for owner, interfaces in bus_data.items():
                 owners.append(owner)
                 if dbus.BUS_DAEMON_IFACE + '.ObjectManager' in interfaces:
                     managers[owner] = path
@@ -152,18 +152,18 @@
         # finally check the root for one too
         try:
             ancestors.update({path: self.get_object(path)})
-        except dbus.exceptions.DBusException, e:
+        except dbus.exceptions.DBusException as e:
             if e.get_dbus_name() != MAPPER_NOT_FOUND:
                 raise
 
-        for path, bus_data in ancestors.iteritems():
-            for owner, interfaces in bus_data.iteritems():
+        for path, bus_data in ancestors.items():
+            for owner, interfaces in bus_data.items():
                 if dbus.BUS_DAEMON_IFACE + '.ObjectManager' in interfaces:
                     managers[owner] = path
 
         # make all the manager gmo (get managed objects) calls
         results = {}
-        for owner, path in managers.iteritems():
+        for owner, path in managers.items():
             if owner not in owners:
                 continue
             obj = self.bus.get_object(owner, path, introspect=False)
@@ -171,17 +171,17 @@
                 obj, dbus.BUS_DAEMON_IFACE + '.ObjectManager')
 
             # flatten (remove interface names) gmo results
-            for path, interfaces in iface.GetManagedObjects().iteritems():
-                if path not in mapper_data.iterkeys():
+            for path, interfaces in iface.GetManagedObjects().items():
+                if path not in iter(mapper_data.keys()):
                     continue
                 properties = {}
-                for iface, props in interfaces.iteritems():
+                for iface, props in interfaces.items():
                     properties.update(props)
                 results.setdefault(path, {}).setdefault(owner, properties)
 
         # make dbus calls for any remaining objects
-        for path, bus_data in mapper_data.iteritems():
-            for owner, interfaces in bus_data.iteritems():
+        for path, bus_data in mapper_data.items():
+            for owner, interfaces in bus_data.items():
                 if results.setdefault(path, {}).setdefault(owner, {}):
                     continue
                 results[path][owner].update(
@@ -189,8 +189,8 @@
                         path, owner, interfaces, match))
 
         objs = obmc.utils.pathtree.PathTree()
-        for path, owners in results.iteritems():
-            for owner, properties in owners.iteritems():
+        for path, owners in results.items():
+            for owner, properties in owners.items():
                 objs.setdefault(path, {}).update(properties)
 
         return objs
diff --git a/obmc/mapper/server.py b/obmc/mapper/server.py
index 0c1df3e..b18b8b5 100644
--- a/obmc/mapper/server.py
+++ b/obmc/mapper/server.py
@@ -61,12 +61,12 @@
 
         @staticmethod
         def _to_path_elements(path):
-            return filter(bool, path.split('/'))
+            return list(filter(bool, path.split('/')))
 
         def __call__(self, path):
             try:
                 self._find_interfaces(path)
-            except Exception, e:
+            except Exception as e:
                 error_callback(service, path, e)
 
         @staticmethod
@@ -88,7 +88,7 @@
                 iface = obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE
                 self.assoc_pending.remove(path)
                 self.results[path][iface]['associations'] = associations
-            except Exception, e:
+            except Exception as e:
                 error_callback(service, path, e)
                 return None
 
@@ -97,9 +97,9 @@
         def _gmo_callback(self, path, objs):
             try:
                 self.gmo_pending.remove(path)
-                for k, v in objs.iteritems():
+                for k, v in objs.items():
                     self.results[k] = v
-            except Exception, e:
+            except Exception as e:
                 error_callback(service, path, e)
                 return None
 
@@ -114,9 +114,9 @@
             try:
                 path_elements = self._to_path_elements(path)
                 root = ET.fromstring(data)
-                ifaces = filter(
+                ifaces = list(filter(
                     self._match,
-                    [x.attrib.get('name') for x in root.findall('interface')])
+                    [x.attrib.get('name') for x in root.findall('interface')]))
                 ifaces = {x: {} for x in ifaces}
                 self.results[path] = ifaces
 
@@ -143,9 +143,9 @@
                         error_handler=lambda e: error_callback(
                             service, path, e))
                 else:
-                    children = filter(
+                    children = list(filter(
                         bool,
-                        [x.attrib.get('name') for x in root.findall('node')])
+                        [x.attrib.get('name') for x in root.findall('node')]))
                     children = [
                         self._to_path(
                             path_elements + self._to_path_elements(x))
@@ -153,7 +153,7 @@
                     for child in filter(subtree_match, children):
                         if child not in self.results:
                             self._find_interfaces(child)
-            except Exception, e:
+            except Exception as e:
                 error_callback(service, path, e)
                 return None
 
@@ -242,14 +242,14 @@
             path_keyword='path',
             sender_keyword='sender')
 
-        print "ObjectMapper startup complete.  Discovery in progress..."
+        print("ObjectMapper startup complete.  Discovery in progress...")
         self.discover()
         gobject.idle_add(self.claim_name)
 
     def claim_name(self):
         if len(self.defer_signals):
             return True
-        print "ObjectMapper discovery complete"
+        print("ObjectMapper discovery complete")
         self.service = dbus.service.BusName(
             obmc.mapper.MAPPER_NAME, self.bus)
         self.manager.unmask_signals()
@@ -308,7 +308,7 @@
     def interfaces_added_handler(self, path, iprops, **kw):
         path = str(path)
         owner = str(kw['sender'])
-        interfaces = self.get_signal_interfaces(owner, iprops.iterkeys())
+        interfaces = self.get_signal_interfaces(owner, iter(iprops.keys()))
         if not interfaces:
             return
 
@@ -371,7 +371,7 @@
         # unique name
         try:
             return self.discover([(owned_name, owner)])
-        except dbus.exceptions.DBusException, e:
+        except dbus.exceptions.DBusException as e:
             if obmc.dbuslib.enums.DBUS_UNKNOWN_SERVICE \
                     not in e.get_dbus_name():
                 raise
@@ -405,12 +405,12 @@
         # __xx -> intf list
         # xx -> intf dict
         if isinstance(old, dict):
-            __old = old.keys()
+            __old = list(old.keys())
         else:
             __old = old
             old = {x: {} for x in old}
         if isinstance(new, dict):
-            __new = new.keys()
+            __new = list(new.keys())
         else:
             __new = new
             new = {x: {} for x in new}
@@ -435,7 +435,7 @@
             path, owner, old_assoc, new_assoc, created, destroyed)
 
     def add_items(self, owner, bus_items):
-        for path, items in bus_items.iteritems():
+        for path, items in bus_items.items():
             self.update_interfaces(path, str(owner), old=[], new=items)
 
     def path_match(self, path):
@@ -473,10 +473,8 @@
                 traceback.print_exception(*sys.exc_info())
 
         if not owners:
-            owned_names = filter(
-                lambda x: not obmc.dbuslib.bindings.is_unique(x),
-                self.bus.list_names())
-            owners = filter(bool, [get_owner(name) for name in owned_names])
+            owned_names = [x for x in self.bus.list_names() if not obmc.dbuslib.bindings.is_unique(x)]
+            owners = list(filter(bool, [get_owner(name) for name in owned_names]))
         for owned_name, o in owners:
             if not self.valid_signal(owned_name):
                 continue
@@ -549,9 +547,9 @@
             svc_filter = lambda svc: svc[1]
 
             obj_map = lambda o: (
-                tuple(*filter(svc_filter, map(svc_map, [o]))))
+                tuple(*list(filter(svc_filter, list(map(svc_map, [o]))))))
 
-            return dict(filter(lambda x: x, map(obj_map, item.iteritems())))
+            return dict([x for x in map(obj_map, iter(item.items())) if x])
 
         # Called with a list of path/object tuples.
         if not ifaces:
@@ -563,7 +561,7 @@
                 x[1],
                 ifaces))
 
-        return dict(filter(lambda x: x[1], map(obj_map, iter(item or []))))
+        return dict([x for x in map(obj_map, iter(item or [])) if x[1]])
 
     @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'sas', 'a{sas}')
     def GetObject(self, path, interfaces):
@@ -593,7 +591,7 @@
 
     @staticmethod
     def has_interfaces(item):
-        for owner in item.iterkeys():
+        for owner in item.keys():
             if ObjectMapper.interfaces_get(item, owner):
                 return True
         return False
@@ -634,7 +632,7 @@
         associations = []
         if not owners:
             index = self.index.get(forward, {})
-            owners = index.get(path, {}).keys()
+            owners = list(index.get(path, {}).keys())
 
         # f: forward
         # r: reverse
@@ -739,7 +737,7 @@
         if not self.cache_get(path):
             raise MapperNotFoundException(path)
 
-        elements = filter(bool, path.split('/'))
+        elements = list(filter(bool, path.split('/')))
         paths = []
         objs = {}
         while elements:
@@ -754,7 +752,7 @@
                 continue
             objs[path] = obj
 
-        return self.filter_interfaces(list(objs.iteritems()), interfaces)
+        return self.filter_interfaces(list(objs.items()), interfaces)
 
     @dbus.service.signal(obmc.mapper.MAPPER_IFACE + '.Private', 's')
     def IntrospectionComplete(self, name):
diff --git a/obmc/mapper/utils.py b/obmc/mapper/utils.py
index 3937cf6..6f1b6b6 100644
--- a/obmc/mapper/utils.py
+++ b/obmc/mapper/utils.py
@@ -24,7 +24,7 @@
 class Wait(object):
     def __init__(self, bus, waitlist, *a, **kw):
         self.bus = bus
-        self.waitlist = dict(zip(waitlist, [None]*len(waitlist)))
+        self.waitlist = dict(list(zip(waitlist, [None]*len(waitlist))))
         mapper = bus.get_object(
             obmc.mapper.MAPPER_NAME,
             obmc.mapper.MAPPER_PATH,
@@ -121,6 +121,5 @@
         if self.done:
             return
 
-        for path in filter(
-                lambda x: not self.waitlist[x], self.waitlist.keys()):
+        for path in [x for x in list(self.waitlist.keys()) if not self.waitlist[x]]:
             self.get_object_async(path, 0)