Skip to content

Commit

Permalink
fix: ipfs plugin imports
Browse files Browse the repository at this point in the history
  • Loading branch information
angrybayblade committed Dec 11, 2023
1 parent e3d2da4 commit bd1c5a0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
16 changes: 11 additions & 5 deletions aea/cli/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,21 @@
from aea.cli.utils.context import Context
from aea.cli.utils.decorators import pass_ctx
from aea.configurations.constants import PACKAGES
from aea.package_manager.base import (
BasePackageManager,
IS_IPFS_PLUGIN_INSTALLED,
PackageFileNotValid,
)
from aea.package_manager.base import BasePackageManager, PackageFileNotValid
from aea.package_manager.v0 import PackageManagerV0
from aea.package_manager.v1 import PackageManagerV1


try:
from aea_cli_ipfs.registry import ( # type: ignore # noqa: F401 # pylint: disable=unused-import
fetch_ipfs,
)

IS_IPFS_PLUGIN_INSTALLED = True
except ModuleNotFoundError: # pragma: nocover # cause obvious
IS_IPFS_PLUGIN_INSTALLED = False


class SyncTypes: # pylint: disable=too-few-public-methods
"""Types of syncs."""

Expand Down
32 changes: 21 additions & 11 deletions aea/package_manager/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@
from aea.helpers.ipfs.base import IPFSHashOnly


try:
from aea_cli_ipfs.registry import fetch_ipfs # type: ignore

IS_IPFS_PLUGIN_INSTALLED = True
except (ImportError, ModuleNotFoundError): # pragma: nocover # cause obvious
IS_IPFS_PLUGIN_INSTALLED = False

PACKAGES_FILE = "packages.json"
PACKAGE_SOURCE_RE = re.compile(r"([a-z-_0-9A-Z]+\/[a-z-_0-9A-Z]+)((:)([a-z\.0-9_-]+))?")

Expand All @@ -71,6 +64,21 @@ def load_configuration(
return cast(PackageConfiguration, configuration_obj)


def load_fetch_ipfs() -> Callable[[str, PublicId, str, bool], Optional[Path]]:
"""Load fetch_ipfs method."""

try:
from aea_cli_ipfs.registry import ( # type: ignore # pylint: disable=import-outside-toplevel
fetch_ipfs,
)

return fetch_ipfs
except ModuleNotFoundError: # pragma: nocover # cause obvious
raise ModuleNotFoundError(
"open-aea-cli-ipfs plugin is not installed; Run pip3 install open-aea-cli-ipfs"
)


class DepedencyMismatchErrors(Enum):
"""Dependency mismatch errors."""

Expand All @@ -87,14 +95,15 @@ def __init__(
self,
path: Path,
config_loader: ConfigLoaderCallableType = load_configuration,
logger: Optional[logging.Logger] = None,
) -> None:
"""Initialize object."""

self.path = path
self.config_loader = config_loader
self._packages_file = path / PACKAGES_FILE

self._logger = logging.getLogger(name="PackageManager")
self._logger = logger or logging.getLogger(name="PackageManager")
self._logger.setLevel(logging.INFO)

def _sync(
Expand All @@ -119,7 +128,7 @@ def _sync(
hash_updates = packages.copy()
package_updates = OrderedDict()

for package_id in packages:
for package_id in packages.copy():
package_path = self.package_path_from_package_id(package_id)
if package_path.exists():
package_hash = IPFSHashOnly.get(str(package_path))
Expand Down Expand Up @@ -359,10 +368,11 @@ def _fetch_package(self, package_id: PackageId) -> None:
(package_type_collection / "__init__.py").touch()

download_path = package_type_collection / package_id.name
fetch_ipfs(
load_fetch_ipfs()(
str(package_id.package_type),
package_id.public_id,
dest=str(download_path),
str(download_path),
True,
)
self._logger.debug(f"Downloaded {package_id.without_hash()}")

Expand Down
8 changes: 7 additions & 1 deletion aea/package_manager/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import copy
import json
import logging
import traceback
from collections import OrderedDict
from enum import Enum
Expand Down Expand Up @@ -73,9 +74,14 @@ def __init__(
dev_packages: Optional[PackageIdToHashMapping] = None,
third_party_packages: Optional[PackageIdToHashMapping] = None,
config_loader: ConfigLoaderCallableType = load_configuration,
logger: Optional[logging.Logger] = None,
) -> None:
"""Initialize object."""
super().__init__(path=path, config_loader=config_loader)
super().__init__(
path=path,
config_loader=config_loader,
logger=logger,
)

self._dev_packages = dev_packages or OrderedDict()
self._third_party_packages = third_party_packages or OrderedDict()
Expand Down

0 comments on commit bd1c5a0

Please sign in to comment.