Skip to content

Commit 27c55d5

Browse files
committed
Delay relative_to and win_to_unix operations over features
1 parent 85748db commit 27c55d5

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

tools/toolchains/__init__.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,29 @@ def update(self, other):
7171
self.eager.update(other)
7272

7373
def iteritems(self):
74+
"""Warning: This forces the evaluation all of the items in this LazyDict
75+
that are iterated over."""
7476
for k, v in self.eager.iteritems():
7577
yield k, v
7678
for k in self.lazy.keys():
7779
yield k, self[k]
7880

81+
def apply(self, fn):
82+
"""Delay the application of a computation to all items of the lazy dict.
83+
Does no computation now. Instead the comuptation is performed when a
84+
consumer attempts to access a value in this LazyDict"""
85+
new_lazy = {}
86+
for k, f in self.lazy.iteritems():
87+
def closure(f=f):
88+
return fn(f())
89+
new_lazy[k] = closure
90+
for k, v in self.eager.iteritems():
91+
def closure(v=v):
92+
return fn(v)
93+
new_lazy[k] = closure
94+
self.lazy = new_lazy
95+
self.eager = {}
96+
7997
class Resources:
8098
def __init__(self, base_path=None):
8199
self.base_path = base_path
@@ -199,7 +217,9 @@ def relative_to(self, base, dot=False):
199217
v = [rel_path(f, base, dot) for f in getattr(self, field)]
200218
setattr(self, field, v)
201219

202-
self.features = {k: f.relative_to(base, dot) for k, f in self.features.iteritems() if f}
220+
def to_apply(feature, base=base, dot=dot):
221+
feature.relative_to(base, dot)
222+
self.features.apply(to_apply)
203223

204224
if self.linker_script is not None:
205225
self.linker_script = rel_path(self.linker_script, base, dot)
@@ -212,7 +232,9 @@ def win_to_unix(self):
212232
v = [f.replace('\\', '/') for f in getattr(self, field)]
213233
setattr(self, field, v)
214234

215-
self.features = {k: f.win_to_unix() for k, f in self.features.iteritems() if f}
235+
def to_apply(feature):
236+
feature.win_to_unix()
237+
self.features.apply(to_apply)
216238

217239
if self.linker_script is not None:
218240
self.linker_script = self.linker_script.replace('\\', '/')

0 commit comments

Comments
 (0)