Added fit.ActionFixture and fixed the loader. Added ListAdapter.
[stuff.git] / pyfit / engines.py
blob1ad4dcc79fffa75911a01c125d8fcde50f6a8182
1 import traceback
2 from util import *
4 class DefaultLoader(object):
5 def load(self, name):
6 try:
7 module = self.do_load(name)
8 except ImportError, inst:
9 a = traceback.format_exc()
10 print '{\n%s}' % inst.value
11 names = name.split('.')[1:]
12 for name in names:
13 module = getattr(module, name)
14 class_ = getattr(module, name)
15 return class_()
17 def do_load(self, name):
18 return __import__(name)
20 class StringLoader(DefaultLoader):
21 def __init__(self, script):
22 self.script = script
23 def do_load(self, name):
24 x = compile(self.script, 'not_a_file.py', 'exec')
25 eval(x)
27 class Engine(object):
29 # return the next object in the flow or None.
30 # check if fixture has attribute with name of next table.
31 # if not create an instance with that name
32 def __init__(self):
33 self.loader = DefaultLoader()
34 self.fixture = None
35 self.print_traceback = False
36 self.adapters = DefaultAdapters()
38 def load_fixture(self, name):
39 if self.fixture is None:
40 self.fixture = self.loader.load(name)
41 else:
42 try:
43 f = getattr(self.fixture, name)
44 self.fixture = f()
45 except AttributeError, inst:
46 self.fixture = self.loader.load(name)
48 if self.fixture is None:
49 raise Exception("fixture '%s' not found." % name)
51 self.fixture.adapters = self.adapters
53 def do_process(self, table):
54 name = table.name()
55 self.load_fixture(name)
56 self.fixture.process(table)
58 def process(self, table, throw = True):
59 name = table.name()
61 if throw == True:
62 self.do_process(table)
63 else:
64 try:
65 self.do_process(table)
66 except Exception, inst:
67 '''Fixme: Should the rest of the table become grey?'''
69 table.cell(0,0).error(inst)
71 if self.print_traceback:
72 print 'Processing table `%s` failed' % table.name()
73 print '====='
74 print traceback.format_exc()
75 print '====='
77 return self.fixture