mirror of
https://github.com/hanzoai/datastore.git
synced 2026-09-26 13:55:07 +00:00
Follow-up to #105171, which added `ruff` with a deliberately narrow, bug-class-only rule set and left the unused-name and star-import rules off pending a dedicated cleanup pass. This enables that second batch in `pyproject.toml` and fixes every violation across the tree (the bulk in integration tests): - `F401` unused-import and `F841` unused-variable: removed, preserving any side-effecting right-hand side (e.g. `result = node.query(...)` becomes `node.query(...)`). Two `try: import ...` feature probes are kept with `# noqa: F401`. - `F541` f-string-missing-placeholders: dropped the redundant `f` prefix. - `F403`/`F405` star imports: every `from module import *` is expanded into explicit imports of the names actually used, taken from each name's real origin, so stdlib/third-party names import from their real source rather than through a test "convenience bundle". The now-empty `tests/integration/helpers/kafka/common_direct.py` bundle is removed and its three remaining consumers import `_VarintBytes`/`KafkaAdminClient` directly. Eliminating the star imports surfaced a real bug the wildcard had masked from `F821`: `generate_presets.py` in `test_prometheus_protocols` called an undefined `execute_instant_query_with_http_api`; fixed to the existing `execute_query_via_http_api`. No `black`/`isort` reformatting was applied (neither is enforced in the style check), so the diff is limited to the lint fixes themselves. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
122 lines
4.1 KiB
Python
122 lines
4.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
import argparse
|
|
import csv
|
|
import socket
|
|
import ssl
|
|
import logging
|
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
|
|
|
|
# Decorator used to see if authentication works for external dictionary who use a HTTP source.
|
|
def check_auth(fn):
|
|
def wrapper(req):
|
|
auth_header = req.headers.get("authorization", None)
|
|
api_key = req.headers.get("api-key", None)
|
|
if (
|
|
not auth_header
|
|
or auth_header != "Basic Zm9vOmJhcg=="
|
|
or not api_key
|
|
or api_key != "secret"
|
|
):
|
|
req.send_response(401)
|
|
else:
|
|
fn(req)
|
|
|
|
return wrapper
|
|
|
|
|
|
def start_server(server_address, data_path, schema, cert_path, address_family):
|
|
class TSVHTTPHandler(BaseHTTPRequestHandler):
|
|
def log_message(self, format, *args):
|
|
logging.info(
|
|
"%s:%s - - [%s] %s",
|
|
self.address_string(),
|
|
self.server.server_port,
|
|
self.log_date_time_string(),
|
|
format % args,
|
|
)
|
|
|
|
@check_auth
|
|
def do_GET(self):
|
|
self.log_message("Processing '%s'", self.requestline)
|
|
self.__send_headers()
|
|
self.__send_data()
|
|
|
|
@check_auth
|
|
def do_POST(self):
|
|
self.log_message("Processing '%s'", self.requestline)
|
|
ids = self.__read_and_decode_post_ids()
|
|
print("ids=", ids)
|
|
self.__send_headers()
|
|
self.__send_data(ids)
|
|
|
|
def __send_headers(self):
|
|
self.send_response(200)
|
|
self.send_header("Content-type", "text/tsv")
|
|
self.end_headers()
|
|
|
|
def __send_data(self, only_ids=None):
|
|
with open(data_path, "r") as fl:
|
|
reader = csv.reader(fl, delimiter="\t")
|
|
for row in reader:
|
|
if not only_ids or (row[0] in only_ids):
|
|
self.wfile.write(("\t".join(row) + "\n").encode())
|
|
|
|
def __read_and_decode_post_ids(self):
|
|
data = self.__read_and_decode_post_data()
|
|
return [_f for _f in data.split() if _f]
|
|
|
|
def __read_and_decode_post_data(self):
|
|
transfer_encoding = self.headers.get("Transfer-encoding")
|
|
decoded = ""
|
|
if transfer_encoding == "chunked":
|
|
while True:
|
|
s = self.rfile.readline().decode()
|
|
chunk_length = int(s, 16)
|
|
if not chunk_length:
|
|
break
|
|
decoded += self.rfile.read(chunk_length).decode()
|
|
self.rfile.readline().decode()
|
|
else:
|
|
content_length = int(self.headers.get("Content-Length", 0))
|
|
decoded = self.rfile.read(content_length).decode()
|
|
return decoded
|
|
|
|
if address_family == "ipv6":
|
|
HTTPServer.address_family = socket.AF_INET6
|
|
httpd = HTTPServer(server_address, TSVHTTPHandler)
|
|
if schema == "https":
|
|
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
|
ctx.load_cert_chain(certfile=cert_path)
|
|
httpd.socket = ctx.wrap_socket(httpd.socket, server_side=True)
|
|
logging.info("Starting serving on %s", httpd.server_address)
|
|
httpd.serve_forever()
|
|
# Never reaches here
|
|
|
|
|
|
if __name__ == "__main__":
|
|
logging.basicConfig(
|
|
level=logging.NOTSET,
|
|
format="%(process)d %(asctime)s %(levelname)s: %(message)s",
|
|
)
|
|
|
|
parser = argparse.ArgumentParser(
|
|
description="Simple HTTP server returns data from file"
|
|
)
|
|
parser.add_argument("--host", default="localhost")
|
|
parser.add_argument("--port", default=5555, type=int)
|
|
parser.add_argument("--data-path", required=True)
|
|
parser.add_argument("--schema", choices=("http", "https"), required=True)
|
|
parser.add_argument("--cert-path", default="./fake_cert.pem")
|
|
parser.add_argument("--address-family", choices=("ipv4", "ipv6"), default="ipv4")
|
|
|
|
args = parser.parse_args()
|
|
|
|
start_server(
|
|
(args.host, args.port),
|
|
args.data_path,
|
|
args.schema,
|
|
args.cert_path,
|
|
args.address_family,
|
|
)
|