Skip to content

Commit a5adadd

Browse files
authored
refactor: extract settings data types out of lib module (#421)
1 parent 1c72261 commit a5adadd

File tree

3 files changed

+73
-65
lines changed

3 files changed

+73
-65
lines changed

python/cocoindex/cli.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import click
22
import datetime
3-
import urllib.parse
43

54
from rich.console import Console
65

7-
from . import flow, lib
6+
from . import flow, lib, setting
87
from .flow import flow_names
98
from .setup import sync_setup, drop_setup, flow_names_with_setup, apply_setup_changes
109
from .runtime import execution_context
@@ -60,9 +59,9 @@ def show(flow_name: str | None, color: bool):
6059
"""
6160
Show the flow spec in a readable format with colored output.
6261
"""
63-
flow = _flow_by_name(flow_name)
62+
fl = _flow_by_name(flow_name)
6463
console = Console(no_color=not color)
65-
console.print(flow._render_text())
64+
console.print(fl._render_text())
6665

6766
@cli.command()
6867
def setup():
@@ -151,7 +150,7 @@ def evaluate(flow_name: str | None, output_dir: str | None, cache: bool = True):
151150
options = flow.EvaluateAndDumpOptions(output_dir=output_dir, use_cache=cache)
152151
fl.evaluate_and_dump(options)
153152

154-
_default_server_settings = lib.ServerSettings.from_env()
153+
_default_server_settings = setting.ServerSettings.from_env()
155154

156155
COCOINDEX_HOST = 'https://cocoindex.io'
157156

@@ -191,7 +190,7 @@ def server(address: str, live_update: bool, quiet: bool, cors_origin: str | None
191190
cors_origins.add(COCOINDEX_HOST)
192191
if cors_local is not None:
193192
cors_origins.add(f"http://localhost:{cors_local}")
194-
lib.start_server(lib.ServerSettings(address=address, cors_origins=list(cors_origins)))
193+
lib.start_server(setting.ServerSettings(address=address, cors_origins=list(cors_origins)))
195194
if live_update:
196195
options = flow.FlowLiveUpdaterOptions(live_mode=True, print_stats=not quiet)
197196
execution_context.run(flow.update_all_flows(options))

python/cocoindex/lib.py

Lines changed: 6 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,23 @@
11
"""
22
Library level functions and states.
33
"""
4-
import os
54
import sys
65
import functools
76
import inspect
87

9-
from typing import Callable, Self, Any
10-
from dataclasses import dataclass
8+
from typing import Callable
119

1210
from . import _engine
13-
from . import flow, query, cli
11+
from . import flow, query, cli, setting
1412
from .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

8330
def 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:

python/cocoindex/setting.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
Data types for settings of the cocoindex library.
3+
"""
4+
import os
5+
6+
from typing import Callable, Self, Any
7+
from dataclasses import dataclass
8+
9+
10+
@dataclass
11+
class DatabaseConnectionSpec:
12+
"""
13+
Connection spec for relational database.
14+
Used by both internal and target storage.
15+
"""
16+
url: str
17+
user: str | None = None
18+
password: str | None = None
19+
20+
def _load_field(target: dict[str, Any], name: str, env_name: str, required: bool = False,
21+
parse: Callable[[str], Any] | None = None):
22+
value = os.getenv(env_name)
23+
if value is None:
24+
if required:
25+
raise ValueError(f"{env_name} is not set")
26+
else:
27+
target[name] = value if parse is None else parse(value)
28+
29+
@dataclass
30+
class Settings:
31+
"""Settings for the cocoindex library."""
32+
database: DatabaseConnectionSpec
33+
34+
@classmethod
35+
def from_env(cls) -> Self:
36+
"""Load settings from environment variables."""
37+
38+
db_kwargs: dict[str, str] = dict()
39+
_load_field(db_kwargs, "url", "COCOINDEX_DATABASE_URL", required=True)
40+
_load_field(db_kwargs, "user", "COCOINDEX_DATABASE_USER")
41+
_load_field(db_kwargs, "password", "COCOINDEX_DATABASE_PASSWORD")
42+
database = DatabaseConnectionSpec(**db_kwargs)
43+
return cls(database=database)
44+
45+
@dataclass
46+
class ServerSettings:
47+
"""Settings for the cocoindex server."""
48+
49+
# The address to bind the server to.
50+
address: str = "127.0.0.1:8080"
51+
52+
# The origins of the clients (e.g. CocoInsight UI) to allow CORS from.
53+
cors_origins: list[str] | None = None
54+
55+
@classmethod
56+
def from_env(cls) -> Self:
57+
"""Load settings from environment variables."""
58+
kwargs: dict[str, Any] = dict()
59+
_load_field(kwargs, "address", "COCOINDEX_SERVER_ADDRESS")
60+
_load_field(kwargs, "cors_origins", "COCOINDEX_SERVER_CORS_ORIGINS",
61+
parse=lambda s: [o for e in s.split(",") if (o := e.strip()) != ""])
62+
return cls(**kwargs)

0 commit comments

Comments
 (0)