4 from qa.Config import Config
5 from qa.logger import log
9 modules_path = os.path.realpath(qa.modules.__path__[0])
13 def __init__(self, globals = globals(), config = None, logging=None):
14 if config is None: self.config = Config()
15 else: self.config = Config(config)
18 real_files = lambda name: not name.startswith('__init__') \
19 and name.endswith('.py')
20 remove_ext = lambda name: name.split(".py")[0]
21 iterator = os.walk(self.modules_path)
22 (root, basenames, files) = iterator.next()
24 self.methods.extend([method_base+file for file in map(remove_ext, filter(real_files, files))])
25 for (root, dirs, files) in iterator:
26 parts = root.split(os.sep)
27 for basename in basenames:
29 method_base = ".".join(parts[parts.index(basename):])+"."
30 files = filter(real_files, files)
31 files = map(remove_ext, files)
32 self.methods.extend([method_base+file for file in files])
34 # Add methods to self and global environment
35 for method in self.methods:
36 callable = self.callable(method)(self.config)
37 if logging: callable = log(callable, method)
38 elif hasattr(self.config, 'log') and self.config.log:
39 callable = log(callable, method)
42 paths = method.split(".")
46 if not hasattr(self, first):
48 setattr(self, first, obj)
49 # Also add to global environment if specified
50 if globals is not None:
53 obj = getattr(self, first)
56 if not hasattr(obj, path):
58 setattr(obj, path, callable)
61 setattr(obj, path, Dummy())
62 obj = getattr(obj, path)
64 if not hasattr(self, method):
65 setattr(self, method, callable)
66 if globals is not None:
67 globals[method] = callable
70 def callable(self, method):
72 Return a new instance of the specified method.
76 if method not in self.methods:
77 raise Exception, "Invalid method: %s" % method
79 # Get new instance of method
81 #classname = method.split(".")[-1]
82 module_name = "qa.modules."+method
83 module = __import__(module_name, globals(), locals(), module_name)
84 components = module_name.split('.')
85 module = getattr(module, components[-1:][0])
87 except ImportError, AttributeError: