Skip to content

Commit 85748db

Browse files
committed
Delay feature scan until it's rquired
1 parent 55801b6 commit 85748db

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

tools/toolchains/__init__.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,40 @@
4242
CPU_COUNT_MIN = 1
4343
CPU_COEF = 1
4444

45+
class LazyDict(dict):
46+
def __init__(self):
47+
self.eager = {}
48+
self.lazy = {}
49+
50+
def add_lazy(self, key, thunk):
51+
self.lazy[key] = thunk
52+
53+
def __getitem__(self, key):
54+
if (key not in self.eager
55+
and key in self.lazy):
56+
self.eager[key] = self.lazy[key]()
57+
del self.lazy[key]
58+
return self.eager[key]
59+
60+
def __setitem__(self, key, value):
61+
self.eager[key] = value
62+
63+
def __contains__(self, key):
64+
return key in self.eager or key in self.lazy
65+
66+
def update(self, other):
67+
if isinstance(other, LazyDict):
68+
self.eager.update(other.eager)
69+
self.lazy.update(other.lazy)
70+
else:
71+
self.eager.update(other)
72+
73+
def iteritems(self):
74+
for k, v in self.eager.iteritems():
75+
yield k, v
76+
for k in self.lazy.keys():
77+
yield k, self[k]
78+
4579
class Resources:
4680
def __init__(self, base_path=None):
4781
self.base_path = base_path
@@ -74,7 +108,7 @@ def __init__(self, base_path=None):
74108
self.json_files = []
75109

76110
# Features
77-
self.features = {}
111+
self.features = LazyDict()
78112

79113
def __add__(self, resources):
80114
if resources is None:
@@ -603,7 +637,9 @@ def _add_dir(self, path, resources, base_path, exclude_paths=None):
603637
elif d.startswith('FEATURE_'):
604638
# Recursively scan features but ignore them in the current scan.
605639
# These are dynamically added by the config system if the conditions are matched
606-
resources.features[d[8:]] = self.scan_resources(dir_path, base_path=base_path)
640+
def closure (dir_path=dir_path, base_path=base_path):
641+
return self.scan_resources(dir_path, base_path=base_path)
642+
resources.features.add_lazy(d[8:], closure)
607643
dirs.remove(d)
608644
elif exclude_paths:
609645
for exclude_path in exclude_paths:

0 commit comments

Comments
 (0)