Skip to content

Commit

Permalink
Merge branch 'main' into cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
FriedrichFroebel authored Jan 3, 2025
2 parents dcb6769 + 4ec28cb commit a9b2743
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python: ['3.8', '3.9', '3.10', '3.11', '3.12']
python: ['3.8', '3.9', '3.10', '3.11', '3.12', '3.13']

steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python: ['3.8', '3.9', '3.10', '3.11', '3.12']
python: ['3.8', '3.9', '3.10', '3.11', '3.12', '3.13']

steps:
- uses: actions/checkout@v4
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,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
17 changes: 12 additions & 5 deletions tests/test_prefs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from pathlib import Path
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 tomli_w import dumps as toml_dumps

from gourmand.prefs import Prefs, update_preferences_file_format

Expand Down Expand Up @@ -34,22 +41,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"]

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 a9b2743

Please sign in to comment.