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)
def grep(args: Dict[str, str], config: Dict[str, str]): """ Grep through the list of artifacts of the knowledge base of kb. Arguments: args: - a dictionary containing the following fields: regex -> the regex to search for case_insensitive -> a boolean, if true, the search will be case insensitive matches -> a boolean, if true, only the raw matches will be shown verbose -> a boolean, if true, a verbose output is produced on screen config: - a configuration dictionary containing at least the following keys: PATH_KB_DB - the database path of KB PATH_KB_DATA - the data directory of KB PATH_KB_HIST - the history menu path of KB """ initializer.init(config) conn = db.create_connection(config["PATH_KB_DB"]) # Get all artifacts rows = db.get_artifacts_by_filter(conn, title="") # Get all the file paths related to the artifacts in the database file_list = [Path(config["PATH_KB_DATA"], r.category, r.title) for r in rows] # Grep in the files results = fs.grep_in_files( file_list, args["regex"], args["case_insensitive"]) # Get the list of artifact tuples in the form (category,title) artifact_names = [fs.get_filename_parts_wo_prefix( res[0], config["PATH_KB_DATA"]) for res in results] # If user specied --matches -> just show matching lines and exit if args["matches"]: printer.print_grep_matches(artifact_names) sys.exit(0) # Get the set of uniq artifacts uniq_artifact_names = set(artifact_names) # Get the number of matches (hits) for each path found filecounts = get_hits_per_artifact_name(artifact_names) grep_result = list() for art in uniq_artifact_names: artifact = db.get_artifacts_by_filter( conn, category=art[0], title=art[1])[0] if artifact: no_of_hits = filecounts[art] grep_result.append((artifact, no_of_hits)) # Sort by number of hits, the largest -> the first grep_result.sort(key=lambda x: x[1], reverse=True) grep_artifacts = [r[0] for r in grep_result] grep_hits = [r[1] for r in grep_result] # Write to history file history.write(config["PATH_KB_HIST"], grep_artifacts) color_mode = not args["no_color"] if args["verbose"]: printer.print_grep_result_verbose( grep_artifacts, grep_hits, color_mode) else: printer.print_grep_result(grep_artifacts, grep_hits, color_mode)