Skip to content

Commit b565cbc

Browse files
committed
Refactor __new__ to ComponentMeta.
1 parent f0cd7f7 commit b565cbc

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

dash/development/base_component.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,33 @@
99
import six
1010

1111

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."""
1415

1516
registry = set()
1617
__dist_cache = collections.defaultdict(dict)
1718

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+
1839
# pylint: disable=arguments-differ
1940
def __new__(mcs, name, bases, attributes):
2041
component = abc.ABCMeta.__new__(mcs, name, bases, attributes)
@@ -25,27 +46,10 @@ def __new__(mcs, name, bases, attributes):
2546
# as it doesn't have the namespace.
2647
return component
2748

28-
mcs.registry.add(module)
49+
ComponentRegistry.registry.add(module)
2950

3051
return component
3152

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-
4953

5054
def is_number(s):
5155
try:
@@ -95,7 +99,7 @@ def wrapper(*args, **kwargs):
9599
return wrapper
96100

97101

98-
@six.add_metaclass(ComponentRegistry)
102+
@six.add_metaclass(ComponentMeta)
99103
class Component(collections.MutableMapping):
100104
class _UNDEFINED(object):
101105
def __repr__(self):

0 commit comments

Comments
 (0)