-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubz_commands_filter.py
309 lines (278 loc) · 13.7 KB
/
subz_commands_filter.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
308
309
import re
import sublime
import sublime_plugin
try:
from .subz_tools import *
except ValueError:
from subz_tools import *
class SubzPromptIonFilterToLinesCommand(sublime_plugin.WindowCommand):
def run(self, search_args = '::'):
text_to_find = get_latest_search()
if len(search_args) > 2:
text_to_find = search_args
sublime.active_window().show_input_panel('Filter ION (write h for help)', text_to_find, self.on_text_to_find_entered, None, None)
def on_text_to_find_entered(self, text_to_find):
set_latest_search(text_to_find)
sublime.save_settings("subz.sublime-settings")
if self.window.active_view():
self.window.active_view().run_command('subz_ion_filter_to_lines', { "arguments": text_to_find })
class SubzIonFilterToLinesCommand(sublime_plugin.TextCommand):
def run(self, edit, arguments):
search_type = 'string'
invert_search = False
text_to_find = ''
lines_to_keep = []
sections_to_filter = []
current_section = SectionType.Other
args = arguments.split(':', 2)
if len(args) == 3:
if 'r' in args[0]:
search_type = 'regex'
if 'd' in args[0]:
search_type = 'date'
if 'e' in args[0]:
invert_search = True
self.parse_section_args(args[1], sections_to_filter)
text_to_find = args[2]
view = self.view
sel = view.sel()
sel.clear()
region = sublime.Region(0, view.size());
sel.add(region)
for r in self.view.sel():
for line_r in self.view.lines(r):
line = self.view.substr(line_r)
current_section = self.process_line(line, search_type, invert_search, text_to_find, current_section, sections_to_filter, lines_to_keep)
text = '\n'.join(lines_to_keep)
self.create_new_tab(text)
else:
text = '\nWrong number of arguments passed\nRequired three groups of arguments separated by \":\" - search_arguments:section_arguments:text_to_search\nOptions:\n\tSearch arguments (can be separated by comma delimiter):\n\t\tr - regex search,\n\t\ts - string search (DEFAULT),\n\t\td - date search,\n\t\ti - lines including search text (DEFAULT),\n\t\te - lines excluding search text\n\tSections arguments (can be separated by comma delimiter):\n\t\tba - sections: RATE.BASE, RATE.SUPPLEMENT, RATE.DISCOUNT, RATE.RULE, RESTRICTION, DEF.ROOM,\n\t\tte - TEST,\n\t\tco - CONTRACT,\n\t\tdh - DEF.HOTEL,\n\t\tdm - DEF.MEAL,\n\t\tdr - DEF.ROOM,\n\t\trp - RATE.PLAN,\n\t\trb - RATE.BASE,\n\t\tru - RATE.RULE,\n\t\trs - RATE.SUPPLEMENT,\n\t\trd - RATE.DISCOUNT,\n\t\tdg - RATE.DISCOUNT_GROUP,\n\t\trr - RESTRICTION,\n\t\tqt - QUERY.TRANSFORM,\n\t\trc - RATE.CNX,\n\t\tta - TAX,\n\t\ttg - TAX_GROUP,\n\t\trm - RATE.MARKUP,\n\t\tab - AVL.BUCKET_STATE,\n\t\tas - AVL.STATE,\n\t\tai - AVL.INV,\nExample commands:\n\t:dr:P1:2 - search for lines including string P1:2 in DEF.ROOM section\n\tde:rb:20180101:20180110 - search for lines in RATE.BASE section which dose not contain passed date in section DATES column\n\tri:rsrd:P[1-2] - search lines matching regex in RATE.SUPPLEMENT and RATE.DISCOUNT sections'
self.create_new_tab(text)
def parse_section_args(self, arguments, sections_to_filter):
if 'te' in arguments:
sections_to_filter.append(SectionType.Test)
if 'co' in arguments:
sections_to_filter.append(SectionType.Contract)
if 'dh' in arguments:
sections_to_filter.append(SectionType.DefHotel)
if 'dm' in arguments:
sections_to_filter.append(SectionType.DefMeal)
if 'dr' in arguments:
sections_to_filter.append(SectionType.DefRoom)
if 'rp' in arguments:
sections_to_filter.append(SectionType.RatePlan)
if 'rb' in arguments:
sections_to_filter.append(SectionType.RateBase)
if 'ru' in arguments:
sections_to_filter.append(SectionType.RateRule)
if 'rs' in arguments:
sections_to_filter.append(SectionType.RateSupplement)
if 'rd' in arguments:
sections_to_filter.append(SectionType.RateDiscount)
if 'dg' in arguments:
sections_to_filter.append(SectionType.RateDiscountGroup)
if 'rr' in arguments:
sections_to_filter.append(SectionType.Restriction)
if 'qt' in arguments:
sections_to_filter.append(SectionType.QueryTransform)
if 'rc' in arguments:
sections_to_filter.append(SectionType.RateCnx)
if 'ta' in arguments:
sections_to_filter.append(SectionType.Tax)
if 'tg' in arguments:
sections_to_filter.append(SectionType.TaxGroup)
if 'rm' in arguments:
sections_to_filter.append(SectionType.RateMarkup)
if 'ab' in arguments:
sections_to_filter.append(SectionType.AvlBucketState)
if 'as' in arguments:
sections_to_filter.append(SectionType.AvlState)
if 'ai' in arguments:
sections_to_filter.append(SectionType.AvlInv)
if 'dc' in arguments:
sections_to_filter.append(SectionType.RateDiscountCat)
if 'sc' in arguments:
sections_to_filter.append(SectionType.RateSupplementalCat)
if 'cf' in arguments:
sections_to_filter.append(SectionType.Config)
if 'ci' in arguments:
sections_to_filter.append(SectionType.CustomInfo)
if 'ba' in arguments:
sections_to_filter.append(SectionType.RateSupplement)
sections_to_filter.append(SectionType.RateDiscount)
sections_to_filter.append(SectionType.RateRule)
sections_to_filter.append(SectionType.RateBase)
sections_to_filter.append(SectionType.Restriction)
sections_to_filter.append(SectionType.DefRoom)
def process_line(self, line, search_type, invert_search, text_to_find, current_section, sections_to_filter, lines_to_keep):
if current_section == SectionType.Other or current_section not in sections_to_filter:
return self.other_section(line, search_type, invert_search, text_to_find, lines_to_keep)
else:
return self.process_section(line, search_type, invert_search, text_to_find, current_section, lines_to_keep)
def other_section(self, line, search_type, invert_search, text_to_find, lines_to_keep):
lines_to_keep.append(line)
return self.get_section_type(line)
def get_section_type(self, line):
if "[TEST]" in line:
return SectionType.Test
elif "[CONTRACT]" in line:
return SectionType.Contract
elif "[DEF.HOTEL]" in line:
return SectionType.DefHotel
elif "[DEF.MEAL]" in line:
return SectionType.DefMeal
elif "[DEF.ROOM]" in line:
return SectionType.DefRoom
elif "[RATE.PLAN]" in line:
return SectionType.RatePlan
elif "[RATE.BASE]" in line:
return SectionType.RateBase
elif "[RATE.RULE]" in line:
return SectionType.RateRule
elif "[RATE.SUPPLEMENT]" in line:
return SectionType.RateSupplement
elif "[RATE.DISCOUNT]" in line:
return SectionType.RateDiscount
elif "[RATE.DISCOUNT_GROUP]" in line:
return SectionType.RateDiscountGroup
elif "[RATE.CNX]" in line:
return SectionType.RateCnx
elif "[TAX]" in line:
return SectionType.Tax
elif "[TAX_GROUP]" in line:
return SectionType.TaxGroup
elif "[RATE.MARKUP]" in line:
return SectionType.RateMarkup
elif "[RESTRICTION]" in line:
return SectionType.Restriction
elif "[QUERY.TRANSFORM]" in line:
return SectionType.QueryTransform
elif "[AVL.BUCKET_STATE]" in line:
return SectionType.AvlBucketState
elif "[AVL.STATE]" in line:
return SectionType.AvlState
elif "[AVL.INV]" in line:
return SectionType.AvlInv
elif "[RATE.DISCOUNT_CAT]" in line:
return SectionType.RateDiscountCat
elif "[RATE.SUPPLEMENT_CAT]" in line:
return SectionType.RateSupplementalCat
elif "[CONFIG]" in line:
return SectionType.Config
elif "[CUSTOM_INFO]" in line:
return SectionType.CustomInfo
else:
return SectionType.Other
def process_section(self, line, search_type, invert_search, text_to_find, current_section, lines_to_keep):
section_header_regex = r"^\|[^\d\[\]\*\:\{\}\,][\s\|a-z\_]*[^\d\[\]\*\:\{\}\,]*\|$"
section_regex = r"^\[[A-Z].*\]$"
if re.search(section_header_regex, line):
lines_to_keep.append(line)
return current_section
else:
if re.search(section_header_regex, line):
lines_to_keep.append(line)
return current_section
else:
if not line:
lines_to_keep.append(line)
return SectionType.Other
else:
if re.search(section_regex, line):
self.global_section(line, search_type, invert_search, text_to_find, lines_to_keep)
return SectionType.Other
else:
self.search(line, search_type, invert_search, text_to_find, lines_to_keep)
return current_section
def search(self, line, search_type, invert_search, text_to_find, lines_to_keep):
if search_type == 'date':
if invert_search == False:
self.search_date(line, text_to_find, False, lines_to_keep)
else:
self.search_date(line, text_to_find, True, lines_to_keep)
elif search_type == 'regex':
if invert_search == False:
if re.search(text_to_find, line):
lines_to_keep.append(line)
else:
if re.search(text_to_find, line) == None:
lines_to_keep.append(line)
else:
if invert_search == False:
if text_to_find in line:
lines_to_keep.append(line)
else:
if text_to_find not in line:
lines_to_keep.append(line)
def search_date(self, line, text_to_find, invert_search, lines_to_keep):
start_date_to_find, end_date_to_find = self.split_to_dates(text_to_find, False)
start_date_from_line, end_date_from_line = self.split_to_dates(line, True)
if start_date_to_find == 0 or end_date_to_find == 0 or start_date_from_line == 0 or end_date_from_line == 0:
lines_to_keep.append(line)
else:
dates_intersect = self.dates_intersect(start_date_to_find, end_date_to_find, start_date_from_line, end_date_from_line)
if dates_intersect == True and invert_search == False:
lines_to_keep.append(line)
if dates_intersect == False and invert_search == True:
lines_to_keep.append(line)
def dates_intersect(self, start_date_to_find, end_date_to_find, start_date_from_line, end_date_from_line):
return start_date_from_line >= start_date_to_find and start_date_from_line <= end_date_to_find or end_date_from_line >= start_date_to_find and end_date_from_line <= end_date_to_find or start_date_to_find >= start_date_from_line and end_date_to_find <= end_date_from_line
def split_to_dates(self, text_to_split, parse_line):
weekdays_pattern = re.compile(r'[W,A,D]\[[1-7]{0,7}\]')
date_type_pattern = re.compile(r'[A,I,P,W]')
dates = ''
if parse_line:
dates_column = text_to_split.split('|', 2)[1]
dates = re.sub(weekdays_pattern, '', dates_column)
dates = re.sub(date_type_pattern, '', dates)
dates = re.sub(r'\s+', '', dates, flags=re.UNICODE)
else:
dates = text_to_split
dates_array = dates.split(':')
if len(dates_array) == 1:
start_date = self.parse_date_to_int(dates_array[0])
return start_date, start_date
else:
start_date = self.parse_date_to_int(dates_array[0])
end_date = self.parse_date_to_int(dates_array[1])
return start_date, end_date
def parse_date_to_int(self, date):
if len(date) != 8:
return 0
try:
return int(date)
except ValueError:
return 0
def create_new_tab(self, text):
results_view = self.view.window().new_file()
results_view.set_name('Filter Results')
results_view.set_scratch(True)
results_view.settings().set('word_wrap', self.view.settings().get('word_wrap'))
results_view.run_command('append', {'characters': text, 'force': True, 'scroll_to_end': False})
results_view.set_syntax_file(self.view.settings().get('syntax'))
class SectionType():
Test = 0
Contract = 1
DefHotel = 2
DefMeal = 3
DefRoom = 4
RatePlan = 5
RateBase = 6
RateRule = 7
RateSupplement = 8
RateDiscount = 9
RateDiscountGroup = 10
Restriction = 11
QueryTransform = 12
RateCnx = 13
Tax = 14
RateMarkup = 15
AvlBucketState = 16
AvlState = 17
AvlInv = 18
RateDiscountCat = 19
RateSupplementalCat = 20
Config = 21
CustomInfo = 22
TaxGroup = 23
Other = 24