Commits

Steven Knight  committed 299289e

Eliminate if-tests for Types in Builder.execute().

  • Participants
  • Parent commits 06322cd

Comments (0)

Files changed (2)

File src/scons/Builder.py

 
 
 import os
-from types import *
+import types
 from scons.Node.FS import Dir, File, lookup
 
 
 			output_suffix = None,
 			node_class = File):
 	self.name = name
-	self.action = action
+	self.action = Action(action)
 	self.insuffix = input_suffix
 	self.outsuffix = output_suffix
 	self.node_class = node_class
     def execute(self, **kw):
 	"""Execute a builder's action to create an output object.
 	"""
-	# XXX THIS SHOULD BE DONE BY TURNING Builder INTO A FACTORY
-	# FOR SUBCLASSES FOR StringType AND FunctionType
-	t = type(self.action)
-	if t == StringType:
-	    cmd = self.action % kw
-	    print cmd
-	    os.system(cmd)
-	elif t == FunctionType:
-	    # XXX WHAT SHOULD WE PRINT HERE
-	    self.action(kw)
+	apply(self.action.execute, (), kw)
+
+
+
+def Action(act):
+    """A factory for action objects."""
+    if type(act) == types.FunctionType:
+	return FunctionAction(act)
+    elif type(act) == types.StringType:
+	return CommandAction(act)
+    else:
+	return None
+
+class ActionBase:
+    """Base class for actions that create output objects.
+    
+    We currently expect Actions will only be accessible through
+    Builder objects, so they don't yet merit their own module."""
+    def __cmp__(self, other):
+	return cmp(self.__dict__, other.__dict__)
+
+    def show(self, string):
+	print string
+
+class CommandAction(ActionBase):
+    """Class for command-execution actions."""
+    def __init__(self, string):
+	self.command = string
+
+    def execute(self, **kw):
+	cmd = self.command % kw
+	self.show(cmd)
+	os.system(cmd)
+
+class FunctionAction(ActionBase):
+    """Class for Python function actions."""
+    def __init__(self, function):
+	self.function = function
+
+    def execute(self, **kw):
+	# XXX:  WHAT SHOULD WE PRINT HERE?
+	self.function(kw)

File src/scons/BuilderTests.py

 import sys
 import unittest
 
-from scons.Builder import Builder
-from TestCmd import TestCmd
+import TestCmd
+from scons.Builder import Builder, CommandAction, FunctionAction
 
 
 # Initial setup of the common environment for all tests,
 # We don't do this as a setUp() method because it's
 # unnecessary to create a separate directory and script
 # for each test, they can just use the one.
-test = TestCmd(workdir = '')
+test = TestCmd.TestCmd(workdir = '')
 
 test.write('act.py', """import os, string, sys
 f = open(sys.argv[1], 'w')
 	Verify that we can retrieve the supplied action attribute.
 	"""
 	builder = Builder(action = "foo")
-	assert builder.action == "foo"
+	assert builder.action.command == "foo"
 
     def test_cmp(self):
 	"""Test simple comparisons of Builder objects