-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvs.py
More file actions
executable file
·214 lines (180 loc) · 5.16 KB
/
svs.py
File metadata and controls
executable file
·214 lines (180 loc) · 5.16 KB
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
#!/usr/bin/env python3
"""
The entrypoint for the CLI.
"""
import os
import sys
from typing import Optional
import click
from dotenv import load_dotenv
from utils.datastore import Datastore
from utils.processing import Processor
from utils.store import Store
from rich.console import Console
from rich.markdown import Markdown
load_dotenv()
console = Console()
datastore = Datastore("datastore")
def get_absolute_path(path: str):
"""
A util to get the proper absolute path of a given path.
"""
joined_path = os.path.join(os.path.dirname(__file__), path)
return os.path.abspath(joined_path)
@click.group()
def cli():
"""
The entrypoint for the CLI.
"""
@click.command()
@click.argument("name")
@click.argument("path")
def add(name: str, path: str):
"""
Add a
"""
abs_path = get_absolute_path(path)
console.print(f'Adding "{name}" to store with location {abs_path}')
datastore.add_new_store(name, abs_path)
@click.command()
@click.option("--name", default=None)
def get(name: Optional[str]):
"""
Gets a store by name, or all the stores if no name is provided.
"""
console.print("\n\n")
if name is None:
ss = datastore.get_all_db_stores()
console.print("All stores:\n")
for s in ss:
console.print(f"- {s[0]} {s[1]}\n")
else:
s = datastore.get_db_store(name)
console.print(f"Store {s[0]}:\n")
@click.command()
def reset():
"""
Resets the datastore.
"""
answer = input(
"Are you sure you want to reset the datastore, all data will be lost and this cannot be undone?\nIf you are sure please type 'RESET': "
)
if answer == "RESET":
try:
datastore.hard_reset()
console.print("Datastore reset successfully.")
except ValueError as e:
console.print("Error resetting datastore: ", e)
except Exception as e:
console.print(
"An error occurred while resetting the datastore: ", e, file=sys.stderr
)
@click.group()
def stores():
"""
Get and add stores.
"""
stores.add_command(add)
stores.add_command(get)
stores.add_command(reset)
@click.group()
def store():
"""
Manage individual stores.
"""
@click.command()
@click.argument("name")
def build(name):
"""
Build a store.
"""
console.print(f"Attempting to build store {name}")
try:
s = datastore.get_store(name)
store_data = datastore.get_db_store(name)
processor = Processor(
directory=store_data[1],
store=s,
)
processor.run_build()
except ValueError as e:
console.print("Error building store: ", e)
@click.command()
@click.argument("name")
@click.argument("query")
@click.option(
"--column", help="The column to search in (title or content).", default="content"
)
@click.option("--limit", help="The number of results to return.", default=5)
@click.option("--threshold", help="The threshold for similarity.", default=0.5)
def search(name, query, column, limit, threshold):
"""
Searches a given store based on a query.
"""
console.print(f"Searching store {name} for query '{query}' in column {column}")
try:
if column not in ["title", "content"]:
raise Exception("Invalid column, must be either 'title' or 'content'")
if column is None:
column = "content"
s = datastore.get_store(name)
results = s.search_and_map_similar_items(query, column, limit, threshold)
for result in results:
console.print(
Markdown(
f"({result[0]}) {result[1]}:\n\n {Store.get_content_summary(result[2], 256)}\n\n\n"
)
)
except Exception as e:
console.print("Error searching store: ", e)
@click.command()
@click.argument("name")
def sync(name):
"""
Sync a store.
"""
console.print(f"Attempting to sync store {name}")
try:
s = datastore.get_store(name)
store_data = datastore.get_db_store(name)
processor = Processor(
directory=store_data[1],
store=s,
)
processor.run_sync()
except ValueError as e:
console.print("Error syncing store: ", e)
@click.command()
@click.argument("name")
@click.argument("new_name")
def rename(name, new_name):
"""
Rename a store.
"""
console.print(f"Attempting to rename store {name} to {new_name}")
try:
datastore.rename_store(name, new_name)
console.print(f"Store {name} renamed to {new_name}")
except ValueError as e:
console.print("Error renaming store: ", e)
@click.command()
@click.argument("name")
def remove(name):
"""
Remove a store.
"""
console.print(f"Attempting to remove store {name}")
try:
datastore.remove_store(name)
console.print(f"Store {name} removed")
except ValueError as e:
console.print("Error removing store: ", e)
store.add_command(build)
store.add_command(search)
store.add_command(sync)
store.add_command(rename)
store.add_command(remove)
if __name__ == "__main__":
cli.add_command(stores)
cli.add_command(store)
cli()