diff options
author | Oscar Najera <hi@oscarnajera.com> | 2023-12-01 17:27:22 +0100 |
---|---|---|
committer | Oscar Najera <hi@oscarnajera.com> | 2024-01-13 02:27:42 +0100 |
commit | a0c2935fd398568335bba31748c6abd7b29c8afb (patch) | |
tree | 1a24df0999fda15913b654cee50c815dfdee3bab | |
parent | dc45b56759e38da793f8976c94b6a798096c968e (diff) | |
download | dotfiles-a0c2935fd398568335bba31748c6abd7b29c8afb.tar.gz dotfiles-a0c2935fd398568335bba31748c6abd7b29c8afb.tar.bz2 dotfiles-a0c2935fd398568335bba31748c6abd7b29c8afb.zip |
server with argparse initialization
-rw-r--r-- | scratch/semgrep/server.py | 39 |
1 files changed, 33 insertions, 6 deletions
diff --git a/scratch/semgrep/server.py b/scratch/semgrep/server.py index 6a8648b..87eac5e 100644 --- a/scratch/semgrep/server.py +++ b/scratch/semgrep/server.py @@ -1,9 +1,11 @@ #!/usr/bin/env python3 -from http.server import BaseHTTPRequestHandler, HTTPServer -import chromadb +import argparse import collections import hashlib import json +from http.server import BaseHTTPRequestHandler, HTTPServer + +import chromadb def checksum(string): @@ -35,13 +37,16 @@ class MyRequestHandler(BaseHTTPRequestHandler): self.send_response(400) if query := data.get("query"): + self.log_message("Processing query '%s'", query.strip()) response = collection.query(query_texts=ensure_list(query)) elif paragraph := data.get("store"): data, metadata = drop_duplicates(paragraph) + node = set(m.get("node-id") for m in metadata) + self.log_message("Processing metadata %s", node) collection.add( documents=data, metadatas=metadata, ids=[checksum(l) for l in data] ) - response = {"added": data} + response = f"Successfully inserted {node}" else: raise ValueError(f"Used wrong method. Sent: {data.keys()}") @@ -86,7 +91,29 @@ def test(): assert drop_duplicates(sample) == (["Hello", "World", "Good"], [5, 8, 3]) +def parse_arguments(args=None): + parser = argparse.ArgumentParser( + description="Run Semantic database server", + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + ) + parser.add_argument( + "-db", "--database", default="org-roam", help="Name of the collection" + ) + parser.add_argument( + "-D", + "--database-dir", + default="semantic-roam", + help="Directory where to store database files", + ) + parser.add_argument( + "-p", "--port", default=8080, type=int, help="Port where server listens" + ) + + return parser.parse_args(args) + + if __name__ == "__main__": - client = chromadb.PersistentClient(path="./semgrep") - collection = client.get_or_create_collection("org-roam") - run_server() + args = parse_arguments() + client = chromadb.PersistentClient(path=args.database_dir) + collection = client.get_or_create_collection(args.database) + run_server(args.port) |