diff options
author | Steven Knight <knight@baldmt.com> | 2004-03-26 22:09:41 (GMT) |
---|---|---|
committer | Steven Knight <knight@baldmt.com> | 2004-03-26 22:09:41 (GMT) |
commit | 6503d03e9933c9daf1bd3ad4ba4bc37a77587591 (patch) | |
tree | aea69e607bf29326ce28b7abc19d6b84c23f2240 /src/engine | |
parent | d8dc50c28a3eeac4a73533140b44e4bd73b8797e (diff) | |
download | SCons-6503d03e9933c9daf1bd3ad4ba4bc37a77587591.zip SCons-6503d03e9933c9daf1bd3ad4ba4bc37a77587591.tar.gz SCons-6503d03e9933c9daf1bd3ad4ba4bc37a77587591.tar.bz2 |
Refactor env.Append() and env.Prepend().
Diffstat (limited to 'src/engine')
-rw-r--r-- | src/engine/SCons/Environment.py | 128 | ||||
-rw-r--r-- | src/engine/SCons/EnvironmentTests.py | 12 |
2 files changed, 98 insertions, 42 deletions
diff --git a/src/engine/SCons/Environment.py b/src/engine/SCons/Environment.py index dc52ee1..1d61ae8 100644 --- a/src/engine/SCons/Environment.py +++ b/src/engine/SCons/Environment.py @@ -454,26 +454,53 @@ class Base: in an Environment. """ kw = our_deepcopy(kw) - for key in kw.keys(): - if not self._dict.has_key(key): - self._dict[key] = kw[key] - elif SCons.Util.is_List(self._dict[key]) and not \ - SCons.Util.is_List(kw[key]): - if kw[key]: - self._dict[key] = self._dict[key] + [ kw[key] ] - #self._dict[key] = map(None, self._dict[key] + [ kw[key] ]) - elif SCons.Util.is_List(kw[key]) and not \ - SCons.Util.is_List(self._dict[key]): - if self._dict[key]: - self._dict[key] = [ self._dict[key] ] + kw[key] - else: - self._dict[key] = kw[key] - #self._dict[key] = map(None, self._dict[key] + [ kw[key] ]) - elif SCons.Util.is_Dict(self._dict[key]) and \ - SCons.Util.is_Dict(kw[key]): - self._dict[key].update(kw[key]) + for key, val in kw.items(): + try: + orig = self._dict[key] + except KeyError: + # No existing variable in the environment, so just set + # it to the new value. + self._dict[key] = val + continue + + try: + # Most straightforward: just try to add them together. + # This will work in most cases, when the original and + # new values are of compatible types. + self._dict[key] = orig + val + continue + except TypeError: + pass + + try: + # Try to update a dictionary value with another. + # If orig isn't a dictionary, it won't have an + # update() method; if val isn't a dictionary, it + # won't have a keys() method. Either way, it's + # an AttributeError. + orig.update(val) + continue + except AttributeError: + pass + + try: + # Check if the original is a list. + add_to_orig = orig.append + except AttributeError: + pass else: - self._dict[key] = self._dict[key] + kw[key] + # The original is a list, so append the new value to it + # (if there's a value to append). + if val: + add_to_orig(val) + continue + + # The original isn't a list, but the new value is (by process + # of elimination), so insert the original in the new value + # (if there's one to insert) and replace the variable with it. + if orig: + val.insert(0, orig) + self._dict[key] = val def AppendENVPath(self, name, newpath, envname = 'ENV', sep = os.pathsep): """Append path elements to the path 'name' in the 'ENV' @@ -662,24 +689,53 @@ class Base: in an Environment. """ kw = our_deepcopy(kw) - for key in kw.keys(): - if not self._dict.has_key(key): - self._dict[key] = kw[key] - elif SCons.Util.is_List(self._dict[key]) and not \ - SCons.Util.is_List(kw[key]): - if kw[key]: - self._dict[key] = [ kw[key] ] + self._dict[key] - elif SCons.Util.is_List(kw[key]) and not \ - SCons.Util.is_List(self._dict[key]): - if self._dict[key]: - self._dict[key] = kw[key] + [ self._dict[key] ] - else: - self._dict[key] = kw[key] - elif SCons.Util.is_Dict(self._dict[key]) and \ - SCons.Util.is_Dict(kw[key]): - self._dict[key].update(kw[key]) + for key, val in kw.items(): + try: + orig = self._dict[key] + except KeyError: + # No existing variable in the environment, so just set + # it to the new value. + self._dict[key] = val + continue + + try: + # Most straightforward: just try to add them together. + # This will work in most cases, when the original and + # new values are of compatible types. + self._dict[key] = val + orig + continue + except TypeError: + pass + + try: + # Try to update a dictionary value with another. + # If orig isn't a dictionary, it won't have an + # update() method; if val isn't a dictionary, it + # won't have a keys() method. Either way, it's + # an AttributeError. + orig.update(val) + continue + except AttributeError: + pass + + try: + # Check if the added value is a list. + add_to_val = val.append + except AttributeError: + pass else: - self._dict[key] = kw[key] + self._dict[key] + # The added value is a list, so append the original to it + # (if there's a value to append). + if orig: + add_to_val(orig) + self._dict[key] = val + continue + + # The added value isn't a list, but the original is (by + # process of elimination), so insert the the new value in + # the original (if there's one to insert). + if val: + orig.insert(0, val) def PrependENVPath(self, name, newpath, envname = 'ENV', sep = os.pathsep): """Prepend path elements to the path 'name' in the 'ENV' diff --git a/src/engine/SCons/EnvironmentTests.py b/src/engine/SCons/EnvironmentTests.py index 0576f83..11fd0d8 100644 --- a/src/engine/SCons/EnvironmentTests.py +++ b/src/engine/SCons/EnvironmentTests.py @@ -875,7 +875,7 @@ class EnvironmentTestCase(unittest.TestCase): ['e7'], [''], ['e7', ''], ['e8'], UL(['']), UL(['e8', '']), - UL(['i1']), 'I1', UL(['i1', 'I1']), + UL(['i1']), 'I1', UL(['i1', 'I', '1']), UL(['i2']), ['I2'], UL(['i2', 'I2']), UL(['i3']), UL(['I3']), UL(['i3', 'I3']), UL(['i4']), '', UL(['i4']), @@ -902,7 +902,7 @@ class EnvironmentTestCase(unittest.TestCase): [], [''], [''], [], UL(['']), UL(['']), - UL([]), 'O1', ['O1'], + UL([]), 'O1', ['O', '1'], UL([]), ['O2'], ['O2'], UL([]), UL(['O3']), UL(['O3']), UL([]), '', UL([]), @@ -920,7 +920,7 @@ class EnvironmentTestCase(unittest.TestCase): [''], [''], ['', ''], [''], UL(['']), UL(['', '']), - UL(['']), 'Q1', ['', 'Q1'], + UL(['']), 'Q1', ['', 'Q', '1'], UL(['']), ['Q2'], ['', 'Q2'], UL(['']), UL(['Q3']), UL(['', 'Q3']), UL(['']), '', UL(['']), @@ -1297,7 +1297,7 @@ class EnvironmentTestCase(unittest.TestCase): ['e7'], [''], ['', 'e7'], ['e8'], UL(['']), UL(['', 'e8']), - UL(['i1']), 'I1', UL(['I1', 'i1']), + UL(['i1']), 'I1', UL(['I', '1', 'i1']), UL(['i2']), ['I2'], UL(['I2', 'i2']), UL(['i3']), UL(['I3']), UL(['I3', 'i3']), UL(['i4']), '', UL(['i4']), @@ -1324,7 +1324,7 @@ class EnvironmentTestCase(unittest.TestCase): [], [''], [''], [], UL(['']), UL(['']), - UL([]), 'O1', UL(['O1']), + UL([]), 'O1', UL(['O', '1']), UL([]), ['O2'], UL(['O2']), UL([]), UL(['O3']), UL(['O3']), UL([]), '', UL([]), @@ -1342,7 +1342,7 @@ class EnvironmentTestCase(unittest.TestCase): [''], [''], ['', ''], [''], UL(['']), UL(['', '']), - UL(['']), 'Q1', UL(['Q1', '']), + UL(['']), 'Q1', UL(['Q', '1', '']), UL(['']), ['Q2'], UL(['Q2', '']), UL(['']), UL(['Q3']), UL(['Q3', '']), UL(['']), '', UL(['']), |