Skip to content

Commit

Permalink
Conversion fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Benson Muite committed Jan 3, 2025
1 parent 2302803 commit c2ff004
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
14 changes: 7 additions & 7 deletions src/gourmand/prefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from typing import Any, Optional

if sys.version_info >= (3, 11):
from tomllib import load as tom_load
from tomllib import loads as tom_loads
else:
from tomli import load as tom_load
from tomli import loads as tom_loads

from tomli_w import dump as tom_dump
from tomli_w import dumps as tom_dumps

from gourmand.gglobals import gourmanddir

Expand Down Expand Up @@ -38,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:
tom_dump(self, fout)
fout.write(tom_dumps(self))

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

with open(filename) as fin:
prefs = tom_load(fin)
prefs = tom_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 @@ -70,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:
tom_dump(prefs, fout)
fout.write(tom_dumps(prefs))


def copy_old_installation_or_initialize(target_dir: Path):
Expand Down
14 changes: 7 additions & 7 deletions tests/test_prefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import pytest

if sys.version_info >= (3, 11):
from tomllib import load as tom_load
from tomllib import loads as tom_loads
else:
from tomli import load as tom_load
from tomli import loads as tom_loads

from pathlib import Path

from tomli_w import dump as tom_dump
from tomli_w import dumps as tom_dumps

from gourmand.prefs import Prefs, update_preferences_file_format

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

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

update_preferences_file_format(Path(tmpdir))

with open(filename) as fin:
d = tom_load(fin)
d = tom_loads(fin.read())

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

with open(filename, 'w') as fout:
tom_dump({}, fout)
fout.write(tom_dumps({})

update_preferences_file_format(Path(tmpdir))

with open(filename) as fin:
d = tom_load(fin)
d = tom_loads(fin.read())

assert d == {}

0 comments on commit c2ff004

Please sign in to comment.