fix: convert charsets to utf-8 when necessary (#32)

improve compatibility on Windows
This commit is contained in:
Nathaniel Landau
2023-03-30 17:37:32 -04:00
committed by GitHub
parent 4df10e785e
commit 72fef38b0f
4 changed files with 31 additions and 22 deletions

10
poetry.lock generated
View File

@@ -98,14 +98,14 @@ files = [
[[package]]
name = "charset-normalizer"
version = "2.1.1"
version = "2.1.0"
description = "The Real First Universal Charset Detector. Open, modern and actively maintained alternative to Chardet."
category = "dev"
category = "main"
optional = false
python-versions = ">=3.6.0"
files = [
{file = "charset-normalizer-2.1.1.tar.gz", hash = "sha256:5a3d016c7c547f69d6f81fb0db9449ce888b418b5b9952cc5e6e66843e9dd845"},
{file = "charset_normalizer-2.1.1-py3-none-any.whl", hash = "sha256:83e9a75d1911279afd89352c68b45348559d1fc0506b054b346651b5e7fee29f"},
{file = "charset-normalizer-2.1.0.tar.gz", hash = "sha256:575e708016ff3a5e3681541cb9d79312c416835686d054a23accb873b254f413"},
{file = "charset_normalizer-2.1.0-py3-none-any.whl", hash = "sha256:5189b6f22b01957427f35b6a08d9a0bc45b46d3788ef5a92e978433c7a35f8a5"},
]
[package.extras]
@@ -1321,4 +1321,4 @@ dev = ["black (>=19.3b0)", "pytest (>=4.6.2)"]
[metadata]
lock-version = "2.0"
python-versions = "^3.10"
content-hash = "61ab3ff1aa55c93eeec465752dcc92de12fc1c02206184913b67db1c590d3ed8"
content-hash = "791c4a20b082a0ae43b35023ff9db5c9cc212f44c4ec5180a10042970f796af5"

View File

@@ -17,15 +17,16 @@
obsidian-metadata = "obsidian_metadata.cli:app"
[tool.poetry.dependencies]
loguru = "^0.6.0"
python = "^3.10"
questionary = "^1.10.0"
regex = "^2023.3.23"
rich = "^13.3.3"
ruamel-yaml = "^0.17.21"
shellingham = "^1.5.0.post1"
tomlkit = "^0.11.7"
typer = "^0.7.0"
charset-normalizer = "2.1.0"
loguru = "^0.6.0"
python = "^3.10"
questionary = "^1.10.0"
regex = "^2023.3.23"
rich = "^13.3.3"
ruamel-yaml = "^0.17.21"
shellingham = "^1.5.0.post1"
tomlkit = "^0.11.7"
typer = "^0.7.0"
[tool.poetry.group.test.dependencies]
pytest = "^7.2.2"

View File

@@ -8,6 +8,7 @@ from pathlib import Path
import rich.repr
import typer
from charset_normalizer import from_path
from rich.table import Table
from obsidian_metadata._utils import alerts, inline_metadata_from_string
@@ -53,12 +54,18 @@ class Note:
self.dry_run: bool = dry_run
try:
with self.note_path.open():
self.file_content: str = self.note_path.read_text()
self.original_file_content: str = self.file_content
result = from_path(self.note_path).best()
self.encoding: str = result.encoding
self.file_content: str = str(result)
self.original_file_content: str = str(result)
except FileNotFoundError as e:
alerts.error(f"Note {self.note_path} not found. Exiting")
raise typer.Exit(code=1) from e
except UnicodeDecodeError as e:
alerts.error(
f"Error decoding note {self.note_path}.\nDetected encoding: {self.encoding}\nExiting"
)
raise typer.Exit(code=1) from e
try:
self.frontmatter: Frontmatter = Frontmatter(self.file_content)
@@ -76,11 +83,12 @@ class Note:
def __rich_repr__(self) -> rich.repr.Result: # pragma: no cover
"""Define rich representation of Vault."""
yield "note_path", self.note_path
yield "dry_run", self.dry_run
yield "encoding", self.encoding
yield "frontmatter", self.frontmatter
yield "tags", self.tags
yield "inline_metadata", self.inline_metadata
yield "note_path", self.note_path
yield "tags", self.tags
def add_metadata( # noqa: C901
self,
@@ -157,9 +165,8 @@ class Note:
return
try:
with p.open(mode="w") as f:
log.trace(f"Writing note {p} to disk")
f.write(self.file_content)
log.trace(f"Writing note {p} to disk")
p.write_text(self.file_content)
except FileNotFoundError as e:
alerts.error(f"Note {p} not found. Exiting")
raise typer.Exit(code=1) from e

View File

@@ -35,6 +35,7 @@ def test_create_note_1(sample_note):
assert note.note_path == Path(sample_note)
assert note.dry_run is True
assert "Lorem ipsum dolor" in note.file_content
assert note.encoding == "utf_8"
assert note.frontmatter.dict == {
"date_created": ["2022-12-22"],
"frontmatter_Key1": ["author name"],