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/SCons/Environment.py | |
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/SCons/Environment.py')
-rw-r--r-- | src/engine/SCons/Environment.py | 128 |
1 files changed, 92 insertions, 36 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' |