From ff436fda901e5440ad98db5c8d363329f54e8e49 Mon Sep 17 00:00:00 2001 From: Oussama Jarrousse Date: Thu, 24 Oct 2024 16:25:37 +0200 Subject: [PATCH 1/3] added a test for journal.categories --- tests/test_journal.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tests/test_journal.py diff --git a/tests/test_journal.py b/tests/test_journal.py new file mode 100644 index 00000000..ff48aec9 --- /dev/null +++ b/tests/test_journal.py @@ -0,0 +1,37 @@ +import pytest + +from datetime import date +from rednotebook.data import Month, Day # Assuming you have access to these + +from rednotebook.journal import Journal + +@pytest.fixture +def mock_month(): + month = Month(2024, 10) + day1 = Day(month, 1, {"text": "Example text", "Aria": {}}) + day2 = Day(month, 2, {"text": "More example text", "Aria": {}, "Opera":{}, "Étude":{}}) + day3 = Day(month, 3, {"text": "Another text", "Sonata": {}, "Prelude": {}, "Opera": {}, "Concerto":{}}) + + month.days[1] = day1 + month.days[2] = day2 + month.days[3] = day3 + + yield month + + +def test_categories(mock_month): + # Create an empty journal instance + journal = Journal() + + # Add a month with no days to the journal + journal.months = { (2024, 10): Month(2024, 10) } + + # Ensure that the categories list is empty + assert journal.categories == [], "Expected no categories in an empty journal" + + # Add a month with days to the journal + journal.months = { (2024, 10): mock_month } + + # Assert the categories property returns expected categories sorted alphabetically + expected_categories = ['Aria', 'Concerto', 'Étude', 'Opera', 'Prelude', 'Sonata'] + assert journal.categories == expected_categories \ No newline at end of file From f563566952ff37aa7d5a83776e33abc209d6552a Mon Sep 17 00:00:00 2001 From: Oussama Jarrousse Date: Thu, 24 Oct 2024 16:29:32 +0200 Subject: [PATCH 2/3] added a fixture and a test to reproduce issue-778, causing journal.categories raise OSError on MacOSX --- tests/test_journal.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_journal.py b/tests/test_journal.py index ff48aec9..e2fa2e38 100644 --- a/tests/test_journal.py +++ b/tests/test_journal.py @@ -11,10 +11,12 @@ def mock_month(): day1 = Day(month, 1, {"text": "Example text", "Aria": {}}) day2 = Day(month, 2, {"text": "More example text", "Aria": {}, "Opera":{}, "Étude":{}}) day3 = Day(month, 3, {"text": "Another text", "Sonata": {}, "Prelude": {}, "Opera": {}, "Concerto":{}}) + day4 = Day(month, 4, {"text": "Regression test for issue 778", "Opera":{}, "المُوَشَّح":{}}) month.days[1] = day1 month.days[2] = day2 month.days[3] = day3 + month.days[3] = day4 yield month From 7a557f1462a1c9c65d86655ad076843aab77edb9 Mon Sep 17 00:00:00 2001 From: Oussama Jarrousse Date: Thu, 24 Oct 2024 16:51:39 +0200 Subject: [PATCH 3/3] added rednotebook.utils.safe_strxfrm fixing issue-778; updated rednotebook.journal.Journal.categories() to use utils.safe_strxfrm in sorting categories instead of locale.strxfrm that was raising OSError in case of characters incompatible with the current locale; added tests.test_journal.py to test the fix for issue-778 --- CHANGELOG.md | 3 +++ rednotebook/journal.py | 2 +- rednotebook/util/utils.py | 12 ++++++++++++ tests/test_journal.py | 5 +++-- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45f0e91a..d0d473e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.36 (2024-10-24) +* Fix: Categories with characters incompatible with the current locale opens (#778, Oussama Jarrousse). + # 2.35 (2024-09-22) * Add option to auto-indent text in editor and activate it by default (#561, #562, Allen Benter, Varunjay Varma). diff --git a/rednotebook/journal.py b/rednotebook/journal.py index a118f1a4..5f9aca37 100644 --- a/rednotebook/journal.py +++ b/rednotebook/journal.py @@ -540,7 +540,7 @@ def show_message(self, msg, title=None, error=False): def categories(self): return sorted( set(itertools.chain.from_iterable(day.categories for day in self.days)), - key=locale.strxfrm, + key=utils.safe_strxfrm, ) def get_entries(self, category): diff --git a/rednotebook/util/utils.py b/rednotebook/util/utils.py index e71653e7..027718ba 100644 --- a/rednotebook/util/utils.py +++ b/rednotebook/util/utils.py @@ -17,6 +17,7 @@ # ----------------------------------------------------------------------- import http.client +import locale import logging import os.path import re @@ -202,3 +203,14 @@ def flush(self): def close(self): for stream in self.streams: stream.close() + +def safe_strxfrm(value): + """ + Safely apply locale-aware sorting. If locale.strxfrm fails, fall back to default sorting. + """ + try: + return locale.strxfrm(value) + except OSError: + return value + except: + return value diff --git a/tests/test_journal.py b/tests/test_journal.py index e2fa2e38..9321098a 100644 --- a/tests/test_journal.py +++ b/tests/test_journal.py @@ -16,7 +16,7 @@ def mock_month(): month.days[1] = day1 month.days[2] = day2 month.days[3] = day3 - month.days[3] = day4 + month.days[4] = day4 yield month @@ -35,5 +35,6 @@ def test_categories(mock_month): journal.months = { (2024, 10): mock_month } # Assert the categories property returns expected categories sorted alphabetically - expected_categories = ['Aria', 'Concerto', 'Étude', 'Opera', 'Prelude', 'Sonata'] + expected_categories = ['Aria', 'Concerto', 'Étude', 'Opera', 'Prelude', 'Sonata', 'المُوَشَّح'] + assert journal.categories == expected_categories \ No newline at end of file