periplus-python-sdk 0.3.0__py3-none-any.whl → 0.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,237 @@
1
+ Metadata-Version: 2.4
2
+ Name: periplus-python-sdk
3
+ Version: 0.5.0
4
+ Summary: Read-only Python client for the public Periplus query API
5
+ License-Expression: Apache-2.0
6
+ Project-URL: Repository, https://github.com/elei-io/periplus
7
+ Project-URL: Issues, https://github.com/elei-io/periplus/issues
8
+ Requires-Python: >=3.11
9
+ Description-Content-Type: text/markdown
10
+ License-File: LICENSE
11
+ License-File: NOTICE
12
+ Requires-Dist: httpx>=0.28
13
+ Requires-Dist: pydantic<3,>=2.12
14
+ Provides-Extra: sqlalchemy
15
+ Requires-Dist: sqlalchemy<3,>=2.0; extra == "sqlalchemy"
16
+ Provides-Extra: notebook
17
+ Requires-Dist: sqlalchemy<3,>=2.0; extra == "notebook"
18
+ Requires-Dist: marimo[sql]>=0.24.1; extra == "notebook"
19
+ Dynamic: license-file
20
+
21
+ # Periplus Python SDK
22
+
23
+ A read-only client for the public Periplus query API. Python 3.11 or later.
24
+ Configure the **public web application URL**, not the internal query or control service.
25
+ No API token, DuckDB installation or lake credentials are needed.
26
+
27
+ ```python
28
+ from periplus_sdk import Client
29
+
30
+ with Client("http://localhost:8080") as client:
31
+ result = client.execute(
32
+ "SELECT capture_id FROM public_v1.capture LIMIT ?", [10]
33
+ )
34
+ print(result.columns, result.types)
35
+ print(result.rows)
36
+ print(result.source_snapshot, result.truncated)
37
+ ```
38
+
39
+ For a hosted deployment, replace the URL with its public HTTPS origin. Alternatively set
40
+ `PERIPLUS_PUBLIC_URL` and use `Client()`. An optional URL path prefix is preserved.
41
+ The client reuses HTTP connections; close it with a context manager or `close()`.
42
+
43
+ ## Marimo SQL cells and schema browser
44
+
45
+ Install the notebook integration from PyPI:
46
+
47
+ ```sh
48
+ uv add "periplus-python-sdk[notebook]>=0.5.0"
49
+ ```
50
+
51
+ In a Python setup cell, create a SQLAlchemy engine:
52
+
53
+ ```python
54
+ from sqlalchemy import create_engine
55
+
56
+ pp = create_engine(
57
+ "periplus:///public_v1",
58
+ connect_args={"base_url": "https://periplus.dev", "mode": "stable"},
59
+ )
60
+ ```
61
+
62
+ Add a SQL cell, select **pp** in its connection dropdown, and enter:
63
+
64
+ ```sql
65
+ SELECT capture_id, requested_url
66
+ FROM public_v1.capture
67
+ LIMIT 10
68
+ ```
69
+
70
+ Marimo displays the result as a table. Expand **pp → public_v1** in Data Sources
71
+ to discover views and expand a view to load its columns for SQL completion.
72
+ Discovery uses bounded `SHOW TABLES` and `DESCRIBE` through the same public API;
73
+ no internal catalogue or storage credentials are used. Truncated discovery fails
74
+ explicitly rather than displaying a silently incomplete schema. To eagerly load
75
+ schemas and views, enable their discovery in marimo's Packages & Data settings.
76
+ Column discovery is on demand by default, to avoid many public API requests.
77
+
78
+ The Python equivalent of a SQL cell is:
79
+
80
+ ```python
81
+ import marimo as mo
82
+
83
+ captures = mo.sql(
84
+ "SELECT capture_id FROM public_v1.capture LIMIT 10",
85
+ engine=pp,
86
+ )
87
+ ```
88
+
89
+ Set `mode="experimental"` in `connect_args` for the experimental service. The URL
90
+ path names the public schema; the HTTPS endpoint belongs in `base_url` (or set
91
+ `PERIPLUS_PUBLIC_URL`). Run `pp.dispose()` when finished. This is a read-only
92
+ SQLAlchemy dialect for textual SQL and reflection, not a writable ORM backend.
93
+ Each statement has its own server snapshot; SQLAlchemy transaction blocks do not
94
+ provide a shared snapshot or rollback. The adapter makes no transaction requests.
95
+
96
+ A complete notebook is in `examples/notebook.py`. The integration is tested with
97
+ marimo 0.24.1 and SQLAlchemy 2.x. For SQLAlchemy without marimo, install the
98
+ `sqlalchemy` extra instead of `notebook`.
99
+
100
+ ## DB-API connection
101
+
102
+ For SQL cells without schema browsing, or standard cursor-based Python code:
103
+
104
+ ```python
105
+ from periplus_sdk import connect
106
+
107
+ with connect("https://periplus.dev", mode="stable") as connection:
108
+ with connection.cursor() as cursor:
109
+ cursor.execute("SELECT capture_id FROM public_v1.capture LIMIT ?", [10])
110
+ print(cursor.description)
111
+ print(cursor.fetchall())
112
+ print(cursor.result.source_snapshot)
113
+ ```
114
+
115
+ Connections expose `cursor`, `execute`, `close`, and context managers. Cursors
116
+ support `execute`, `fetchone`, `fetchmany`, `fetchall`, iteration, and close.
117
+ Use positional `?` parameters. Decimal and temporal parameters are sent as
118
+ strings; use explicit SQL casts. Binary and nested parameters are not supported
119
+ by this adapter. Fetching only consumes the bounded result already received;
120
+ it never issues pagination or retries. Connections/cursors are not thread-shared.
121
+ `commit()` is a no-op; `rollback()` and `executemany()` are unsupported.
122
+
123
+ `cursor.result` preserves the original query response. `connection.last_result`
124
+ also retains it after marimo closes a cursor; a new execution clears it first.
125
+ Truncation emits `periplus_sdk.dbapi.TruncationWarning` and sets `rowcount` to -1.
126
+ DB-API failures use the standard exception hierarchy in `periplus_sdk.dbapi`;
127
+ HTTP errors retain `status_code`, `code`, and `retry_after_seconds`.
128
+
129
+ Scalar integer, floating-point, decimal, date, time, timestamp and BLOB results
130
+ are decoded to Python values. UUIDs remain strings. Nested/other SQL types keep
131
+ their JSON wire representation; out-of-range dates/timestamps remain strings.
132
+ Temporal precision is limited to what the server JSON transport preserves.
133
+ The cursor preserves duplicate column names, but dataframe libraries/marimo may
134
+ not: use unique SQL aliases. Dataframe inference can lose types for empty or
135
+ all-null results; `cursor.description` retains the SQL type names.
136
+
137
+ ## Stable and experimental APIs
138
+
139
+ Both clients accept `mode="stable"` (the default) or `mode="experimental"` at initialization:
140
+
141
+ ```python
142
+ with Client("https://periplus.dev", mode="experimental") as client:
143
+ result = client.execute("SELECT capture_id FROM public_v1.capture LIMIT 1")
144
+ print(result.query_mode, result.compiler_version, result.optimizations)
145
+ ```
146
+
147
+ The selected mode applies to preparation, execution, and helper discovery. Experimental
148
+ requests use the public application's `/api/query/experimental/` routes. There is no
149
+ automatic fallback to stable if the experimental service is unavailable.
150
+ `AsyncClient` accepts the same option. Invalid modes raise `ConfigurationError`.
151
+
152
+ ## Preparation and helpers
153
+
154
+ ```python
155
+ with Client("http://localhost:8080") as client:
156
+ prepared = client.prepare("SELECT capture_id FROM public_v1.capture LIMIT ?", [10])
157
+ print(prepared.diagnostics, prepared.plan)
158
+ result = client.execute(prepared.sql, prepared.parameters)
159
+ helpers = client.helpers()
160
+ print(helpers.catalogue_version, helpers.helpers)
161
+ ```
162
+
163
+ Preparation validates and explains without executing the analytical query. Execution independently
164
+ validates and prepares; a prior preparation never authorizes SQL. Linting, diagnostics and future
165
+ SQL optimizations belong to the server. The SDK sends SQL unchanged.
166
+
167
+ ## Async use
168
+
169
+ ```python
170
+ from periplus_sdk import AsyncClient
171
+
172
+ async def observations():
173
+ async with AsyncClient("http://localhost:8080") as client:
174
+ return await client.execute("SELECT capture_id FROM public_v1.capture LIMIT 10")
175
+ ```
176
+
177
+ Use `aclose()` when managing an async client's lifetime explicitly.
178
+
179
+ ## Permissions, results and errors
180
+
181
+ - The same public SQL feature switch, shared rate budget, namespace validation and read-only
182
+ execution apply as in the public web workspace. The SDK provides no writes, crawling,
183
+ administrative controls or direct lake attachment.
184
+ - Results retain `query_id`, SQL, parameters, diagnostics, plan, columns, SQL types, JSON rows,
185
+ elapsed milliseconds, `source_snapshot` and `truncated`. Decimals and large integers remain
186
+ strings exactly as returned by the server. Duplicate column names are preserved.
187
+ - Operator-configured execution limits default to 1,000 rows, an 8 MiB result budget and a
188
+ 20-second server deadline. Always inspect `truncated`. The SDK does not silently fetch more rows or retry.
189
+ - `ApiError` exposes `status_code`, safe `code`, and `retry_after_seconds` when supplied.
190
+ `TransportError` means HTTP failed; `ResponseError` means a malformed successful response.
191
+ The client timeout defaults to 140 seconds and can be set with `timeout=`. A timeout or local
192
+ cancellation does not guarantee server cancellation. Redirects are not followed automatically.
193
+ - Preparation and execution are attributed to `sdk` in the existing private query history.
194
+ Original SQL and parameters are retained for 30 days; result rows are not stored. Recording is
195
+ best-effort and can be lost during outages or backpressure. This label is not a user identity.
196
+
197
+ ## Installation and verification
198
+
199
+ Install the public-v1 client from PyPI:
200
+
201
+ ```sh
202
+ python -m pip install "periplus-python-sdk>=0.5.0"
203
+ ```
204
+
205
+ Version 0.5.0 supports the current public-v1 contract. For production, configure
206
+ `PERIPLUS_PUBLIC_URL=https://periplus.dev`; no API token is required.
207
+ Run the installed package against an available public app:
208
+
209
+ ```sh
210
+ PERIPLUS_PUBLIC_URL=http://localhost:8080 python packages/periplus-python-sdk/examples/smoke.py
211
+ ```
212
+
213
+ ## Releasing
214
+
215
+ Repository CI publishes immutable releases from tags named
216
+ `periplus-python-sdk-v<version>`. The tag must exactly match the static version
217
+ in `pyproject.toml`; for example, version `0.5.0` is released with:
218
+
219
+ ```sh
220
+ git tag periplus-python-sdk-v0.5.0
221
+ git push origin periplus-python-sdk-v0.5.0
222
+ ```
223
+
224
+ PyPI publishing uses Trusted Publishing rather than a stored API token. The
225
+ PyPI publisher must be configured for GitHub owner `elei-io`, repository
226
+ `periplus`, workflow `python-sdk-release.yml`, and environment `pypi`. Protect
227
+ that GitHub environment with required reviewers before the first release.
228
+
229
+ ## Public v1
230
+
231
+ Install the updated SDK from PyPI with `python -m pip install "periplus-python-sdk>=0.5.0"`. The previously published 0.2.0 release predates this contract. `prepare` and `execute` accept keyword-only `schema_version="public_v1"` (the default); responses preserve `schema_version` separately from `source_snapshot`. Unavailable versions are rejected by the server.
232
+
233
+ ## License
234
+
235
+ Copyright (c) 2026 Ekku Leivonen (elei.io). Licensed under [Apache-2.0](LICENSE);
236
+ see [NOTICE](NOTICE). The server and other repository packages have separate
237
+ licensing described in the root LICENSING.md.
@@ -0,0 +1,14 @@
1
+ periplus_python_sdk-0.5.0.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
2
+ periplus_python_sdk-0.5.0.dist-info/licenses/NOTICE,sha256=bhbYSqcUB3U_P1-XzloiT81JGniqoYaRLxNkQ1Pm9MQ,52
3
+ periplus_sdk/__init__.py,sha256=WimXYlPB6tCimBO4VSwhcp00dwSL87jMmMuQ4-kINfM,546
4
+ periplus_sdk/client.py,sha256=ZwMmVJKNF-FrGh_qwiQ5myhu8XJodb_96ohkqK47yDA,6557
5
+ periplus_sdk/dbapi.py,sha256=lK9ZROaMKXm26wvC7DCKywm3qwSvqeHa7zkktnJVP80,11244
6
+ periplus_sdk/errors.py,sha256=rB1n-v8Hc2tu2dtHivz-MlqsCoRC5pTcWogTfM7SMLw,855
7
+ periplus_sdk/py.typed,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
8
+ periplus_sdk/sqlalchemy.py,sha256=axFpEMnMP9uSxbsxzVNSUHNh41q2NruSX8W_5E8F59Q,4877
9
+ periplus_sdk/types.py,sha256=PTdJO6BYTY97dBd3HMEZ52k6P9s0cMvidjpnc8zUpy4,1045
10
+ periplus_python_sdk-0.5.0.dist-info/METADATA,sha256=-3wrgS0gK18Kf6UGV9jrVu2rhm2K51q6ScgZUyiOB7U,10091
11
+ periplus_python_sdk-0.5.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
12
+ periplus_python_sdk-0.5.0.dist-info/entry_points.txt,sha256=Pr14L_7AhLinq-4qDxB4awFVubrvR1BEfkaFVRpdCbU,73
13
+ periplus_python_sdk-0.5.0.dist-info/top_level.txt,sha256=o41t5TzwgoxzSmbKoP6olWW1FyEAGWVYTjeoKadBK40,13
14
+ periplus_python_sdk-0.5.0.dist-info/RECORD,,
@@ -0,0 +1,2 @@
1
+ [sqlalchemy.dialects]
2
+ periplus = periplus_sdk.sqlalchemy:PeriplusDialect
periplus_sdk/__init__.py CHANGED
@@ -1,8 +1,9 @@
1
1
  """Read-only Python clients for the public Periplus query API."""
