Skip to main content

tableio-cfg-json

tableio-cfg-json stores TableIO configuration as validated JSON by using config-as-json.

Use it when an application uses TableIO for table-like files and wants persistent, user-editable configuration for formats, implementations and format-specific options. The configuration objects are both TableIO ConfigData objects and config-as-json Config objects, so the same object can be written as configuration file (as JSON), read back later, validated, and passed to TableIO.

Is this package for you?

This package is a good fit when one or more of these apply:

  • Your application uses TableIO.
  • You already use config-as-json, or can accept using it for persistent configuration.
  • You want one configuration file to describe one TableIO input or output endpoint.
  • You want to nest one or more TableIO endpoint configurations inside a larger application configuration file.
  • You want validation and generated user documentation for the TableIO options that are relevant to your application's capabilities.
  • You want an interactive wizard that asks a user for the TableIO configuration.
  • You want your users to edit stored TableIO configuration in an edit-cfg-json editor, without writing the TableIO documentation down a second time.

This package is probably not the right one when:

  • You are looking for the table reader or writer itself. Use TableIO directly.
  • Your program always uses one hard-coded table format and has no persistent configuration.
  • You do not want to use config-as-json for configuration files.
  • You only want the wizard user interface bridge. That is the separate package wizard-ui-bridge, which does not depend on TableIO.

Installation

tableio-cfg-json requires Python 3.12 or newer.

pip install --upgrade tableio-cfg-json

Quick start

Create a compact JSON configuration file for one TableIO endpoint:

from pathlib import Path
import sys

from tableio import FileAccess, access_capabilities
from tableio_cfg_json import tio_json_config_default

config_file = Path('tableio.cfg')
file_access = FileAccess.CREATE
capabilities = access_capabilities(file_access, error_file=sys.stderr)
config = tio_json_config_default(capabilities=capabilities,
                                 file_access=file_access,
                                 format_name='CSV')
config.write(to_json_filename=config_file)

For CSV this writes a small file like:

{
    "format_name": "CSV"
}

Read the configuration back and use it with TableIO:

from pathlib import Path
import sys

from tableio import FileAccess, access_capabilities, tio_config_create
from tableio_cfg_json import TioJsonConfig

config_file = Path('tableio.cfg')
table_file = Path('capitals.csv')
file_access = FileAccess.CREATE
capabilities = access_capabilities(file_access, error_file=sys.stderr)
config = TioJsonConfig(capabilities=capabilities,
                       file_access=file_access,
                       from_json_filename=config_file)
with tio_config_create(config=config, file_name=table_file,
                       file_access=file_access,
                       capabilities=capabilities) as table_io:
    table_io.write_table_listdata([
        ['Capital', 'Country'],
        ['Copenhagen', 'Denmark']
    ])

If implementation is omitted, TableIO chooses a matching implementation at runtime. If the user wants to lock down a specific implementation, it can be stored explicitly in JSON.

Optional settings can be added at the top level or in format-specific nested sections such as csv, html and latex. Compact output omits unset optional values, while template-style output can include all current default options.

A setting that holds an enum is stored as the name of the enum member. For example timedelta_fallback says how a timedelta value is written by the formats that have no native duration type, such as CSV, Markdown and PDF:

{
    "format_name": "CSV",
    "timedelta_fallback": "DHMS_STRING"
}

Please see the teaching examples for a more thorough introduction.

Main entry points

  • TioJsonConfig Complete JSON-backed TableIO configuration for one endpoint. It can read JSON, write JSON and be passed to TableIO as normal configuration data.

  • tio_config_create() TableIO's own function for creating a TableIO object. Import it from tableio and pass it a TioJsonConfig object.

Helpers and details

  • tio_json_config_default() Create a validated default TioJsonConfig using TableIO's recommended choices for the requested capabilities and file access.

  • TioJsonCsvConfig, TioJsonHtmlConfig, TioJsonLatexConfig Optional nested configuration sections for format-specific settings.

  • describe_config(), describe_config_members(), describe_config_reference(), describe_config_example(), get_config_member_names() and get_general_cfg_info() Helpers for generating plain text syntax guides for configuration files.

  • tio_json_config_wizard() Interactive helper for creating one TableIO endpoint configuration through a user interface bridge.

  • tio_json_descriptions() and TIO_JSON_DESCRIPTIONS What each configuration member means, as an edit_cfg_json.Descriptions mapping. See Editing the configuration below.

  • tio_json_loader() How an edit-cfg-json editor constructs a TioJsonConfig.

  • tio_json_read_loader, tio_json_create_loader and tio_json_update_loader Ready-made loaders, one per file access, for a program that needs a name to point at rather than a call to make.

