-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add module to write party to TOML document
- Loading branch information
1 parent
846ad57
commit 98d092e
Showing
3 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
|
||
## 0.6.0 (unreleased) | ||
|
||
- Added module to write a party to a TOML document. | ||
|
||
|
||
## 0.5.0 (2024-06-30) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |