execsql2 2.35.0__py3-none-any.whl → 2.38.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.
- execsql/api.py +111 -53
- execsql/cli/commands/run.py +2 -2
- execsql/cli/lint.py +60 -7
- execsql/cli/run.py +10 -10
- execsql/config.py +7 -0
- execsql/data/execsql.conf.template +31 -7
- execsql/db/base.py +64 -26
- execsql/db/duckdb.py +51 -5
- execsql/db/firebird.py +8 -0
- execsql/db/mysql.py +8 -3
- execsql/db/oracle.py +5 -0
- execsql/db/postgres.py +2 -2
- execsql/db/sqlite.py +117 -10
- execsql/db/sqlserver.py +11 -7
- execsql/db/target_types.py +151 -4
- execsql/debug/repl.py +4 -3
- execsql/exceptions.py +14 -0
- execsql/exporters/html.py +26 -42
- execsql/exporters/ods.py +93 -21
- execsql/exporters/sqlite.py +82 -47
- execsql/format.py +12 -4
- execsql/gui/desktop.py +2 -1
- execsql/gui/tui.py +5 -2
- execsql/importers/base.py +9 -7
- execsql/importers/json.py +37 -13
- execsql/importers/ods.py +22 -2
- execsql/lsp/document.py +6 -1
- execsql/lsp/hover.py +2 -1
- execsql/metacommands/conditions.py +3 -1
- execsql/metacommands/connect.py +28 -12
- execsql/metacommands/dispatch.py +10 -0
- execsql/metacommands/io.py +1 -0
- execsql/metacommands/io_export.py +1 -2
- execsql/metacommands/io_fileops.py +32 -93
- execsql/metacommands/io_import.py +26 -8
- execsql/metacommands/io_write.py +3 -1
- execsql/metacommands/reference.py +7 -0
- execsql/metacommands/script_ext.py +13 -10
- execsql/metacommands/system.py +13 -6
- execsql/parser.py +14 -37
- execsql/script/ast.py +21 -0
- execsql/script/control.py +73 -3
- execsql/script/executor.py +503 -227
- execsql/script/parser.py +102 -52
- execsql/script/preparation.py +42 -0
- execsql/state.py +23 -0
- execsql/types.py +6 -5
- execsql/utils/errors.py +24 -6
- execsql/utils/fileio.py +342 -134
- execsql/utils/sigterm.py +154 -0
- execsql/utils/spreadsheet.py +74 -0
- execsql/utils/strings.py +13 -7
- {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/README.md +5 -5
- {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/example_config_prompt.sql +2 -2
- {execsql2-2.35.0.dist-info → execsql2-2.38.0.dist-info}/METADATA +32 -119
- {execsql2-2.35.0.dist-info → execsql2-2.38.0.dist-info}/RECORD +71 -68
- {execsql2-2.35.0.dist-info → execsql2-2.38.0.dist-info}/WHEEL +1 -1
- {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/config_settings.sqlite +0 -0
- {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/make_config_db.sql +0 -0
- {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/md_compare.sql +0 -0
- {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/md_glossary.sql +0 -0
- {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/md_upsert.sql +0 -0
- {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/pg_compare.sql +0 -0
- {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/pg_glossary.sql +0 -0
- {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/script_template.sql +0 -0
- {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/ss_compare.sql +0 -0
- {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/ss_glossary.sql +0 -0
- {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/ss_upsert.sql +0 -0
- {execsql2-2.35.0.dist-info → execsql2-2.38.0.dist-info}/entry_points.txt +0 -0
- {execsql2-2.35.0.dist-info → execsql2-2.38.0.dist-info}/licenses/LICENSE.txt +0 -0
- {execsql2-2.35.0.dist-info → execsql2-2.38.0.dist-info}/licenses/NOTICE +0 -0
execsql/db/target_types.py
CHANGED
|
@@ -21,7 +21,7 @@ import datetime
|
|
|
21
21
|
import math
|
|
22
22
|
import re
|
|
23
23
|
from collections.abc import Callable
|
|
24
|
-
from decimal import Decimal
|
|
24
|
+
from decimal import Decimal, InvalidOperation
|
|
25
25
|
from typing import Any, TypeAlias
|
|
26
26
|
|
|
27
27
|
from execsql.exceptions import ErrInfo
|
|
@@ -39,7 +39,15 @@ from execsql.types import (
|
|
|
39
39
|
DT_TimestampTZ,
|
|
40
40
|
)
|
|
41
41
|
|
|
42
|
-
__all__ = [
|
|
42
|
+
__all__ = [
|
|
43
|
+
"decimal_parameter",
|
|
44
|
+
"ColumnKinds",
|
|
45
|
+
"ColumnMetadata",
|
|
46
|
+
"existing_table_converter",
|
|
47
|
+
"kind_for_python_type",
|
|
48
|
+
"kind_for_type_name",
|
|
49
|
+
"kind_and_scale_for_type_name",
|
|
50
|
+
]
|
|
43
51
|
|
|
44
52
|
#: Lower-cased column name -> the execsql type values for it convert to, or None.
|
|
45
53
|
#: A string, not ``dict[...]``: on Python 3.10 that object passes
|
|
@@ -47,6 +55,17 @@ __all__ = ["ColumnKinds", "existing_table_converter", "kind_for_python_type", "k
|
|
|
47
55
|
#: would take it for one.
|
|
48
56
|
ColumnKinds: TypeAlias = "dict[str, type[DataType] | None]"
|
|
49
57
|
|
|
58
|
+
|
|
59
|
+
class ColumnMetadata(dict[str, type[DataType] | None]):
|
|
60
|
+
"""A compatible column-kind dictionary carrying known decimal scales."""
|
|
61
|
+
|
|
62
|
+
def __init__(self, columns: dict[str, tuple[type[DataType] | None, int | None]]) -> None:
|
|
63
|
+
super().__init__((name, kind) for name, (kind, _scale) in columns.items())
|
|
64
|
+
self.decimal_scales = {
|
|
65
|
+
name: scale for name, (kind, scale) in columns.items() if kind is DT_Decimal and scale is not None
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
50
69
|
_TEXT = {
|
|
51
70
|
"char",
|
|
52
71
|
"character",
|
|
@@ -162,6 +181,28 @@ def kind_for_type_name(type_name: str, *, oracle: bool = False) -> type[DataType
|
|
|
162
181
|
return None
|
|
163
182
|
|
|
164
183
|
|
|
184
|
+
def kind_and_scale_for_type_name(type_name: str, *, oracle: bool = False) -> tuple[type[DataType] | None, int | None]:
|
|
185
|
+
"""Return the existing kind and a declared decimal scale, if known."""
|
|
186
|
+
kind = kind_for_type_name(type_name, oracle=oracle)
|
|
187
|
+
if kind not in (DT_Decimal, DT_Long):
|
|
188
|
+
return kind, None
|
|
189
|
+
name = type_name.strip().lower()
|
|
190
|
+
base = " ".join(_PARAMS.sub(" ", name).replace(" unsigned", " ").replace(" zerofill", " ").split())
|
|
191
|
+
params = _PARAMS.search(name)
|
|
192
|
+
if base not in _DECIMAL or params is None:
|
|
193
|
+
return kind, None
|
|
194
|
+
args = [arg.strip() for arg in params.group(1).split(",")]
|
|
195
|
+
if len(args) == 1:
|
|
196
|
+
return kind, 0
|
|
197
|
+
if len(args) != 2:
|
|
198
|
+
return kind, None
|
|
199
|
+
try:
|
|
200
|
+
scale = int(args[1])
|
|
201
|
+
except ValueError:
|
|
202
|
+
scale = None
|
|
203
|
+
return kind, scale
|
|
204
|
+
|
|
205
|
+
|
|
165
206
|
def kind_for_python_type(type_code: object, scale: object = None) -> type[DataType] | None:
|
|
166
207
|
"""The execsql type for a column a driver describes by the Python type it returns (pyodbc, firebird-driver).
|
|
167
208
|
|
|
@@ -201,7 +242,8 @@ Converter = Callable[[Any], Any]
|
|
|
201
242
|
|
|
202
243
|
|
|
203
244
|
def _as_given(value: Any) -> Any:
|
|
204
|
-
|
|
245
|
+
# Bind decimal text ourselves before a driver can cast it through DOUBLE.
|
|
246
|
+
return str(value) if isinstance(value, Decimal) and value.is_finite() else value
|
|
205
247
|
|
|
206
248
|
|
|
207
249
|
def _whole_number_converter(column: str, table: str, default: Converter) -> Converter:
|
|
@@ -255,23 +297,128 @@ def _true_false_as_number(default: Converter) -> Converter:
|
|
|
255
297
|
return convert
|
|
256
298
|
|
|
257
299
|
|
|
300
|
+
def _check_decimal_scale(number: Decimal, value: Any, scale: int | None, column: str, table: str) -> None:
|
|
301
|
+
"""Check coefficient digits without quantizing, expanding or using context precision."""
|
|
302
|
+
if scale is None or not number.is_finite() or number.is_zero():
|
|
303
|
+
return
|
|
304
|
+
parts = number.as_tuple()
|
|
305
|
+
exponent = parts.exponent
|
|
306
|
+
assert isinstance(exponent, int)
|
|
307
|
+
discarded = -scale - exponent
|
|
308
|
+
if discarded <= 0:
|
|
309
|
+
return
|
|
310
|
+
if discarded >= len(parts.digits) or any(parts.digits[-discarded:]):
|
|
311
|
+
raise ErrInfo(
|
|
312
|
+
type="error",
|
|
313
|
+
other_msg=(
|
|
314
|
+
f"Can't load {value!r} into column {column} of {table}: the column's scale is {scale}, "
|
|
315
|
+
"and the database would round the value."
|
|
316
|
+
),
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def decimal_parameter(number: Decimal, binding_precision: int | None, column: str, table: str) -> Decimal | str:
|
|
321
|
+
"""Bind a finite Decimal exactly within an adapter's native parameter capacity."""
|
|
322
|
+
if binding_precision is None:
|
|
323
|
+
return number
|
|
324
|
+
if number.is_zero():
|
|
325
|
+
return Decimal(0).copy_sign(number)
|
|
326
|
+
parts = number.as_tuple()
|
|
327
|
+
exponent = parts.exponent
|
|
328
|
+
assert isinstance(exponent, int) # Only finite values reach this helper.
|
|
329
|
+
if exponent > 0:
|
|
330
|
+
if number.adjusted() >= binding_precision:
|
|
331
|
+
raise ErrInfo(
|
|
332
|
+
type="error",
|
|
333
|
+
other_msg=(
|
|
334
|
+
f"Can't load {number!r} into column {column} of {table}: "
|
|
335
|
+
f"the database supports at most {binding_precision} decimal digits."
|
|
336
|
+
),
|
|
337
|
+
)
|
|
338
|
+
# Expansion is bounded by the adapter, never by the input exponent.
|
|
339
|
+
return Decimal(format(number, "f"))
|
|
340
|
+
native_width = len(parts.digits)
|
|
341
|
+
if -exponent > native_width:
|
|
342
|
+
native_width = -exponent + 1
|
|
343
|
+
if native_width > binding_precision:
|
|
344
|
+
# Avoid a driver's silent Decimal-to-double fallback. Fixed text
|
|
345
|
+
# also avoids exponent parsing at the maximum supported scale.
|
|
346
|
+
if exponent >= -binding_precision:
|
|
347
|
+
return format(number, "f")
|
|
348
|
+
return str(number)
|
|
349
|
+
return number
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def _exact_decimal_converter(
|
|
353
|
+
default: Converter,
|
|
354
|
+
column: str,
|
|
355
|
+
table: str,
|
|
356
|
+
binding_precision: int | None,
|
|
357
|
+
scale: int | None,
|
|
358
|
+
) -> Converter:
|
|
359
|
+
"""Retain finite exact source numbers for an existing decimal column.
|
|
360
|
+
|
|
361
|
+
Floats and non-numeric values keep the scanned conversion; converting a
|
|
362
|
+
float here cannot recover source digits already lost by its reader.
|
|
363
|
+
"""
|
|
364
|
+
|
|
365
|
+
def convert(value: Any) -> Any:
|
|
366
|
+
if isinstance(value, bool):
|
|
367
|
+
return default(value)
|
|
368
|
+
if isinstance(value, int):
|
|
369
|
+
_check_decimal_scale(Decimal(value), value, scale, column, table)
|
|
370
|
+
return value
|
|
371
|
+
if isinstance(value, Decimal) and value.is_finite():
|
|
372
|
+
_check_decimal_scale(value, value, scale, column, table)
|
|
373
|
+
return decimal_parameter(value, binding_precision, column, table)
|
|
374
|
+
if isinstance(value, str) and _NUMBER_TEXT.fullmatch(value):
|
|
375
|
+
try:
|
|
376
|
+
number = Decimal(value)
|
|
377
|
+
except InvalidOperation:
|
|
378
|
+
return default(value)
|
|
379
|
+
if number.is_finite():
|
|
380
|
+
_check_decimal_scale(number, value, scale, column, table)
|
|
381
|
+
return decimal_parameter(number, binding_precision, column, table)
|
|
382
|
+
return default(value)
|
|
383
|
+
|
|
384
|
+
return convert
|
|
385
|
+
|
|
386
|
+
|
|
258
387
|
def existing_table_converter(
|
|
259
388
|
kind: type[DataType] | None,
|
|
260
389
|
scanned: type[DataType],
|
|
261
390
|
default: Converter,
|
|
262
391
|
column: str,
|
|
263
392
|
table: str,
|
|
393
|
+
*,
|
|
394
|
+
decimal_binding_precision: int | None = None,
|
|
395
|
+
decimal_scale: int | None = None,
|
|
264
396
|
) -> Converter:
|
|
265
397
|
"""How a value for *column* of existing table *table* is converted.
|
|
266
398
|
|
|
267
399
|
*kind* is the column's type (``None`` if execsql has none that fits),
|
|
268
400
|
*scanned* the type the source column scanned as, and *default* the
|
|
269
401
|
conversion to that scanned type, which a new table's values get.
|
|
402
|
+
*decimal_binding_precision* bounds native Decimal parameter conversion
|
|
403
|
+
for drivers that need it. *decimal_scale* rejects exact source values that
|
|
404
|
+
need rounding; None leaves the destination scale to the database.
|
|
270
405
|
"""
|
|
271
406
|
if kind is DT_Text:
|
|
272
407
|
return _as_given
|
|
273
408
|
if kind in (DT_Long, DT_Decimal, DT_Float) and scanned is DT_Boolean:
|
|
274
|
-
|
|
409
|
+
boolean_converter = _true_false_as_number(default)
|
|
410
|
+
if kind is DT_Decimal and decimal_scale is not None:
|
|
411
|
+
|
|
412
|
+
def checked_boolean(value: Any) -> Any:
|
|
413
|
+
result = boolean_converter(value)
|
|
414
|
+
if isinstance(result, (int, Decimal)):
|
|
415
|
+
_check_decimal_scale(Decimal(result), value, decimal_scale, column, table)
|
|
416
|
+
return result
|
|
417
|
+
|
|
418
|
+
return checked_boolean
|
|
419
|
+
return boolean_converter
|
|
275
420
|
if kind is DT_Long:
|
|
276
421
|
return _whole_number_converter(column, table, default)
|
|
422
|
+
if kind is DT_Decimal:
|
|
423
|
+
return _exact_decimal_converter(default, column, table, decimal_binding_precision, decimal_scale)
|
|
277
424
|
return default
|
execsql/debug/repl.py
CHANGED
|
@@ -6,8 +6,8 @@ Input runs as if it were written where the script paused, through
|
|
|
6
6
|
:mod:`execsql.interactive`: SQL (ending with ``;``, results shown as tables),
|
|
7
7
|
metacommands and blocks, in the paused script's variable scope and
|
|
8
8
|
transaction. A ``SUB`` typed here is still set
|
|
9
|
-
when the script resumes; a ``BREAK``
|
|
10
|
-
ends the run. An error ends only the input that caused it.
|
|
9
|
+
when the script resumes; a ``BREAK`` leaves the LOOP, SCRIPT or included file
|
|
10
|
+
the script paused in; ``HALT`` ends the run. An error ends only the input that caused it.
|
|
11
11
|
|
|
12
12
|
Commands to the REPL itself start with ``.``: ``.continue`` and ``.next``
|
|
13
13
|
resume the script, ``.where`` and ``.stack`` show where it paused, ``.quit``
|
|
@@ -27,6 +27,7 @@ from pathlib import Path
|
|
|
27
27
|
from typing import Any
|
|
28
28
|
|
|
29
29
|
import execsql.state as _state
|
|
30
|
+
from execsql.exceptions import ScriptExit
|
|
30
31
|
from execsql.interactive import Prompt, common_dot_command, read_eval_loop, unknown_dot_command
|
|
31
32
|
from execsql.utils.color import color_disabled_by_env
|
|
32
33
|
|
|
@@ -237,7 +238,7 @@ def _handle_dot_command(line: str) -> bool:
|
|
|
237
238
|
if command in ("continue", "c"):
|
|
238
239
|
return False
|
|
239
240
|
if command in ("abort", "q", "quit"):
|
|
240
|
-
raise
|
|
241
|
+
raise ScriptExit(1)
|
|
241
242
|
if command in ("next", "n"):
|
|
242
243
|
_enable_step_mode()
|
|
243
244
|
return False
|
execsql/exceptions.py
CHANGED
|
@@ -21,6 +21,7 @@ can import from a single location. Notable exceptions:
|
|
|
21
21
|
— spreadsheet I/O failures.
|
|
22
22
|
- :class:`ConsoleUIError` — GUI console errors.
|
|
23
23
|
- :class:`CondParserError` / :class:`NumericParserError` — parser failures.
|
|
24
|
+
- :class:`ScriptExit` — execsql ending the run on purpose (a ``SystemExit``).
|
|
24
25
|
|
|
25
26
|
Note:
|
|
26
27
|
The ``execsql`` top-level package re-exports a *separate*
|
|
@@ -49,6 +50,7 @@ __all__ = [
|
|
|
49
50
|
"ConsoleUIError",
|
|
50
51
|
"CondParserError",
|
|
51
52
|
"NumericParserError",
|
|
53
|
+
"ScriptExit",
|
|
52
54
|
]
|
|
53
55
|
|
|
54
56
|
|
|
@@ -272,3 +274,15 @@ class CondParserError(ExecSqlError):
|
|
|
272
274
|
|
|
273
275
|
class NumericParserError(ExecSqlError):
|
|
274
276
|
"""Raised for numeric-expression parse errors."""
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
class ScriptExit(SystemExit):
|
|
280
|
+
"""execsql ending the run on purpose.
|
|
281
|
+
|
|
282
|
+
Raised by ``exit_now()`` (``HALT``, an error that halts the run, a
|
|
283
|
+
cancelled ``PROMPT``), by a cancelled dialog, and by ``.quit`` / ``.abort``
|
|
284
|
+
at a ``BREAKPOINT`` prompt. A ``SystemExit``, so everything that catches
|
|
285
|
+
one still does. A plain ``SystemExit`` -- from a signal handler, execsql's
|
|
286
|
+
own SIGTERM handler, or quitting the Textual console -- is an interrupt:
|
|
287
|
+
the statement it stops is rolled back like a failed one.
|
|
288
|
+
"""
|
execsql/exporters/html.py
CHANGED
|
@@ -28,6 +28,26 @@ from execsql.utils.fileio import filewriter_close
|
|
|
28
28
|
__all__ = ["export_html", "export_cgi_html", "write_query_to_html", "write_query_to_cgi_html"]
|
|
29
29
|
|
|
30
30
|
|
|
31
|
+
def _write_table(f: Any, hdrs: list[str], rows: Any, desc: str | None) -> None:
|
|
32
|
+
"""Write the table fragment shared by standalone and CGI HTML exports."""
|
|
33
|
+
f.write("<table>\n")
|
|
34
|
+
if desc is not None:
|
|
35
|
+
f.write(f"<caption>{desc}</caption>\n")
|
|
36
|
+
f.write("<thead><tr>")
|
|
37
|
+
for h in hdrs:
|
|
38
|
+
f.write(f"<th>{html_mod.escape(str(h))}</th>")
|
|
39
|
+
f.write("</tr></thead>\n<tbody>\n")
|
|
40
|
+
for r in rows:
|
|
41
|
+
f.write("<tr>")
|
|
42
|
+
for v in r:
|
|
43
|
+
# `if v is not None`, not `if v`: 0, False, and an empty
|
|
44
|
+
# string are all falsy, and a zero measurement silently became
|
|
45
|
+
# an empty cell.
|
|
46
|
+
f.write(f"<td>{html_mod.escape(str(v)) if v is not None else ''}</td>")
|
|
47
|
+
f.write("</tr>\n")
|
|
48
|
+
f.write("</tbody>\n</table>\n")
|
|
49
|
+
|
|
50
|
+
|
|
31
51
|
def export_html(
|
|
32
52
|
outfile: str,
|
|
33
53
|
hdrs: list[str],
|
|
@@ -40,24 +60,6 @@ def export_html(
|
|
|
40
60
|
"""Write a complete HTML document containing a data table to a file or ZIP archive."""
|
|
41
61
|
conf = _state.conf
|
|
42
62
|
|
|
43
|
-
def write_table(f):
|
|
44
|
-
f.write("<table>\n")
|
|
45
|
-
if desc is not None:
|
|
46
|
-
f.write(f"<caption>{desc}</caption>\n")
|
|
47
|
-
f.write("<thead><tr>")
|
|
48
|
-
for h in hdrs:
|
|
49
|
-
f.write(f"<th>{html_mod.escape(str(h))}</th>")
|
|
50
|
-
f.write("</tr></thead>\n<tbody>\n")
|
|
51
|
-
for r in rows:
|
|
52
|
-
f.write("<tr>")
|
|
53
|
-
for v in r:
|
|
54
|
-
# `if v is not None`, not `if v`: 0, False, and an empty
|
|
55
|
-
# string are all falsy, and a zero measurement silently became
|
|
56
|
-
# an empty cell.
|
|
57
|
-
f.write(f"<td>{html_mod.escape(str(v)) if v is not None else ''}</td>")
|
|
58
|
-
f.write("</tr>\n")
|
|
59
|
-
f.write("</tbody>\n</table>\n")
|
|
60
|
-
|
|
61
63
|
script, lno = current_script_line()
|
|
62
64
|
# If not append, write a complete HTML document with header and table.
|
|
63
65
|
# If append and the file does not exist, write just the table.
|
|
@@ -112,7 +114,7 @@ def export_html(
|
|
|
112
114
|
f.write("tbody tr { border-bottom: 1px dotted #814324; }\n")
|
|
113
115
|
f.write("</style>")
|
|
114
116
|
f.write("\n</head>\n<body>\n")
|
|
115
|
-
|
|
117
|
+
_write_table(f, hdrs, rows, desc)
|
|
116
118
|
f.write("</body>\n</html>\n")
|
|
117
119
|
finally:
|
|
118
120
|
if outfile.lower() != "stdout":
|
|
@@ -120,14 +122,14 @@ def export_html(
|
|
|
120
122
|
elif not zipfile and append:
|
|
121
123
|
if outfile.lower() == "stdout":
|
|
122
124
|
f = sys.stdout
|
|
123
|
-
|
|
125
|
+
_write_table(f, hdrs, rows, desc)
|
|
124
126
|
elif not Path(outfile).is_file():
|
|
125
127
|
from execsql.utils.fileio import EncodedFile
|
|
126
128
|
|
|
127
129
|
ef = EncodedFile(outfile, conf.output_encoding)
|
|
128
130
|
f = ef.open("wt")
|
|
129
131
|
try:
|
|
130
|
-
|
|
132
|
+
_write_table(f, hdrs, rows, desc)
|
|
131
133
|
finally:
|
|
132
134
|
f.close()
|
|
133
135
|
else:
|
|
@@ -152,7 +154,7 @@ def export_html(
|
|
|
152
154
|
else:
|
|
153
155
|
t.write(line)
|
|
154
156
|
t.write("\n")
|
|
155
|
-
|
|
157
|
+
_write_table(t, hdrs, rows, desc)
|
|
156
158
|
t.write(remainder)
|
|
157
159
|
for line in f:
|
|
158
160
|
t.write(line)
|
|
@@ -183,24 +185,6 @@ def export_cgi_html(
|
|
|
183
185
|
"""Write a CGI-style HTML fragment (Content-Type header + table) to a file or ZIP archive."""
|
|
184
186
|
conf = _state.conf
|
|
185
187
|
|
|
186
|
-
def write_table(f):
|
|
187
|
-
f.write("<table>\n")
|
|
188
|
-
if desc is not None:
|
|
189
|
-
f.write(f"<caption>{desc}</caption>\n")
|
|
190
|
-
f.write("<thead><tr>")
|
|
191
|
-
for h in hdrs:
|
|
192
|
-
f.write(f"<th>{html_mod.escape(str(h))}</th>")
|
|
193
|
-
f.write("</tr></thead>\n<tbody>\n")
|
|
194
|
-
for r in rows:
|
|
195
|
-
f.write("<tr>")
|
|
196
|
-
for v in r:
|
|
197
|
-
# `if v is not None`, not `if v`: 0, False, and an empty
|
|
198
|
-
# string are all falsy, and a zero measurement silently became
|
|
199
|
-
# an empty cell.
|
|
200
|
-
f.write(f"<td>{html_mod.escape(str(v)) if v is not None else ''}</td>")
|
|
201
|
-
f.write("</tr>\n")
|
|
202
|
-
f.write("</tbody>\n</table>\n")
|
|
203
|
-
|
|
204
188
|
script, lno = current_script_line()
|
|
205
189
|
if zipfile or not append or (append and not Path(outfile).is_file()):
|
|
206
190
|
if zipfile is None:
|
|
@@ -216,7 +200,7 @@ def export_cgi_html(
|
|
|
216
200
|
f = ZipWriter(zipfile, outfile, append)
|
|
217
201
|
try:
|
|
218
202
|
f.write("Content-Type: text/html\n\n")
|
|
219
|
-
|
|
203
|
+
_write_table(f, hdrs, rows, desc)
|
|
220
204
|
finally:
|
|
221
205
|
if outfile.lower() != "stdout":
|
|
222
206
|
f.close()
|
|
@@ -229,7 +213,7 @@ def export_cgi_html(
|
|
|
229
213
|
ef = EncodedFile(outfile, conf.output_encoding)
|
|
230
214
|
f = ef.open("a")
|
|
231
215
|
try:
|
|
232
|
-
|
|
216
|
+
_write_table(f, hdrs, rows, desc)
|
|
233
217
|
finally:
|
|
234
218
|
if outfile.lower() != "stdout":
|
|
235
219
|
f.close()
|
execsql/exporters/ods.py
CHANGED
|
@@ -24,6 +24,7 @@ from execsql.script import current_script_line
|
|
|
24
24
|
from execsql.utils.errors import exception_desc, fatal_error, install_hint
|
|
25
25
|
from execsql.utils.fileio import filewriter_close
|
|
26
26
|
from execsql.utils.strings import unquoted
|
|
27
|
+
from execsql.utils.spreadsheet import CellTextBudget, SheetBudget
|
|
27
28
|
|
|
28
29
|
__all__ = ["OdsFile", "export_ods", "write_query_to_ods", "write_queries_to_ods"]
|
|
29
30
|
|
|
@@ -179,26 +180,36 @@ class OdsFile:
|
|
|
179
180
|
return sheet
|
|
180
181
|
return None
|
|
181
182
|
|
|
182
|
-
def sheet_data(self, sheetname: Any, junk_header_rows: int = 0) -> list:
|
|
183
|
-
"""
|
|
183
|
+
def sheet_data(self, sheetname: Any, junk_header_rows: int = 0, *, budget: SheetBudget | None = None) -> list:
|
|
184
|
+
"""Read a sheet after SKIP, checking optional budgets before expanding rows or padding."""
|
|
184
185
|
sheet = self.sheet_named(sheetname)
|
|
185
186
|
if not sheet:
|
|
186
187
|
raise OdsFileError(f"There is no sheet named {sheetname}")
|
|
187
188
|
|
|
188
189
|
rows: list[list | None] = []
|
|
189
190
|
pending_empty = 0
|
|
191
|
+
skip_rows = max(0, junk_header_rows)
|
|
190
192
|
for sheetrow in sheet.getElementsByType(of.table.TableRow):
|
|
191
|
-
|
|
192
|
-
|
|
193
|
+
repeat = _expansion_count(sheetrow, "numberrowsrepeated")
|
|
194
|
+
skipped = min(skip_rows, repeat)
|
|
195
|
+
skip_rows -= skipped
|
|
196
|
+
repeat -= skipped
|
|
197
|
+
if repeat == 0:
|
|
198
|
+
_validate_expansion_counts(sheetrow)
|
|
199
|
+
continue
|
|
200
|
+
data = _row_values(sheetrow, budget, row_repeat=repeat, pending_rows=pending_empty)
|
|
193
201
|
if not data:
|
|
194
202
|
# Empty rows are kept only when data follows them, so the
|
|
195
203
|
# filler rows a spreadsheet writes below the data are dropped.
|
|
196
204
|
pending_empty += repeat
|
|
197
205
|
continue
|
|
206
|
+
if budget is not None:
|
|
207
|
+
budget.add_row(data, repeat, pending_empty)
|
|
198
208
|
rows.extend(None for _ in range(pending_empty))
|
|
199
209
|
pending_empty = 0
|
|
200
210
|
rows.extend(list(data) for _ in range(repeat))
|
|
201
|
-
|
|
211
|
+
if junk_header_rows < 0:
|
|
212
|
+
rows = rows[junk_header_rows:]
|
|
202
213
|
# Every row is as wide as the widest, so a blank trailing header is
|
|
203
214
|
# still seen, and an empty row inside the data is a row of NULLs that
|
|
204
215
|
# EMPTY_ROWS keeps or drops.
|
|
@@ -309,20 +320,55 @@ class OdsFile:
|
|
|
309
320
|
self.wbk = None
|
|
310
321
|
|
|
311
322
|
|
|
312
|
-
def
|
|
323
|
+
def _expansion_count(node: Any, attribute: str) -> int:
|
|
324
|
+
"""Read a non-negative ODS repeat/span/space count without expanding it."""
|
|
325
|
+
value = node.getAttribute(attribute) or "1"
|
|
326
|
+
try:
|
|
327
|
+
count = int(value)
|
|
328
|
+
if count < 0:
|
|
329
|
+
raise ValueError("negative count")
|
|
330
|
+
except (ValueError, TypeError) as exc:
|
|
331
|
+
shown = value[:120] + "..." if isinstance(value, str) and len(value) > 120 else value
|
|
332
|
+
raise ErrInfo(type="error", other_msg=f"Invalid ODS expansion count {shown!r} for {attribute}.") from exc
|
|
333
|
+
return count
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def _validate_expansion_counts(node: Any) -> None:
|
|
337
|
+
"""Validate skipped content without allocating its repeated rows or text."""
|
|
338
|
+
qname = getattr(node, "qname", None)
|
|
339
|
+
if qname == (of.table.TABLENS, "table-row"):
|
|
340
|
+
_expansion_count(node, "numberrowsrepeated")
|
|
341
|
+
elif qname in ((of.table.TABLENS, "table-cell"), (of.table.TABLENS, "covered-table-cell")):
|
|
342
|
+
_expansion_count(node, "numbercolumnsrepeated")
|
|
343
|
+
if qname == (of.table.TABLENS, "table-cell"):
|
|
344
|
+
_expansion_count(node, "numbercolumnsspanned")
|
|
345
|
+
_expansion_count(node, "numberrowsspanned")
|
|
346
|
+
elif qname == (of.text.TEXTNS, "s"):
|
|
347
|
+
_expansion_count(node, "c")
|
|
348
|
+
for child in getattr(node, "childNodes", ()):
|
|
349
|
+
_validate_expansion_counts(child)
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def _paragraph_text(node: Any, budget: CellTextBudget | None = None) -> str:
|
|
313
353
|
"""Return the text of a ``<text:p>``, expanding ODF's space, tab and line-break elements."""
|
|
314
354
|
parts: list[str] = []
|
|
315
355
|
for child in node.childNodes:
|
|
316
356
|
if child.nodeType == child.TEXT_NODE:
|
|
317
|
-
|
|
357
|
+
value = str(child)
|
|
358
|
+
if budget is not None:
|
|
359
|
+
budget.add(len(value))
|
|
318
360
|
elif child.qname == (of.text.TEXTNS, "s"):
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
361
|
+
count = _expansion_count(child, "c")
|
|
362
|
+
if budget is not None:
|
|
363
|
+
budget.add(count)
|
|
364
|
+
value = " " * count
|
|
365
|
+
elif child.qname in ((of.text.TEXTNS, "tab"), (of.text.TEXTNS, "line-break")):
|
|
366
|
+
if budget is not None:
|
|
367
|
+
budget.add(1)
|
|
368
|
+
value = "\t" if child.qname == (of.text.TEXTNS, "tab") else "\n"
|
|
324
369
|
else:
|
|
325
|
-
|
|
370
|
+
value = _paragraph_text(child, budget)
|
|
371
|
+
parts.append(value)
|
|
326
372
|
return "".join(parts)
|
|
327
373
|
|
|
328
374
|
|
|
@@ -336,7 +382,7 @@ def _time_text(duration: str) -> str:
|
|
|
336
382
|
return f"{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}{fraction}"
|
|
337
383
|
|
|
338
384
|
|
|
339
|
-
def _cell_value(cell: Any) -> str | None:
|
|
385
|
+
def _cell_value(cell: Any, budget: CellTextBudget | None = None) -> str | None:
|
|
340
386
|
"""Return a cell's stored value as text, or ``None`` for an empty cell.
|
|
341
387
|
|
|
342
388
|
The value comes from the cell's ``office:value-type`` and value
|
|
@@ -361,25 +407,39 @@ def _cell_value(cell: Any) -> str | None:
|
|
|
361
407
|
else:
|
|
362
408
|
value = None
|
|
363
409
|
if value:
|
|
410
|
+
if budget is not None:
|
|
411
|
+
budget.add(len(value))
|
|
364
412
|
return value
|
|
365
413
|
if vtype == "string":
|
|
366
414
|
value = cell.getAttribute("stringvalue")
|
|
367
415
|
if value:
|
|
416
|
+
if budget is not None:
|
|
417
|
+
budget.add(len(value))
|
|
368
418
|
return value
|
|
369
419
|
# A string cell, a cell with no type, or a typed cell missing its value
|
|
370
420
|
# attribute: the paragraphs are the value, one line each.
|
|
371
|
-
paragraphs = [
|
|
372
|
-
|
|
373
|
-
|
|
421
|
+
paragraphs: list[str] = []
|
|
422
|
+
for child in cell.childNodes:
|
|
423
|
+
if getattr(child, "qname", None) == (of.text.TEXTNS, "p"):
|
|
424
|
+
if paragraphs and budget is not None:
|
|
425
|
+
budget.add(1)
|
|
426
|
+
paragraphs.append(_paragraph_text(child, budget))
|
|
374
427
|
text = "\n".join(paragraphs)
|
|
375
428
|
return text or None
|
|
376
429
|
|
|
377
430
|
|
|
378
|
-
def _row_values(
|
|
431
|
+
def _row_values(
|
|
432
|
+
sheetrow: Any,
|
|
433
|
+
budget: SheetBudget | None = None,
|
|
434
|
+
*,
|
|
435
|
+
row_repeat: int = 1,
|
|
436
|
+
pending_rows: int = 0,
|
|
437
|
+
) -> list:
|
|
379
438
|
"""Return one value per column of a ``<table:table-row>``, without trailing empty cells."""
|
|
380
439
|
values: list = []
|
|
381
440
|
pending_empty = 0
|
|
382
441
|
covered_by_span = 0
|
|
442
|
+
row_text = 0
|
|
383
443
|
for cell in sheetrow.childNodes:
|
|
384
444
|
qname = getattr(cell, "qname", None)
|
|
385
445
|
if qname == (of.table.TABLENS, "table-cell"):
|
|
@@ -388,7 +448,7 @@ def _row_values(sheetrow: Any) -> list:
|
|
|
388
448
|
covered = True
|
|
389
449
|
else:
|
|
390
450
|
continue
|
|
391
|
-
repeat =
|
|
451
|
+
repeat = _expansion_count(cell, "numbercolumnsrepeated")
|
|
392
452
|
if covered:
|
|
393
453
|
# A horizontal merge repeats its value across the columns it
|
|
394
454
|
# covers (below); covered cells left over belong to a merge from
|
|
@@ -398,11 +458,20 @@ def _row_values(sheetrow: Any) -> list:
|
|
|
398
458
|
repeat -= skip
|
|
399
459
|
value = None
|
|
400
460
|
else:
|
|
401
|
-
|
|
402
|
-
|
|
461
|
+
spanned = _expansion_count(cell, "numbercolumnsspanned")
|
|
462
|
+
_expansion_count(cell, "numberrowsspanned")
|
|
403
463
|
if spanned > 1:
|
|
404
464
|
covered_by_span = spanned - 1
|
|
405
465
|
repeat *= spanned
|
|
466
|
+
if repeat == 0:
|
|
467
|
+
_validate_expansion_counts(cell)
|
|
468
|
+
continue
|
|
469
|
+
text_budget = (
|
|
470
|
+
CellTextBudget(budget, budget.text_chars + row_text * row_repeat, repeat * row_repeat)
|
|
471
|
+
if budget is not None
|
|
472
|
+
else None
|
|
473
|
+
)
|
|
474
|
+
value = _cell_value(cell, text_budget)
|
|
406
475
|
if repeat <= 0:
|
|
407
476
|
continue
|
|
408
477
|
if value is None:
|
|
@@ -410,6 +479,9 @@ def _row_values(sheetrow: Any) -> list:
|
|
|
410
479
|
# repeated across the whole sheet width costs nothing.
|
|
411
480
|
pending_empty += repeat
|
|
412
481
|
continue
|
|
482
|
+
if budget is not None:
|
|
483
|
+
budget.check_row(len(values) + pending_empty + repeat, row_repeat, pending_rows)
|
|
484
|
+
row_text += len(value) * repeat
|
|
413
485
|
values.extend([None] * pending_empty)
|
|
414
486
|
pending_empty = 0
|
|
415
487
|
values.extend([value] * repeat)
|