diff options
Diffstat (limited to 'src/engine/SCons/Scanner')
| -rw-r--r-- | src/engine/SCons/Scanner/Dir.py | 2 | ||||
| -rw-r--r-- | src/engine/SCons/Scanner/ScannerTests.py | 64 | ||||
| -rw-r--r-- | src/engine/SCons/Scanner/__init__.py | 48 |
3 files changed, 99 insertions, 15 deletions
diff --git a/src/engine/SCons/Scanner/Dir.py b/src/engine/SCons/Scanner/Dir.py index fb23d1b..535150a 100644 --- a/src/engine/SCons/Scanner/Dir.py +++ b/src/engine/SCons/Scanner/Dir.py @@ -23,8 +23,6 @@ __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" -import string - import SCons.Node.FS import SCons.Scanner diff --git a/src/engine/SCons/Scanner/ScannerTests.py b/src/engine/SCons/Scanner/ScannerTests.py index bd8546f..30dc1df 100644 --- a/src/engine/SCons/Scanner/ScannerTests.py +++ b/src/engine/SCons/Scanner/ScannerTests.py @@ -82,29 +82,37 @@ class FindPathDirsTestCase(unittest.TestCase): class ScannerTestCase(unittest.TestCase): def test_creation(self): - """Test creation of Scanner objects through the Scanner() function""" + """Test creation of Scanner objects""" def func(self): pass - s = SCons.Scanner.Scanner(func) + s = SCons.Scanner.Base(func) + assert isinstance(s, SCons.Scanner.Base), s + s = SCons.Scanner.Base({}) assert isinstance(s, SCons.Scanner.Base), s - s = SCons.Scanner.Scanner({}) - assert isinstance(s, SCons.Scanner.Selector), s - s = SCons.Scanner.Scanner(func, name='fooscan') + s = SCons.Scanner.Base(func, name='fooscan') assert str(s) == 'fooscan', str(s) - s = SCons.Scanner.Scanner({}, name='barscan') + s = SCons.Scanner.Base({}, name='barscan') assert str(s) == 'barscan', str(s) - s = SCons.Scanner.Scanner(func, name='fooscan', argument=9) + s = SCons.Scanner.Base(func, name='fooscan', argument=9) assert str(s) == 'fooscan', str(s) assert s.argument == 9, s.argument - s = SCons.Scanner.Scanner({}, name='fooscan', argument=888) + s = SCons.Scanner.Base({}, name='fooscan', argument=888) assert str(s) == 'fooscan', str(s) assert s.argument == 888, s.argument class BaseTestCase(unittest.TestCase): + class skey_node: + def __init__(self, key): + self.key = key + def scanner_key(self): + return self.key + def rexists(self): + return 1 + def func(self, filename, env, target, *args): self.filename = filename self.env = env @@ -132,6 +140,29 @@ class BaseTestCase(unittest.TestCase): else: self.failIf(hasattr(self, "arg"), "an argument was given when it shouldn't have been") + def test___call__dict(self): + """Test calling Scanner.Base objects with a dictionary""" + called = [] + def s1func(node, env, path, called=called): + called.append('s1func') + called.append(node) + return [] + def s2func(node, env, path, called=called): + called.append('s2func') + called.append(node) + return [] + s1 = SCons.Scanner.Base(s1func) + s2 = SCons.Scanner.Base(s2func) + selector = SCons.Scanner.Base({'.x' : s1, '.y' : s2}) + nx = self.skey_node('.x') + env = DummyEnvironment() + selector(nx, env, []) + assert called == ['s1func', nx], called + del called[:] + ny = self.skey_node('.y') + selector(ny, env, []) + assert called == ['s2func', ny], called + def test_path(self): """Test the Scanner.Base path() method""" def pf(env, cwd, target, source, argument=None): @@ -277,6 +308,23 @@ class BaseTestCase(unittest.TestCase): s = scanner.select('.x') assert s is scanner, s + selector = SCons.Scanner.Base({'.x' : 1, '.y' : 2}) + s = selector.select(self.skey_node('.x')) + assert s == 1, s + s = selector.select(self.skey_node('.y')) + assert s == 2, s + s = selector.select(self.skey_node('.z')) + assert s is None, s + + def test_add_scanner(self): + """Test the Scanner.Base add_scanner() method""" + selector = SCons.Scanner.Base({'.x' : 1, '.y' : 2}) + s = selector.select(self.skey_node('.z')) + assert s is None, s + selector.add_scanner('.z', 3) + s = selector.select(self.skey_node('.z')) + assert s == 3, s + def test___str__(self): """Test the Scanner.Base __str__() method""" scanner = SCons.Scanner.Base(function = self.func) diff --git a/src/engine/SCons/Scanner/__init__.py b/src/engine/SCons/Scanner/__init__.py index 679efca..db93f61 100644 --- a/src/engine/SCons/Scanner/__init__.py +++ b/src/engine/SCons/Scanner/__init__.py @@ -45,9 +45,17 @@ class _Null: _null = _Null def Scanner(function, *args, **kw): - """Public interface factory function for creating different types + """ + Public interface factory function for creating different types of Scanners based on the different types of "functions" that may - be supplied.""" + be supplied. + + TODO: Deprecate this some day. We've moved the functionality + inside the Base class and really don't need this factory function + any more. It was, however, used by some of our Tool modules, so + the call probably ended up in various people's custom modules + patterned on SCons code. + """ if SCons.Util.is_Dict(function): return apply(Selector, (function,) + args, kw) else: @@ -83,7 +91,7 @@ class Base: function, name = "NONE", argument = _null, - skeys = [], + skeys = _null, path_function = None, node_class = SCons.Node.FS.Entry, node_factory = None, @@ -159,7 +167,14 @@ class Base: self.path_function = path_function self.name = name self.argument = argument + + if skeys is _null: + if SCons.Util.is_Dict(function): + skeys = function.keys() + else: + skeys = [] self.skeys = skeys + self.node_class = node_class self.node_factory = node_factory self.scan_check = scan_check @@ -188,10 +203,13 @@ class Base: if self.scan_check and not self.scan_check(node, env): return [] + self = self.select(node) + if not self.argument is _null: list = self.function(node, env, path, self.argument) else: list = self.function(node, env, path) + kw = {} if hasattr(node, 'dir'): kw['directory'] = node.dir @@ -221,12 +239,19 @@ class Base: self.skeys.append(skey) def get_skeys(self, env=None): - if SCons.Util.is_String(self.skeys): + if env and SCons.Util.is_String(self.skeys): return env.subst_list(self.skeys)[0] return self.skeys def select(self, node): - return self + if SCons.Util.is_Dict(self.function): + key = node.scanner_key() + try: + return self.function[key] + except KeyError: + return None + else: + return self def _recurse_all_nodes(self, nodes): return nodes @@ -236,15 +261,27 @@ class Base: recurse_nodes = _recurse_no_nodes + def add_scanner(self, skey, scanner): + self.function[skey] = scanner + self.add_skey(skey) + class Selector(Base): """ A class for selecting a more specific scanner based on the scanner_key() (suffix) for a specific Node. + + TODO: This functionality has been moved into the inner workings of + the Base class, and this class will be deprecated at some point. + (It was never exposed directly as part of the public interface, + although it is used by the Scanner() factory function that was + used by various Tool modules and therefore was likely a template + for custom modules that may be out there.) """ def __init__(self, dict, *args, **kw): apply(Base.__init__, (self, None,)+args, kw) self.dict = dict + self.skeys = dict.keys() def __call__(self, node, env, path = ()): return self.select(node)(node, env, path) @@ -257,6 +294,7 @@ class Selector(Base): def add_scanner(self, skey, scanner): self.dict[skey] = scanner + self.add_skey(skey) class Current(Base): |
