Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Issue 778. #779

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 2.36 (2024-10-24)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# 2.36 (2024-10-24)
# 2.36 (unreleased)

* Fix: Categories with characters incompatible with the current locale opens (#778, Oussama Jarrousse).
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Fix: Categories with characters incompatible with the current locale opens (#778, Oussama Jarrousse).
* Fix sorting words that are incompatible with the current locale (#778, #779, 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).

Expand Down
2 changes: 1 addition & 1 deletion rednotebook/journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
12 changes: 12 additions & 0 deletions rednotebook/util/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# -----------------------------------------------------------------------

import http.client
import locale
import logging
import os.path
import re
Expand Down Expand Up @@ -202,3 +203,14 @@ def flush(self):
def close(self):
for stream in self.streams:
stream.close()

def safe_strxfrm(value):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def safe_strxfrm(value):
def safe_strxfrm(s: str):

"""
Safely apply locale-aware sorting. If locale.strxfrm fails, fall back to default sorting.
"""
try:
return locale.strxfrm(value)
except OSError:
return value
except:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need the bare except? I think it's better to remove it since we have the except for OSError.

return value
40 changes: 40 additions & 0 deletions tests/test_journal.py
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test!

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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":{}})
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[4] = day4

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
Loading