[conduit/iphone: 2/4] Change how we interact with gudev



commit 5bf90147e124bcd8eeaf1e6d72e8277020ca3e22
Author: John Stowers <john stowers gmail com>
Date:   Thu Sep 23 00:48:34 2010 +1200

    Change how we interact with gudev
    
    	* Do away with the Singleton, it got in the way
    	  of derived factories that want to inspect different
    	  udev subsystems
    	* Add a pythonic wrapper, UDevClient

 conduit/dataproviders/HalFactory.py         |   35 ++++++++++----------------
 conduit/dataproviders/MediaPlayerFactory.py |    8 +++---
 conduit/modules/iPodModule/iPodModule.py    |    4 ++-
 conduit/utils/UDev.py                       |   24 ++++++++++++++----
 4 files changed, 40 insertions(+), 31 deletions(-)
---
diff --git a/conduit/dataproviders/HalFactory.py b/conduit/dataproviders/HalFactory.py
index af34d46..90bc19a 100644
--- a/conduit/dataproviders/HalFactory.py
+++ b/conduit/dataproviders/HalFactory.py
@@ -11,15 +11,21 @@ import conduit.dataproviders.SimpleFactory as SimpleFactory
 log.info("Module Information: %s" % Utils.get_module_information(gudev, "__version__"))
 
 class HalFactory(SimpleFactory.SimpleFactory):
+    """
+    Base class for Factories that wish to be notified upon changes to
+    the udev subsystem(s) specified in the UDEV_SUBSYSTEMS class attribute
 
-    SUBSYSTEMS = ("usb", "block")
+    HalFactory.UDEV_SUBSYSTEMS is a list of strings
+    """
+
+    UDEV_SUBSYSTEMS = None
 
     def __init__(self, **kwargs):
         SimpleFactory.SimpleFactory.__init__(self, **kwargs)
 
-        assert hasattr(self.SUBSYSTEMS, "__iter__")
+        assert hasattr(self.UDEV_SUBSYSTEMS, "__iter__")
 
-        self.gudev = UDev.UDevSingleton(self.SUBSYSTEMS)
+        self.gudev = UDev.UDevHelper(*self.UDEV_SUBSYSTEMS)
         self.gudev.connect("uevent", self._on_uevent)
 
     def _print_device(self, device):
@@ -51,31 +57,18 @@ class HalFactory(SimpleFactory.SimpleFactory):
             self._maybe_new(device)
         elif action == "remove":
             log.debug("Device removed")
-            sysfs_path = self.get_sysfs_path_for_device(device)
+            sysfs_path = device.get_sysfs_path()
             self.item_removed(sysfs_path)
         else:
             log.info("Device unknown action: %s" % action)
 
-    def _get_device_properties(self, device):
-        props = {}
-        for key in device.get_property_keys():
-            props[key.upper()] = device.get_property(key)
-        return props
-
     def _maybe_new(self, device):
-        props = self._get_device_properties(device)
-        sysfs_path = self.get_sysfs_path_for_device(device)
+        props = self.gudev.get_device_properties(device)
+        sysfs_path = device.get_sysfs_path()
         if self.is_interesting(sysfs_path, props):
             self.item_added(sysfs_path, **props)
 
-    def get_udev_device_for_sysfs_path(self, sysfs_path):
-        return self.gudev.query_by_sysfs_path(sysfs_path)
-
-    def get_sysfs_path_for_device(self, device):
-        return device.get_sysfs_path()
-
     def probe(self):
-        for s in self.SUBSYSTEMS:
-            for d in self.gudev.query_by_subsystem(s):
-                self._maybe_new(d)
+        for d in self.gudev.query_by_subsystems():
+            self._maybe_new(d)
 
