Skip to content

Commit

Permalink
try to fix encodings on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
vpoulailleau committed Dec 13, 2024
1 parent 488a727 commit d72af83
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
1 change: 0 additions & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@
"""

for message in grammalecte_text(texte_bidon):
print(message.message.encode("utf-8"))
print(message)
18 changes: 14 additions & 4 deletions pygrammalecte/pygrammalecte.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import sys
import sysconfig
import tempfile
from contextlib import redirect_stdout
from dataclasses import dataclass, field
from pathlib import Path
from typing import Generator, List, Union
Expand All @@ -16,6 +15,12 @@
import requests


def safe_string(text: str) -> str:
if os.name == "nt":
return text.encode("cp1252", errors="replace").decode("cp1252")
return text


@dataclass
class GrammalecteMessage:
"""Base class for Grammalecte messages."""
Expand Down Expand Up @@ -55,7 +60,7 @@ def from_dict(line: int, grammalecte_dict: dict) -> "GrammalecteSpellingMessage"
line=line,
start=int(grammalecte_dict["nStart"]),
end=int(grammalecte_dict["nEnd"]),
word=grammalecte_dict["sValue"],
word=safe_string(grammalecte_dict["sValue"]),
)


Expand Down Expand Up @@ -85,8 +90,13 @@ def from_dict(line: int, grammalecte_dict: dict) -> "GrammalecteGrammarMessage":
end=int(grammalecte_dict["nEnd"]),
url=grammalecte_dict["URL"],
color=grammalecte_dict["aColor"],
suggestions=grammalecte_dict["aSuggestions"],
message=grammalecte_dict["sMessage"].replace("“", "« ").replace("”", " »"),
suggestions=[
safe_string(suggestion)
for suggestion in grammalecte_dict["aSuggestions"]
],
message=safe_string(
grammalecte_dict["sMessage"].replace("“", "« ").replace("”", " »")
),
rule=grammalecte_dict["sRuleId"],
type=grammalecte_dict["sType"],
)
Expand Down

0 comments on commit d72af83

Please sign in to comment.