-
Notifications
You must be signed in to change notification settings - Fork 5
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
18 changed files
with
334 additions
and
60 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
55 changes: 55 additions & 0 deletions
55
packages/ragbits-core/src/ragbits/core/utils/decorators.py
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,55 @@ | ||
# pylint: disable=missing-function-docstring,missing-return-doc | ||
|
||
import asyncio | ||
from functools import wraps | ||
from importlib.util import find_spec | ||
from typing import Callable, ParamSpec, TypeVar | ||
|
||
_P = ParamSpec("_P") | ||
_T = TypeVar("_T") | ||
|
||
|
||
def requires_dependencies( | ||
dependencies: str | list[str], | ||
extras: str | None = None, | ||
) -> Callable[[Callable[_P, _T]], Callable[_P, _T]]: | ||
""" | ||
Decorator to check if the dependencies are installed before running the function. | ||
Args: | ||
dependencies: The dependencies to check. | ||
extras: The extras to install. | ||
Returns: | ||
The decorated function. | ||
""" | ||
if isinstance(dependencies, str): | ||
dependencies = [dependencies] | ||
|
||
def decorator(func: Callable[_P, _T]) -> Callable[_P, _T]: | ||
def run_check() -> None: | ||
missing_dependencies = [dependency for dependency in dependencies if not find_spec(dependency)] | ||
if len(missing_dependencies) > 0: | ||
missing_deps = ", ".join(missing_dependencies) | ||
install_cmd = ( | ||
f"pip install 'ragbits[{extras}]'" if extras else f"pip install {' '.join(missing_dependencies)}" | ||
) | ||
raise ImportError( | ||
f"Following dependencies are missing: {missing_deps}. Please install them using `{install_cmd}`." | ||
) | ||
|
||
@wraps(func) | ||
def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _T: | ||
run_check() | ||
return func(*args, **kwargs) | ||
|
||
@wraps(func) | ||
async def wrapper_async(*args: _P.args, **kwargs: _P.kwargs) -> _T: | ||
run_check() | ||
return await func(*args, **kwargs) # type: ignore | ||
|
||
if asyncio.iscoroutinefunction(func): | ||
return wrapper_async # type: ignore | ||
return wrapper | ||
|
||
return decorator |
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,47 @@ | ||
import pytest | ||
|
||
from ragbits.core.utils.decorators import requires_dependencies | ||
|
||
|
||
def test_single_dependency_installed() -> None: | ||
@requires_dependencies("pytest") | ||
def some_function() -> str: | ||
return "success" | ||
|
||
assert some_function() == "success" | ||
|
||
|
||
def test_single_dependency_missing() -> None: | ||
@requires_dependencies("nonexistent_dependency") | ||
def some_function() -> str: | ||
return "success" | ||
|
||
with pytest.raises(ImportError) as exc: | ||
some_function() | ||
|
||
assert ( | ||
str(exc.value) | ||
== "Following dependencies are missing: nonexistent_dependency. Please install them using `pip install nonexistent_dependency`." | ||
) | ||
|
||
|
||
def test_multiple_dependencies_installed() -> None: | ||
@requires_dependencies(["pytest", "asyncio"]) | ||
def some_function() -> str: | ||
return "success" | ||
|
||
assert some_function() == "success" | ||
|
||
|
||
def test_multiple_dependencies_some_missing() -> None: | ||
@requires_dependencies(["pytest", "nonexistent_dependency"]) | ||
def some_function() -> str: | ||
return "success" | ||
|
||
with pytest.raises(ImportError) as exc: | ||
some_function() | ||
|
||
assert ( | ||
str(exc.value) | ||
== "Following dependencies are missing: nonexistent_dependency. Please install them using `pip install nonexistent_dependency`." | ||
) |
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
27 changes: 27 additions & 0 deletions
27
packages/ragbits-document-search/src/ragbits/document_search/documents/exceptions.py
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,27 @@ | ||
class SourceError(Exception): | ||
""" | ||
Class for all exceptions raised by the document source. | ||
""" | ||
|
||
def __init__(self, message: str) -> None: | ||
super().__init__(message) | ||
self.message = message | ||
|
||
|
||
class SourceConnectionError(SourceError): | ||
""" | ||
Raised when there is an error connecting to the document source. | ||
""" | ||
|
||
def __init__(self) -> None: | ||
super().__init__("Connection error.") | ||
|
||
|
||
class SourceNotFoundError(SourceError): | ||
""" | ||
Raised when the document is not found. | ||
""" | ||
|
||
def __init__(self, source_id: str) -> None: | ||
super().__init__(f"Source with ID {source_id} not found.") | ||
self.source_id = source_id |
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
Oops, something went wrong.