From a0c2935fd398568335bba31748c6abd7b29c8afb Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Fri, 1 Dec 2023 17:27:22 +0100 Subject: server with argparse initialization --- scratch/semgrep/server.py | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) (limited to 'scratch/semgrep') 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) -- cgit v1.2.3