11"""
22Library level functions and states.
33"""
4- import os
54import sys
65import functools
76import inspect
87
9- from typing import Callable , Self , Any
10- from dataclasses import dataclass
8+ from typing import Callable
119
1210from . import _engine
13- from . import flow , query , cli
11+ from . import flow , query , cli , setting
1412from .convert import dump_engine_object
1513
1614
17- def _load_field (target : dict [str , Any ], name : str , env_name : str , required : bool = False ,
18- parse : Callable [[str ], Any ] | None = None ):
19- value = os .getenv (env_name )
20- if value is None :
21- if required :
22- raise ValueError (f"{ env_name } is not set" )
23- else :
24- target [name ] = value if parse is None else parse (value )
25-
26- @dataclass
27- class DatabaseConnectionSpec :
28- url : str
29- user : str | None = None
30- password : str | None = None
31-
32- @dataclass
33- class Settings :
34- """Settings for the cocoindex library."""
35- database : DatabaseConnectionSpec
36-
37- @classmethod
38- def from_env (cls ) -> Self :
39- """Load settings from environment variables."""
40-
41- db_kwargs : dict [str , str ] = dict ()
42- _load_field (db_kwargs , "url" , "COCOINDEX_DATABASE_URL" , required = True )
43- _load_field (db_kwargs , "user" , "COCOINDEX_DATABASE_USER" )
44- _load_field (db_kwargs , "password" , "COCOINDEX_DATABASE_PASSWORD" )
45- database = DatabaseConnectionSpec (** db_kwargs )
46- return cls (database = database )
47-
48-
49- def init (settings : Settings ):
15+ def init (settings : setting .Settings ):
5016 """Initialize the cocoindex library."""
5117 _engine .init (dump_engine_object (settings ))
5218
53- @dataclass
54- class ServerSettings :
55- """Settings for the cocoindex server."""
56-
57- # The address to bind the server to.
58- address : str = "127.0.0.1:8080"
59-
60- # The origins of the clients (e.g. CocoInsight UI) to allow CORS from.
61- cors_origins : list [str ] | None = None
62-
63- @classmethod
64- def from_env (cls ) -> Self :
65- """Load settings from environment variables."""
66- kwargs : dict [str , Any ] = dict ()
67- _load_field (kwargs , "address" , "COCOINDEX_SERVER_ADDRESS" )
68- _load_field (kwargs , "cors_origins" , "COCOINDEX_SERVER_CORS_ORIGINS" ,
69- parse = lambda s : [o for e in s .split ("," ) if (o := e .strip ()) != "" ])
70- return cls (** kwargs )
71-
7219
73- def start_server (settings : ServerSettings ):
20+ def start_server (settings : setting . ServerSettings ):
7421 """Start the cocoindex server."""
7522 flow .ensure_all_flows_built ()
7623 query .ensure_all_handlers_built ()
@@ -81,7 +28,7 @@ def stop():
8128 _engine .stop ()
8229
8330def main_fn (
84- settings : Settings | None = None ,
31+ settings : setting . Settings | None = None ,
8532 cocoindex_cmd : str = 'cocoindex' ,
8633 ) -> Callable [[Callable ], Callable ]:
8734 """
@@ -92,7 +39,7 @@ def main_fn(
9239 """
9340
9441 def _pre_init () -> None :
95- effective_settings = settings or Settings .from_env ()
42+ effective_settings = settings or setting . Settings .from_env ()
9643 init (effective_settings )
9744
9845 def _should_run_cli () -> bool :
0 commit comments