Skip to content

Commit

Permalink
Enable use of Python <3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
bkmgit authored and Benson Muite committed Jan 3, 2025
1 parent 5437057 commit d6e87f7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
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',
'tomli_w>=1.0.0',
'tomli_w>=1.0.0;python_version>="3.11"',
'toml=0.10.2;python_version<"3.11"',
'recipe-scrapers>=14.27.0',
],
extras_require={
Expand Down
17 changes: 11 additions & 6 deletions src/gourmand/prefs.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import shutil
from pathlib import Path
from typing import Any, Optional
import sys

import tomllib
import tomli_w
if sys.version_info >= (3, 11):
import tomllib.load as tom_load
import tomli_w.dump as tom_dump
else:
import tomli.load as tom_load
import tomli.dump as tom_dump

from gourmand.gglobals import gourmanddir

Expand Down Expand Up @@ -33,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:
tomli_w.dump(self, fout)
tom_dump(self, fout)

Check failure

Code scanning / CodeQL

Non-callable called Error

Call to a
non-callable
of
builtin-class module
.

def load(self) -> bool:
if self.filename.is_file():
with open(self.filename) as fin:
for k, v in tomllib.load(fin).items():
for k, v in tom_load(fin).items():

Check failure

Code scanning / CodeQL

Non-callable called Error

Call to a
non-callable
of
builtin-class module
.
self.__setitem__(k, v)
return True
return False
Expand All @@ -55,7 +60,7 @@ def update_preferences_file_format(target_dir: Path = gourmanddir):
return

with open(filename) as fin:
prefs = tomllib.load(fin)
prefs = tom_load(fin)

Check failure

Code scanning / CodeQL

Non-callable called Error

Call to a
non-callable
of
builtin-class module
.

# 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 @@ -65,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:
tomli_w.dump(prefs, fout)
tom_dump(prefs, fout)

Check failure

Code scanning / CodeQL

Non-callable called Error

Call to a
non-callable
of
builtin-class module
.


def copy_old_installation_or_initialize(target_dir: Path):
Expand Down
18 changes: 12 additions & 6 deletions tests/test_prefs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import pytest
import tomllib
import tomli_w
import sys

if sys.version_info >= (3, 11):
import tomllib.load as tom_load
import tomli_w.dump as tom_dump
else:
import tomli.load as tom_load
import tomli.dump as tom_dump

from pathlib import Path

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

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

Check failure

Code scanning / CodeQL

Non-callable called Error test

Call to a
non-callable
of
builtin-class module
.

update_preferences_file_format(Path(tmpdir))

with open(filename) as fin:
d = tomllib.load(fin)
d = tom_load(fin)

Check failure

Code scanning / CodeQL

Non-callable called Error test

Call to a
non-callable
of
builtin-class module
.

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

with open(filename, 'w') as fout:
tomli_w.dump({}, fout)
tom_dump({}, fout)

update_preferences_file_format(Path(tmpdir))

with open(filename) as fin:
d = tomllib.load(fin)
d = tom_load(fin)

assert d == {}

0 comments on commit d6e87f7

Please sign in to comment.