tosholi


Nametosholi JSON
Version 0.1.0 PyPI version JSON
download
home_page
SummaryRead and write TOML config files with dataclasses.
upload_time2023-09-23 21:26:26
maintainer
docs_urlNone
authorGreg Pauloski
requires_python>=3.8
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Tosholi

[![tests](https://github.com/gpauloski/tosholi/actions/workflows/tests.yml/badge.svg)](https://github.com/gpauloski/tosholi/actions)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/gpauloski/tosholi/main.svg)](https://results.pre-commit.ci/latest/github/gpauloski/tosholi/main)

Tosholi is a simple library for reading and writing TOML configuration
files using Python dataclasses. *Tosholi* means to interpret or translate and
comes from the Chickasaw and Choctaw languages.

## Installation

```bash
$ pip install tosholi
```

## Get Started

Create your configuration as a
[Python dataclass](https://docs.python.org/3/library/dataclasses.html).
Configuration dataclasses can be nested, but can only be made of
[TOML support types](https://docs.python.org/3/library/tomllib.html#conversion-table).


Consider the following `config.toml` that we want to read into a dataclass.
(This example is based on [toml.io](https://toml.io/en/)).

```toml
title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00

[database]
enabled = true
ports = [ 8000, 8001, 8002 ]
temp_targets = { cpu = 79.5, case = 72.0 }

[servers]

[servers.alpha]
ip = "10.0.0.1"
role = "frontend"

[servers.beta]
ip = "10.0.0.2"
role = "backend"
```

We can describe the configuration format with dataclasses.

```python
from __future__ import annotations

import dataclasses
from datetime import datetime

@dataclasses.dataclass
class Owner:
    name: str
    dob: datetime.datetime

@dataclasses.dataclass
class Database:
    enabled: bool
    ports: list[int]
    temp_targets: dict[str, float]

@dataclasses.dataclass
class Server:
    ip: str
    role: str

@dataclasses.dataclass
class Config:
    title: str
    owner: Owner
    database: Database
    servers: dict[str, Server]
```

Then the configuration file can be read using the `Config` dataclass as a
the template for parsing.

```python
with open('config.toml', 'rb') as f:
    config = tosholi.load(Config, f)
```

Similarly, we can convert in the opposite direction. A `Config` instance
can be converted to a `str` with `tosholi.dumps()` or written it to a file
with `tosholi.dump()`.

```python
import tosholi
from datetime import tzinfo

config = Config(
    title='TOML Example',
    owner=Owner(
        name='Tom Preston-Werner',
        dob=datetime(1979, 5, 27, 7, 32, 0),
    ),
    database=Database(
        enabled=True,
        ports=[8000, 8001, 8002],
        temp_targets={'cpu': 79.5, 'case': 72.0},
    ),
    servers={
        'alpha': Server(ip='10.0.0.1', role='frontend'),
        'beta': Server(ip='10.0.0.2', role='backend'),
    }
)

with open('config.toml', 'wb') as f:
    tosholi.dump(config, f)
```

## Developing

We use [tox](https://tox.wiki/) for testing and
[pre-commit](https://pre-commit.com/) for linting. Get started for local
development with:
```bash
$ tox --devenv venv -e py311
$ . venv/bin/activate
$ pre-commit install
```
or
```bash
$ python -m venv venv
$ . venv/bin/activate
$ pip install -e .[dev]
$ pre-commit install
```

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "tosholi",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "",
    "author": "Greg Pauloski",
    "author_email": "jgpauloski@uchicago.edu",
    "download_url": "https://files.pythonhosted.org/packages/03/2f/dee68e05e0e0b61a100c66354e02ff57535bcd4dd6eaf43eda3d4620b061/tosholi-0.1.0.tar.gz",
    "platform": null,
    "description": "# Tosholi\n\n[![tests](https://github.com/gpauloski/tosholi/actions/workflows/tests.yml/badge.svg)](https://github.com/gpauloski/tosholi/actions)\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/gpauloski/tosholi/main.svg)](https://results.pre-commit.ci/latest/github/gpauloski/tosholi/main)\n\nTosholi is a simple library for reading and writing TOML configuration\nfiles using Python dataclasses. *Tosholi* means to interpret or translate and\ncomes from the Chickasaw and Choctaw languages.\n\n## Installation\n\n```bash\n$ pip install tosholi\n```\n\n## Get Started\n\nCreate your configuration as a\n[Python dataclass](https://docs.python.org/3/library/dataclasses.html).\nConfiguration dataclasses can be nested, but can only be made of\n[TOML support types](https://docs.python.org/3/library/tomllib.html#conversion-table).\n\n\nConsider the following `config.toml` that we want to read into a dataclass.\n(This example is based on [toml.io](https://toml.io/en/)).\n\n```toml\ntitle = \"TOML Example\"\n\n[owner]\nname = \"Tom Preston-Werner\"\ndob = 1979-05-27T07:32:00-08:00\n\n[database]\nenabled = true\nports = [ 8000, 8001, 8002 ]\ntemp_targets = { cpu = 79.5, case = 72.0 }\n\n[servers]\n\n[servers.alpha]\nip = \"10.0.0.1\"\nrole = \"frontend\"\n\n[servers.beta]\nip = \"10.0.0.2\"\nrole = \"backend\"\n```\n\nWe can describe the configuration format with dataclasses.\n\n```python\nfrom __future__ import annotations\n\nimport dataclasses\nfrom datetime import datetime\n\n@dataclasses.dataclass\nclass Owner:\n    name: str\n    dob: datetime.datetime\n\n@dataclasses.dataclass\nclass Database:\n    enabled: bool\n    ports: list[int]\n    temp_targets: dict[str, float]\n\n@dataclasses.dataclass\nclass Server:\n    ip: str\n    role: str\n\n@dataclasses.dataclass\nclass Config:\n    title: str\n    owner: Owner\n    database: Database\n    servers: dict[str, Server]\n```\n\nThen the configuration file can be read using the `Config` dataclass as a\nthe template for parsing.\n\n```python\nwith open('config.toml', 'rb') as f:\n    config = tosholi.load(Config, f)\n```\n\nSimilarly, we can convert in the opposite direction. A `Config` instance\ncan be converted to a `str` with `tosholi.dumps()` or written it to a file\nwith `tosholi.dump()`.\n\n```python\nimport tosholi\nfrom datetime import tzinfo\n\nconfig = Config(\n    title='TOML Example',\n    owner=Owner(\n        name='Tom Preston-Werner',\n        dob=datetime(1979, 5, 27, 7, 32, 0),\n    ),\n    database=Database(\n        enabled=True,\n        ports=[8000, 8001, 8002],\n        temp_targets={'cpu': 79.5, 'case': 72.0},\n    ),\n    servers={\n        'alpha': Server(ip='10.0.0.1', role='frontend'),\n        'beta': Server(ip='10.0.0.2', role='backend'),\n    }\n)\n\nwith open('config.toml', 'wb') as f:\n    tosholi.dump(config, f)\n```\n\n## Developing\n\nWe use [tox](https://tox.wiki/) for testing and\n[pre-commit](https://pre-commit.com/) for linting. Get started for local\ndevelopment with:\n```bash\n$ tox --devenv venv -e py311\n$ . venv/bin/activate\n$ pre-commit install\n```\nor\n```bash\n$ python -m venv venv\n$ . venv/bin/activate\n$ pip install -e .[dev]\n$ pre-commit install\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Read and write TOML config files with dataclasses.",
    "version": "0.1.0",
    "project_urls": {
        "homepage": "https://github.com/gpauloski/tosholi"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5a7308bae9fa404bbea02433fb4ed84f4e61f450f08b5c5d206f1d98ae435bd1",
                "md5": "3a9d53ba4471acf4ca38eed5e3e404ac",
                "sha256": "ede72182603cbd295da0bfc7f812dba04f964c8886fa99eef5e35757d5aaae84"
            },
            "downloads": -1,
            "filename": "tosholi-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3a9d53ba4471acf4ca38eed5e3e404ac",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 7218,
            "upload_time": "2023-09-23T21:26:24",
            "upload_time_iso_8601": "2023-09-23T21:26:24.054028Z",
            "url": "https://files.pythonhosted.org/packages/5a/73/08bae9fa404bbea02433fb4ed84f4e61f450f08b5c5d206f1d98ae435bd1/tosholi-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "032fdee68e05e0e0b61a100c66354e02ff57535bcd4dd6eaf43eda3d4620b061",
                "md5": "24169a9c857835f354d0aaf5f60ccc65",
                "sha256": "326ca4b8d8310b32af99e8be73a3b6628b30f3c8b4dbb8e7bc60e42a25a91a52"
            },
            "downloads": -1,
            "filename": "tosholi-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "24169a9c857835f354d0aaf5f60ccc65",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 18732,
            "upload_time": "2023-09-23T21:26:26",
            "upload_time_iso_8601": "2023-09-23T21:26:26.240522Z",
            "url": "https://files.pythonhosted.org/packages/03/2f/dee68e05e0e0b61a100c66354e02ff57535bcd4dd6eaf43eda3d4620b061/tosholi-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-23 21:26:26",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "gpauloski",
    "github_project": "tosholi",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "tosholi"
}
        
Elapsed time: 0.41203s