Skip to content

Commit

Permalink
Made the encoding of text files explicitly UTF-8
Browse files Browse the repository at this point in the history
  • Loading branch information
videbar committed Nov 2, 2023
1 parent c30dafa commit f543e91
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion kobo_highlights/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def save_file(self, config_filepath: Path) -> Config:
field: str(path) for field, path in self.dict().items()
}

config_filepath.write_text(toml.dumps(toml_representation))
config_filepath.write_text(toml.dumps(toml_representation), encoding="utf-8")
return self

def __rich__(self) -> Table:
Expand Down
18 changes: 8 additions & 10 deletions kobo_highlights/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def setup_missing_config(config_path: Path) -> Config:

error_console.print("[bold]No valid configuration file was found")
if Confirm.ask("would you like to create one?"):

try:
config = Config.create_interactively().save_file(config_path)
console.print(
Expand Down Expand Up @@ -122,7 +121,6 @@ def query_bookmarks_from_ereader(
all_bookmarks: list[Bookmark] = []

for bookmark_data in cursor_bookmark.execute(BM_QUERY):

current_bookmark: dict = {
"id": bookmark_data[0],
"text": bookmark_data[1].strip(),
Expand Down Expand Up @@ -188,9 +186,8 @@ def query_bookmark_ids_from_json(json_filepath: Path) -> list[Bookmark_id]:
list[Bookmark_id]: List of bookamrk IDs.
"""
try:

json_content: dict[str, list[Bookmark_id]] = json.loads(
json_filepath.read_text()
json_filepath.read_text(encoding="utf-8")
)

# The JSON file should correspond to a dictionary with the key
Expand All @@ -202,9 +199,8 @@ def query_bookmark_ids_from_json(json_filepath: Path) -> list[Bookmark_id]:

# If no JSON file exists, create one.
except FileNotFoundError:

json_content: dict[str, list[Bookmark_id]] = {"imported_bookmark_ids": []}
json_filepath.write_text(json.dumps(json_content))
json_filepath.write_text(json.dumps(json_content), encoding="utf-8")

# If there is a JSON file but the structure is wrong, the user will be asked if
# they want to create a new one.
Expand All @@ -221,7 +217,7 @@ def query_bookmark_ids_from_json(json_filepath: Path) -> list[Bookmark_id]:
console=error_console,
):
json_content: dict[str, list[Bookmark_id]] = {"imported_bookmark_ids": []}
json_filepath.write_text(json.dumps(json_content))
json_filepath.write_text(json.dumps(json_content), encoding="utf-8")

else:
raise typer.Abort()
Expand All @@ -242,7 +238,9 @@ def write_bookmark_id_to_json(json_filepath: Path, id: Bookmark_id):
if id not in stored_ids:
stored_ids.append(id)

json_filepath.write_text(json.dumps({"imported_bookmark_ids": stored_ids}))
json_filepath.write_text(
json.dumps({"imported_bookmark_ids": stored_ids}), encoding="utf-8"
)


def add_bookmark_to_md(bookmark: Bookmark, md_dir: Path):
Expand Down Expand Up @@ -283,8 +281,8 @@ def add_bookmark_to_md(bookmark: Bookmark, md_dir: Path):
text_existing_file: str = f"\n\n***\n\n{md_blockquote}"

if md_filepath.is_file():
with md_filepath.open("a") as md_file:
with md_filepath.open("a", encoding="utf-8") as md_file:
md_file.write(text_existing_file)

else:
md_filepath.write_text(text_new_file)
md_filepath.write_text(text_new_file, encoding="utf-8")
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "kobo-highlights"
version = "1.0.2"
version = "1.0.3"
description = """\
Kobo Highlights is a CLI application to manage the bookmarks of your Kobo ereader. \
It can import them into a human-friendly markdown database.\
Expand Down
9 changes: 5 additions & 4 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
CONFIG_FILE_MISSING_FIELDS,
)


# Tests
def test_from_file_correct(tmp_path):
"""Test the `from_file()` method with a valid config file. This is done using the
Expand All @@ -36,7 +37,7 @@ def test_from_file_correct(tmp_path):
"""

config_filepath: Path = tmp_path / "config.toml"
config_filepath.write_text(CONFIG_FILE_CORRECT)
config_filepath.write_text(CONFIG_FILE_CORRECT, encoding="utf-8")

target_dir = Path(CONFIG_FILE_CORRECT_PATHS["target_dir"])
ereader_dir = Path(CONFIG_FILE_CORRECT_PATHS["ereader_dir"])
Expand All @@ -54,7 +55,7 @@ def test_from_file_extra_fields(tmp_path):
"""

config_filepath: Path = tmp_path / "config.toml"
config_filepath.write_text(CONFIG_FILE_EXTRA_FIELDS)
config_filepath.write_text(CONFIG_FILE_EXTRA_FIELDS, encoding="utf-8")

assert config_filepath.is_file()
with pytest.raises(ConfigError):
Expand All @@ -67,7 +68,7 @@ def test_from_file_missing_fields(tmp_path):
"""

config_filepath: Path = tmp_path / "config.toml"
config_filepath.write_text(CONFIG_FILE_MISSING_FIELDS)
config_filepath.write_text(CONFIG_FILE_MISSING_FIELDS, encoding="utf-8")

assert config_filepath.is_file()
with pytest.raises(ConfigError):
Expand All @@ -76,7 +77,7 @@ def test_from_file_missing_fields(tmp_path):

def test_from_file_no_file(tmp_path):
"""Test the `from_file()` method when there's no config file at all. If the path
passed to `from_file()` does not contain a file, it should raise a `ConfigError`.
passed to `from_file()` does not contain a file, it should raise a `ConfigError`.
"""

config_filepath: Path = tmp_path / "config.toml"
Expand Down
41 changes: 21 additions & 20 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,9 @@ def test_add_bookmark_to_md_new(tmp_path):
patch.object(Path, "write_text") as mock_write_text,
patch.object(Path, "is_file", return_value=False),
):

assert not filepath.is_file()
add_bookmark_to_md(BOOKMARKS_TO_ADD[0], tmp_path)
mock_write_text.assert_called_once_with(REFERENCE_MARKDOWN[0])
mock_write_text.assert_called_once_with(REFERENCE_MARKDOWN[0], encoding="utf-8")


def test_add_bookmark_to_md_existing(tmp_path):
Expand All @@ -132,11 +131,10 @@ def test_add_bookmark_to_md_existing(tmp_path):
patch.object(Path, "open", mock_open()) as mocked_path_open,
patch.object(Path, "is_file", return_value=True),
):