2
+ from .dbapi import connect
2
3
  from .client import AsyncClient, Client
3
4
  from .errors import ApiError, ConfigurationError, PeriplusError, ResponseError, TransportError
4
5
  from .types import Diagnostic, PreparedQuery, QueryHelper, QueryHelpers, QueryResult
5
6
 
6
- __all__ = ["AsyncClient", "Client", "ApiError", "ConfigurationError", "PeriplusError",
7
+ __all__ = ["connect", "AsyncClient", "Client", "ApiError", "ConfigurationError", "PeriplusError",
7
8
  "ResponseError", "TransportError", "Diagnostic", "PreparedQuery", "QueryHelper",
8
9
  "QueryHelpers", "QueryResult"]
periplus_sdk/client.py CHANGED
@@ -6,7 +6,7 @@ from datetime import UTC, datetime
6
6
  from email.utils import parsedate_to_datetime
7
7
  import math
8
8
  import os
9
- from typing import TypeVar
9
+ from typing import Literal, TypeVar
10
10
 
11
11
  import httpx
12
12
  from pydantic import BaseModel, JsonValue, ValidationError
@@ -79,7 +79,10 @@ def _payload(sql: str, parameters: Sequence[JsonValue] | None, schema_version: s
79
79
  class Client:
80
80
  """Reusable synchronous public query client. Close it or use a with block."""
81
81
 
82
- def __init__(self, base_url: str | None = None, *, timeout: float = 140):
82
+ def __init__(self, base_url: str | None = None, *, timeout: float = 140, mode: Literal["stable", "experimental"] = "stable"):
83
+ if mode not in {"stable", "experimental"}:
84
+ raise ConfigurationError("mode must be stable or experimental.")
85
+ self._query_path = "api/query/experimental/" if mode == "experimental" else "api/query/"
83
86
  self._http = httpx.Client(**_options(base_url, timeout))
84
87
 
85
88
  def __enter__(self) -> Client:
@@ -93,7 +96,7 @@ class Client:
93
96
 
94
97
  def _request(self, method: str, path: str, model: type[Model], **kwargs) -> Model:
95
98
  try:
96
- response = self._http.request(method, "api/query/" + path, **kwargs)
99
+ response = self._http.request(method, self._query_path + path, **kwargs)
97
100
  except httpx.RequestError:
98
101
  raise TransportError("Could not complete the public query request.") from None
99
102
  return _decode(response, model)
@@ -111,7 +114,10 @@ class Client:
111
114
  class AsyncClient:
112
115
  """Reusable asynchronous public query client. Use an async with block."""
113
116
 
114
- def __init__(self, base_url: str | None = None, *, timeout: float = 140):
117
+ def __init__(self, base_url: str | None = None, *, timeout: float = 140, mode: Literal["stable", "experimental"] = "stable"):
118
+ if mode not in {"stable", "experimental"}:
119
+ raise ConfigurationError("mode must be stable or experimental.")
120
+ self._query_path = "api/query/experimental/" if mode == "experimental" else "api/query/"
115
121
  self._http = httpx.AsyncClient(**_options(base_url, timeout))
116
122
 
117
123
  async def __aenter__(self) -> AsyncClient:
@@ -125,7 +131,7 @@ class AsyncClient:
125
131
 
126
132
  async def _request(self, method: str, path: str, model: type[Model], **kwargs) -> Model:
127
133
  try:
128
- response = await self._http.request(method, "api/query/" + path, **kwargs)
134
+ response = await self._http.request(method, self._query_path + path, **kwargs)
129
135
  except httpx.RequestError:
130
136
  raise TransportError("Could not complete the public query request.") from None
131
137
  return _decode(response, model)
periplus_sdk/dbapi.py ADDED
@@ -0,0 +1,324 @@
1
+ """Read-only DB-API 2.0 connection over the public query API.
2
+
3
+ Each execute is an independent server snapshot. Fetching consumes a bounded local
4
+ result, never a remote cursor. Connections and cursors must not be shared by threads.
5
+ """
6
+ from __future__ import annotations
7
+
8
+ import base64
9
+ import builtins
10
+ from collections.abc import Sequence
11
+ from datetime import date, datetime, time
12
+ from decimal import Decimal
13
+ from typing import Any, Literal
14
+ import warnings
15
+
16
+ from .client import Client
17
+ from .errors import ApiError, ConfigurationError, PeriplusError, ResponseError, TransportError
18
+ from .types import QueryResult
19
+
20
+ apilevel = "2.0"
21
+ threadsafety = 1
22
+ paramstyle = "qmark"
23
+
24
+
25
+ class Warning(builtins.Warning):
26
+ """DB-API warning."""
27
+
28
+
29
+ class TruncationWarning(Warning):
30
+ """The server returned only part of the query result."""
31
+
32
+
33
+ class Error(PeriplusError):
34
+ """Base DB-API error; API failures preserve their safe error attributes."""
35
+
36
+ def __init__(self, message: str, *, status_code: int | None = None,
37
+ code: str | None = None, retry_after_seconds: float | None = None):
38
+ super().__init__(message)
39
+ self.status_code = status_code
40
+ self.code = code
41
+ self.retry_after_seconds = retry_after_seconds
42
+
43
+
44
+ class InterfaceError(Error):
45
+ """Invalid connection or wire response."""
46
+
47
+
48
+ class DatabaseError(Error):
49
+ """Query failure."""
50
+
51
+
52
+ class DataError(DatabaseError):
53
+ """A value cannot be represented."""
54
+
55
+
56
+ class OperationalError(DatabaseError):
57
+ """Service, policy or transport failure."""
58
+
59
+
60
+ class IntegrityError(DatabaseError):
61
+ """Integrity constraint failure."""
62
+
63
+
64
+ class InternalError(DatabaseError):
65
+ """Internal query failure."""
66
+
67
+
68
+ class ProgrammingError(DatabaseError):
69
+ """Invalid SQL, parameters or cursor use."""
70
+
71
+
72
+ class NotSupportedError(DatabaseError):
73
+ """Operation is outside the read-only query contract."""
74
+
75
+
76
+ Date = date
77
+ Time = time
78
+ Timestamp = datetime
79
+ Binary = bytes
80
+
81
+
82
+ def DateFromTicks(ticks: float) -> date:
83
+ return datetime.fromtimestamp(ticks).date()
84
+
85
+
86
+ def TimeFromTicks(ticks: float) -> time:
87
+ return datetime.fromtimestamp(ticks).time()
88
+
89
+
90
+ def TimestampFromTicks(ticks: float) -> datetime:
91
+ return datetime.fromtimestamp(ticks)
92
+
93
+
94
+ _INTEGER_TYPES = {"TINYINT", "SMALLINT", "INTEGER", "BIGINT", "HUGEINT", "UTINYINT",
95
+ "USMALLINT", "UINTEGER", "UBIGINT", "UHUGEINT", "BIGNUM"}
96
+ _FLOAT_TYPES = {"FLOAT", "DOUBLE", "REAL"}
97
+ _TIME_TYPES = {"TIME", "TIME WITH TIME ZONE", "TIMETZ"}
98
+ _TIMESTAMP_TYPES = {"TIMESTAMP", "TIMESTAMP_S", "TIMESTAMP_MS", "TIMESTAMP_NS",
99
+ "TIMESTAMP WITH TIME ZONE", "TIMESTAMPTZ"}
100
+
101
+
102
+ class _TypeCategory:
103
+ def __init__(self, names: set[str], prefix: str = ""):
104
+ self.names, self.prefix = names, prefix
105
+
106
+ def __eq__(self, other: object) -> bool:
107
+ return isinstance(other, str) and (other in self.names or bool(self.prefix and other.startswith(self.prefix)))
108
+
109
+
110
+ STRING = _TypeCategory({"VARCHAR", "UUID", "JSON", "ENUM"})
111
+ BINARY = _TypeCategory({"BLOB"})
112
+ NUMBER = _TypeCategory(_INTEGER_TYPES | _FLOAT_TYPES | {"BOOLEAN"}, "DECIMAL(")
113
+ DATETIME = _TypeCategory({"DATE"} | _TIME_TYPES | _TIMESTAMP_TYPES)
114
+ ROWID = _TypeCategory(set())
115
+
116
+
117
+ def _value(value: Any, sql_type: str) -> Any:
118
+ if value is None:
119
+ return None
120
+ if sql_type in _INTEGER_TYPES:
121
+ return int(value)
122
+ if sql_type in _FLOAT_TYPES:
123
+ return float(value)
124
+ if sql_type.startswith("DECIMAL("):
125
+ return Decimal(str(value))
126
+ # Preserve infinities and out-of-range dates rather than clipping them.
127
+ if sql_type == "DATE":
128
+ try:
129
+ return date.fromisoformat(value)
130
+ except ValueError:
131
+ return value
132
+ if sql_type in _TIME_TYPES:
133
+ return time.fromisoformat(value)
134
+ if sql_type in _TIMESTAMP_TYPES:
135
+ try:
136
+ return datetime.fromisoformat(value)
137
+ except ValueError:
138
+ return value
139
+ if sql_type == "BLOB":
140
+ return base64.b64decode(value, validate=True)
141
+ # UUIDs remain strings, and nested/other types retain their JSON wire values.
142
+ return value
143
+
144
+
145
+ def _parameter(value: Any) -> Any:
146
+ if value is None or isinstance(value, (str, bool, int, float)):
147
+ return value
148
+ if isinstance(value, (Decimal, date, time)):
149
+ return str(value) if isinstance(value, Decimal) else value.isoformat()
150
+ raise ProgrammingError("Parameters must be scalar JSON values, Decimal, date, time or datetime; use explicit SQL casts for typed strings.")
151
+
152
+
153
+ class Connection:
154
+ """Marimo-discoverable, read-only connection; commit is a no-op."""
155
+
156
+ dialect = "duckdb"
157
+
158
+ def __init__(self, base_url: str | None = None, *, timeout: float = 140,
159
+ mode: Literal["stable", "experimental"] = "stable",
160
+ schema_version: str = "public_v1"):
161
+ try:
162
+ self._client = Client(base_url, timeout=timeout, mode=mode)
163
+ except ConfigurationError as exc:
164
+ raise InterfaceError(str(exc)) from exc
165
+ self.schema_version = schema_version
166
+ self.closed = False
167
+ self.last_result: QueryResult | None = None
168
+
169
+ def _check(self) -> None:
170
+ if self.closed:
171
+ raise InterfaceError("Connection is closed.")
172
+
173
+ def cursor(self) -> Cursor:
174
+ self._check()
175
+ return Cursor(self)
176
+
177
+ def execute(self, operation: str, parameters: Sequence[Any] | None = None) -> Cursor:
178
+ cursor = self.cursor()
179
+ try:
180
+ return cursor.execute(operation, parameters)
181
+ except BaseException:
182
+ cursor.close()
183
+ raise
184
+
185
+ def commit(self) -> None:
186
+ """No-op: each read executes in its own server transaction."""
187
+ self._check()
188
+
189
+ def rollback(self) -> None:
190
+ self._check()
191
+ raise NotSupportedError("Periplus has no client transactions to roll back.")
192
+
193
+ def close(self) -> None:
194
+ if not self.closed:
195
+ self._client.close()
196
+ self.closed = True
197
+ self.last_result = None
198
+
199
+ def __enter__(self) -> Connection:
200
+ self._check()
201
+ return self
202
+
203
+ def __exit__(self, *args: Any) -> None:
204
+ self.close()
205
+
206
+
207
+ def connect(base_url: str | None = None, *, timeout: float = 140,
208
+ mode: Literal["stable", "experimental"] = "stable",
209
+ schema_version: str = "public_v1") -> Connection:
210
+ return Connection(base_url, timeout=timeout, mode=mode, schema_version=schema_version)
211
+
212
+
213
+ class Cursor:
214
+ """A buffered result. Metadata stays on result after rows are consumed."""
215
+
216
+ arraysize = 1
217
+
218
+ def __init__(self, connection: Connection):
219
+ self.connection = connection
220
+ self.closed = False
221
+ self.result: QueryResult | None = None
222
+ self.description: list[tuple[Any, ...]] | None = None
223
+ self.rowcount = -1
224
+ self._rows: list[tuple[Any, ...]] = []
225
+ self._position = 0
226
+
227
+ def _check(self, *, result: bool = False) -> None:
228
+ self.connection._check()
229
+ if self.closed:
230
+ raise InterfaceError("Cursor is closed.")
231
+ if result and self.result is None:
232
+ raise ProgrammingError("Execute a query before fetching rows.")
233
+
234
+ def execute(self, operation: str, parameters: Sequence[Any] | None = None) -> Cursor:
235
+ self._check()
236
+ self.result, self.description, self.rowcount = None, None, -1
237
+ self._rows, self._position = [], 0
238
+ self.connection.last_result = None
239
+ if not isinstance(operation, str):
240
+ raise ProgrammingError("SQL must be a string.")
241
+ if parameters is not None and (not isinstance(parameters, Sequence) or isinstance(parameters, (str, bytes))):
242
+ raise ProgrammingError("Use a positional parameter sequence with ? placeholders.")
243
+ values = [_parameter(v) for v in parameters] if parameters is not None else []
244
+ try:
245
+ result = self.connection._client.execute(operation, values, schema_version=self.connection.schema_version)
246
+ except ApiError as exc:
247
+ error = ProgrammingError if exc.code == "sql_invalid" else OperationalError
248
+ raise error(str(exc), status_code=exc.status_code, code=exc.code,
249
+ retry_after_seconds=exc.retry_after_seconds) from exc
250
+ except TransportError as exc:
251
+ raise OperationalError(str(exc)) from exc
252
+ except ResponseError as exc:
253
+ raise InterfaceError(str(exc)) from exc
254
+ if len(result.columns) != len(result.types) or any(len(r) != len(result.columns) for r in result.rows):
255
+ raise InterfaceError("Query columns, types and rows have inconsistent widths.")
256
+ try:
257
+ rows = [tuple(_value(v, t) for v, t in zip(row, result.types, strict=True)) for row in result.rows]
258
+ except (ValueError, TypeError, ArithmeticError) as exc:
259
+ raise DataError("Query value does not match its SQL type.") from exc
260
+ self.result = self.connection.last_result = result
261
+ self.description = [(name, kind, None, None, None, None, None)
262
+ for name, kind in zip(result.columns, result.types, strict=True)]
263
+ self._rows = rows
264
+ self.rowcount = -1 if result.truncated else len(rows)
265
+ if result.truncated:
266
+ warnings.warn(f"Periplus returned a truncated result ({len(rows)} rows); inspect connection.last_result or cursor.result. Fetching does not retrieve additional rows.",
267
+ TruncationWarning, stacklevel=2)
268
+ return self
269
+
270
+ def fetchone(self) -> tuple[Any, ...] | None:
271
+ self._check(result=True)
272
+ if self._position == len(self._rows):
273
+ return None
274
+ row = self._rows[self._position]
275
+ self._position += 1
276
+ return row
277
+
278
+ def fetchmany(self, size: int | None = None) -> list[tuple[Any, ...]]:
279
+ self._check(result=True)
280
+ size = self.arraysize if size is None else size
281
+ if not isinstance(size, int) or size < 0:
282
+ raise ProgrammingError("Fetch size must be a non-negative integer.")
283
+ end = min(self._position + size, len(self._rows))
284
+ rows = self._rows[self._position:end]
285
+ self._position = end
286
+ return rows
287
+
288
+ def fetchall(self) -> list[tuple[Any, ...]]:
289
+ self._check(result=True)
290
+ return self.fetchmany(len(self._rows) - self._position)
291
+
292
+ def executemany(self, operation: str, seq_of_parameters: Any) -> None:
293
+ self._check()
294
+ raise NotSupportedError("Batch execution is not supported by the read-only query API.")
295
+
296
+ def setinputsizes(self, sizes: Any) -> None:
297
+ self._check()
298
+
299
+ def setoutputsize(self, size: int, column: int | None = None) -> None:
300
+ self._check()
301
+
302
+ def close(self) -> None:
303
+ self.closed = True
304
+ self._rows = []
305
+ self.result = None
306
+ self.description = None
307
+ self.rowcount = -1
308
+
309
+ def __iter__(self) -> Cursor:
310
+ self._check(result=True)
311
+ return self
312
+
313
+ def __next__(self) -> tuple[Any, ...]:
314
+ row = self.fetchone()
315
+ if row is None:
316
+ raise StopIteration
317
+ return row
318
+
319
+ def __enter__(self) -> Cursor:
320
+ self._check()
321
+ return self
322
+
323
+ def __exit__(self, *args: Any) -> None:
324
+ self.close()
@@ -0,0 +1,139 @@
1
+ """SQLAlchemy dialect for public Periplus queries and bounded reflection."""
2
+ from __future__ import annotations
3
+
4
+ from typing import Any
5
+
6
+ from sqlalchemy import exc, types
7
+ from sqlalchemy.engine import default
8
+ from sqlalchemy.engine.reflection import cache
9
+ from sqlalchemy.sql.compiler import IdentifierPreparer
10
+
11
+ from . import dbapi
12
+
13
+
14
+ class SQLType(types.UserDefinedType):
15
+ """Preserve DuckDB type names, including nested types, during reflection."""
16
+
17
+ cache_ok = True
18
+
19
+ def __init__(self, name: str):
20
+ self.name = name
21
+
22
+ def get_col_spec(self, **kw: Any) -> str:
23
+ return self.name
24
+
25
+ @property
26
+ def python_type(self) -> type:
27
+ if self.name in dbapi._INTEGER_TYPES:
28
+ return int
29
+ if self.name in dbapi._FLOAT_TYPES:
30
+ return float
31
+ if self.name == "BOOLEAN":
32
+ return bool
33
+ if self.name.startswith("DECIMAL("):
34
+ return dbapi.Decimal
35
+ if self.name == "DATE":
36
+ return dbapi.date
37
+ if self.name in dbapi._TIMESTAMP_TYPES:
38
+ return dbapi.datetime
39
+ if self.name in dbapi._TIME_TYPES:
40
+ return dbapi.time
41
+ if self.name == "BLOB":
42
+ return bytes
43
+ return str
44
+
45
+
46
+ class PeriplusDialect(default.DefaultDialect):
47
+ # The server speaks DuckDB SQL; this enables the correct notebook SQL dialect.
48
+ name = "duckdb"
49
+ driver = "periplus"
50
+ supports_statement_cache = False
51
+ supports_sane_rowcount = False
52
+ supports_sane_multi_rowcount = False
53
+ supports_native_decimal = True
54
+ default_paramstyle = "qmark"
55
+ preparer = IdentifierPreparer
56
+
57
+ @classmethod
58
+ def import_dbapi(cls):
59
+ return dbapi
60
+
61
+ def create_connect_args(self, url):
62
+ if url.username or url.password or url.host or url.port:
63
+ raise exc.ArgumentError("Use periplus:///public_v1 with base_url and mode in connect_args.")
64
+ if url.query:
65
+ raise exc.ArgumentError("Pass connection options in connect_args, not URL query parameters.")
66
+ return [], {"schema_version": url.database or "public_v1"}
67
+
68
+ def initialize(self, connection):
69
+ self.default_schema_name = connection.connection.dbapi_connection.schema_version
70
+
71
+ def do_rollback(self, dbapi_connection):
72
+ # SQLAlchemy resets pooled connections this way. There is no remote
73
+ # transaction: each read already completed in its own server snapshot.
74
+ pass
75
+
76
+ def do_begin(self, dbapi_connection):
77
+ pass
78
+
79
+ def do_commit(self, dbapi_connection):
80
+ dbapi_connection.commit()
81
+
82
+ def _schema(self, connection, schema):
83
+ current = connection.connection.dbapi_connection.schema_version
84
+ if schema is not None and schema != current:
85
+ raise exc.InvalidRequestError(f"Only the configured public schema {current!r} is available.")
86
+ return current
87
+
88
+ @cache
89
+ def get_schema_names(self, connection, **kw):
90
+ return [self._schema(connection, None)]
91
+
92
+ def _complete(self, result):
93
+ raw = result.cursor.result
94
+ if raw.truncated:
95
+ result.close()
96
+ raise exc.InvalidRequestError("Catalogue discovery was truncated by public query limits; refusing an incomplete schema.")
97
+ try:
98
+ return result.fetchall()
99
+ finally:
100
+ result.close()
101
+
102
+ @cache
103
+ def get_view_names(self, connection, schema=None, **kw):
104
+ schema = self._schema(connection, schema)
105
+ result = connection.exec_driver_sql(f"SHOW TABLES FROM {self.identifier_preparer.quote_identifier(schema)}")
106
+ return [row[0] for row in self._complete(result)]
107
+
108
+ @cache
109
+ def get_table_names(self, connection, schema=None, **kw):
110
+ self._schema(connection, schema)
111
+ # All queryable public catalogue relations are views.
112
+ return []
113
+
114
+ @cache
115
+ def has_table(self, connection, table_name, schema=None, **kw):
116
+ return table_name in self.get_view_names(connection, schema)
117
+
118
+ @cache
119
+ def get_columns(self, connection, table_name, schema=None, **kw):
120
+ schema = self._schema(connection, schema)
121
+ quote = self.identifier_preparer.quote_identifier
122
+ result = connection.exec_driver_sql(f"DESCRIBE {quote(schema)}.{quote(table_name)}")
123
+ return [{"name": row[0], "type": SQLType(row[1]), "nullable": row[2] != "NO",
124
+ "default": row[4]} for row in self._complete(result)]
125
+
126
+ @cache
127
+ def get_pk_constraint(self, connection, table_name, schema=None, **kw):
128
+ self._schema(connection, schema)
129
+ return {"name": None, "constrained_columns": []}
130
+
131
+ @cache
132
+ def get_foreign_keys(self, connection, table_name, schema=None, **kw):
133
+ self._schema(connection, schema)
134
+ return []
135
+
136
+ @cache
137
+ def get_indexes(self, connection, table_name, schema=None, **kw):
138
+ self._schema(connection, schema)
139
+ return []
periplus_sdk/types.py CHANGED
@@ -1,4 +1,6 @@
1
1
  """Public query wire types; SQL types and JSON values are preserved."""
2
+ from typing import Literal
3
+
2
4
  from pydantic import BaseModel, Field, JsonValue
3
5
 
4
6
 
@@ -9,6 +11,9 @@ class Diagnostic(BaseModel):
9
11
 
10
12
 
11
13
  class PreparedQuery(BaseModel):
14
+ query_mode: Literal["stable", "experimental"]
15
+ compiler_version: str
16
+ optimizations: list[str]
12
17
  schema_version: str
13
18
  query_id: str
14
19
  sql: str
@@ -1,123 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: periplus-python-sdk
3
- Version: 0.3.0
4
- Summary: Read-only Python client for the public Periplus query API
5
- License-Expression: Apache-2.0
6
- Project-URL: Repository, https://github.com/elei-io/periplus
7
- Project-URL: Issues, https://github.com/elei-io/periplus/issues
8
- Requires-Python: >=3.11
9
- Description-Content-Type: text/markdown
10
- License-File: LICENSE
11
- License-File: NOTICE
12
- Requires-Dist: httpx>=0.28
13
- Requires-Dist: pydantic<3,>=2.12
14
- Dynamic: license-file
15
-
16
- # Periplus Python SDK
17
-
18
- A read-only client for the public Periplus query API. Python 3.11 or later.
19
- Configure the **public web application URL**, not the internal query or control service.
20
- No API token, DuckDB installation or lake credentials are needed.
21
-
22
- ```python
23
- from periplus_sdk import Client
24
-
25
- with Client("http://localhost:8080") as client:
26
- result = client.execute(
27
- "SELECT capture_id FROM public_v1.capture LIMIT ?", [10]
28
- )
29
- print(result.columns, result.types)
30
- print(result.rows)
31
- print(result.source_snapshot, result.truncated)
32
- ```
33
-
34
- For a hosted deployment, replace the URL with its public HTTPS origin. Alternatively set
35
- `PERIPLUS_PUBLIC_URL` and use `Client()`. An optional URL path prefix is preserved.
36
- The client reuses HTTP connections; close it with a context manager or `close()`.
37
-
38
- ## Preparation and helpers
39
-
40
- ```python
41
- with Client("http://localhost:8080") as client:
42
- prepared = client.prepare("SELECT capture_id FROM public_v1.capture LIMIT ?", [10])
43
- print(prepared.diagnostics, prepared.plan)
44
- result = client.execute(prepared.sql, prepared.parameters)
45
- helpers = client.helpers()
46
- print(helpers.catalogue_version, helpers.helpers)
47
- ```
48
-
49
- Preparation validates and explains without executing the analytical query. Execution independently
50
- validates and prepares; a prior preparation never authorizes SQL. Linting, diagnostics and future
51
- SQL optimizations belong to the server. The SDK sends SQL unchanged.
52
-
53
- ## Async use
54
-
55
- ```python
56
- from periplus_sdk import AsyncClient
57
-
58
- async def observations():
59
- async with AsyncClient("http://localhost:8080") as client:
60
- return await client.execute("SELECT capture_id FROM public_v1.capture LIMIT 10")
61
- ```
62
-
63
- Use `aclose()` when managing an async client's lifetime explicitly.
64
-
65
- ## Permissions, results and errors
66
-
67
- - The same public SQL feature switch, shared rate budget, namespace validation and read-only
68
- execution apply as in the public web workspace. The SDK provides no writes, crawling,
69
- administrative controls or direct lake attachment.
70
- - Results retain `query_id`, SQL, parameters, diagnostics, plan, columns, SQL types, JSON rows,
71
- elapsed milliseconds, `source_snapshot` and `truncated`. Decimals and large integers remain
72
- strings exactly as returned by the server. Duplicate column names are preserved.
73
- - Operator-configured execution limits default to 1,000 rows, an 8 MiB result budget and a
74
- 20-second server deadline. Always inspect `truncated`. The SDK does not silently fetch more rows or retry.
75
- - `ApiError` exposes `status_code`, safe `code`, and `retry_after_seconds` when supplied.
76
- `TransportError` means HTTP failed; `ResponseError` means a malformed successful response.
77
- The client timeout defaults to 140 seconds and can be set with `timeout=`. A timeout or local
78
- cancellation does not guarantee server cancellation. Redirects are not followed automatically.
79
- - Preparation and execution are attributed to `sdk` in the existing private query history.
80
- Original SQL and parameters are retained for 30 days; result rows are not stored. Recording is
81
- best-effort and can be lost during outages or backpressure. This label is not a user identity.
82
-
83
- ## Installation and verification
84
-
85
- Install the public-v1 client from PyPI:
86
-
87
- ```sh
88
- python -m pip install "periplus-python-sdk>=0.3.0"
89
- ```
90
-
91
- Version 0.3.0 supports the current public-v1 contract. For production, configure
92
- `PERIPLUS_PUBLIC_URL=https://periplus.dev`; no API token is required.
93
- Run the installed package against an available public app:
94
-
95
- ```sh
96
- PERIPLUS_PUBLIC_URL=http://localhost:8080 python packages/periplus-python-sdk/examples/smoke.py
97
- ```
98
-
99
- ## Releasing
100
-
101
- Repository CI publishes immutable releases from tags named
102
- `periplus-python-sdk-v<version>`. The tag must exactly match the static version
103
- in `pyproject.toml`; for example, version `0.3.0` is released with:
104
-
105
- ```sh
106
- git tag periplus-python-sdk-v0.3.0
107
- git push origin periplus-python-sdk-v0.3.0
108
- ```
109
-
110
- PyPI publishing uses Trusted Publishing rather than a stored API token. The
111
- PyPI publisher must be configured for GitHub owner `elei-io`, repository
112
- `periplus`, workflow `python-sdk-release.yml`, and environment `pypi`. Protect
113
- that GitHub environment with required reviewers before the first release.
114
-
115
- ## Public v1
116
-
117
- Install the updated SDK from PyPI with `python -m pip install "periplus-python-sdk>=0.3.0"`. The previously published 0.2.0 release predates this contract. `prepare` and `execute` accept keyword-only `schema_version="public_v1"` (the default); responses preserve `schema_version` separately from `source_snapshot`. Unavailable versions are rejected by the server.
118
-
119
- ## License
120
-
121
- Copyright (c) 2026 Ekku Leivonen (elei.io). Licensed under [Apache-2.0](LICENSE);
122
- see [NOTICE](NOTICE). The server and other repository packages have separate
123
- licensing described in the root LICENSING.md.
@@ -1,11 +0,0 @@
1
- periplus_python_sdk-0.3.0.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
2
- periplus_python_sdk-0.3.0.dist-info/licenses/NOTICE,sha256=bhbYSqcUB3U_P1-XzloiT81JGniqoYaRLxNkQ1Pm9MQ,52
3
- periplus_sdk/__init__.py,sha256=bcJbEVnslFVfOdR74j7r8V1uap7pSD5KtNF2w-b01XY,508
4
- periplus_sdk/client.py,sha256=mZBBwR4pCAwylGATbXt2Rqj4YnzkaCpMF5LjoNWiTm8,5986
5
- periplus_sdk/errors.py,sha256=rB1n-v8Hc2tu2dtHivz-MlqsCoRC5pTcWogTfM7SMLw,855
6
- periplus_sdk/py.typed,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
7
- periplus_sdk/types.py,sha256=XmnrUsgOF7nqnPs7OZqqgZMXoAU95sYF8-mHq8_dolo,912
8
- periplus_python_sdk-0.3.0.dist-info/METADATA,sha256=8pGzaM_NZyFmJ3mppv00Imuov5PyWbM3Q-0eYM1WUpk,5323
9
- periplus_python_sdk-0.3.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
10
- periplus_python_sdk-0.3.0.dist-info/top_level.txt,sha256=o41t5TzwgoxzSmbKoP6olWW1FyEAGWVYTjeoKadBK40,13
11
- periplus_python_sdk-0.3.0.dist-info/RECORD,,