소스 검색

devices: add DeviceInfo.devclass attribute

Have DeviceInfo also carry information about its class, to avoid the
need to iterate all the devices to get that information.
Marek Marczykowski-Górecki 6 년 전
부모
커밋
bf240cc928
2개의 변경된 파일11개의 추가작업 그리고 8개의 파일을 삭제
  1. 9 6
      qubesadmin/devices.py
  2. 2 2
      qubesadmin/tests/devices.py

+ 9 - 6
qubesadmin/devices.py

@@ -76,10 +76,12 @@ class DeviceAssignment(object):  # pylint: disable=too-few-public-methods
 class DeviceInfo(object):
     ''' Holds all information about a device '''
     # pylint: disable=too-few-public-methods
-    def __init__(self, backend_domain, ident, description=None,
+    def __init__(self, backend_domain, devclass, ident, description=None,
                  options=None, **kwargs):
         #: domain providing this device
         self.backend_domain = backend_domain
+        #: device class
+        self.devclass = devclass
         #: device identifier (unique for given domain and device type)
         self.ident = ident
         #: human readable description/name of the device
@@ -107,12 +109,12 @@ class UnknownDevice(DeviceInfo):
     # pylint: disable=too-few-public-methods
     '''Unknown device - for example exposed by domain not running currently'''
 
-    def __init__(self, backend_domain, ident, description=None,
+    def __init__(self, backend_domain, devclass, ident, description=None,
             **kwargs):
         if description is None:
             description = "Unknown device"
-        super(UnknownDevice, self).__init__(backend_domain, ident, description,
-            **kwargs)
+        super(UnknownDevice, self).__init__(backend_domain, devclass, ident,
+            description, **kwargs)
 
 
 class DeviceCollection(object):
@@ -229,7 +231,8 @@ class DeviceCollection(object):
             info, _, description = info.partition('description=')
             info_dict = dict(info_single.split('=', 1)
                 for info_single in info.split(' ') if info_single)
-            yield DeviceInfo(self._vm, ident, description=description,
+            yield DeviceInfo(self._vm, self._class, ident,
+                description=description,
                 options=None, **info_dict)
 
     def update_persistent(self, device, persistent):
@@ -271,7 +274,7 @@ class DeviceCollection(object):
                 return dev
         # if still nothing, return UnknownDevice instance for the reason
         # explained in docstring, but don't cache it
-        return UnknownDevice(self._vm, item)
+        return UnknownDevice(self._vm, self._class, item)
 
 
 

+ 2 - 2
qubesadmin/tests/devices.py

@@ -298,7 +298,7 @@ class TC_00_DeviceCollection(qubesadmin.tests.QubesTestCase):
             ('test-vm', 'admin.vm.device.test.Set.persistent', 'test-vm2+dev1',
                 b'True')] = b'0\0'
         dev = qubesadmin.devices.DeviceInfo(
-            self.app.domains['test-vm2'], 'dev1')
+            self.app.domains['test-vm2'], 'test', 'dev1')
         self.vm.devices['test'].update_persistent(dev, True)
         self.assertAllCalled()
 
@@ -307,6 +307,6 @@ class TC_00_DeviceCollection(qubesadmin.tests.QubesTestCase):
             ('test-vm', 'admin.vm.device.test.Set.persistent', 'test-vm2+dev1',
                 b'False')] = b'0\0'
         dev = qubesadmin.devices.DeviceInfo(
-            self.app.domains['test-vm2'], 'dev1')
+            self.app.domains['test-vm2'], 'test', 'dev1')
         self.vm.devices['test'].update_persistent(dev, False)
         self.assertAllCalled()