Wizard user interface bridge

  • WizardUiBridge, WizardUiBridgeConsole, WizardUiBridgeTextual and make_text_ui_bridge Interfaces for connecting the wizard to a console, GUI or scripted UI. They belong to the separate package wizard-ui-bridge; import them from wizard_ui_bridge. Versions before 2.0 also re-exported them from tableio_cfg_json, deprecated, and 2.0 removed those re-exports. tableio-cfg-json does not depend on Textual. An application that wants the full-screen Textual wizard declares wizard-ui-bridge[textual] in its own dependencies.

Editing the configuration

The edit-cfg-json family gives an application a folding editor for a config-as-json configuration object. It discovers the editable structure by introspection, so the configuration is never described a second time to get one. The editors an end user sees are edit-cfg-json-tk and edit-cfg-json-textual. Neither is a dependency of tableio-cfg-json; an application that wants one declares it, or leaves it to its users to install the one they prefer.

What that editor cannot work out is what a member is for, which values a plain string member accepts and what those values mean, because it reads the class and never a validator. tio_json_descriptions() is the one source of truth for that text, so an application that stores TableIO configuration does not repeat the TableIO documentation and cannot have it drift:

from edit_cfg_json import edit_in_ui
from tableio import FileAccess, access_capabilities
from tableio_cfg_json import TIO_JSON_DESCRIPTIONS, tio_json_config_default, \
    tio_json_loader

file_access = FileAccess.CREATE
capabilities = access_capabilities(file_access)
config = tio_json_config_default(capabilities, file_access,
                                 include_all_options=True)
saved = edit_in_ui(config, descriptions=TIO_JSON_DESCRIPTIONS,
                   in_file='tableio.cfg',
                   loader=tio_json_loader(capabilities, file_access))

edit_in_ui() opens the editor in whichever installed user interface can run on the machine: a window where there is a display, a terminal screen where there is none. An application that has chosen its editor calls edit() instead and passes that backend, such as edit_cfg_json_textual.TextualEditor(), with the same keywords.

tio_json_loader() is needed because TioJsonConfig takes the runtime capabilities and file access that no configuration file holds, so the editor cannot construct it on its own.

A program that is told a name rather than making a call uses one of the ready-made loaders instead. There is one per file access, and which one is right is the caller's to know, because the access is not in the file. The edit-cfg-json program, which comes with the edit-cfg-json core, then edits a TableIO configuration file with no program written for it at all:

edit-cfg-json --module tableio_cfg_json \
  --loader tio_json_create_loader --descriptions TIO_JSON_DESCRIPTIONS \
  --input tableio.cfg

The inspection utility python3 -m edit_cfg_json.dump takes the same options, and prints what the editor would show instead of opening it:

python3 -m edit_cfg_json.dump --module tableio_cfg_json \
  --loader tio_json_create_loader --descriptions TIO_JSON_DESCRIPTIONS \
  --input tableio.cfg --unfold

An application that nests TioJsonConfig inside its own configuration class needs no loader, and passes the path of the member that holds each endpoint:

descriptions = {**own_descriptions,
                **tio_json_descriptions(('input',)),
                **tio_json_descriptions(('output',))}

A member that is not in the configuration file is not a row in the editor, so tio_json_loader() builds on a complete set of defaults by default. The editor then marks every value the file did not hold, and a compact configuration file opens with everything there is to set. Pass include_all_options=False to keep an edited file as compact as it was.

Validation model

The configuration file (in JSON) stores durable TableIO choices such as format_name, implementation, character encoding, presentation options and format-specific settings. Runtime values such as the actual file name are not stored in this configuration.

Validation happens in two layers:

  • config-as-json validates JSON structure, member names and member value types.
  • TableIO validates whether the selected format, implementation, capabilities and file access can work together.

Choice values are matched case-insensitively where TableIO defines a finite set of choices. For example, configuration file may use excel and the config object will store TableIO's normal Excel spelling after validation.

A diagnostic names the whole path

