-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
113 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" | |
|
||
[tool.poetry] | ||
name = "readmeai" | ||
version = "0.5.061" | ||
version = "0.5.062" | ||
description = "👾 Automated README file generator, powered by large language model APIs." | ||
authors = ["Eli <[email protected]>"] | ||
license = "MIT" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"""Utility methods to read package resources.""" | ||
|
||
from importlib import resources | ||
from pathlib import Path | ||
|
||
from readmeai.exceptions import FileReadError | ||
from readmeai.utils.file_handler import FileHandler | ||
from readmeai.utils.logger import Logger | ||
|
||
_logger = Logger(__name__) | ||
|
||
|
||
def get_resource_path( | ||
handler: FileHandler, | ||
file_path: str, | ||
package: str = "readmeai.config", | ||
submodule: str = "settings", | ||
) -> dict: | ||
"""Get configuration dictionary from TOML file.""" | ||
try: | ||
resource_path = resources.files(package).joinpath(submodule, file_path) | ||
_logger.debug(f"Loading file via importlib.resources: {resource_path}") | ||
|
||
except TypeError as exc: | ||
_logger.debug(f"Error using importlib.resources: {exc}") | ||
|
||
try: | ||
import pkg_resources | ||
|
||
submodule = submodule.replace(".", "/") | ||
resource_path = Path( | ||
pkg_resources.resource_filename( | ||
"readmeai", f"{submodule}/{file_path}" | ||
) | ||
).resolve() | ||
|
||
except Exception as exc: | ||
_logger.error(f"Error loading file via pkg_resources: {exc}") | ||
|
||
raise FileReadError( | ||
"Error loading file via pkg_resources", str(file_path) | ||
) from exc | ||
|
||
if not resource_path.exists(): | ||
_logger.error(f"File not found: {str(resource_path)}") | ||
raise FileReadError("File not found", str(resource_path)) | ||
|
||
if str(file_path).endswith(".toml"): | ||
return handler.read_toml(str(resource_path)) | ||
|
||
elif str(file_path).endswith(".json"): | ||
return handler.read_json(str(resource_path)) | ||
|
||
_logger.error(f"Unsupported file format: {file_path}") | ||
|
||
raise FileReadError("Unsupported file format", str(file_path)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"""Tests for package resources utility methods.""" | ||
|
||
import pytest | ||
|
||
from readmeai.config.utils import get_resource_path | ||
from readmeai.exceptions import FileReadError | ||
from readmeai.utils.file_handler import FileHandler | ||
|
||
|
||
def test_get_resource_path(): | ||
"""Test that the resource path is returned correctly.""" | ||
try: | ||
file_path = get_resource_path( | ||
FileHandler(), "config.toml", "readmeai.config", "settings" | ||
) | ||
assert isinstance(file_path, dict) | ||
except FileReadError as exc: | ||
assert isinstance(exc, FileReadError) | ||
|
||
|
||
def test_file_read_error(): | ||
"""Test that the FileReadError is raised correctly.""" | ||
with pytest.raises(Exception) as exc: | ||
get_resource_path(FileHandler(), "config.yaml", "readmeai", "config") | ||
assert isinstance(exc.value, FileReadError) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters