Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace toml by tomllib and tomli_w #169

Merged
merged 2 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ def run(self):
'pillow>=8.3.2',
'pygobject>=3.40.1',
'sqlalchemy>=1.4.36,<2',
'toml>=0.10.2',
'tomli_w>=1.0.0',
'toml==0.10.2; python_version<"3.11"',
'recipe-scrapers>=14.27.0,<15',
],
extras_require={
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 == {}
Loading