6 class BoolAdapter(object):
14 raise Exception("Can't convert `%s`" % s
)
20 class ListAdapter(object):
22 return [x
.lstrip() for x
in s
.split(',')]
24 class MethodCall(object):
25 def __init__(self
, name
):
28 def apply(self
, fixture
, cell
, adapters
):
29 f
= getattr(fixture
, self
.name
)
32 raise Exception('returned None')
35 target_type
= type(actual
)
36 if adapters
.has_key(target_type
):
37 adapter
= adapters
[target_type
]
38 expected
= adapter
.convert(value
)
40 expected
= type(actual
)(str(cell
))
42 if expected
== actual
:
47 class SetAttribute(object):
48 def __init__(self
, name
):
50 def apply(self
, fixture
, cell
, adapters
):
53 old
= getattr(fixture
, self
.name
)
54 target_type
= type(old
)
55 cell_value
= str(cell
)
56 if adapters
.has_key(target_type
):
57 adapter
= adapters
[target_type
]
58 new
= adapter
.parse(cell_value
)
60 new
= type(old
)(str(cell
))
61 setattr(fixture
, self
.name
, new
)
63 except AttributeError, inst
:
66 def parse_action(action_desc
):
67 res
= re
.search('(.*)\(\)', action_desc
)
69 action_name
= res
.group(1)
70 action_name
= string
.replace(action_name
, ' ', '_')
71 #return MethodCall(res.group(1))
72 return MethodCall(action_name
)
74 return SetAttribute(action_desc
)
76 class FileAdapter(object):
77 def __init__(self
, data
):
82 block
= self
.data
[self
.offset
:end
]
86 return self
.offset
< len(self
.data
)
88 def format_10_digit_number(n
):
91 def wiki_table_to_html(table
):
93 for line
in table
.split('\n'):
94 row
= line
.lstrip().split('|')[1:-1]
98 output
+= "<td>%s</td>\n" % cell
.lstrip().rstrip()
111 class RowIter(object):
112 def __init__(self
, iter):
117 return self
.iter.next()
120 for i
in range(0, n
):
121 result
.append(str(self
.next()))
124 def print_table(table
):
127 print '|' + '|'.join([str(x
) for x
in row
]) + '|'
129 class ImportError(Exception):
130 def __init__(self
, value
):
133 return repr(self
.value
)
135 class Importer(object):
136 def do_import_module(self
, module_name
):
138 #__import__(module_name)
140 def import_module(self
, name
):
142 self
.do_import_module(name
)
144 tb
= traceback
.format_exc()
145 lines
= tb
.split('\n')
146 str = '\n' + lines
[0] + '\n' + '\n'.join(lines
[5:])
147 raise ImportError(str)
149 class CreateFixture(object):
150 def __init__(self
, globals):
151 self
.globals = globals
153 def load(self
, name
):
155 type = self
.globals[name
]
156 except KeyError, inst
:
157 raise Exception("Could not create fixture '%s'" % name
)
159 fixture
.adapters
= {}
162 def add_to_python_path(path
):
163 paths
= path
.split(':')[:-3] # remove classes:fitnesse.jar:fitlibrary.jar'
164 sys
.path
.extend(paths
)
166 def DefaultAdapters():
168 adapters
[bool] = BoolAdapter()
169 adapters
[list] = ListAdapter()