/
grep.py
267 lines (213 loc) · 8.33 KB
/
grep.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
# -*- encoding: utf-8 -*-
# kb v0.1.6
# A knowledge base organizer
# Copyright © 2020, gnc.
# See /LICENSE for licensing information.
"""
kb printer for grep command module
:Copyright: © 2020, gnc.
:License: GPLv3 (see /LICENSE).
"""
import pathlib
from typing import List
from kb.config import DEFAULT_CONFIG as config
import kb.filesystem as fs
from kb.printer.style import ALT_BGROUND, BOLD, UND, RESET
from kb.entities.artifact import Artifact
def generate_grep_header(
grep_result: List[Artifact],
hits_list: List[int],
color: bool = True
) -> None:
"""
Generates kb grep results header.
Arguments:
grep_result - the list of Artifacts for generating
an adapted header
hits_list - a list ordered as the artifacts
representing the number of grep
matches for that artifact
color - a boolean, True if color is enabled
Returns:
A string representing the header for the list of artifacts
"""
if not grep_result:
return
min_length = 20
len_id = max(len(str(len(grep_result) - 1)), 2)
len_title = max(
max([len(art.title) if art.title else 0 for art in grep_result]), min_length)
len_categ = max(max(
[len(art.category) if art.category else 0 for art in grep_result]), min_length)
len_tags = max(
max([len(art.tags) if art.tags else 0 for art in grep_result]), min_length)
len_hits = max(len(str(max(hits_list))), 4)
header = " [ {id} ] {title} {category} {hits} {tags}".format(
id="ID".rjust(len_id),
title="Title".ljust(len_title),
category="Category".ljust(len_categ),
hits="Hits".ljust(len_hits),
tags="Tags".ljust(len_tags))
if color:
return UND + BOLD + header + RESET
return header
def generate_grep_header_verbose(
grep_result: List[Artifact],
hits_list: List[int],
color: bool = True
) -> None:
"""
Generates kb grep results header in verbose mode.
Arguments:
grep_result - the list of Artifacts for generating
an adapted header
hits_list - a list ordered as the artifacts
representing the number of grep
matches for that artifact
color - a boolean, True if color is enabled
Returns:
A string representing the header for the list of artifacts
"""
if not grep_result:
return
min_length = 20
sec_min_length = 10
len_id = max(len(str(len(grep_result) - 1)), 2)
len_title = max(
max([len(art.title) if art.title else 0 for art in grep_result]), min_length)
len_categ = max(max(
[len(art.category) if art.category else 0 for art in grep_result]), min_length)
len_tags = max(
max([len(art.tags) if art.tags else 0 for art in grep_result]), min_length)
len_author = max(max(
[len(art.author) if art.author else 0 for art in grep_result]), sec_min_length)
len_status = max(max(
[len(art.status) if art.status else 0 for art in grep_result]), sec_min_length)
len_hits = max(len(str(max(hits_list))), 4)
header = " [ {id} ] {title} {category} {hits} {tags} {author} {status}".format(
id="ID".rjust(len_id),
title="Title".ljust(len_title),
category="Category".ljust(len_categ),
hits="Hits".ljust(len_hits),
tags="Tags".ljust(len_tags),
author="Author".ljust(len_author),
status="Status".ljust(len_status))
if color:
return UND + BOLD + header + RESET
return header
def print_grep_result(
grep_result: List[Artifact],
hits_list: List[int],
color: bool = True
) -> None:
"""
Print kb query grep results.
Arguments:
grep_result - the list of Artifacts to print
in the form of grep result
hits_list - a list ordered as the artifacts
representing the number of grep
matches for that artifact
color - a boolean, if True, color is enabled
"""
if not grep_result:
return
print(generate_grep_header(grep_result, hits_list, color=color))
print()
min_length = 20
len_id = max(len(str(len(grep_result) - 1)), 2)
len_title = max(
max([len(art.title) if art.title else 0 for art in grep_result]), min_length)
len_categ = max(max(
[len(art.category) if art.category else 0 for art in grep_result]), min_length)
len_tags = max(
max([len(art.tags) if art.tags else 0 for art in grep_result]), min_length)
len_hits = max(len(str(max(hits_list))), 4)
# Print results
for view_id, artifact in enumerate(grep_result):
tags = artifact.tags if artifact.tags else ""
hits_id = view_id
hits = str(hits_list[hits_id])
result_line = " [ {id} ] {title} {category} {hits} {tags}".format(
id=str(view_id).rjust(len_id),
title=artifact.title.ljust(len_title),
category=artifact.category.ljust(len_categ),
hits=hits.ljust(len_hits),
tags=tags.ljust(len_tags))
if color and (view_id % 2 == 0):
print(ALT_BGROUND + result_line + RESET)
else:
print(result_line)
def print_grep_result_verbose(
grep_result: List[Artifact],
hits_list: List[int],
color: bool = True
) -> None:
"""
Print more verbose kb query grep results.
Arguments:
grep_result - the list of Artifacts to print
sorted by number of hits
hits_list - a list ordered as the grep_result artifacts
(i.e., by number of hits) representing the number
of grep matches for that artifact
color - a boolean, if True, color is enabled
"""
if not grep_result:
return
print(generate_grep_header(grep_result, hits_list, color=color))
print()
min_length = 20
sec_min_length = 10
len_id = max(len(str(len(grep_result) - 1)), 2)
len_title = max(
max([len(art.title) if art.title else 0 for art in grep_result]), min_length)
len_categ = max(max(
[len(art.category) if art.category else 0 for art in grep_result]), min_length)
len_tags = max(
max([len(art.tags) if art.tags else 0 for art in grep_result]), min_length)
len_author = max(max(
[len(art.author) if art.author else 0 for art in grep_result]), sec_min_length)
len_status = max(max(
[len(art.status) if art.status else 0 for art in grep_result]), sec_min_length)
len_hits = max(len(str(max(hits_list))), 4)
# Print results
for view_id, artifact in enumerate(grep_result):
tags = artifact.tags if artifact.tags else ""
author = artifact.author if artifact.author else ""
status = artifact.status if artifact.status else ""
hits_id = view_id
hits = str(hits_list[hits_id])
result_line = " [ {id} ] {title} {category} {hits} {tags} {author} {status}".format(
id=str(view_id).rjust(len_id),
title=artifact.title.ljust(len_title),
category=artifact.category.ljust(len_categ),
hits=hits.ljust(len_hits),
tags=tags.ljust(len_tags),
author=author.ljust(len_author),
status=status.ljust(len_status))
if color and (view_id % 2 == 0):
print(ALT_BGROUND + result_line + RESET)
else:
print(result_line)
# This function still has to be implemented, this is just a placeholder
def print_grep_matches(grep_matches, color=True):
"""
Print text associated to grep matches.
Arguments:
grep_matches - the list of Artifacts to print
in the form of grep matches
color - a boolean, if True, color is enabled
"""
for view_id, match in enumerate(grep_matches):
path = "/".join(fs.get_filename_parts_wo_prefix(match[0], config["PATH_KB_DATA"]))
line_number = match[1]
matched_text = match[2]
if color:
path = BOLD + str(path) + RESET
line_number = BOLD + str(line_number) + RESET
result_line = "{path}:{line_number}:{matched_text}".format(
path=path,
line_number=line_number,
matched_text=matched_text)
print(result_line)