forked from kylederkacz/lettuce-farmer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LettuceFarmer.py
307 lines (247 loc) · 9.65 KB
/
LettuceFarmer.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import threading
import os
import sys
import re
import sublime
import sublime_plugin
__file__ = os.path.normpath(os.path.abspath(__file__))
__path__ = os.path.dirname(__file__)
libs_path = os.path.join(__path__, 'lib')
if libs_path not in sys.path:
sys.path.insert(0, libs_path)
import lettuce
LOADING_STATUS_LENGTH = 25
PYTHON_EXTENSION = 'py'
FEATURES_EXTENSION = 'feature'
STEP_PATTERN = re.compile(
"@step\(u?[\'\\\"](?P<step_regex>.*)[\'\\\"]\)([\s.]*)" + \
"(?P<step_function>def (?P<step_function_name>[\w\d\_]+)" + \
"\s*\((?P<step_function_args>[\w\d\,\= ]*)\))", re.MULTILINE)
FEATURE_REGISTRY = []
FEATURE_STEP_REGISTRY = {}
QUEUE = {}
ERROR_MARKS_KEY = 'lettuce.feature.marks'
class StepLoader(threading.Thread):
complete = 0
total_count = 0
load_count = 0
def __init__(self, folders, python_files=[], feature_files=[]):
self.folders = folders
self.python_files = python_files
self.feature_files = feature_files
lettuce.core.STEP_REGISTRY = {}
threading.Thread.__init__(self)
def run(self):
self.complete = 0
self.total_count = 0
self.load_count = 0
self.init_files()
self.load_steps()
self.load_feature_files()
def init_files(self):
for folder in self.folders:
for root, sub_folders, files in os.walk(folder):
for file in files:
if file not in self.python_files \
and file not in self.feature_files:
extension = _get_file_extension(file)
if extension == PYTHON_EXTENSION:
self.python_files.append(os.path.join(root, file))
elif extension == FEATURES_EXTENSION:
self.feature_files.append(os.path.join(root, file))
self.total_count = float(len(self.python_files)) + \
float(len(self.feature_files))
def load_steps(self):
'''
Scan python files in any projects or open files for @step decorators
used by lettuce.
'''
for file in self.python_files:
self.load_steps_in_file(file)
self.load_count += 1
self.complete = int(round((self.load_count / self.total_count) *
LOADING_STATUS_LENGTH))
@classmethod
def load_steps_in_file(cls, file):
ifile = open(file, 'r')
text = ifile.read()
ifile.close()
for step in STEP_PATTERN.finditer(text):
definition = step.group('step_regex')
params = {
'regex': definition,
'function_name': step.group('step_function_name'),
'function_args': step.group('step_function_args').split(', '),
}
def temp_func():
return params
if definition not in lettuce.core.STEP_REGISTRY.keys():
lettuce.core.STEP_REGISTRY[definition] = temp_func
def load_feature_files(self):
for file in self.feature_files:
StepLoader.load_features_in_file(file)
self.load_count += 1
self.complete = int(round(self.load_count / self.total_count) *
LOADING_STATUS_LENGTH)
@classmethod
def load_features_in_file(cls, file):
try:
feature = lettuce.core.Feature.from_file(file)
except:
return
for scenario in feature.scenarios:
for step in scenario.steps:
matched = False
try:
matched, definition = step.pre_run(True)
except lettuce.exceptions.NoDefinitionFound:
pass
if matched:
FEATURE_STEP_REGISTRY[step.sentence] = \
StepLoader.tokenize_sentence(step, matched)
@classmethod
def tokenize_sentence(cls, step, regex):
if regex.re.pattern in lettuce.core.STEP_REGISTRY:
params = lettuce.core.STEP_REGISTRY[regex.re.pattern]()
if 'step' in params['function_args']:
params['function_args'].remove('step')
token = regex.string
offset = 0
for i, match in enumerate(regex.groups()):
replace = "<" + params['function_args'][i] + ">"
start = regex.start(i + 1) + offset
end = regex.end(i + 1) + offset
token = token[:start] + replace + token[end:]
offset = len(replace) - (regex.end(i + 1) - regex.start(i + 1))
return token
else:
return step.sentence
class FeatureValidator(threading.Thread):
def __init__(self, text, id):
self.text = text
self.id = id
threading.Thread.__init__(self)
def run(self):
try:
feature = lettuce.core.Feature.from_string(self.text)
except:
return
for scenario in feature.scenarios:
for step in scenario.steps:
try:
matched, definition = step.pre_run(True)
except lettuce.exceptions.NoDefinitionFound:
QUEUE[self.id].append(step)
class LettuceFarmerEventListener(sublime_plugin.EventListener):
is_active = False
window = None
def __init__(self):
super(LettuceFarmerEventListener, self).__init__()
_reload_step_definitions(sublime.active_window())
def on_modified(self, view):
if _get_file_extension(view.file_name()) == FEATURES_EXTENSION:
_queue_validator(view)
def on_activated(self, view):
if _get_file_extension(view.file_name()) == FEATURES_EXTENSION:
_queue_validator(view)
self.is_active = True
else:
self.is_active = False
def on_post_save(self, view):
extension = _get_file_extension(view.file_name())
if extension == PYTHON_EXTENSION:
StepLoader.load_steps_in_file(view.file_name())
elif extension == FEATURES_EXTENSION:
StepLoader.load_features_in_file(view.file_name())
_queue_validator(view)
def on_query_completions(self, view, prefix, locations):
if self.is_active:
return ([(sentence, tokenized_sentence) \
for sentence, tokenized_sentence \
in FEATURE_STEP_REGISTRY.iteritems()],
sublime.INHIBIT_WORD_COMPLETIONS)
class LettuceFarmer(sublime_plugin.TextCommand):
def run(self, *args, **kwargs):
action = kwargs.get('action')
if action == 'reload':
def reload_result(result):
sublime.message_dialog(result)
_reload_step_definitions(sublime.active_window(), reload_result)
def _queue_validator(view, callback=None):
if view.id() in QUEUE.keys():
return
QUEUE[view.id()] = []
text = view.substr(sublime.Region(0, view.size())).encode('utf-8')
validator = FeatureValidator(text, view.id())
validator.start()
def _validator_callback():
if len(QUEUE[view.id()]):
_add_marks(QUEUE[view.id()], view)
del QUEUE[view.id()]
_handle_feature_validator(validator, callback=_validator_callback)
def _add_marks(steps, view):
_erase_marks(view)
regions = []
for step in steps:
if step.has_definition or len(step.sentence) < 3:
continue
if step.described_at.line:
line = step.described_at.line
lines = [view.full_line(view.text_point(line - 1, 0))]
else:
sentence = r"\s{%d}%s$" % (step.indentation, step.sentence)
matches = view.find_all(sentence)
lines = [view.full_line(region) for region in matches]
regions.extend(lines)
view.add_regions(ERROR_MARKS_KEY, regions, 'lettuce.feature.outline',
'dot', sublime.DRAW_OUTLINED)
def _erase_marks(view):
view.erase_regions(ERROR_MARKS_KEY)
def _handle_feature_validator(thread, callback=None):
next_thread = None
if thread.is_alive():
next_thread = thread
thread = next_thread
if thread:
sublime.set_timeout(lambda: _handle_feature_validator(thread, callback),
50)
return
if callback:
callback()
def _handle_step_loader(thread, window, callback=None):
next_thread = None
if thread.is_alive():
next_thread = thread
status_key = 'LettuceFarmer.loading_steps'
thread = next_thread
if thread:
window.active_view().set_status(status_key,
'Loading steps [%s%s]' % (
"=" * thread.complete,
" " * int(LOADING_STATUS_LENGTH - thread.complete)))
sublime.set_timeout(lambda: _handle_step_loader(thread, window,
callback), 50)
return
window.active_view().erase_status(status_key)
if callback:
callback('Successfully loaded %d step definitions and %d steps.' %
(len(lettuce.core.STEP_REGISTRY),
len(FEATURE_STEP_REGISTRY.keys())))
def _reload_step_definitions(window, callback=None):
_python_files_to_scan = []
_feature_files_to_scan = []
for view in window.views():
extension = _get_file_extension(view.file_name())
if extension == PYTHON_EXTENSION:
_python_files_to_scan.append(view.file_name())
elif extension == FEATURES_EXTENSION:
_feature_files_to_scan.append(view.file_name())
step_loader = StepLoader(window.folders(),
python_files=_python_files_to_scan,
feature_files=_feature_files_to_scan)
step_loader.start()
_handle_step_loader(step_loader, window, callback)
def _get_file_extension(file_name):
if not file_name:
return
return file_name.split(".")[-1]