|
42 | 42 | CPU_COUNT_MIN = 1
|
43 | 43 | CPU_COEF = 1
|
44 | 44 |
|
| 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 | + |
45 | 79 | class Resources:
|
46 | 80 | def __init__(self, base_path=None):
|
47 | 81 | self.base_path = base_path
|
@@ -74,7 +108,7 @@ def __init__(self, base_path=None):
|
74 | 108 | self.json_files = []
|
75 | 109 |
|
76 | 110 | # Features
|
77 |
| - self.features = {} |
| 111 | + self.features = LazyDict() |
78 | 112 |
|
79 | 113 | def __add__(self, resources):
|
80 | 114 | if resources is None:
|
@@ -603,7 +637,9 @@ def _add_dir(self, path, resources, base_path, exclude_paths=None):
|
603 | 637 | elif d.startswith('FEATURE_'):
|
604 | 638 | # Recursively scan features but ignore them in the current scan.
|
605 | 639 | # 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) |
607 | 643 | dirs.remove(d)
|
608 | 644 | elif exclude_paths:
|
609 | 645 | for exclude_path in exclude_paths:
|
|
0 commit comments