Skip to content

Commit 8644846

Browse files
committed
change the download default to false, making this feature opt-in
1 parent 77c9ce6 commit 8644846

10 files changed

Lines changed: 166 additions & 32 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
Sphinx directive for documenting dataclass enums in tabular format, with support for enum-properties.
1414

15-
Render [dataclass](https://docs.python.org/3/library/dataclasses.html) enums as tables with a row for each member and a column for every field. Each table has CSV and JSON download buttons. Enums with dataclass values, named tuple values and plain enums work too, and [enum-properties](https://enum-properties.readthedocs.io) enums are supported as a special case: each property becomes a column.
15+
Render [dataclass](https://docs.python.org/3/library/dataclasses.html) enums as tables with a row for each member and a column for every field. Tables can optionally offer CSV and JSON download buttons. Enums with dataclass values, named tuple values and plain enums work too, and [enum-properties](https://enum-properties.readthedocs.io) enums are supported as a special case: each property becomes a column.
1616

1717
## Installation
1818

@@ -70,7 +70,7 @@ Which renders a table with `name`, `mass` and `radius` columns. Columns, members
7070
:columns: name, radius
7171
:members: EARTH, MERCURY
7272
:headers: name=Planet, radius=Radius (m)
73-
:download: csv
73+
:download: csv, json
7474
```
7575

7676
## Documentation

‎doc/source/changelog.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ Changelog
66
0.1.0 (2026-09-28)
77
------------------
88

9-
* Initial release: the ``enum-table`` directive with CSV and JSON downloads.
9+
* Initial release: the ``enum-table`` directive with optional CSV and JSON downloads.
1010
* Tables render in PDF (LaTeX) builds, large tables wrap long cells and break across pages.

‎doc/source/conf.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,6 @@
4444

4545
# xelatex handles the unicode in the module docstring banner
4646
latex_engine = "xelatex"
47+
48+
# show the download buttons on every example table
49+
enum_table_download = True

‎doc/source/index.rst‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ sphinxcontrib-enum
5454

5555

5656
A Sphinx_ directive for documenting dataclass_ enums in tabular format. Each member is a row and
57-
each dataclass field is a column. Tables can be downloaded as CSV or JSON.
57+
each dataclass field is a column. Tables can optionally be downloaded as CSV or JSON.
5858

5959
* Documents enums that mix in a dataclass_ or whose values are dataclasses (or named tuples).
6060
* Columns may be any attribute, property or dotted path on the member or its value.
6161
* Filter and reorder columns and members, rename headers, add captions and cross references.
6262
* Customize how cells render with a formatter function.
63-
* CSV and JSON download buttons (html builders only).
63+
* Optional CSV and JSON download buttons (html builders only).
6464
* :ref:`Supports enum-properties <enum_properties>` enums as a special case, and plain enums work
6565
too.
6666

‎doc/source/reference/configuration.rst‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,28 @@ The following configuration values can be set in your
1515
"sphinxcontrib_enum",
1616
]
1717
18-
# only offer json downloads
18+
# offer csv and json downloads for every table (off by default)
19+
enum_table_download = True
20+
21+
# or only offer json downloads
1922
enum_table_download = ["json"]
2023
2124
# format cells with a custom function
2225
enum_table_formatter = "mypackage.docs.format_cell"
2326
2427
.. confval:: enum_table_download
25-
:type: ``list[str]``
26-
:default: ``["csv", "json"]``
28+
:type: ``bool | list[str] | str | None``
29+
:default: ``False``
30+
31+
The download formats to offer beneath every table. Downloads are off by default.
32+
33+
* ``False``, ``None`` or an empty list - no download buttons.
34+
* ``True`` - offer every supported format (``csv`` and ``json``).
35+
* A list of formats, e.g. ``["json"]`` or ``["csv", "json"]``, or the same as a comma
36+
separated string (``"csv, json"``). Buttons are rendered in the order given.
2737

28-
The download formats to offer beneath every table. Set to an empty list to disable downloads.
29-
Can be overridden per table with :rst:dir:`enum-table:download`.
38+
Unsupported formats are a configuration error. Can be overridden per table with
39+
:rst:dir:`enum-table:download`, so individual tables can opt in or out.
3040

3141
.. confval:: enum_table_formatter
3242
:type: ``str | Callable | None``

‎doc/source/reference/directive.rst‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ Directive
8989
**default**: :confval:`enum_table_download`
9090
9191
The download formats to offer for this table. Supports ``csv`` and ``json``, or ``none`` to
92-
disable downloads.
92+
disable downloads. Overrides :confval:`enum_table_download`, which is off by default, so
93+
use this to add downloads to individual tables.
9394
9495
.. rst:directive:option:: formatter: import path of a cell formatter
9596
:type: text

‎doc/source/usage.rst‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,16 @@ to the default formatting:
120120
Downloads
121121
=========
122122

123-
HTML builders render download buttons for CSV and JSON versions of the table below it. Other
124-
builders (e.g. LaTeX/PDF, text and epub) omit them. Tables render natively in every
125-
builder, including PDF.
123+
Tables can offer download buttons for CSV and JSON versions of their data. Downloads are off by
124+
default. Turn them on for every table with :confval:`enum_table_download`:
125+
126+
.. code-block:: python
127+
128+
# conf.py
129+
enum_table_download = True # or a list of formats, e.g. ["json"]
130+
131+
HTML builders render the buttons below the table. Other builders (e.g. LaTeX/PDF, text and epub)
132+
omit them. Tables render natively in every builder, including PDF.
126133

127134
* **CSV** files contain the header row and the display text of every cell, exactly as rendered.
128135
* **JSON** files contain an object keyed by member name. Each member maps to an object keyed by
@@ -138,13 +145,13 @@ builder, including PDF.
138145
"VENUS": {"mass": 4.869e+24, "radius": 6051800.0, "moons": 0}
139146
}
140147
141-
Use :confval:`enum_table_download` to change the default formats for all tables or
142-
:rst:dir:`enum-table:download` for a single table:
148+
Use :rst:dir:`enum-table:download` to override the setting for a single table, either to add
149+
downloads to a table when they are off globally or to remove them when they are on:
143150

144151
.. code-block:: rst
145152
146153
.. enum-table:: examples.Planet
147-
:download: json
154+
:download: csv, json
148155
149156
.. enum-table:: examples.Planet
150157
:download: none

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ classifiers = [
3434
"Programming Language :: Python :: 3.13",
3535
"Programming Language :: Python :: 3.14",
3636
"Programming Language :: Python :: 3.15",
37+
"Typing :: Typed",
3738
"Topic :: Software Development :: Libraries",
3839
]
3940

‎src/sphinxcontrib_enum/directive.py‎

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from docutils import nodes
1515
from docutils.parsers.rst import directives
1616
from sphinx.application import Sphinx
17+
from sphinx.config import Config
18+
from sphinx.errors import ConfigError
1719
from sphinx.util import logging
1820
from sphinx.util.docutils import SphinxDirective, SphinxTranslator
1921
from sphinx.util.osutil import ensuredir, relative_uri
@@ -34,6 +36,7 @@
3436
"DOWNLOAD_FORMATS",
3537
"EnumTableDirective",
3638
"Formatter",
39+
"download_formats",
3740
"enum_table_downloads",
3841
]
3942

@@ -100,9 +103,24 @@ def headers_option(argument: str | None) -> dict[str, str]:
100103
return headers
101104

102105

103-
def download_option(argument: str | None) -> list[str]:
104-
formats = [fmt.lower() for fmt in _split(argument)]
105-
if formats in ([], ["none"]):
106+
def download_formats(value: t.Any) -> list[str]:
107+
"""
108+
Normalize a download setting into a list of formats.
109+
110+
* falsey values (``False``, ``None``, empty) disable downloads
111+
* ``True`` enables every supported format
112+
* strings are comma or whitespace separated formats (or ``none``)
113+
* any other iterable is a collection of formats
114+
115+
:raises ValueError: If a format is not supported.
116+
"""
117+
if not value:
118+
return []
119+
if value is True:
120+
return list(DOWNLOAD_FORMATS)
121+
items = _split(value) if isinstance(value, str) else list(value)
122+
formats = [str(fmt).strip().lower() for fmt in items]
123+
if formats == ["none"]:
106124
return []
107125
for fmt in formats:
108126
if fmt not in DOWNLOAD_FORMATS:
@@ -113,6 +131,10 @@ def download_option(argument: str | None) -> list[str]:
113131
return list(dict.fromkeys(formats))
114132

115133

134+
def download_option(argument: str | None) -> list[str]:
135+
return download_formats(argument)
136+
137+
116138
def widths_option(argument: str | None) -> str | list[int]:
117139
if (argument or "").strip().lower() in ("auto", "grid"):
118140
return (argument or "").strip().lower()
@@ -218,7 +240,9 @@ def run(self) -> list[nodes.Node]:
218240
container = nodes.container(classes=["enum-table-container"])
219241
container += table
220242

221-
formats = self.options.get("download", self.config.enum_table_download)
243+
formats = self.options.get(
244+
"download", download_formats(self.config.enum_table_download)
245+
)
222246
if formats:
223247
text = [[_text(cell) for cell in row] for row in display]
224248
contents = {
@@ -450,6 +474,13 @@ def _latex_width_hints(table: nodes.table, rows: list[nodes.row]) -> None:
450474
colspec["colwidth"] = round(width) + _LATEX_CELL_PADDING
451475

452476

477+
def _check_download_config(app: Sphinx, config: Config) -> None:
478+
try:
479+
download_formats(config.enum_table_download)
480+
except (TypeError, ValueError) as err:
481+
raise ConfigError(f"Invalid enum_table_download: {err}") from err
482+
483+
453484
def setup(app: Sphinx) -> None:
454485
app.add_node(
455486
enum_table_downloads,
@@ -458,5 +489,13 @@ def setup(app: Sphinx) -> None:
458489
app.add_directive("enum-table", EnumTableDirective)
459490
app.connect("doctree-resolved", _remove_downloads)
460491
app.connect("doctree-resolved", _wrap_latex_longtables)
461-
app.add_config_value("enum_table_download", list(DOWNLOAD_FORMATS), "env")
492+
app.add_config_value(
493+
"enum_table_download",
494+
False,
495+
"env",
496+
# frozenset/set must not be listed, sphinx converts sequences to frozensets
497+
# when they are, losing the format order
498+
types=(bool, list, tuple, str, type(None)),
499+
)
500+
app.connect("config-inited", _check_download_config)
462501
app.add_config_value("enum_table_formatter", None, "env")

0 commit comments

Comments
 (0)