Pārlūkot izejas kodu

base: make WrapperObjectsCollection behave like a dict

In core-admin matching collections are real dicts, so clone this API
behaviour here too. Specific changes:
 - iteration yields keys, not values
 - implement values and items methods

Additionally fix keys method, it was broken on python2 (list have no
copy method).
Marek Marczykowski-Górecki 6 gadi atpakaļ
vecāks
revīzija
04f8d85819

+ 13 - 3
qubesadmin/base.py

@@ -324,10 +324,20 @@ class WrapperObjectsCollection(object):
 
     def __iter__(self):
         self.refresh_cache()
-        for obj in self._names_list:
-            yield self[obj]
+        for key in self._names_list:
+            yield key
 
     def keys(self):
         '''Get list of names.'''
         self.refresh_cache()
-        return self._names_list.copy()
+        return [key for key in self._names_list]
+
+    def items(self):
+        '''Get list of (key, value) pairs'''
+        self.refresh_cache()
+        return [(key, self.get_blind(key)) for key in self._names_list]
+
+    def values(self):
+        '''Get list of objects'''
+        self.refresh_cache()
+        return [self.get_blind(key) for key in self._names_list]

+ 32 - 1
qubesadmin/tests/label.py

@@ -28,11 +28,42 @@ class TC_00_Label(qubesadmin.tests.QubesTestCase):
             ('dom0', 'admin.label.List', None, None)] = \
             b'0\x00green\nred\nblack\n'
         seen = set()
-        for label in self.app.labels:
+        for label in self.app.labels.values():
             self.assertNotIn(label.name, seen)
             seen.add(label.name)
         self.assertEqual(seen, set(['green', 'red', 'black']))
 
+    def test_001_list_names(self):
+        self.app.expected_calls[
+            ('dom0', 'admin.label.List', None, None)] = \
+            b'0\x00green\nred\nblack\n'
+        seen = set()
+        for label in self.app.labels:
+            self.assertNotIn(label, seen)
+            seen.add(label)
+        self.assertEqual(seen, set(['green', 'red', 'black']))
+
+    def test_002_list_keys(self):
+        self.app.expected_calls[
+            ('dom0', 'admin.label.List', None, None)] = \
+            b'0\x00green\nred\nblack\n'
+        seen = set()
+        for label in self.app.labels.keys():
+            self.assertNotIn(label, seen)
+            seen.add(label)
+        self.assertEqual(seen, set(['green', 'red', 'black']))
+
+    def test_003_list_items(self):
+        self.app.expected_calls[
+            ('dom0', 'admin.label.List', None, None)] = \
+            b'0\x00green\nred\nblack\n'
+        seen = set()
+        for name, label in self.app.labels.items():
+            self.assertEqual(name, label.name)
+            self.assertNotIn(name, seen)
+            seen.add(name)
+        self.assertEqual(seen, set(['green', 'red', 'black']))
+
     def test_010_get(self):
         self.app.expected_calls[
             ('dom0', 'admin.label.List', None, None)] = \

+ 1 - 1
qubesadmin/tests/storage.py

@@ -277,7 +277,7 @@ class TestPool(qubesadmin.tests.QubesTestCase):
         self.app.expected_calls[('dom0', 'admin.pool.List', None, None)] = \
             b'0\x00file\nlvm\n'
         seen = set()
-        for pool in self.app.pools:
+        for pool in self.app.pools.values():
             self.assertIsInstance(pool, qubesadmin.storage.Pool)
             self.assertIn(pool.name, ('file', 'lvm'))
             self.assertNotIn(pool.name, seen)

+ 1 - 1
qubesadmin/tools/qvm_pool.py

@@ -54,7 +54,7 @@ def pool_info(pool):
 def list_pools(app):
     ''' Prints out all known pools and their drivers '''
     result = [('NAME', 'DRIVER')]
-    for pool in app.pools:
+    for pool in app.pools.values():
         result += [(pool.name, pool.driver)]
     qubesadmin.tools.print_table(result)