diff --git a/conduit/dataproviders/MediaPlayerFactory.py b/conduit/dataproviders/MediaPlayerFactory.py
index 9ac2cf5..d6c1fc3 100644
--- a/conduit/dataproviders/MediaPlayerFactory.py
+++ b/conduit/dataproviders/MediaPlayerFactory.py
@@ -4,6 +4,7 @@ import logging
 log = logging.getLogger("dataproviders.MediaPlayerFactory")
 
 import conduit.utils as Utils
+import conduit.utils.UDev as UDev
 import conduit.dataproviders.HalFactory as HalFactory
 
 class MediaPlayerFactory(HalFactory.HalFactory):
@@ -14,8 +15,7 @@ class MediaPlayerFactory(HalFactory.HalFactory):
     MPI_ICON            = ("Device", "Icon",           "MPI_ICON")
     MPI_KEYS = (MPI_ACCESS_PROTOCOL, MPI_ICON)
 
-    def __init__(self, *args, **kwargs):
-        HalFactory.HalFactory.__init__(self, *args, **kwargs)
+    UDEV_SUBSYSTEMS = ("block",)
 
     #taken from quodlibet
     def __get_mpi_dir(self):
@@ -38,8 +38,8 @@ class MediaPlayerFactory(HalFactory.HalFactory):
             if read: return parser
 
     def _maybe_new(self, device):
-        props = self._get_device_properties(device)
-        sysfs_path = self.get_sysfs_path_for_device(device)
+        props = self.gudev.get_device_properties(device)
+        sysfs_path = device.get_sysfs_path()
         try:
             mplayer_id = props["ID_MEDIA_PLAYER"]
 
diff --git a/conduit/modules/iPodModule/iPodModule.py b/conduit/modules/iPodModule/iPodModule.py
index 8ad1cf6..5348b62 100644
--- a/conduit/modules/iPodModule/iPodModule.py
+++ b/conduit/modules/iPodModule/iPodModule.py
@@ -90,13 +90,15 @@ def _get_apple_icon(props):
 
 class iPhoneFactory(HalFactory.HalFactory):
 
+    UDEV_SUBSYSTEMS = ("usb",)
+
     def is_interesting(self, sysfs_path, props):
         #there is no media-player-info support for the apple iphone, so instead 
         #we have to look for the correct model name instead.
         if "Apple" in props.get("ID_VENDOR", "") and "iPhone" in props.get("ID_MODEL", ""):
             #also have to check the iPhone has a valid serial, as that is used
             #with gvfs to generate the uuid of the moint
-            self._print_device(self.get_udev_device_for_sysfs_path(sysfs_path))
+            self._print_device(self.gudev.query_by_sysfs_path(sysfs_path))
             if props.get("ID_SERIAL_SHORT"):
                 uuid = "afc://%s/" % props["ID_SERIAL_SHORT"]
                 for m in gio.volume_monitor_get().get_mounts():
diff --git a/conduit/utils/UDev.py b/conduit/utils/UDev.py
index 62cb5aa..7c85d61 100644
--- a/conduit/utils/UDev.py
+++ b/conduit/utils/UDev.py
@@ -5,9 +5,23 @@ import logging
 log = logging.getLogger("utils.UDev")
 
 import conduit.utils as Utils
-import conduit.utils.Singleton as Singleton
 
-class UDevSingleton(Singleton.Singleton, gudev.Client):
-    def __init__(self, *args, **kwargs):
-        super(UDevSingleton, self).__init__(*args, **kwargs)
-        log.debug("Constructed: %s" % self)
+class UDevHelper(gudev.Client):
+    def __init__(self, *subsystems):
+        gudev.Client.__init__(self, subsystems)
+        self._subsystems = subsystems
+
+    def query_by_subsystems(self, *subsystems):
+        if not subsystems:
+            subsystems = self._subsystems
+        devices = []
+        for s in subsystems:
+            devices.extend( self.query_by_subsystem(s) )
+        return devices
+
+    def get_device_properties(self, device):
+        props = {}
+        for key in device.get_property_keys():
+            props[key.upper()] = device.get_property(key)
+        return props
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]