Show More
@@ -59,34 +59,30 b' class MetaComponentTracker(type):' | |||||
59 | c.__instance_refs[c.__numcreated] = instance |
|
59 | c.__instance_refs[c.__numcreated] = instance | |
60 | return instance |
|
60 | return instance | |
61 |
|
61 | |||
62 |
def get_instances(cls, name=None, |
|
62 | def get_instances(cls, name=None, root=None): | |
63 | """Get all instances of cls and its subclasses. |
|
63 | """Get all instances of cls and its subclasses. | |
64 |
|
64 | |||
65 | Parameters |
|
65 | Parameters | |
66 | ---------- |
|
66 | ---------- | |
67 | name : str |
|
67 | name : str | |
68 | Limit to components with this name. |
|
68 | Limit to components with this name. | |
69 | klass : class |
|
|||
70 | Limit to components having isinstance(component, klass) |
|
|||
71 | root : Component or subclass |
|
69 | root : Component or subclass | |
72 | Limit to components having this root. |
|
70 | Limit to components having this root. | |
73 | """ |
|
71 | """ | |
74 | instances = cls.__instance_refs.values() |
|
72 | instances = cls.__instance_refs.values() | |
75 | if name is not None: |
|
73 | if name is not None: | |
76 | instances = [i for i in instances if i.name == name] |
|
74 | instances = [i for i in instances if i.name == name] | |
77 | if klass is not None: |
|
|||
78 | instances = [i for i in instances if isinstance(i, klass)] |
|
|||
79 | if root is not None: |
|
75 | if root is not None: | |
80 | instances = [i for i in instances if i.root == root] |
|
76 | instances = [i for i in instances if i.root == root] | |
81 | return instances |
|
77 | return instances | |
82 |
|
78 | |||
83 |
def get_instances_by_condition(cls, call, name=None, |
|
79 | def get_instances_by_condition(cls, call, name=None, root=None): | |
84 | """Get all instances of cls, i such that call(i)==True. |
|
80 | """Get all instances of cls, i such that call(i)==True. | |
85 |
|
81 | |||
86 |
This also takes the ``name`` |
|
82 | This also takes the ``name`` and ``root`` arguments of | |
87 | :meth:`get_instance` |
|
83 | :meth:`get_instance` | |
88 | """ |
|
84 | """ | |
89 |
return [i for i in cls.get_instances(name, |
|
85 | return [i for i in cls.get_instances(name, root) if call(i)] | |
90 |
|
86 | |||
91 |
|
87 | |||
92 | class ComponentNameGenerator(object): |
|
88 | class ComponentNameGenerator(object): |
@@ -52,7 +52,7 b' class TestComponentMeta(TestCase):' | |||||
52 | c2 = MyOtherComponent(c1) |
|
52 | c2 = MyOtherComponent(c1) | |
53 | c3 = MyOtherComponent(c2) |
|
53 | c3 = MyOtherComponent(c2) | |
54 | self.assertEquals(MyComponent.get_instances(), [c1, c2, c3]) |
|
54 | self.assertEquals(MyComponent.get_instances(), [c1, c2, c3]) | |
55 |
self.assertEquals(MyComponent.get_instances( |
|
55 | self.assertEquals(MyOtherComponent.get_instances(), [c2, c3]) | |
56 |
|
56 | |||
57 | def test_get_instances_root(self): |
|
57 | def test_get_instances_root(self): | |
58 | class MyComponent(Component): |
|
58 | class MyComponent(Component): |
General Comments 0
You need to be logged in to leave comments.
Login now