-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkb.py
63 lines (44 loc) · 1.25 KB
/
pkb.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
#!/usr/bin/env python
from pymongo import MongoClient
from subprocess import Popen
import click
import time
import gridfs
import os
# Create db if not existing
where = os.getcwd()
if not os.path.isdir(where+"/db/"):
os.mkdir("db")
#########################################################
# DB
client = MongoClient("localhost")
db = client.local;
collection = db.entries
collection.createIndex({name: "text", contents: "text"});
now = time.strftime("%c")
#########################################################
# add
def add():
title = input("Title: ")
content = input("Content: ")
collection.insert_one({"name": title, "contents": content, "date": now, "type": "word"})
click.clear()
# query
def search(query):
click.echo("Search: ")
data = collection.find( { "$text": { "$search": query } } )
for x in data:
click.echo(x)
#########################################################
@click.command()
@click.option('--insert', nargs=1, help='Add')
@click.option('--query', nargs=1, help='Search')
def cli(insert, query):
if insert:
add()
if query:
search(query)
#########################################################
# future additions: all file imports, support more types
if __name__ == '__main__':
cli()