Skip to content

Commit

Permalink
Replace toml by tomllib and tomli_w
Browse files Browse the repository at this point in the history
  • Loading branch information
bkmgit authored and FriedrichFroebel committed Jan 3, 2025
1 parent ca2f5ec commit 9210e52
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,9 @@ def run(self):
'pillow>=8.3.2',
'pygobject>=3.40.1',
'sqlalchemy>=1.4.36,<2',
'toml>=0.10.2',
'recipe-scrapers>=14.27.0,<15',
'tomli_w>=1.0.0',
'toml==0.10.2; python_version<"3.11"',
'recipe-scrapers>=14.27.0',
],
extras_require={
'epub-export': ['ebooklib==0.17.1'],
Expand Down
16 changes: 11 additions & 5 deletions src/gourmand/prefs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import shutil
from pathlib import Path
from sys import version_info
from typing import Any, Optional

import toml
if version_info >= (3, 11):
from tomllib import loads as toml_loads
else:
from tomli import loads as toml_loads

from tomli_w import dumps as toml_dumps

from gourmand.gglobals import gourmanddir

Expand Down Expand Up @@ -32,12 +38,12 @@ def get(self, key: str, default: Optional[Any] = None) -> Optional[Any]:
def save(self):
self.filename.parent.mkdir(exist_ok=True)
with open(self.filename, 'w') as fout:
toml.dump(self, fout)
fout.write(toml_dumps(self))

def load(self) -> bool:
if self.filename.is_file():
with open(self.filename) as fin:
for k, v in toml.load(fin).items():
for k, v in toml_loads(fin.read()).items():
self.__setitem__(k, v)
return True
return False
Expand All @@ -54,7 +60,7 @@ def update_preferences_file_format(target_dir: Path = gourmanddir):
return

with open(filename) as fin:
prefs = toml.load(fin)
prefs = toml_loads(fin.read())

# Gourmand 1.2.0: several sorting parameters can be saved.
# The old format had `column=name` and `ascending=bool`, which are now `name=bool`
Expand All @@ -64,7 +70,7 @@ def update_preferences_file_format(target_dir: Path = gourmanddir):
prefs['sort_by'] = {sort_by['column']: sort_by['ascending']}

with open(filename, 'w') as fout:
toml.dump(prefs, fout)
fout.write(toml_dumps(prefs))


def copy_old_installation_or_initialize(target_dir: Path):
Expand Down
23 changes: 14 additions & 9 deletions tests/test_prefs.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
from sys import version_info

import pytest
import toml

if version_info >= (3, 11):
from tomllib import loads as toml_loads
else:
from tomli import loads as toml_loads

from pathlib import Path

from gourmand.prefs import (
Prefs,
update_preferences_file_format
)
from tomli_w import dumps as toml_dumps

from gourmand.prefs import Prefs, update_preferences_file_format


def test_singleton():
Expand Down Expand Up @@ -37,22 +42,22 @@ def test_update_preferences_file_format(tmpdir):
filename = tmpdir.join('preferences.toml')

with open(filename, 'w') as fout:
toml.dump({'sort_by': {'column': 'title', 'ascending': True}}, fout)
fout.write(toml_dumps({'sort_by': {'column': 'title', 'ascending': True}}))

update_preferences_file_format(Path(tmpdir))

with open(filename) as fin:
d = toml.load(fin)
d = toml_loads(fin.read())

assert 'category' not in d['sort_by'].keys()
assert d['sort_by']['title'] == True

with open(filename, 'w') as fout:
toml.dump({}, fout)
fout.write(toml_dumps({}))

update_preferences_file_format(Path(tmpdir))

with open(filename) as fin:
d = toml.load(fin)
d = toml_loads(fin.read())

assert d == {}

0 comments on commit 9210e52

Please sign in to comment.