summaryrefslogtreecommitdiff
path: root/src/engine/SCons/Subst.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/SCons/Subst.py')
-rw-r--r--src/engine/SCons/Subst.py103
1 files changed, 48 insertions, 55 deletions
diff --git a/src/engine/SCons/Subst.py b/src/engine/SCons/Subst.py
index fe18f08..d9b029a 100644
--- a/src/engine/SCons/Subst.py
+++ b/src/engine/SCons/Subst.py
@@ -25,15 +25,11 @@ SCons string substitution.
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-#
-__revision__ = "src/engine/SCons/Subst.py 4720 2010/03/24 03:14:11 jars"
+__revision__ = "src/engine/SCons/Subst.py 5023 2010/06/14 22:05:46 scons"
+import collections
import re
-import string
-import types
-import UserList
-import UserString
import SCons.Errors
@@ -50,19 +46,19 @@ AllowableExceptions = (IndexError, NameError)
def SetAllowableExceptions(*excepts):
global AllowableExceptions
- AllowableExceptions = filter(None, excepts)
+ AllowableExceptions = [_f for _f in excepts if _f]
def raise_exception(exception, target, s):
name = exception.__class__.__name__
msg = "%s `%s' trying to evaluate `%s'" % (name, exception, s)
if target:
- raise SCons.Errors.BuildError, (target[0], msg)
+ raise SCons.Errors.BuildError(target[0], msg)
else:
- raise SCons.Errors.UserError, msg
+ raise SCons.Errors.UserError(msg)
-class Literal:
+class Literal(object):
"""A wrapper for a string. If you use this object wrapped
around a string, then it will be interpreted as literal.
When passed to the command interpreter, all special
@@ -82,7 +78,7 @@ class Literal:
def is_literal(self):
return 1
-class SpecialAttrWrapper:
+class SpecialAttrWrapper(object):
"""This is a wrapper for what we call a 'Node special attribute.'
This is any of the attributes of a Node that we can reference from
Environment variable substitution, such as $TARGET.abspath or
@@ -121,7 +117,7 @@ def quote_spaces(arg):
else:
return str(arg)
-class CmdStringHolder(UserString.UserString):
+class CmdStringHolder(collections.UserString):
"""This is a special class used to hold strings generated by
scons_subst() and scons_subst_list(). It defines a special method
escape(). When passed a function with an escape algorithm for a
@@ -129,7 +125,7 @@ class CmdStringHolder(UserString.UserString):
proper escape sequences inserted.
"""
def __init__(self, cmd, literal=None):
- UserString.UserString.__init__(self, cmd)
+ collections.UserString.__init__(self, cmd)
self.literal = literal
def is_literal(self):
@@ -152,7 +148,7 @@ class CmdStringHolder(UserString.UserString):
else:
return self.data
-def escape_list(list, escape_func):
+def escape_list(mylist, escape_func):
"""Escape a list of arguments by running the specified escape_func
on every object in the list that has an escape() method."""
def escape(obj, escape_func=escape_func):
@@ -162,9 +158,9 @@ def escape_list(list, escape_func):
return obj
else:
return e(escape_func)
- return map(escape, list)
+ return list(map(escape, mylist))
-class NLWrapper:
+class NLWrapper(object):
"""A wrapper class that delays turning a list of sources or targets
into a NodeList until it's needed. The specified function supplied
when the object is initialized is responsible for turning raw nodes
@@ -183,28 +179,28 @@ class NLWrapper:
def _return_nodelist(self):
return self.nodelist
def _gen_nodelist(self):
- list = self.list
- if list is None:
- list = []
- elif not is_Sequence(list):
- list = [list]
+ mylist = self.list
+ if mylist is None:
+ mylist = []
+ elif not is_Sequence(mylist):
+ mylist = [mylist]
# The map(self.func) call is what actually turns
# a list into appropriate proxies.
- self.nodelist = SCons.Util.NodeList(map(self.func, list))
+ self.nodelist = SCons.Util.NodeList(list(map(self.func, mylist)))
self._create_nodelist = self._return_nodelist
return self.nodelist
_create_nodelist = _gen_nodelist
-class Targets_or_Sources(UserList.UserList):
+class Targets_or_Sources(collections.UserList):
"""A class that implements $TARGETS or $SOURCES expansions by in turn
wrapping a NLWrapper. This class handles the different methods used
to access the list, calling the NLWrapper to create proxies on demand.
- Note that we subclass UserList.UserList purely so that the
+ Note that we subclass collections.UserList purely so that the
is_Sequence() function will identify an object of this class as
a list during variable expansion. We're not really using any
- UserList.UserList methods in practice.
+ collections.UserList methods in practice.
"""
def __init__(self, nl):
self.nl = nl
@@ -225,7 +221,7 @@ class Targets_or_Sources(UserList.UserList):
nl = self.nl._create_nodelist()
return repr(nl)
-class Target_or_Source:
+class Target_or_Source(object):
"""A class that implements $TARGET or $SOURCE expansions by in turn
wrapping a NLWrapper. This class handles the different methods used
to access an individual proxy Node, calling the NLWrapper to create
@@ -240,7 +236,7 @@ class Target_or_Source:
except IndexError:
# If there is nothing in the list, then we have no attributes to
# pass through, so raise AttributeError for everything.
- raise AttributeError, "NodeList has no attribute: %s" % attr
+ raise AttributeError("NodeList has no attribute: %s" % attr)
return getattr(nl0, attr)
def __str__(self):
nl = self.nl._create_nodelist()
@@ -256,9 +252,6 @@ class Target_or_Source:
class NullNodeList(SCons.Util.NullSeq):
def __call__(self, *args, **kwargs): return ''
def __str__(self): return ''
- # TODO(1.5): unneeded after new-style classes introduce iterators
- def __getitem__(self, i):
- raise IndexError
NullNodesList = NullNodeList()
@@ -344,7 +337,7 @@ _regex_remove = [ _rm, None, _remove ]
def _rm_list(list):
#return [ l for l in list if not l in ('$(', '$)') ]
- return filter(lambda l: not l in ('$(', '$)'), list)
+ return [l for l in list if not l in ('$(', '$)')]
def _remove_list(list):
result = []
@@ -399,10 +392,10 @@ def scons_subst(strSubst, env, mode=SUBST_RAW, target=None, source=None, gvars={
handles separating command lines into lists of arguments, so see
that function if that's what you're looking for.
"""
- if type(strSubst) == types.StringType and string.find(strSubst, '$') < 0:
+ if isinstance(strSubst, str) and strSubst.find('$') < 0:
return strSubst
- class StringSubber:
+ class StringSubber(object):
"""A class to construct the results of a scons_subst() call.
This binds a specific construction environment, mode, target and
@@ -438,7 +431,7 @@ def scons_subst(strSubst, env, mode=SUBST_RAW, target=None, source=None, gvars={
return s
else:
key = s[1:]
- if key[0] == '{' or string.find(key, '.') >= 0:
+ if key[0] == '{' or key.find('.') >= 0:
if key[0] == '{':
key = key[1:-1]
try:
@@ -450,9 +443,9 @@ def scons_subst(strSubst, env, mode=SUBST_RAW, target=None, source=None, gvars={
return ''
raise_exception(e, lvars['TARGETS'], s)
else:
- if lvars.has_key(key):
+ if key in lvars:
s = lvars[key]
- elif self.gvars.has_key(key):
+ elif key in self.gvars:
s = self.gvars[key]
elif not NameError in AllowableExceptions:
raise_exception(NameError(key), lvars['TARGETS'], s)
@@ -472,13 +465,13 @@ def scons_subst(strSubst, env, mode=SUBST_RAW, target=None, source=None, gvars={
# are probably more the exception than the norm,
# so it should be tolerable for now.
lv = lvars.copy()
- var = string.split(key, '.')[0]
+ var = key.split('.')[0]
lv[var] = ''
return self.substitute(s, lv)
elif is_Sequence(s):
def func(l, conv=self.conv, substitute=self.substitute, lvars=lvars):
return conv(substitute(l, lvars))
- return map(func, s)
+ return list(map(func, s))
elif callable(s):
try:
s = s(target=lvars['TARGETS'],
@@ -507,8 +500,8 @@ def scons_subst(strSubst, env, mode=SUBST_RAW, target=None, source=None, gvars={
if is_String(args) and not isinstance(args, CmdStringHolder):
args = str(args) # In case it's a UserString.
try:
- def sub_match(match, conv=self.conv, expand=self.expand, lvars=lvars):
- return conv(expand(match.group(1), lvars))
+ def sub_match(match):
+ return self.conv(self.expand(match.group(1), lvars))
result = _dollar_exps.sub(sub_match, args)
except TypeError:
# If the internal conversion routine doesn't return
@@ -523,7 +516,7 @@ def scons_subst(strSubst, env, mode=SUBST_RAW, target=None, source=None, gvars={
if len(result) == 1:
result = result[0]
else:
- result = string.join(map(str, result), '')
+ result = ''.join(map(str, result))
return result
else:
return self.expand(args, lvars)
@@ -540,7 +533,7 @@ def scons_subst(strSubst, env, mode=SUBST_RAW, target=None, source=None, gvars={
# If we dropped that behavior (or found another way to cover it),
# we could get rid of this call completely and just rely on the
# Executor setting the variables.
- if not lvars.has_key('TARGET'):
+ if 'TARGET' not in lvars:
d = subst_dict(target, source)
if d:
lvars = lvars.copy()
@@ -571,7 +564,7 @@ def scons_subst(strSubst, env, mode=SUBST_RAW, target=None, source=None, gvars={
if mode != SUBST_RAW:
# Compress strings of white space characters into
# a single space.
- result = string.strip(_space_sep.sub(' ', result))
+ result = _space_sep.sub(' ', result).strip()
elif is_Sequence(result):
remove = _list_remove[mode]
if remove:
@@ -595,7 +588,7 @@ def scons_subst_list(strSubst, env, mode=SUBST_RAW, target=None, source=None, gv
# Subst_List_Strings[strSubst] = 1
# import SCons.Debug
# SCons.Debug.caller_trace(1)
- class ListSubber(UserList.UserList):
+ class ListSubber(collections.UserList):
"""A class to construct the results of a scons_subst_list() call.
Like StringSubber, this class binds a specific construction
@@ -612,16 +605,16 @@ def scons_subst_list(strSubst, env, mode=SUBST_RAW, target=None, source=None, gv
internally.
"""
def __init__(self, env, mode, conv, gvars):
- UserList.UserList.__init__(self, [])
+ collections.UserList.__init__(self, [])
self.env = env
self.mode = mode
self.conv = conv
self.gvars = gvars
if self.mode == SUBST_RAW:
- self.add_strip = lambda x, s=self: s.append(x)
+ self.add_strip = lambda x: self.append(x)
else:
- self.add_strip = lambda x, s=self: None
+ self.add_strip = lambda x: None
self.in_strip = None
self.next_line()
@@ -653,7 +646,7 @@ def scons_subst_list(strSubst, env, mode=SUBST_RAW, target=None, source=None, gv
self.close_strip('$)')
else:
key = s[1:]
- if key[0] == '{' or string.find(key, '.') >= 0:
+ if key[0] == '{' or key.find('.') >= 0:
if key[0] == '{':
key = key[1:-1]
try:
@@ -665,9 +658,9 @@ def scons_subst_list(strSubst, env, mode=SUBST_RAW, target=None, source=None, gv
return
raise_exception(e, lvars['TARGETS'], s)
else:
- if lvars.has_key(key):
+ if key in lvars:
s = lvars[key]
- elif self.gvars.has_key(key):
+ elif key in self.gvars:
s = self.gvars[key]
elif not NameError in AllowableExceptions:
raise_exception(NameError(), lvars['TARGETS'], s)
@@ -680,7 +673,7 @@ def scons_subst_list(strSubst, env, mode=SUBST_RAW, target=None, source=None, gv
# string for the value of the variable name
# we just expanded.
lv = lvars.copy()
- var = string.split(key, '.')[0]
+ var = key.split('.')[0]
lv[var] = ''
self.substitute(s, lv, 0)
self.this_word()
@@ -735,7 +728,7 @@ def scons_subst_list(strSubst, env, mode=SUBST_RAW, target=None, source=None, gv
"""Arrange for the next word to start a new line. This
is like starting a new word, except that we have to append
another line to the result."""
- UserList.UserList.append(self, [])
+ collections.UserList.append(self, [])
self.next_word()
def this_word(self):
@@ -834,7 +827,7 @@ def scons_subst_list(strSubst, env, mode=SUBST_RAW, target=None, source=None, gv
# If we dropped that behavior (or found another way to cover it),
# we could get rid of this call completely and just rely on the
# Executor setting the variables.
- if not lvars.has_key('TARGET'):
+ if 'TARGET' not in lvars:
d = subst_dict(target, source)
if d:
lvars = lvars.copy()
@@ -870,7 +863,7 @@ def scons_subst_once(strSubst, env, key):
We do this with some straightforward, brute-force code here...
"""
- if type(strSubst) == types.StringType and string.find(strSubst, '$') < 0:
+ if isinstance(strSubst, str) and strSubst.find('$') < 0:
return strSubst
matchlist = ['$' + key, '${' + key + '}']
@@ -880,7 +873,7 @@ def scons_subst_once(strSubst, env, key):
if a in matchlist:
a = val
if is_Sequence(a):
- return string.join(map(str, a))
+ return ' '.join(map(str, a))
else:
return str(a)