assert filepath.is_file()
add_bookmark_to_md(BOOKMARKS_TO_ADD[1], tmp_path)

mocked_path_open.assert_called_once_with("a")
mocked_path_open.assert_called_once_with("a", encoding="utf-8")
mocked_path_open().write.assert_called_once_with(REFERENCE_MARKDOWN[1])


Expand All @@ -146,13 +144,15 @@ def test_write_bookmark_id_to_json_new(tmp_path):
"""

json_filepath: Path = tmp_path / ".imported_bookmarks.json"
json_filepath.write_text(JSON_CONTENTS)
json_filepath.write_text(JSON_CONTENTS, encoding="utf-8")
new_id: str = "11111111-1111-1111-1111-111111111114"

assert json_filepath.is_file()
write_bookmark_id_to_json(json_filepath, new_id)

json_object_after_writing: dict = json.loads(json_filepath.read_text())
json_object_after_writing: dict = json.loads(
json_filepath.read_text(encoding="utf-8")
)
bookmark_ids_after_writing: list[str] = json_object_after_writing[
"imported_bookmark_ids"
]
Expand All @@ -166,13 +166,15 @@ def test_write_bookmark_id_to_json_existing(tmp_path):
"""

json_filepath: Path = tmp_path / ".imported_bookmarks.json"
json_filepath.write_text(JSON_CONTENTS)
json_filepath.write_text(JSON_CONTENTS, encoding="utf-8")
existing_id: str = "11111111-1111-1111-1111-111111111113"

assert json_filepath.is_file()
write_bookmark_id_to_json(json_filepath, existing_id)

json_object_after_writing: dict = json.loads(json_filepath.read_text())
json_object_after_writing: dict = json.loads(
json_filepath.read_text(encoding="utf-8")
)
bookmark_ids_after_writing: list[str] = json_object_after_writing[
"imported_bookmark_ids"
]
Expand All @@ -187,11 +189,11 @@ def test_query_bookmark_ids_from_json_correct(tmp_path):
"""

json_filepath: Path = tmp_path / ".imported_bookmarks.json"
json_filepath.write_text(JSON_CONTENTS)
json_filepath.write_text(JSON_CONTENTS, encoding="utf-8")

assert json_filepath.is_file()

json.loads(json_filepath.read_text())
json.loads(json_filepath.read_text(encoding="utf-8"))

queried_ids: list = query_bookmark_ids_from_json(json_filepath)

Expand All @@ -211,7 +213,7 @@ def test_query_bookmark_ids_from_json_no_file(tmp_path):

assert queried_ids == []
assert json_filepath.is_file()
assert json_filepath.read_text() == EMPTY_JSON_CONTENTS
assert json_filepath.read_text(encoding="utf-8") == EMPTY_JSON_CONTENTS


def test_query_bookmark_ids_from_json_wrong_json(tmp_path):
Expand All @@ -222,7 +224,7 @@ def test_query_bookmark_ids_from_json_wrong_json(tmp_path):
"""

