Skip to content

Commit

Permalink
Update conditional tomllib file reading logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
eli64s committed Mar 4, 2024
1 parent f6e7cc6 commit 602a3a0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
12 changes: 6 additions & 6 deletions readmeai/utils/file_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@

if sys.version_info < (3, 11):
import toml

enc = "utf-8"
else:
import tomllib as toml

enc = "rb"


class FileHandler:
"""File I/O factory class to read and write files."""
Expand Down Expand Up @@ -92,8 +88,12 @@ def read_markdown(file_path: Union[str, Path]) -> str:
@functools.lru_cache(maxsize=100)
def read_toml(file_path: Union[str, Path]) -> Dict[str, Any]:
"""Read the content of a TOML file."""
with open(file_path, encoding=enc) as file:
data = toml.load(file)
if sys.version_info < (3, 11):
with open(file_path, encoding="utf-8") as file:
data = toml.load(file)
else:
with open(file_path, "rb") as file:
data = toml.load(file)
return {key.lower(): value for key, value in data.items()}

@staticmethod
Expand Down
7 changes: 6 additions & 1 deletion tests/parsers/language/test_rust.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
"""Unit tests for Rust-based dependency parsers."""

import toml
import sys

from readmeai.parsers.language.rust import CargoTomlParser

if sys.version_info < (3, 11):
import toml
else:
import tomllib as toml

content = """
[package]
name = "lightning-invoice"
Expand Down

0 comments on commit 602a3a0

Please sign in to comment.