Skip to content

Commit

Permalink
Add module to write party to TOML document
Browse files Browse the repository at this point in the history
  • Loading branch information
homeworkprod committed Jun 30, 2024
1 parent 846ad57 commit 98d092e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

## 0.6.0 (unreleased)

- Added module to write a party to a TOML document.


## 0.5.0 (2024-06-30)

Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ description = "Python library for the OrgaTalk LAN Party Database"
authors = [
{ name = "Jochen Kupperschmidt", email = "[email protected]" }
]
dependencies = []
dependencies = [
"tomlkit>=0.12.5",
]
readme = "README.md"
requires-python = ">= 3.11"
license = { text = "MIT" }
Expand Down
48 changes: 48 additions & 0 deletions src/lanpartydb/writing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
lanpartydb.writing
~~~~~~~~~~~~~~~~~~
Data writing
:Copyright: 2024 Jochen Kupperschmidt
:License: MIT
"""

import dataclasses
from typing import Any

from lanpartydb.models import Party
import tomlkit


# party


def serialize_party(party: Party) -> str:
"""Serialize party to TOML document."""
party_dict = _party_to_sparse_dict(party)
return _write_toml(party_dict)


def _party_to_sparse_dict(party: Party) -> dict[str, Any]:
data = dataclasses.asdict(party)
_remove_default_values(data)
return data


# util


def _write_toml(d: dict[str, Any]) -> str:
return tomlkit.dumps(d)


def _remove_default_values(d: dict[str, Any]) -> dict[str, Any]:
"""Remove `None` and `False`, values from first level of dictionary."""
for k, v in list(d.items()):
if (v is None) or (v is False):
del d[k]
elif isinstance(v, dict):
_remove_default_values(v)

return d

0 comments on commit 98d092e

Please sign in to comment.