flexablelib 2.0.17__tar.gz → 2.0.19__tar.gz
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.
- {flexablelib-2.0.17 → flexablelib-2.0.19}/CHANGELOG.md +70 -0
- {flexablelib-2.0.17/flexablelib.egg-info → flexablelib-2.0.19}/PKG-INFO +3 -3
- {flexablelib-2.0.17 → flexablelib-2.0.19}/README.md +2 -2
- {flexablelib-2.0.17 → flexablelib-2.0.19}/flexablelib/cells.py +19 -4
- {flexablelib-2.0.17 → flexablelib-2.0.19}/flexablelib/colors.py +42 -10
- {flexablelib-2.0.17 → flexablelib-2.0.19}/flexablelib/columns.py +11 -2
- {flexablelib-2.0.17 → flexablelib-2.0.19}/flexablelib/merges.py +21 -5
- {flexablelib-2.0.17 → flexablelib-2.0.19}/flexablelib/readers.py +139 -14
- {flexablelib-2.0.17 → flexablelib-2.0.19}/flexablelib/table.py +210 -74
- {flexablelib-2.0.17 → flexablelib-2.0.19}/flexablelib/table_strings.py +42 -2
- {flexablelib-2.0.17 → flexablelib-2.0.19}/flexablelib/writers.py +8 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19/flexablelib.egg-info}/PKG-INFO +3 -3
- {flexablelib-2.0.17 → flexablelib-2.0.19}/flexablelib.egg-info/SOURCES.txt +3 -0
- flexablelib-2.0.19/package.json +16 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tests/test_v217_features.py +14 -3
- flexablelib-2.0.19/tests/test_v218_features.py +108 -0
- flexablelib-2.0.19/tests/test_v219_coverage.py +162 -0
- flexablelib-2.0.19/tests/test_v219_fixes.py +480 -0
- flexablelib-2.0.17/package.json +0 -47
- {flexablelib-2.0.17 → flexablelib-2.0.19}/LICENSE +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/MANIFEST.in +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/flexablelib/__init__.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/flexablelib/_speedups.c +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/flexablelib/_width_tables.h +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/flexablelib/fast.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/flexablelib/options.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/flexablelib/py.typed +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/flexablelib/style_compositions.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/flexablelib/text_width.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/flexablelib/utils.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/flexablelib.egg-info/dependency_links.txt +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/flexablelib.egg-info/requires.txt +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/flexablelib.egg-info/top_level.txt +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/pytest.ini +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/setup.cfg +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/setup.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/style_examples.md +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tests/conftest.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tests/helpers.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tests/test_cells.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tests/test_columns.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tests/test_excel.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tests/test_features.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tests/test_issues.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tests/test_merge_edge_matrix.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tests/test_merges.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tests/test_readme_examples.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tests/test_styles.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tests/test_text_width.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tests/test_two_pass_measurement.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tests/test_upstream_issues.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tests/test_v213_features.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tests/test_v214_features.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tests/test_v215_features.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tests/test_v216_features.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tools/benchmark.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tools/generate_width_tables.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tools/readme_gallery.py +0 -0
- {flexablelib-2.0.17 → flexablelib-2.0.19}/tools/text_to_png.py +0 -0
|
@@ -2,6 +2,76 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [2.0.19] - 2026-11-01
|
|
6
|
+
|
|
7
|
+
### Fixed
|
|
8
|
+
- ``column_max_width`` trims by terminal columns and line by line: CJK text
|
|
9
|
+
is cut to fit, emoji ZWJ sequences are no longer over-trimmed and
|
|
10
|
+
multi-line cells keep their lines instead of collapsing to ``...``.
|
|
11
|
+
- A width cap never narrows a column below content that cannot shrink (the
|
|
12
|
+
index column, a float's integer digits, a 2-column glyph in a 1-column
|
|
13
|
+
cap); the frame used to come out narrower than the text.
|
|
14
|
+
- ``show_index`` with ``column_max_width`` raised ``IndexError`` or trimmed
|
|
15
|
+
the wrong column.
|
|
16
|
+
- Float columns fill wider columns (``column_min_width``, a long header)
|
|
17
|
+
instead of leaving rows short.
|
|
18
|
+
- A numeric-looking header (``'0'``, ``'2024'``) over a float column crashed
|
|
19
|
+
with ``valign`` and overflowed without it; it is now right-aligned.
|
|
20
|
+
- ``sort_by`` on mixed types or ``None`` raised ``TypeError``. Numbers sort
|
|
21
|
+
before text, other types group by type, and missing values (``None``,
|
|
22
|
+
NaN) always go last, also with ``sort_reverse``.
|
|
23
|
+
- ``border_color`` and ``border_text`` emitted ANSI escapes with
|
|
24
|
+
``use_colors=False``, under ``NO_COLOR`` or into a pipe (regression from
|
|
25
|
+
2.0.11). Coloured themes now leave ``use_colors`` on auto instead of
|
|
26
|
+
forcing it on, so ``apply_theme`` output to a file stays clean; set
|
|
27
|
+
``use_colors = True`` to force colour.
|
|
28
|
+
- Left/right ``border_text`` overwrote cell text on styles without an outer
|
|
29
|
+
border, and started on the title line when a ``title`` was set.
|
|
30
|
+
- ``sticks`` alignment indicators overhung columns narrower than 3.
|
|
31
|
+
- ``add_divider`` on styles without a body rule (``dashes``,
|
|
32
|
+
``grid_eheader``) drew a rule too wide after a wrapped row.
|
|
33
|
+
- Merged cells follow ``cell_padding`` (0, 2, 3...); ``hrules`` with merges
|
|
34
|
+
on styles whose frame has no horizontal glyph raised ``TypeError``.
|
|
35
|
+
- Three or more repeated headers (``['x', 'x', 'x']``) produced a duplicate
|
|
36
|
+
name and ``compose()`` raised ``IndexError``.
|
|
37
|
+
- Readers: ``from_markdown`` unescapes ``\|``; ``from_records`` accepts the
|
|
38
|
+
dicts ``to_records`` returns; ``from_html`` reads ``to_html`` pages, lays
|
|
39
|
+
out ``rowspan``/``colspan`` (later cells used to slide left) and restores
|
|
40
|
+
them as merges, and takes the ``<th>`` header from the selected table.
|
|
41
|
+
- HTML/XLSX colours: a background-only ``'on red'`` exported as red text,
|
|
42
|
+
and ``greyNN`` / ``color:N`` colours exported nothing.
|
|
43
|
+
- ``to_latex`` escapes ``|``, ``<`` and ``>``.
|
|
44
|
+
|
|
45
|
+
### Tests
|
|
46
|
+
- Regression tests for every fix above and behaviour tests for exporters,
|
|
47
|
+
readers and colour helpers (coverage 87% -> 90%).
|
|
48
|
+
|
|
49
|
+
## [2.0.18] - 2026-10-31
|
|
50
|
+
|
|
51
|
+
### Fixed
|
|
52
|
+
- ``border_text(..., align='right' | 'center')`` now works with the default
|
|
53
|
+
offset; you no longer have to pass ``offset=None``. The ``offset`` default
|
|
54
|
+
is now ``None`` (automatic: a one-cell margin inside the frame corners).
|
|
55
|
+
An explicit ``offset`` is a margin from the aligned edge: the start column
|
|
56
|
+
for ``left`` (unchanged), the gap before the right edge for ``right``, and
|
|
57
|
+
the margin on both sides for ``center``. Default (left) placement is
|
|
58
|
+
unchanged.
|
|
59
|
+
- ``border_text`` on ``where='left' | 'right'`` honours ``align`` too
|
|
60
|
+
(``left`` = top, ``center``, ``right`` = bottom) and, by default, no longer
|
|
61
|
+
overwrites the top corner.
|
|
62
|
+
- ``border_text`` rejects a negative or non-integer ``offset`` with a clear
|
|
63
|
+
error.
|
|
64
|
+
|
|
65
|
+
### Packaging
|
|
66
|
+
- CPython **3.14** wheels (Linux manylinux/musllinux, Windows, macOS) via
|
|
67
|
+
cibuildwheel 3.4.1.
|
|
68
|
+
- Removed the unused npm toolchain (commitlint, husky, standard-version…) and
|
|
69
|
+
``package-lock.json``. ``package.json`` only carries the version number now,
|
|
70
|
+
which clears all 35 Dependabot alerts; none of them ever reached PyPI users.
|
|
71
|
+
- The test suite passes against an installed package with only ``tests/``,
|
|
72
|
+
``pytest.ini`` and ``style_examples.md`` copied next to it (the conda-forge
|
|
73
|
+
recipe layout, #26): checks that need a source checkout now skip there.
|
|
74
|
+
|
|
5
75
|
## [2.0.17] - 2026-10-31
|
|
6
76
|
|
|
7
77
|
### Security
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: flexablelib
|
|
3
|
-
Version: 2.0.
|
|
3
|
+
Version: 2.0.19
|
|
4
4
|
Summary: Fast, dependency-free terminal tables for Python: 56 border styles, ANSI colors, cell merges, decimal alignment, width fitting, and CSV/Markdown/HTML/Excel/LaTeX I/O.
|
|
5
5
|
Home-page: https://github.com/Opus-Perpetuus/flexablelib
|
|
6
6
|
Author: Benjamin Ramirez
|
|
@@ -204,7 +204,7 @@ one place.
|
|
|
204
204
|
tabulate and ~18× faster than rich's table. pandas `to_string()` is faster
|
|
205
205
|
still (~85 ms). Full method and numbers:
|
|
206
206
|
[docs/BENCHMARKS.md](https://github.com/Opus-Perpetuus/flexablelib/blob/main/docs/BENCHMARKS.md).
|
|
207
|
-
- **Wheels.** Prebuilt for CPython 3.8 – 3.
|
|
207
|
+
- **Wheels.** Prebuilt for CPython 3.8 – 3.14 on Linux (manylinux, musllinux),
|
|
208
208
|
Windows x64 and macOS arm64. Anywhere else it builds from source or falls
|
|
209
209
|
back to pure Python — installation never fails for want of a compiler.
|
|
210
210
|
`from flexablelib.fast import implementation; implementation()` tells you
|
|
@@ -479,7 +479,7 @@ flexablelib/ # GitHub repository
|
|
|
479
479
|
├── logos/ brand assets — see logos/README.md
|
|
480
480
|
├── ARCHITECTURE.md how it all fits together
|
|
481
481
|
├── style_examples.md all 56 styles rendered
|
|
482
|
-
├── package.json canonical version number
|
|
482
|
+
├── package.json canonical version number (no npm deps)
|
|
483
483
|
└── setup.py reads the version from package.json
|
|
484
484
|
```
|
|
485
485
|
|
|
@@ -139,7 +139,7 @@ one place.
|
|
|
139
139
|
tabulate and ~18× faster than rich's table. pandas `to_string()` is faster
|
|
140
140
|
still (~85 ms). Full method and numbers:
|
|
141
141
|
[docs/BENCHMARKS.md](https://github.com/Opus-Perpetuus/flexablelib/blob/main/docs/BENCHMARKS.md).
|
|
142
|
-
- **Wheels.** Prebuilt for CPython 3.8 – 3.
|
|
142
|
+
- **Wheels.** Prebuilt for CPython 3.8 – 3.14 on Linux (manylinux, musllinux),
|
|
143
143
|
Windows x64 and macOS arm64. Anywhere else it builds from source or falls
|
|
144
144
|
back to pure Python — installation never fails for want of a compiler.
|
|
145
145
|
`from flexablelib.fast import implementation; implementation()` tells you
|
|
@@ -414,7 +414,7 @@ flexablelib/ # GitHub repository
|
|
|
414
414
|
├── logos/ brand assets — see logos/README.md
|
|
415
415
|
├── ARCHITECTURE.md how it all fits together
|
|
416
416
|
├── style_examples.md all 56 styles rendered
|
|
417
|
-
├── package.json canonical version number
|
|
417
|
+
├── package.json canonical version number (no npm deps)
|
|
418
418
|
└── setup.py reads the version from package.json
|
|
419
419
|
```
|
|
420
420
|
|
|
@@ -186,6 +186,21 @@ def fljust(string: str,
|
|
|
186
186
|
return ''.join([left, point or fill_char, right])
|
|
187
187
|
|
|
188
188
|
|
|
189
|
+
def __fill_to(aligned: str, cell_length: int, fill_char: str) -> str:
|
|
190
|
+
"""
|
|
191
|
+
Right-align a decimal-aligned block within a wider column.
|
|
192
|
+
|
|
193
|
+
The block is exactly as wide as the column's widest number. When the
|
|
194
|
+
column is wider than that -- a ``column_min_width``, a long header, or
|
|
195
|
+
a text cell such as ``None`` -- the numbers came out short and the row
|
|
196
|
+
border moved left. Padding every block on the left by the same amount
|
|
197
|
+
keeps the decimal points on one axis.
|
|
198
|
+
"""
|
|
199
|
+
if visible_width(aligned) < cell_length:
|
|
200
|
+
return pad_to_width(aligned, cell_length, 'r', fill_char)
|
|
201
|
+
return aligned
|
|
202
|
+
|
|
203
|
+
|
|
189
204
|
def __fljust_part(cell_part: Any,
|
|
190
205
|
cell_length: int,
|
|
191
206
|
float_column_sizes: Union[List[int], Tuple[int, int, int]],
|
|
@@ -228,11 +243,11 @@ def __fljust_part(cell_part: Any,
|
|
|
228
243
|
|
|
229
244
|
# If it is a float, align it as a float.
|
|
230
245
|
if is_float_number:
|
|
231
|
-
return fljust(
|
|
246
|
+
return __fill_to(fljust(
|
|
232
247
|
str_cell,
|
|
233
248
|
float_column_sizes,
|
|
234
249
|
fill_char
|
|
235
|
-
)
|
|
250
|
+
), cell_length, fill_char)
|
|
236
251
|
|
|
237
252
|
# If it is an int, align it as an int.
|
|
238
253
|
# Will be aligned to the int part of the float.
|
|
@@ -266,13 +281,13 @@ def __fljust_part(cell_part: Any,
|
|
|
266
281
|
fixed_right_width = right_width + float_column_sizes[POINT_WIDTH_I]
|
|
267
282
|
|
|
268
283
|
# Add the spaces to the left and right.
|
|
269
|
-
return _add_cell_spacing(
|
|
284
|
+
return __fill_to(_add_cell_spacing(
|
|
270
285
|
str_cell,
|
|
271
286
|
fixed_left_width,
|
|
272
287
|
fixed_right_width,
|
|
273
288
|
0,
|
|
274
289
|
fill_char
|
|
275
|
-
)
|
|
290
|
+
), cell_length, fill_char)
|
|
276
291
|
|
|
277
292
|
# If it is not a float nor an int, align it to the right.
|
|
278
293
|
else:
|
|
@@ -269,7 +269,12 @@ def _split_spec(spec: Union[str, int, None]):
|
|
|
269
269
|
return [], None
|
|
270
270
|
if isinstance(spec, int):
|
|
271
271
|
return [], None
|
|
272
|
-
|
|
272
|
+
lowered = str(spec).lower().strip()
|
|
273
|
+
# Same grammar as parse_spec: a bare 'on <bg>' is background-only.
|
|
274
|
+
if lowered.startswith('on '):
|
|
275
|
+
foreground_part, background_part = '', lowered[3:]
|
|
276
|
+
else:
|
|
277
|
+
foreground_part, _, background_part = lowered.partition(' on ')
|
|
273
278
|
fg_tokens = foreground_part.split()
|
|
274
279
|
bg_token = None
|
|
275
280
|
if background_part:
|
|
@@ -278,15 +283,44 @@ def _split_spec(spec: Union[str, int, None]):
|
|
|
278
283
|
return fg_tokens, bg_token
|
|
279
284
|
|
|
280
285
|
|
|
286
|
+
def _xterm_hex(index: int) -> Optional[str]:
|
|
287
|
+
"""sRGB hex for an xterm 256-colour index (16..255; 0..15 are themed)."""
|
|
288
|
+
if 16 <= index <= 231:
|
|
289
|
+
index -= 16
|
|
290
|
+
steps = [0, 95, 135, 175, 215, 255]
|
|
291
|
+
rgb = (steps[index // 36], steps[(index // 6) % 6], steps[index % 6])
|
|
292
|
+
elif 232 <= index <= 255:
|
|
293
|
+
level = 8 + 10 * (index - 232)
|
|
294
|
+
rgb = (level, level, level)
|
|
295
|
+
else:
|
|
296
|
+
return None
|
|
297
|
+
return '{0:02X}{1:02X}{2:02X}'.format(*rgb)
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
def _token_hex(token: str, named: dict) -> Optional[str]:
|
|
301
|
+
"""Hex for one colour token: #rrggbb, a name, greyNN or a 256 index."""
|
|
302
|
+
hex_match = _HEX_RE.match(token)
|
|
303
|
+
if hex_match:
|
|
304
|
+
return hex_match.group(1).upper()
|
|
305
|
+
if token in named:
|
|
306
|
+
return named[token]
|
|
307
|
+
grey_match = _GREY_RE.match(token)
|
|
308
|
+
if grey_match and int(grey_match.group(1)) <= 100:
|
|
309
|
+
return _xterm_hex(_grey_index(int(grey_match.group(1))))
|
|
310
|
+
index_match = _INDEX_RE.match(token)
|
|
311
|
+
if index_match:
|
|
312
|
+
return _xterm_hex(int(index_match.group(1)))
|
|
313
|
+
if token.isdigit():
|
|
314
|
+
return _xterm_hex(int(token))
|
|
315
|
+
return None
|
|
316
|
+
|
|
317
|
+
|
|
281
318
|
def background_hex(spec: Union[str, int, None]) -> Optional[str]:
|
|
282
319
|
"""sRGB hex (no ``#``) for the background half of a colour spec, if any."""
|
|
283
320
|
_fg, bg = _split_spec(spec)
|
|
284
321
|
if bg is None:
|
|
285
322
|
return None
|
|
286
|
-
|
|
287
|
-
if hex_match:
|
|
288
|
-
return hex_match.group(1).upper()
|
|
289
|
-
return _BG_HEX.get(bg)
|
|
323
|
+
return _token_hex(bg, _BG_HEX)
|
|
290
324
|
|
|
291
325
|
|
|
292
326
|
def foreground_hex(spec: Union[str, int, None]) -> Optional[str]:
|
|
@@ -295,11 +329,9 @@ def foreground_hex(spec: Union[str, int, None]) -> Optional[str]:
|
|
|
295
329
|
for token in fg_tokens:
|
|
296
330
|
if token in ATTRIBUTES:
|
|
297
331
|
continue
|
|
298
|
-
|
|
299
|
-
if
|
|
300
|
-
return
|
|
301
|
-
if token in _FG_HEX:
|
|
302
|
-
return _FG_HEX[token]
|
|
332
|
+
found = _token_hex(token, _FG_HEX)
|
|
333
|
+
if found:
|
|
334
|
+
return found
|
|
303
335
|
return None
|
|
304
336
|
|
|
305
337
|
|
|
@@ -852,8 +852,12 @@ def __align_one_header(column: Union[str, list, tuple],
|
|
|
852
852
|
column
|
|
853
853
|
))
|
|
854
854
|
else:
|
|
855
|
-
# Apply to the cell.
|
|
856
|
-
|
|
855
|
+
# Apply to the cell. The float sizes must be passed here too: a
|
|
856
|
+
# numeric-looking header ('0', '2024') over a float column was
|
|
857
|
+
# decimal-aligned with no sizes and raised TypeError.
|
|
858
|
+
yield __align_single_cell(
|
|
859
|
+
column, col_width, to_where, margin, float_column_sizes
|
|
860
|
+
)
|
|
857
861
|
|
|
858
862
|
|
|
859
863
|
def _align_headers(style_composition: TableComposition,
|
|
@@ -875,6 +879,11 @@ def _align_headers(style_composition: TableComposition,
|
|
|
875
879
|
for column_i, column in enumerate(headers):
|
|
876
880
|
# Get the alignment of the current column.
|
|
877
881
|
to_where = col_alignments[column_i]
|
|
882
|
+
if to_where == COLUMN_ALIGNS.float:
|
|
883
|
+
# A header is a label, not a number. Decimal-aligning one that
|
|
884
|
+
# looks numeric ('2024') pushed it past the column edge; text
|
|
885
|
+
# headers already came out right-aligned, so all of them do.
|
|
886
|
+
to_where = COLUMN_ALIGNS.right
|
|
878
887
|
|
|
879
888
|
# Get the width that the current column should have.
|
|
880
889
|
col_width = col_widths[column_i]
|
|
@@ -166,7 +166,7 @@ def compute_layout(column_widths, composition, hidden=()) -> Layout:
|
|
|
166
166
|
so counting them here would put every offset after them too far right.
|
|
167
167
|
"""
|
|
168
168
|
body = composition.vertical_table_body_lines
|
|
169
|
-
margin =
|
|
169
|
+
margin = max(0, int(composition.margin or 0))
|
|
170
170
|
hidden = set(hidden)
|
|
171
171
|
|
|
172
172
|
offset = len(body.left) if body.left is not None else 0
|
|
@@ -227,6 +227,10 @@ def _region_bounds(region, layout, index_offset):
|
|
|
227
227
|
_H_CHARS = set('─━═┄┅┈┉╌╍▬−' '-~=')
|
|
228
228
|
# A real rule has a *run* of dashes. A lone '-' inside "Beta-1" is not a rule.
|
|
229
229
|
_H_RUN = re.compile(r'[─━═┄┅┈┉╌╍▬−\-~=]{2,}')
|
|
230
|
+
# Corner and tee glyphs a rule may carry; plain verticals are left out.
|
|
231
|
+
_JUNCTIONS = set('+') | {
|
|
232
|
+
chr(c) for c in range(0x2500, 0x2580)
|
|
233
|
+
} - set('│┃║┊┋┆┇╎╏')
|
|
230
234
|
_PURE_H = {(0, 0, 1, 1), (0, 0, 1, 0), (0, 0, 0, 1)}
|
|
231
235
|
_ROUND_CORNERS = set('╭╮╰╯')
|
|
232
236
|
|
|
@@ -362,7 +366,7 @@ def _synthesize_body_rule(composition):
|
|
|
362
366
|
if v.middle in '|+':
|
|
363
367
|
return SeparatorLine(
|
|
364
368
|
left='+' if v.left else None,
|
|
365
|
-
middle=h if h in '-=~' else '-',
|
|
369
|
+
middle=h if h and h in '-=~' else '-',
|
|
366
370
|
intersection='+',
|
|
367
371
|
right='+' if v.right else None,
|
|
368
372
|
)
|
|
@@ -394,7 +398,7 @@ def _build_rule_line(layout, composition, column_widths, hidden, rule=None):
|
|
|
394
398
|
if rule is None:
|
|
395
399
|
return None
|
|
396
400
|
v = composition.vertical_table_body_lines
|
|
397
|
-
margin =
|
|
401
|
+
margin = max(0, int(composition.margin or 0))
|
|
398
402
|
hidden = set(hidden or ())
|
|
399
403
|
chunks = []
|
|
400
404
|
first = True
|
|
@@ -470,7 +474,19 @@ def _looks_like_rule(line: str, h_glyph: str = None) -> bool:
|
|
|
470
474
|
return False
|
|
471
475
|
if h_glyph and len(h_glyph) >= 1 and (h_glyph * 2) in line:
|
|
472
476
|
return True
|
|
473
|
-
|
|
477
|
+
if _H_RUN.search(line) is not None:
|
|
478
|
+
return True
|
|
479
|
+
# cell_padding=0 with 1-column cells draws rules like '├─┼─┤', with no
|
|
480
|
+
# run of two dashes (and a merge may have blanked part of it). Such a
|
|
481
|
+
# line is a rule when it holds dashes and junctions and nothing else but
|
|
482
|
+
# spaces; a body line always has text or a plain vertical.
|
|
483
|
+
plain = strip_ansi(line)
|
|
484
|
+
return (
|
|
485
|
+
any(ch in _H_CHARS for ch in plain)
|
|
486
|
+
and any(ch in _JUNCTIONS for ch in plain)
|
|
487
|
+
and all(ch in _H_CHARS or ch in _JUNCTIONS or ch == ' '
|
|
488
|
+
for ch in plain)
|
|
489
|
+
)
|
|
474
490
|
|
|
475
491
|
|
|
476
492
|
def _repair_rule_junctions(line, layout, composition, h_glyph, missing_south=None, missing_north=None,
|
|
@@ -574,7 +590,7 @@ def apply(lines, row_line_map, regions, column_widths, composition,
|
|
|
574
590
|
|
|
575
591
|
layout = compute_layout(column_widths, composition, hidden_columns)
|
|
576
592
|
lines = list(lines)
|
|
577
|
-
margin =
|
|
593
|
+
margin = max(0, int(composition.margin or 0))
|
|
578
594
|
body_rule = composition.table_body_line
|
|
579
595
|
h_glyph = body_rule.middle if body_rule is not None else None
|
|
580
596
|
# Separator lines whose horizontal run was blanked by a vertical span.
|
|
@@ -28,6 +28,7 @@ import csv as _csv
|
|
|
28
28
|
import io as _io
|
|
29
29
|
import re
|
|
30
30
|
from html.parser import HTMLParser
|
|
31
|
+
from collections.abc import Mapping
|
|
31
32
|
from typing import Any, Dict, Iterable, List, Optional, Sequence
|
|
32
33
|
|
|
33
34
|
# Numeric text that should become a number. Deliberately strict: no leading
|
|
@@ -101,6 +102,16 @@ def from_records(table_class, rows: Iterable[Sequence],
|
|
|
101
102
|
|
|
102
103
|
With no headers, the first row is used as the header row.
|
|
103
104
|
"""
|
|
105
|
+
rows = list(rows)
|
|
106
|
+
if rows and all(isinstance(row, Mapping) for row in rows):
|
|
107
|
+
# ``list(dict)`` is its keys, so dict rows (what ``to_records``
|
|
108
|
+
# returns) used to become a table of repeated header names.
|
|
109
|
+
if headers is None:
|
|
110
|
+
return from_dicts(table_class, rows, parse_numbers=parse_numbers,
|
|
111
|
+
**options)
|
|
112
|
+
headers = list(headers)
|
|
113
|
+
rows = [[row.get(key, '') for key in headers] for row in rows]
|
|
114
|
+
return _new_table(table_class, headers, rows, parse_numbers, **options)
|
|
104
115
|
rows = [list(row) for row in rows]
|
|
105
116
|
if headers is None:
|
|
106
117
|
if not rows:
|
|
@@ -220,6 +231,11 @@ class _TableExtractor(HTMLParser):
|
|
|
220
231
|
self._current_cell = None
|
|
221
232
|
self._header_flags: List[bool] = []
|
|
222
233
|
self._row_is_header = False
|
|
234
|
+
# Per table: which rows are <th> rows, and the rowspan/colspan blocks
|
|
235
|
+
# as (row, column, last_row, last_column) in the expanded grid.
|
|
236
|
+
self.header_flags: List[List[bool]] = []
|
|
237
|
+
self.spans: List[List[tuple]] = []
|
|
238
|
+
self._cell_span = (1, 1)
|
|
223
239
|
|
|
224
240
|
def handle_starttag(self, tag, attrs):
|
|
225
241
|
if tag == 'table':
|
|
@@ -235,13 +251,17 @@ class _TableExtractor(HTMLParser):
|
|
|
235
251
|
self._row_is_header = False
|
|
236
252
|
elif tag in ('td', 'th'):
|
|
237
253
|
self._current_cell = []
|
|
254
|
+
self._cell_span = (_span(attrs, 'rowspan'), _span(attrs, 'colspan'))
|
|
238
255
|
if tag == 'th':
|
|
239
256
|
self._row_is_header = True
|
|
240
257
|
|
|
241
258
|
def handle_endtag(self, tag):
|
|
242
259
|
if tag == 'table':
|
|
243
260
|
if self._table_depth == 1 and self._current_table is not None:
|
|
244
|
-
|
|
261
|
+
grid, spans = _expand_spans(self._current_table)
|
|
262
|
+
self.tables.append(grid)
|
|
263
|
+
self.spans.append(spans)
|
|
264
|
+
self.header_flags.append(list(self._header_flags))
|
|
245
265
|
self._current_table = None
|
|
246
266
|
self._table_depth = max(0, self._table_depth - 1)
|
|
247
267
|
return
|
|
@@ -252,7 +272,7 @@ class _TableExtractor(HTMLParser):
|
|
|
252
272
|
# Collapse whitespace the way a browser would render it.
|
|
253
273
|
cleaned = ' '.join(text.split())
|
|
254
274
|
if self._current_row is not None:
|
|
255
|
-
self._current_row.append(cleaned)
|
|
275
|
+
self._current_row.append((cleaned,) + self._cell_span)
|
|
256
276
|
self._current_cell = None
|
|
257
277
|
elif tag == 'tr' and self._current_row is not None:
|
|
258
278
|
self._current_table.append(self._current_row)
|
|
@@ -263,14 +283,74 @@ class _TableExtractor(HTMLParser):
|
|
|
263
283
|
if self._current_cell is not None:
|
|
264
284
|
self._current_cell.append(data)
|
|
265
285
|
|
|
266
|
-
def header_row_index(self) -> Optional[int]:
|
|
267
|
-
"""Index of the first row made of <th> cells, if
|
|
268
|
-
for index, is_header in enumerate(self.
|
|
286
|
+
def header_row_index(self, table: int = 0) -> Optional[int]:
|
|
287
|
+
"""Index of the first row made of <th> cells in one table, if any."""
|
|
288
|
+
for index, is_header in enumerate(self.header_flags[table]):
|
|
269
289
|
if is_header:
|
|
270
290
|
return index
|
|
271
291
|
return None
|
|
272
292
|
|
|
273
293
|
|
|
294
|
+
def _span(attrs, name) -> int:
|
|
295
|
+
"""A rowspan/colspan attribute as a sane positive int (default 1)."""
|
|
296
|
+
for key, value in attrs:
|
|
297
|
+
if key == name:
|
|
298
|
+
try:
|
|
299
|
+
return max(1, min(int(str(value).strip()), 1000))
|
|
300
|
+
except ValueError:
|
|
301
|
+
return 1
|
|
302
|
+
return 1
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
def _expand_spans(rows):
|
|
306
|
+
"""
|
|
307
|
+
Lay ``(text, rowspan, colspan)`` cells out on a grid, as a browser does.
|
|
308
|
+
|
|
309
|
+
The cells a span covers are absent from the markup, so reading cells in
|
|
310
|
+
order slid every later value of those rows one column left. Covered
|
|
311
|
+
cells become '' and each span is returned as a block.
|
|
312
|
+
"""
|
|
313
|
+
cells, spans = {}, []
|
|
314
|
+
for r, row in enumerate(rows):
|
|
315
|
+
c = 0
|
|
316
|
+
for text, rowspan, colspan in row:
|
|
317
|
+
while (r, c) in cells:
|
|
318
|
+
c += 1
|
|
319
|
+
rowspan = min(rowspan, len(rows) - r)
|
|
320
|
+
for dr in range(rowspan):
|
|
321
|
+
for dc in range(colspan):
|
|
322
|
+
cells[(r + dr, c + dc)] = text if dr == dc == 0 else ''
|
|
323
|
+
if rowspan > 1 or colspan > 1:
|
|
324
|
+
spans.append((r, c, r + rowspan - 1, c + colspan - 1))
|
|
325
|
+
c += colspan
|
|
326
|
+
out = []
|
|
327
|
+
for r in range(len(rows)):
|
|
328
|
+
width = max([c + 1 for (rr, c) in cells if rr == r] + [0])
|
|
329
|
+
out.append([cells.get((r, c), '') for c in range(width)])
|
|
330
|
+
return out, spans
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
_PAYLOAD_RE = re.compile(r'^var DATA = (\{.*\});$', re.MULTILINE)
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def _flexablelib_payload(markup: str):
|
|
337
|
+
"""The ``{headers, rows}`` JSON a ``to_html`` page embeds, or None."""
|
|
338
|
+
import json
|
|
339
|
+
match = _PAYLOAD_RE.search(markup)
|
|
340
|
+
if match is None:
|
|
341
|
+
return None
|
|
342
|
+
try:
|
|
343
|
+
payload = json.loads(match.group(1))
|
|
344
|
+
except ValueError:
|
|
345
|
+
return None
|
|
346
|
+
if not isinstance(payload, dict):
|
|
347
|
+
return None
|
|
348
|
+
headers, rows = payload.get('headers'), payload.get('rows')
|
|
349
|
+
if not isinstance(headers, list) or not isinstance(rows, list):
|
|
350
|
+
return None
|
|
351
|
+
return {'headers': headers, 'rows': rows}
|
|
352
|
+
|
|
353
|
+
|
|
274
354
|
def from_html(table_class, source, index: int = 0, parse_numbers: bool = True,
|
|
275
355
|
encoding: str = 'utf-8', **options):
|
|
276
356
|
"""
|
|
@@ -303,22 +383,42 @@ def from_html(table_class, source, index: int = 0, parse_numbers: bool = True,
|
|
|
303
383
|
)
|
|
304
384
|
|
|
305
385
|
rows = extractor.tables[index]
|
|
386
|
+
if len(extractor.tables) == 1 and len(rows) <= 1:
|
|
387
|
+
# ``Table.to_html`` renders its rows from an inline JSON payload, so
|
|
388
|
+
# the <tbody> is empty in the markup. Read that payload instead of
|
|
389
|
+
# returning a header-only table for our own export.
|
|
390
|
+
payload = _flexablelib_payload(markup)
|
|
391
|
+
if payload is not None:
|
|
392
|
+
return _new_table(table_class, payload['headers'],
|
|
393
|
+
payload['rows'], parse_numbers, **options)
|
|
306
394
|
if not rows:
|
|
307
395
|
return table_class(**options)
|
|
308
396
|
|
|
309
|
-
header_index = extractor.header_row_index()
|
|
310
|
-
if header_index is
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
headers, body = rows[0], rows[1:]
|
|
397
|
+
header_index = extractor.header_row_index(index)
|
|
398
|
+
if header_index is None or header_index >= len(rows):
|
|
399
|
+
header_index = 0
|
|
400
|
+
headers = rows[header_index]
|
|
401
|
+
body = rows[:header_index] + rows[header_index + 1:]
|
|
315
402
|
|
|
316
403
|
# Ragged rows are padded so every row reaches the column count.
|
|
317
404
|
width = max([len(headers)] + [len(row) for row in body]) if body else len(headers)
|
|
318
405
|
headers = list(headers) + [f'column {i + 1}' for i in range(len(headers), width)]
|
|
319
406
|
body = [list(row) + [''] * (width - len(row)) for row in body]
|
|
320
407
|
|
|
321
|
-
|
|
408
|
+
table = _new_table(table_class, headers, body, parse_numbers, **options)
|
|
409
|
+
# Restore rowspan/colspan blocks that lie wholly in the body as merges.
|
|
410
|
+
for first_row, first_col, last_row, last_col in extractor.spans[index]:
|
|
411
|
+
if first_row <= header_index <= last_row:
|
|
412
|
+
continue
|
|
413
|
+
shift = 1 if first_row > header_index else 0
|
|
414
|
+
first_row, last_row = first_row - shift, last_row - shift
|
|
415
|
+
if last_row >= table.row_count or last_col >= table.column_count:
|
|
416
|
+
continue
|
|
417
|
+
try:
|
|
418
|
+
table.merge_cells(first_row, first_col, last_row, last_col)
|
|
419
|
+
except ValueError:
|
|
420
|
+
continue
|
|
421
|
+
return table
|
|
322
422
|
|
|
323
423
|
|
|
324
424
|
def from_pandas(table_class, dataframe, include_index: bool = False, **options):
|
|
@@ -442,6 +542,31 @@ def from_excel(table_class, path, sheet=None, has_header: bool = True,
|
|
|
442
542
|
return table
|
|
443
543
|
|
|
444
544
|
|
|
545
|
+
def _split_pipe_row(body: str) -> List[str]:
|
|
546
|
+
"""
|
|
547
|
+
Split a Markdown row on unescaped pipes and unescape ``\\|``.
|
|
548
|
+
|
|
549
|
+
``to_markdown`` writes a literal pipe as ``\\|``; splitting on every
|
|
550
|
+
``|`` cut such a cell in two and left a stray backslash behind, so the
|
|
551
|
+
table did not round-trip.
|
|
552
|
+
"""
|
|
553
|
+
cells, current, i = [], [], 0
|
|
554
|
+
while i < len(body):
|
|
555
|
+
ch = body[i]
|
|
556
|
+
if ch == '\\' and i + 1 < len(body) and body[i + 1] == '|':
|
|
557
|
+
current.append('|')
|
|
558
|
+
i += 2
|
|
559
|
+
continue
|
|
560
|
+
if ch == '|':
|
|
561
|
+
cells.append(''.join(current))
|
|
562
|
+
current = []
|
|
563
|
+
else:
|
|
564
|
+
current.append(ch)
|
|
565
|
+
i += 1
|
|
566
|
+
cells.append(''.join(current))
|
|
567
|
+
return cells
|
|
568
|
+
|
|
569
|
+
|
|
445
570
|
def from_markdown(table_class, source, parse_numbers: bool = True, **options):
|
|
446
571
|
"""
|
|
447
572
|
Parse a GitHub-flavoured Markdown pipe table into a Table.
|
|
@@ -469,9 +594,9 @@ def from_markdown(table_class, source, parse_numbers: bool = True, **options):
|
|
|
469
594
|
if '|' not in ln:
|
|
470
595
|
continue
|
|
471
596
|
body = ln[1:] if ln.startswith('|') else ln
|
|
472
|
-
if body.endswith('|'):
|
|
597
|
+
if body.endswith('|') and not body.endswith('\\|'):
|
|
473
598
|
body = body[:-1]
|
|
474
|
-
rows_raw.append([c.strip() for c in body
|
|
599
|
+
rows_raw.append([c.strip() for c in _split_pipe_row(body)])
|
|
475
600
|
|
|
476
601
|
if not rows_raw:
|
|
477
602
|
return _new_table(table_class, [], [], parse_numbers, **options)
|