A message about a refused value names the path from the top of the configuration down to that value, and not just the local member name. A delimiter refused in the CSV section is csv.delimiter, and the same delimiter in an endpoint that an application configuration calls input is input.csv.delimiter. This matters because every endpoint declares the same member names, so a message naming only delimiter would leave the user guessing which endpoint it was about.

The TableIO whole-configuration rules take part in the same naming. TableIO reports an issue under its own dotted parameter name, such as csv.quoting, and that name is joined onto the path of the endpoint it came from, so an application configuration reports not_less_than_output.implementation.

TioJsonConfig, the three section classes and tio_json_config_default() all take a member_name keyword for this, and so does tio_json_loader(). A whole configuration file is a member of nothing and leaves it out.

Reading a file that lacks values

TioJsonConfig and the three section classes take the config-as-json keyword ok_to_use_defaults and pass it on. With ok_to_use_defaults=True a JSON source may leave members out, and each of them keeps its default; kept_value_paths() tells which ones did. The defaults TableIO recommends depend on the format, so TioJsonConfig then builds them for the format and implementation that the JSON source selects, rather than for the preferred ones given to the constructor:

config = TioJsonConfig(capabilities, file_access, include_all_options=True,
                       from_json_data_text='{"format_name": "CSV"}',
                       ok_to_use_defaults=True)

That configuration gets the CSV implementation and CSV settings TableIO recommends. Without ok_to_use_defaults the source has to hold every required member, and an optional member that it leaves out is None.

ok_to_use_defaults is for a file of the current format that leaves values out, such as one trimmed by hand. It is not the way to read a file written by an older version of an application: that is what the "Read Old Configuration File" (ROCF) rules of config-as-json are for, which migrate renamed, moved and removed keys and report every change.

Nested application configs

TioJsonConfig can be used as the whole configuration file for a small program, or as a nested member inside a larger config-as-json application configuration. This is useful when one application has several TableIO endpoints, for example one input table and two independently configured output tables.

For larger configs, create each nested TioJsonConfig with the capabilities and file access for that endpoint. A read endpoint and a create endpoint may need different defaults and may validate different implementations.

Because the two endpoints differ, the application declares each of them with a config_as_json.ConfigFactory of its own, or one factory that reads the member_name it is called with. Passing that member_name on to TioJsonConfig is what names the endpoint in its diagnostics; a factory that does not accept it is called without it and config-as-json warns that it should be changed.

A factory should accept ok_to_use_defaults as well, and pass it on to TioJsonConfig, and so should the constructor of the application configuration. Then an application configuration read with ok_to_use_defaults=True, as an edit-cfg-json editor does under its default-filling load policy, fills in what an endpoint lacks from the TableIO defaults for its format. A factory that does not accept it is called without it, with a warning, and reads its endpoint strictly.

The teaching examples show both styles.

Documentation

License

MIT

Test summary

  • Test result: 687 passed in 32s
  • No flake8 warnings.
  • No mypy errors found.
  • No pylint warnings.
  • No python layout warnings.
  • Built version(s): 2.1
  • Build and test using Python 3.15.2

Metadata

Release files for tableio-cfg-json 2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tableio-cfg-json 2.1
File Size Uploaded Provenance Actions
tableio_cfg_json-2.1.tar.gz 37.4 kB Without provenance Details for tableio_cfg_json-2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tableio-cfg-json 2.1
File Interpreter ABI Platform Actions
tableio_cfg_json-2.1-py3-none-any.whl Python 3 none any Details for tableio_cfg_json-2.1-py3-none-any.whl

Total release size: 72.3 kB

Release files / tableio_cfg_json-2.1.tar.gz

Download URL tableio_cfg_json-2.1.tar.gz
Size 37.4 kB
Tags Source
SHA-256 checksum
How to use checksums
bfa09cfb0dc18ac2f3560c4069c187c1740207e658411b339efccbb615e48568
BLAKE2b-256 checksum
How to use checksums
0c9ebe48c4bd603cfa36c4bdd1abb5ce3c731c9f2471daf3f05f68bab70d7c08
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.2

Release files / tableio_cfg_json-2.1-py3-none-any.whl

Download URL tableio_cfg_json-2.1-py3-none-any.whl
Size 34.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6f34d00d520f666600c460a21bf96d1ecf5f99976086382ae45922660adea3fe
BLAKE2b-256 checksum
How to use checksums
498f3eba66baf7ae8198a29e4daa851a481177202cdbd1be88b2c8a14f814b2b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.2

Security for tableio-cfg-json 2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page