-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
164 lines (141 loc) · 4.24 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import re
import sys
import traceback
import string
from inspect import *
class BoolAdapter(object):
def parse(self, s):
if s == 'true':
value = True
else:
if s == 'false':
value = False
else:
raise Exception("Can't convert `%s`" % s)
return value
def convert(self, s):
return s == 'true'
class ListAdapter(object):
def parse(self, s):
return [x.lstrip() for x in s.split(',')]
class MethodCall(object):
def __init__(self, name):
self.name = name
def apply(self, fixture, cell, engine):
f = getattr(fixture, self.name)
actual = f()
if actual is None:
raise Exception('returned None')
engine.compare(cell, actual)
class SetAttribute(object):
def __init__(self, name):
self.name = name
def apply(self, fixture, cell, engine):
try:
attribute = getattr(fixture, self.name)
if ismethod(attribute):
cell_value = str(cell)
attribute(cell_value)
else:
target_type = type(attribute)
cell_value = str(cell)
if engine.adapters.has_key(target_type):
adapter = engine.adapters[target_type]
new = adapter.parse(cell_value)
else:
new = type(attribute)(str(cell))
setattr(fixture, self.name, new)
except AttributeError, inst:
cell.error(str(inst))
raise
def parse_action(action_desc):
res = re.search('(.*)\(\)', action_desc)
if res is not None:
action_name = res.group(1)
action_name = string.replace(action_name, ' ', '_')
return MethodCall(action_name)
else:
return SetAttribute(action_desc)
class FileAdapter(object):
def __init__(self, data):
self.data = data
self.offset = 0
def read(self, n):
end = self.offset + n
block = self.data[self.offset:end]
self.offset = end
return block
def eof(self):
return self.offset >= len(self.data)
def format_10_digit_number(n):
return "%010i" % n
def wiki_table_to_html(table):
output = "<table>"
for line in table.split('\n'):
row = line.lstrip().split('|')[1:-1]
if len(row):
output += "<tr>"
for cell in row:
output += "<td>%s</td>\n" % cell.lstrip().rstrip()
output += "</tr>\n"
output += "</table>"
return output
def rzip(a,b):
prev = ''
for x,y in zip(a,b):
if x == '':
x = prev
prev = x
yield (x,y)
class RowIter(object):
def __init__(self, iter):
self.iter = iter
def __iter__(self):
return self
def next(self):
return self.iter.next()
def get(self, n):
result = []
for i in range(0, n):
result.append(str(self.next()))
return result
def print_table(table):
print
for row in table:
print '|' + '|'.join([str(x) for x in row]) + '|'
class ImportError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class Importer(object):
def do_import_module(self, module_name):
pass
#__import__(module_name)
def import_module(self, name):
try:
self.do_import_module(name)
except Exception, e:
tb = traceback.format_exc()
lines = tb.split('\n')
str = '\n' + lines[0] + '\n' + '\n'.join(lines[5:])
raise ImportError(str)
class CreateFixture(object):
def __init__(self, globals):
self.globals = globals
def load(self, name):
try:
type = self.globals[name]
except KeyError, inst:
raise Exception("Could not create fixture '%s'" % name)
fixture = type()
#fixture.adapters = {}
return fixture
def add_to_python_path(path):
paths = path.split(':')[:-3] # remove classes:fitnesse.jar:fitlibrary.jar'
sys.path.extend(paths)
def DefaultAdapters():
adapters = {}
adapters[bool] = BoolAdapter()
adapters[list] = ListAdapter()
return adapters