initial checkin of qa api
[tests.git] / qaapi / qa / QAAPI.py
1 import sys, os
2 import traceback
3 import qa.modules
4 from qa.Config import Config
5 from qa.logger import log       
6
7 class QAAPI:
8    
9     modules_path = os.path.realpath(qa.modules.__path__[0])
10     methods = []
11                 
12         
13     def __init__(self, globals = globals(), config = None, logging=None):
14         if config is None: self.config = Config()
15         else: self.config = Config(config)
16
17         # Load methods
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()
23         method_base = ""
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:
28                 if basename in parts:
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]) 
33
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)
40             
41             class Dummy: pass
42             paths = method.split(".")
43             if len(paths) > 1:
44                 first = paths.pop(0)
45
46                 if not hasattr(self, first):
47                     obj = Dummy()
48                     setattr(self, first, obj)
49                     # Also add to global environment if specified
50                     if globals is not None:
51                         globals[first] = obj
52
53                 obj = getattr(self, first)
54
55                 for path in paths:
56                     if not hasattr(obj, path):
57                         if path == paths[-1]:
58                             setattr(obj, path, callable)
59                             globals[method]=obj  
60                         else:
61                             setattr(obj, path, Dummy())
62                     obj = getattr(obj, path)
63             else:
64                 if not hasattr(self, method):
65                     setattr(self, method, callable)
66                 if globals is not None:
67                     globals[method] = callable          
68                 
69
70     def callable(self, method):
71         """
72         Return a new instance of the specified method. 
73         """      
74         
75         # Look up test  
76         if method not in self.methods:
77             raise Exception, "Invalid method: %s" % method
78
79         # Get new instance of method
80         try:
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])        
86             return module
87         except ImportError, AttributeError:
88             raise  
89          
90