Skip to content

prevent ethpm import issues without ipfshttpclient #2775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions ethpm/_utils/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
from eth_utils import (
to_tuple,
)
from ipfshttpclient.exceptions import (
ConnectionError,
)

from ethpm.backends.base import (
BaseURIBackend,
Expand All @@ -30,6 +27,12 @@
RegistryURIBackend,
)

try:
from ipfshttpclient.exceptions import ConnectionError as IpfsConnectionError
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was renamed to IpfsConnectionError because there is a builtin ConenctionError... so we'd be mistakenly catching an unwanted exception if the ipfshttpclient module is not available.

except ImportError:
pass


logger = logging.getLogger("ethpm.utils.backend")

ALL_URI_BACKENDS = [
Expand All @@ -41,6 +44,19 @@
]


def _handle_optional_ipfs_backend_exception(e: Exception) -> None:
try:
# if optional `ipfshttpclient` module is present, catch and debug if
# IpfsConnectionError, else raise original exception.
if isinstance(e, IpfsConnectionError):
logger.debug("No local IPFS node available on port 5001.", exc_info=True)
else:
raise e
except NameError:
# if optional `ipfshttpclient` module is not present, raise original exception
raise e


@to_tuple
def get_translatable_backends_for_uri(
uri: URI,
Expand All @@ -50,8 +66,8 @@ def get_translatable_backends_for_uri(
try:
if backend().can_translate_uri(uri): # type: ignore
yield backend
except ConnectionError:
logger.debug("No local IPFS node available on port 5001.", exc_info=True)
except Exception as e:
_handle_optional_ipfs_backend_exception(e)


@to_tuple
Expand All @@ -71,7 +87,5 @@ def get_resolvable_backends_for_uri(
try:
if backend_class().can_resolve_uri(uri): # type: ignore
yield backend_class
except ConnectionError:
logger.debug(
"No local IPFS node available on port 5001.", exc_info=True
)
except Exception as e:
_handle_optional_ipfs_backend_exception(e)
12 changes: 9 additions & 3 deletions ethpm/_utils/ipfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
parse,
)

from base58 import (
b58encode,
)
from eth_utils import (
to_text,
)
Expand All @@ -24,6 +21,15 @@
PBNode,
)

try:
# `ipfshttpclient` backend is optional. This is only imported if the "web3[ipfs]"
# install extra is installed
from base58 import (
b58encode,
)
except ImportError:
pass


def dummy_ipfs_pin(path: Path) -> Dict[str, str]:
"""
Expand Down
8 changes: 7 additions & 1 deletion ethpm/backends/ipfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import_string,
to_bytes,
)
import ipfshttpclient

from ethpm import (
get_ethpm_spec_dir,
Expand All @@ -39,6 +38,13 @@
EthPMValidationError,
)

try:
# `ipfshttpclient` backend is optional. This is only imported if the "web3[ipfs]"
# install extra is installed
import ipfshttpclient
except ImportError:
pass


class BaseIPFSBackend(BaseURIBackend):
"""
Expand Down
1 change: 1 addition & 0 deletions newsfragments/2775.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``ethpm`` import issues after making ``ipfshttpclient`` optional.