json_filepath: Path = tmp_path / ".imported_bookmarks.json"
json_filepath.write_text(WRONG_JSON_CONTENTS)
json_filepath.write_text(WRONG_JSON_CONTENTS, encoding="utf-8")

with (
patch.object(Confirm, "ask", return_value=True) as mock_ask,
Expand All @@ -233,7 +235,7 @@ def test_query_bookmark_ids_from_json_wrong_json(tmp_path):
mock_print.assert_called_once()
mock_ask.assert_called_once()
assert queried_ids == []
assert json_filepath.read_text() == EMPTY_JSON_CONTENTS
assert json_filepath.read_text(encoding="utf-8") == EMPTY_JSON_CONTENTS


def test_query_bookmark_ids_from_json_no_dict(tmp_path):
Expand All @@ -245,7 +247,7 @@ def test_query_bookmark_ids_from_json_no_dict(tmp_path):
"""

json_filepath: Path = tmp_path / ".imported_bookmarks.json"
json_filepath.write_text(JSON_CONTENTS_NO_DICT)
json_filepath.write_text(JSON_CONTENTS_NO_DICT, encoding="utf-8")

with (
patch.object(Confirm, "ask", return_value=True) as mock_ask,
Expand All @@ -256,7 +258,7 @@ def test_query_bookmark_ids_from_json_no_dict(tmp_path):
mock_print.assert_called_once()
mock_ask.assert_called_once()
assert queried_ids == []
assert json_filepath.read_text() == EMPTY_JSON_CONTENTS
assert json_filepath.read_text(encoding="utf-8") == EMPTY_JSON_CONTENTS


def test_query_bookmark_ids_from_json_wrong_key(tmp_path):
Expand All @@ -268,7 +270,7 @@ def test_query_bookmark_ids_from_json_wrong_key(tmp_path):
"""

json_filepath: Path = tmp_path / ".imported_bookmarks.json"
json_filepath.write_text(JSON_CONTENTS_WRONG_KEY)
json_filepath.write_text(JSON_CONTENTS_WRONG_KEY, encoding="utf-8")

with (
patch.object(Confirm, "ask", return_value=True) as mock_ask,
Expand All @@ -279,7 +281,7 @@ def test_query_bookmark_ids_from_json_wrong_key(tmp_path):
mock_print.assert_called_once()
mock_ask.assert_called_once()
assert queried_ids == []
assert json_filepath.read_text() == EMPTY_JSON_CONTENTS
assert json_filepath.read_text(encoding="utf-8") == EMPTY_JSON_CONTENTS


def test_query_bookmark_ids_from_json_wrong_value(tmp_path):
Expand All @@ -292,7 +294,7 @@ def test_query_bookmark_ids_from_json_wrong_value(tmp_path):
"""

json_filepath: Path = tmp_path / ".imported_bookmarks.json"
json_filepath.write_text(JSON_CONTENTS_WRONG_VALUE)
json_filepath.write_text(JSON_CONTENTS_WRONG_VALUE, encoding="utf-8")

with (
patch.object(Confirm, "ask", return_value=True) as mock_ask,
Expand All @@ -303,7 +305,7 @@ def test_query_bookmark_ids_from_json_wrong_value(tmp_path):
mock_print.assert_called_once()
mock_ask.assert_called_once()
assert queried_ids == []
assert json_filepath.read_text() == EMPTY_JSON_CONTENTS
assert json_filepath.read_text(encoding="utf-8") == EMPTY_JSON_CONTENTS


def test_setup_missing_config(tmp_path):
Expand All @@ -326,7 +328,6 @@ def test_setup_missing_config(tmp_path):
Config, "create_interactively", return_value=reference_config
) as mock_create_interactively,
):

return_config = setup_missing_config(config_filpath)

assert reference_config == return_config
Expand Down

0 comments on commit f543e91

Please sign in to comment.