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).
This commit is contained in:
Marek Marczykowski-Górecki 2017-12-23 01:33:26 +01:00
parent 8584ca4057
commit 04f8d85819
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
4 changed files with 47 additions and 6 deletions

View File

@ -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]

View File

@ -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)] = \

View File

@ -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)

View File

@ -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)