9
9
import six
10
10
11
11
12
- class ComponentRegistry (abc .ABCMeta ):
13
- """Just importing a component lib will make it be loaded on the index"""
12
+ # pylint: disable=no-init,too-few-public-methods
13
+ class ComponentRegistry :
14
+ """Holds a registry of the namespaces used by components."""
14
15
15
16
registry = set ()
16
17
__dist_cache = collections .defaultdict (dict )
17
18
19
+ @classmethod
20
+ def get_resources (cls , resource_name ):
21
+ cached = cls .__dist_cache .get (resource_name )
22
+ current_len = len (cls .registry )
23
+
24
+ if cached and current_len == cached .get ('len' ):
25
+ return cached .get ('resources' )
26
+
27
+ cls .__dist_cache [resource_name ]['resources' ] = resources = []
28
+ cls .__dist_cache [resource_name ]['len' ] = current_len
29
+
30
+ for module_name in cls .registry :
31
+ module = sys .modules [module_name ]
32
+ resources .extend (getattr (module , resource_name , []))
33
+
34
+ return resources
35
+
36
+
37
+ class ComponentMeta (abc .ABCMeta ):
38
+
18
39
# pylint: disable=arguments-differ
19
40
def __new__ (mcs , name , bases , attributes ):
20
41
component = abc .ABCMeta .__new__ (mcs , name , bases , attributes )
@@ -25,27 +46,10 @@ def __new__(mcs, name, bases, attributes):
25
46
# as it doesn't have the namespace.
26
47
return component
27
48
28
- mcs .registry .add (module )
49
+ ComponentRegistry .registry .add (module )
29
50
30
51
return component
31
52
32
- @classmethod
33
- def get_resources (mcs , resource_name ):
34
- cached = mcs .__dist_cache .get (resource_name )
35
- current_len = len (mcs .registry )
36
-
37
- if cached and current_len == cached .get ('len' ):
38
- return cached .get ('resources' )
39
-
40
- mcs .__dist_cache [resource_name ]['resources' ] = resources = []
41
- mcs .__dist_cache [resource_name ]['len' ] = current_len
42
-
43
- for module_name in mcs .registry :
44
- module = sys .modules [module_name ]
45
- resources .extend (getattr (module , resource_name , []))
46
-
47
- return resources
48
-
49
53
50
54
def is_number (s ):
51
55
try :
@@ -95,7 +99,7 @@ def wrapper(*args, **kwargs):
95
99
return wrapper
96
100
97
101
98
- @six .add_metaclass (ComponentRegistry )
102
+ @six .add_metaclass (ComponentMeta )
99
103
class Component (collections .MutableMapping ):
100
104
class _UNDEFINED (object ):
101
105
def __repr__ (self ):
0 commit comments