Skip to content

Commit 38e996c

Browse files
committed
fix fts5 operation error, basic type detection
- sqlite3.operationalerror fix - basic type detection - update readme
1 parent 8a46a8b commit 38e996c

File tree

5 files changed

+139
-7
lines changed

5 files changed

+139
-7
lines changed

README.md

+101-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,101 @@
1-
# knovleks
2-
Personal Search Engine for different types of resources
1+
# Knovleks
2+
3+
Personal Search Engine for different types of resources.
4+
5+
![Screenshot of Knovleks TUI](https://user-images.githubusercontent.com/4940804/175700234-41b43332-7031-4852-a397-d6af8a8577d2.png)
6+
7+
Knovleks can currently index websites, pdf files and text notes.
8+
9+
- [Install](#install)
10+
- [Usage](#usage)
11+
* [Index](#index)
12+
* [Search](#search)
13+
* [Tag filter](#tag-filter)
14+
* [TUI](#tui)
15+
+ [Searchbar focused](#searchbar-focused)
16+
+ [Results focused](#results-focused)
17+
18+
## Install
19+
20+
```
21+
pip install knovleks
22+
```
23+
24+
## Usage
25+
26+
```
27+
Usage: knovleks [OPTIONS] COMMAND [ARGS]...
28+
29+
Options:
30+
-h, --help Show this message and exit.
31+
32+
Commands:
33+
index
34+
search full-text search
35+
tag-filter tag filter
36+
tui terminal user interface (experimental)
37+
```
38+
39+
### Index
40+
41+
```
42+
Usage: knovleks index [OPTIONS] DOCUMENT
43+
44+
Options:
45+
-t, --tag TEXT
46+
--title TEXT
47+
-d, --type, --document-type TEXT
48+
-h, --help Show this message and exit.
49+
```
50+
51+
### Search
52+
53+
```
54+
Usage: knovleks search [OPTIONS] QUERY
55+
56+
full-text search
57+
58+
Options:
59+
-t, --tag TEXT
60+
-st, --show-tags
61+
-l, --limit INTEGER
62+
-dt, --doc-type TEXT
63+
-ft, --full-text display full text
64+
-h, --help Show this message and exit.
65+
```
66+
67+
### Tag filter
68+
69+
```
70+
Usage: knovleks tag-filter [OPTIONS] [TAG]...
71+
72+
tag filter
73+
74+
Options:
75+
-st, --show-tags
76+
-l, --limit INTEGER
77+
-dt, --doc-type TEXT
78+
-h, --help Show this message and exit.
79+
```
80+
81+
### TUI
82+
83+
```
84+
Switch focus: TAB
85+
Next result: ctrl+j
86+
Previous result: ctrl+k
87+
Open result without closing: ctrl+l
88+
```
89+
90+
#### Searchbar focused
91+
92+
```
93+
Exit: ESC
94+
```
95+
96+
#### Results focused
97+
98+
```
99+
Switch focus to searchbar: ESC
100+
Open result: Enter
101+
```

knovleks/__main__.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@ def get_supported_document_types() -> Mapping[str, Type[IdocumentType]]:
4343
}
4444

4545

46+
def is_url(path: str) -> bool:
47+
url_prefixes = ["https://", "http://"]
48+
path = path.lower()
49+
return any(map(path.startswith, url_prefixes))
50+
51+
52+
def determine_doc_type(document: str) -> str:
53+
# TODO: determine ooc based on configuration file
54+
if is_url(document):
55+
return "website"
56+
# XXX: filetype shouldn't be determined based on extension
57+
elif document.endswith(".pdf"):
58+
return "pdf"
59+
else:
60+
return "note"
61+
62+
4663
@click.group(context_settings=dict(help_option_names=["-h", "--help"]))
4764
@click.pass_context
4865
def cli(ctx):
@@ -54,10 +71,12 @@ def cli(ctx):
5471
@click.argument("document")
5572
@click.option("-t", "--tag", multiple=True)
5673
@click.option("--title", default="")
57-
@click.option("-d", "--type", "--document-type", default="note")
74+
@click.option("-d", "--type", "--document-type", default="auto")
5875
@click.pass_obj
5976
def index(knov: Knovleks, document: str, tag: Tuple[str],
6077
title: str, type: str):
78+
if type == "auto":
79+
type = determine_doc_type(document)
6180
knov.index_document(type, document, title, set(tag))
6281

6382

knovleks/document_types/pdf_document.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ def parse(self):
1616
@staticmethod
1717
def open_doc(href, elem_idx):
1818
dn = subprocess.DEVNULL
19-
subprocess.Popen(["/usr/bin/zathura", f"{href}", f"-P", f"{elem_idx}"],
20-
stdin=dn, stdout=dn, stderr=dn, close_fds=True)
19+
subprocess.Popen(["/usr/bin/zathura", f"{href}", "-P", f"{elem_idx}"],
20+
stdin=dn, stdout=dn, stderr=dn, close_fds=True)

knovleks/knovleks.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ def _content_column_snippet(self,
197197
(snip.left, snip.right, snip.trunc_text, f"{snip.token_nr}"))
198198
return "snippet(doc_parts_fts, 0, ?, ?, ?, ?)"
199199

200+
def _quote_string(self, string: str) -> str:
201+
string = string.replace('"', '""')
202+
return f'"{string}"'
203+
200204
def search(self, search_query: str, tags: Set[str] = set(),
201205
limit: Optional[int] = None,
202206
doc_type: Optional[str] = None,
@@ -218,10 +222,17 @@ def search(self, search_query: str, tags: Set[str] = set(),
218222
"WHERE dpf.rowid = dp.id AND dp.doc_id = d.id AND "
219223
"dpf.doccontent MATCH ? ORDER BY rank")
220224
parameters.append(search_query)
225+
search_query_idx = len(parameters) - 1
221226
if limit is not None:
222227
parameters.append(f"{limit}")
223228
query += " LIMIT ?"
224-
yield from self.db_con.execute(query, parameters)
229+
try:
230+
# use fts syntax
231+
yield from self.db_con.execute(query, parameters)
232+
except sqlite3.OperationalError:
233+
parameters[search_query_idx] = self._quote_string(search_query)
234+
print(parameters[search_query_idx])
235+
yield from self.db_con.execute(query, parameters)
225236

226237
def open_document(self, doc_type, href, elem_idx):
227238
self.supported_types[doc_type].open_doc(href, elem_idx)

setup.cfg

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
[metadata]
22
name = knovleks
3-
version = 0.0.1
3+
version = 0.0.2
44
author = Loris Reiff
55
author_email = [email protected]
66
license = Apache 2.0
7+
long_description = file: README.md, LICENSE
8+
long_description_content_type = text/markdown
79

810
[options]
911
packages = find:
@@ -23,6 +25,7 @@ ignore = E701,E731
2325

2426
[flake8]
2527
ignore = E701,E731
28+
per-file-ignores = __init__.py:F401
2629
exclude = tests/context.py
2730
statistics = true
2831
show-source = true

0 commit comments

Comments
 (0)