-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewtree_search_cli.py
432 lines (408 loc) · 16.8 KB
/
viewtree_search_cli.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#!/usr/bin/env python
'''
Provides a CLI for a user to load JSON from files
or URLs and then search it with selectors
'''
import logging
import sys
import re
import urllib.request
from json import load, loads, dumps
__author__ = "Erik Pohl"
__copyright__ = "None"
__credits__ = ["Erik Pohl"]
__license__ = "MIT"
__version__ = "1.0.0"
__maintainer__ = "Erik Pohl"
__email__ = "erik.pohl.444@gmail.com"
__status__ = "Code Review"
class viewtree_search_cli:
'''
Provides a CLI for a user to load JSON from files
or URLs and then search it with selectors
'''
def __init__(self):
'''
Set up important attributes for the viewtree
search CLI, and output a welcome message
'''
self.json_source = None
self.debug_mode = False # log message to stdout
self._allow_double_matching = False # > 1 match report for class
self._case_sensitive_search = True # tag key and value case sensitivity
self._check_full_search_match = lambda hits: all(
[hit_or_miss for hit_or_miss in hits]
)
self._selector_prefixes = ['#', '.']
self._recursable_tags = ('subviews', 'contentView', 'input', 'control')
self._selector_keys = ('identifier', 'classNames', 'class')
self._easter_eggs = ('\\easter egg', '\\uuddlrlrba')
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
self._output_welcome_message()
def _output_welcome_message(self):
'''
Output a welcome message for the viewtree CLI
'''
print("ViewTree Search")
print("ViewTree Search CLI is a CLI which allows a user to "
"load ViewTree JSON scripts from files or from URLs and"
" then perform selector searches on it.")
print("Type '\\help' for instructions for use.")
return True
def _set_logging(self):
'''
set logging on or off for info
logging based on debug_mode
method
'''
logging.disable(logging.NOTSET) \
if self.debug_mode \
else logging.disable(logging.INFO)
return self.debug_mode
def _split_string_command(self,
string_command
):
'''
Break apart a string command which is a set of selectors
into a command useful for the search function
'''
self._set_logging()
logging.info('string command argument: ' + str(string_command))
command_list = [string_command]
''' split by selector prefixes'''
split_list = re.split(
r"([" + ''.join(self._selector_prefixes) + "])",
string_command
)
logging.info('Initial split of the list:')
logging.info(split_list)
logging.info('Is it a compound selector?')
logging.info(
set(
string_command[1:]).intersection(
self._selector_prefixes
)
)
''' if selectors are compounded, need to join prefixes back in'''
if set(string_command[1:]).intersection(self._selector_prefixes):
'''the re.split resulted in either
a class as the first list item or a null'''
command_list = [
''.join(prefix_pair) for prefix_pair in list(
zip(split_list[1::2], split_list[2::2])
)
]
# handles the class as first list item
if string_command[0] not in self._selector_prefixes:
command_list.insert(0, split_list[0])
logging.info('resulting command list:' + str(command_list))
return command_list
def _apply_case_sensitivity(self, value):
if not self._case_sensitive_search:
return value.lower()
return value
def viewtree_search(
self,
selectors,
json_view_tree=None,
search_hits=None
):
'''
perform a viewtree search on a json_view_tree using search_commands
output the list of findings of that search
'''
self._set_logging()
if not search_hits:
search_hits = len(selectors) * [0]
if not json_view_tree:
json_view_tree = self.json_source
local_selectors = selectors[:]
local_search_hits = search_hits[:]
results = []
logging.info('called [type and data of tree]:' +
str(type(json_view_tree)) +
str(json_view_tree)
)
'''
is the json view tree at a dict?
iterate over the keys
'''
try:
match = False
for current_json_key in json_view_tree.keys():
logging.info('found a dictionary. searching its key value pairs.')
'''
let's see if the value of the json_key
warrants further recursion
'''
current_json_value = json_view_tree[current_json_key]
'''
if the key is a recursable tag (dict, list)
which is not classNames
'''
if (current_json_key in self._recursable_tags):
log_message = 'recursing into type/value :' + str(type(
current_json_value)) + str(current_json_value)
logging.info(log_message)
results.extend(
self.viewtree_search(
local_selectors,
json_view_tree=current_json_value,
search_hits=local_search_hits
)
)
else:
'''
this entry in the JSON dictionary
is not recursable.
check it for matches with
the selectors
'''
if (self._allow_double_matching) or (not match) and \
(current_json_key in self._selector_keys):
current_json_key = self._apply_case_sensitivity(
current_json_key
)
try:
current_json_value = self._apply_case_sensitivity(
current_json_value
)
except:
current_json_value = [self._apply_case_sensitivity(
className
) for className in
current_json_value]
classnames = self._apply_case_sensitivity('classNames')
for selector_index, selector in \
enumerate(local_selectors):
selector = self._apply_case_sensitivity(selector)
offset_selector = selector[1:]
if (
(
selector.startswith('#') and
current_json_key == 'identifier' and
current_json_value ==
offset_selector
) or
(
selector.startswith('.') and
current_json_key ==
classnames and
offset_selector in
current_json_value
) or
(
selector[0] not in
self._selector_prefixes and
current_json_key
== 'class' and
current_json_value ==
selector
)
):
local_search_hits[selector_index] += 1
if self._check_full_search_match(
local_search_hits):
match = True
results.append(json_view_tree)
except:
try:
'''
is the json_view_tree at a list?
iterate over it
'''
for index, json_list_element in enumerate(json_view_tree):
logging.info('found a list [not going to '
'be a tuple or set]. searching it.')
logging.info('recursing list item [ ' + str(index))
results.extend(
self.viewtree_search(
local_selectors,
json_view_tree=json_list_element,
search_hits=local_search_hits
)
)
except:
'''
not a dict or list
'''
pass
return results
def _output_one_result(self, finding_number, result):
print('Match #{}'.format(finding_number + 1))
print('-' * 80)
print(dumps(result, indent=4))
print('-' * 80)
return 1
def output_results(self, results):
'''
format the output of
the search results
'''
[
self._output_one_result(finding_number, result)
for finding_number, result
in enumerate(results)
]
print("Found 1 entry") \
if len(results) == 1 \
else print("Found {} entries".format(len(results)))
return len(results)
def viewtree_search_wrapper(self, command_string):
command_list = self._split_string_command(command_string)
search_results = self.viewtree_search(
command_list
)
self.output_results(list(search_results))
return search_results
def _json_source_status(self):
'''
Is there JSON source data available for searching?
This outputs the status.
'''
print("JSON source data is still loaded from before") \
if self.json_source else \
print("No JSON source data exists for viewtree searching.")
return self.json_source is not None
def _toggle_debug_mode(self):
'''
Turns off and on debug mode for the viewtree search cli
'''
self.debug_mode = not self.debug_mode
print("Debug mode enabled.") if self.debug_mode else \
print("Debug mode disabled.")
return self.debug_mode
def load_json_from_file(self,
json_file_name
):
'''
Loads json from a file starting with ! and ending with the file name
'''
windowsed_file_name = json_file_name.replace('\\', '\\\\')
try:
with open(windowsed_file_name, 'r') as json_file_handle:
self.json_source = load(json_file_handle)
print('Loaded source from ', json_file_name)
if self.debug_mode:
print('Source: ', dumps(self.json_source, indent=4))
success = True
except:
print("Error loading file: ", json_file_name)
print("Please review the file path and name and try again.")
self._json_source_status()
success = False
return success
def _print_help(self):
'''
Prints the help statement for viewtree_search
'''
print('ViewTree Search Help')
print(
"ViewTree Search is a CLI which allows you to load JSON "
"from a file or from a URL and search it for various selectors.")
print("? toggles debug mode")
print("\\help outputs the help instructions [these]")
print("\\exit exits the CLI")
print("\\source outputs the current JSON source ")
print(" available for viewtree searching")
print("![file name] allows you to load a file of ")
print(" JSON named file name-- "
"don't use the brackets.")
print("@[URL] allows you to load a URL of JSON ")
print(" using the specified URL-- "
"don't use the brackets.")
print("[Simple selector] Searches loaded JSON ")
print(" using a simple selector")
print(80 * '-')
print(' A simple selector can be one of the following:')
print(' * Begins with a #-- an identifier selector. '
'e.g. #myidentifier')
print(' * Begins with a .-- a classNames selector. '
'e.g. .oneclassname')
print(' * Does not begin with a . or a # -- a class '
'selector. e.g. AClass')
print(80 * '-')
print('[Compound selector] Searches loaded '
'JSON using a compound selector')
print(80 * '-')
print(' A compound selector starts with 0 or 1 class selectors ')
print(' followed by 0 or many of a mix of identifier selectors')
print(' and classNames selectors')
print(80 * '-')
return True
def load_json_from_url(self,
command_url
):
'''
Loads json from a command string
starting with @ and ending with the URL
'''
try:
url_request = urllib.request.urlopen(command_url)
data = url_request.read()
encoding = url_request.info().get_content_charset('utf-8')
self.json_source = loads(data.decode(encoding))
print('Loaded source from ', command_url)
if self.debug_mode:
print('Source: ', dumps(self.json_source, indent=4))
success = True
except:
print("Error loading URL ", command_url)
print("Please review the URL/internet connection and try again.")
self._json_source_status()
success = False
return success
def _attempt_cli_exit(self):
'''
Confirms exit from the CLI and then exits if sure
'''
command_string = input("Are you sure?")
if command_string.lower().startswith('y'):
exit()
else:
print("Exit attempt cancelled. Resuming CLI")
self._json_source_status()
return True
def _print_easter_egg(self):
print("Obi-Wan: Mos Eisley spaceport. "
"You will never find a more wretched hive "
"of scum and villainy. We must be cautious.")
return "Help me, Obi-Wan Kenobi. You're my only hope."
def _output_json_source(self):
print(dumps(self.json_source, indent=4)) \
if self.json_source \
else print("No JSON source has been loaded "
"for viewtree searching")
return self.json_source
def prompt(self):
'''
Prompts for command inputs and
redirects to the appropriate functionality
'''
while True:
command_string = ''
while command_string == '':
command_string = input(">>")
if command_string.startswith('!'):
self.load_json_from_file(command_string[1:])
elif command_string == '?':
self._toggle_debug_mode()
elif command_string.startswith('@'):
self.load_json_from_url(command_string[1:])
elif command_string.lower() == '\\help':
self._print_help()
elif command_string.lower() == '\\exit':
self._attempt_cli_exit()
elif command_string.lower() in self._easter_eggs:
self._print_easter_egg()
elif command_string.lower() == '\\source':
self._output_json_source()
else: # this code is used to perform a viewtree
# search since all other command options are not in play
if not self.json_source:
print("No JSON source is loaded. "
"Please load one from file or URL.")
print("Remember: typing '\\help' will get you "
"instructions to use this CLI.")
else:
self.viewtree_search_wrapper(command_string)