-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
search.py
71 lines (59 loc) · 2.26 KB
/
search.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
# -*- encoding: utf-8 -*-
# kb v0.1.6
# A knowledge base organizer
# Copyright © 2020, gnc.
# See /LICENSE for licensing information.
"""
kb search command module
:Copyright: © 2020, gnc.
:License: GPLv3 (see /LICENSE).
"""
from typing import Dict
import kb.db as db
import kb.initializer as initializer
import kb.printer.search as printer
import kb.history as history
def search(args: Dict[str, str], config: Dict[str, str]):
"""
Search artifacts within the knowledge base of kb.
Arguments:
args: - a dictionary containing the following fields:
query -> filter for the title field of the artifact
category -> filter for the category field of the artifact
tags -> filter for the tags field of the artifact
author -> filter for the author field of the artifact
status -> filter for the status field of the artifact
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
EDITOR - the editor program to call
"""
# Check initialization
initializer.init(config)
tags_list = None
if args["tags"] and args["tags"] != "":
tags_list = args["tags"].split(';')
conn = db.create_connection(config["PATH_KB_DB"])
rows = db.get_artifacts_by_filter(
conn,
title=args["query"],
category=args["category"],
tags=tags_list,
status=args["status"],
author=args["author"])
# rows.sort(key=lambda x: x[1])
artifacts = sorted(rows, key=lambda x: x.title)
# Write to history file
history.write(config["PATH_KB_HIST"], artifacts)
# Is full_identifier mode enabled?
if args["full_identifier"]:
printer.print_search_result_full_mode(artifacts)
return
# Print resulting list
color_mode = not args["no_color"]
if args["verbose"]:
printer.print_search_result_verbose(artifacts, color_mode)
else:
printer.print_search_result(artifacts, color_mode)